Skip to content

Commit 0569965

Browse files
authored
Merge pull request #192 from pennam/pdm_gain
Align usage of PDM.setGain()
2 parents f733042 + 78ea5cd commit 0569965

File tree

8 files changed

+77
-39
lines changed

8 files changed

+77
-39
lines changed

libraries/PDM/src/PDM.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ class PDMClass
3030
PDMClass(int dinPin, int clkPin, int pwrPin);
3131
virtual ~PDMClass();
3232

33-
int begin(int channels, long sampleRate);
33+
int begin(int channels, int sampleRate);
3434
void end();
3535

3636
virtual int available();
3737
virtual int read(void* buffer, size_t size);
3838

3939
void onReceive(void(*)(void));
4040

41+
//PORTENTA_H7 min -12 max 51
42+
//NANO 33 BLE SENSe min 0 max 80
4143
void setGain(int gain);
4244
void setBufferSize(int bufferSize);
4345

@@ -50,7 +52,11 @@ class PDMClass
5052
int _pwrPin;
5153

5254
int _channels;
53-
55+
int _samplerate;
56+
57+
int _gain;
58+
int _init;
59+
5460
PDMDoubleBuffer _doubleBuffer;
5561

5662
void (*_onReceive)(void);

libraries/PDM/src/nrf52/PDM.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,22 @@ PDMClass::PDMClass(int dinPin, int clkPin, int pwrPin) :
3939
_dinPin(dinPin),
4040
_clkPin(clkPin),
4141
_pwrPin(pwrPin),
42-
_onReceive(NULL)
42+
_onReceive(NULL),
43+
_gain(-1),
44+
_channels(-1),
45+
_samplerate(-1),
46+
_init(-1)
4347
{
4448
}
4549

4650
PDMClass::~PDMClass()
4751
{
4852
}
4953

50-
int PDMClass::begin(int channels, long sampleRate)
54+
int PDMClass::begin(int channels, int sampleRate)
5155
{
5256
_channels = channels;
57+
_samplerate = sampleRate;
5358

5459
// Enable high frequency oscillator if not already enabled
5560
if (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {
@@ -83,7 +88,10 @@ int PDMClass::begin(int channels, long sampleRate)
8388
return 0; // unsupported
8489
}
8590

86-
setGain(DEFAULT_PDM_GAIN);
91+
if(_gain == -1) {
92+
_gain = DEFAULT_PDM_GAIN;
93+
}
94+
nrf_pdm_gain_set(_gain, _gain);
8795

8896
// configure the I/O and mux
8997
pinMode(_clkPin, OUTPUT);
@@ -175,7 +183,8 @@ void PDMClass::onReceive(void(*function)(void))
175183

176184
void PDMClass::setGain(int gain)
177185
{
178-
nrf_pdm_gain_set(gain, gain);
186+
_gain = gain;
187+
nrf_pdm_gain_set(_gain, _gain);
179188
}
180189

181190
void PDMClass::setBufferSize(int bufferSize)

libraries/PDM/src/rp2040/OpenPDMFilter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ void Open_PDM_Filter_Init(TPDMFilter_InitStruct *Param)
193193
}
194194

195195
sub_const = sum >> 1;
196-
div_const = sub_const * Param->MaxVolume / 32768 / filterGain;
196+
div_const = sub_const * Param->MaxVolume / 32768 / Param->filterGain;
197197
div_const = (div_const == 0 ? 1 : div_const);
198198

199199
#ifdef USE_LUT

libraries/PDM/src/rp2040/OpenPDMFilter.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@
5454
#define SINCN 3
5555
#define DECIMATION_MAX 128
5656

57-
//#define FILTER_GAIN 16
58-
extern uint16_t filterGain;
59-
60-
6157
#define HTONS(A) ((((uint16_t)(A) & 0xff00) >> 8) | \
6258
(((uint16_t)(A) & 0x00ff) << 8))
6359
#define RoundDiv(a, b) (((a)>0)?(((a)+(b)/2)/(b)):(((a)-(b)/2)/(b)))
@@ -84,6 +80,7 @@ typedef struct {
8480
uint16_t HP_ALFA;
8581
uint16_t bit[5];
8682
uint16_t byte;
83+
uint16_t filterGain;
8784
} TPDMFilter_InitStruct;
8885

8986

libraries/PDM/src/rp2040/PDM.cpp

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ int16_t* volatile finalBuffer;
3232
// OpenPDM filter used to convert PDM into PCM
3333
#define FILTER_GAIN 16
3434
TPDMFilter_InitStruct filter;
35-
uint16_t filterGain = FILTER_GAIN;
36-
3735

3836
extern "C" {
3937
__attribute__((__used__)) void dmaHandler(void)
@@ -47,15 +45,19 @@ PDMClass::PDMClass(int dinPin, int clkPin, int pwrPin) :
4745
_dinPin(dinPin),
4846
_clkPin(clkPin),
4947
_pwrPin(pwrPin),
50-
_onReceive(NULL)
48+
_onReceive(NULL),
49+
_gain(-1),
50+
_channels(-1),
51+
_samplerate(-1),
52+
_init(-1)
5153
{
5254
}
5355

5456
PDMClass::~PDMClass()
5557
{
5658
}
5759

58-
int PDMClass::begin(int channels, long sampleRate)
60+
int PDMClass::begin(int channels, int sampleRate)
5961
{
6062
//_channels = channels; // only one channel available
6163

@@ -71,15 +73,19 @@ int PDMClass::begin(int channels, long sampleRate)
7173
rawBufferLength = finalBufferLength;
7274
}
7375

74-
/* Initialize Open PDM library */
75-
filter.Fs = sampleRate;
76-
filter.nSamples = rawBufferLength;
77-
filter.LP_HZ = sampleRate/2;
78-
filter.HP_HZ = 10;
79-
filter.In_MicChannels = 1;
80-
filter.Out_MicChannels = 1;
81-
filter.Decimation = decimation;
82-
Open_PDM_Filter_Init(&filter);
76+
/* Initialize Open PDM library */
77+
filter.Fs = sampleRate;
78+
filter.nSamples = rawBufferLength;
79+
filter.LP_HZ = sampleRate/2;
80+
filter.HP_HZ = 10;
81+
filter.In_MicChannels = 1;
82+
filter.Out_MicChannels = 1;
83+
filter.Decimation = decimation;
84+
if(_gain == -1) {
85+
_gain = FILTER_GAIN;
86+
}
87+
filter.filterGain = _gain;
88+
Open_PDM_Filter_Init(&filter);
8389

8490
// Configure PIO state machine
8591
float clkDiv = (float)clock_get_hz(clk_sys) / sampleRate / decimation / 2;
@@ -110,6 +116,8 @@ int PDMClass::begin(int channels, long sampleRate)
110116
true // Start immediately
111117
);
112118

119+
_init = 1;
120+
113121
return 1;
114122
}
115123

@@ -142,7 +150,11 @@ void PDMClass::onReceive(void(*function)(void))
142150

143151
void PDMClass::setGain(int gain)
144152
{
145-
filterGain = gain;
153+
_gain = gain;
154+
if(_init == 1) {
155+
filter.filterGain = _gain;
156+
Open_PDM_Filter_Init(&filter);
157+
}
146158
}
147159

148160
void PDMClass::setBufferSize(int bufferSize)
@@ -185,4 +197,4 @@ void PDMClass::IrqHandler(bool halftranfer)
185197

186198
PDMClass PDM(PIN_PDM_DIN, PIN_PDM_CLK, -1);
187199

188-
#endif
200+
#endif

libraries/PDM/src/stm32/PDM.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,19 @@ PDMClass::PDMClass(int dinPin, int clkPin, int pwrPin) :
3434
_dinPin(dinPin),
3535
_clkPin(clkPin),
3636
_pwrPin(pwrPin),
37-
_onReceive(NULL)
37+
_onReceive(NULL),
38+
_gain(-1),
39+
_channels(-1),
40+
_samplerate(-1),
41+
_init(-1)
3842
{
3943
}
4044

4145
PDMClass::~PDMClass()
4246
{
4347
}
4448

45-
static int gain_db = -1;
46-
static int _samplerate = -1;
47-
48-
int PDMClass::begin(int channels, long sampleRate) {
49+
int PDMClass::begin(int channels, int sampleRate) {
4950

5051
if (isBoardRev2()) {
5152
mbed::I2C i2c(PB_7, PB_6);
@@ -65,14 +66,13 @@ int PDMClass::begin(int channels, long sampleRate) {
6566
_channels = channels;
6667
_samplerate = sampleRate;
6768

68-
if (gain_db == -1) {
69-
gain_db = 24;
69+
if (_gain == -1) {
70+
_gain = 24;
7071
}
7172

72-
//g_pcmbuf = (uint16_t*)_doubleBuffer.data();
73-
74-
if(py_audio_init(channels, sampleRate, gain_db, 0.9883f)) {
73+
if(py_audio_init(channels, sampleRate, _gain, 0.9883f)) {
7574
py_audio_start_streaming();
75+
_init = 1;
7676
return 1;
7777
}
7878
return 0;
@@ -103,9 +103,10 @@ void PDMClass::onReceive(void(*function)(void))
103103

104104
void PDMClass::setGain(int gain)
105105
{
106-
gain_db = gain;
107-
//end();
108-
//begin(_channels, _samplerate);
106+
_gain = gain;
107+
if(_init == 1) {
108+
py_audio_gain_set(gain);
109+
}
109110
}
110111

111112
void PDMClass::setBufferSize(int bufferSize)

libraries/PDM/src/stm32/audio.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,18 @@ int py_audio_init(size_t channels, uint32_t frequency, int gain_db, float highpa
309309
return 1;
310310
}
311311

312+
void py_audio_gain_set(int gain_db)
313+
{
314+
// Configure PDM filters
315+
for (int i=0; i<g_i_channels; i++) {
316+
PDM_FilterConfig[i].mic_gain = gain_db;
317+
//This will be called only after init so PDM_FilterConfig structure is already filled
318+
//PDM_FilterConfig[i].output_samples_number = samples_per_channel;
319+
//PDM_FilterConfig[i].decimation_factor = decimation_factor_const;
320+
PDM_Filter_setConfig(&PDM_FilterHandler[i], &PDM_FilterConfig[i]);
321+
}
322+
}
323+
312324
void py_audio_deinit()
313325
{
314326
// Stop SAI DMA.

libraries/PDM/src/stm32/audio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
void py_audio_deinit();
3434
int py_audio_init(size_t g_channels, uint32_t frequency, int gain_db, float highpass);
35+
void py_audio_gain_set(int gain_db);
3536
void audio_pendsv_callback(void);
3637
void py_audio_start_streaming();
3738
void py_audio_stop_streaming();

0 commit comments

Comments
 (0)