@@ -186,6 +186,7 @@ class AudioSource {
186186 bool _i2sMaster; // when false, ESP32 will be in I2S SLAVE mode (for devices that only operate in MASTER mode). Only works in newer IDF >= 4.4.x
187187 float _sampleScale; // pre-scaling factor for I2S samples
188188 I2S_datatype newSampleBuffer[I2S_SAMPLES_MAX+4 ] = { 0 }; // global buffer for i2s_read
189+ I2S_datatype newSampleBuffer4x[(I2S_SAMPLES_MAX*4 )+4 ] = { 0 }; // global buffer for i2s_read
189190};
190191
191192/* Basic I2S microphone source
@@ -194,12 +195,12 @@ class AudioSource {
194195*/
195196class I2SSource : public AudioSource {
196197 public:
197- I2SSource (SRate_t sampleRate, int blockSize, float sampleScale = 1 .0f , bool i2sMaster=true ) :
198+ I2SSource (SRate_t sampleRate, int blockSize, float sampleScale = 1 .0f , bool i2sMaster=false ) :
198199 AudioSource (sampleRate, blockSize, sampleScale, i2sMaster) {
199200 _config = {
200201 .mode = i2sMaster ? i2s_mode_t (I2S_MODE_MASTER | I2S_MODE_RX) : i2s_mode_t (I2S_MODE_SLAVE | I2S_MODE_RX),
201- .sample_rate = _sampleRate,
202- .bits_per_sample = I2S_SAMPLE_RESOLUTION, // slave mode: may help to set this to 96000, as the other side (master) controls sample rates
202+ .sample_rate = _sampleRate, // slave mode: may help to set this to 96000, as the other side (master) controls sample rates
203+ .bits_per_sample = I2S_SAMPLE_RESOLUTION,
203204 .channel_format = I2S_MIC_CHANNEL,
204205#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0)
205206 .communication_format = i2s_comm_format_t (I2S_COMM_FORMAT_STAND_I2S),
@@ -394,6 +395,8 @@ class I2SSource : public AudioSource {
394395
395396 memset (buffer, 0 , sizeof (float ) * num_samples); // clear output buffer
396397 I2S_datatype *newSamples = newSampleBuffer; // use global input buffer
398+ I2S_datatype *newSamples_buff = newSampleBuffer4x; // use oversampling global input buffer
399+
397400 if (num_samples > I2S_SAMPLES_MAX) num_samples = I2S_SAMPLES_MAX; // protect the buffer from overflow
398401
399402 err = i2s_read (AR_I2S_PORT, (void *)newSamples, num_samples * sizeof (I2S_datatype), &bytes_read, portMAX_DELAY);
@@ -408,6 +411,15 @@ class I2SSource : public AudioSource {
408411 return ;
409412 }
410413
414+ if (_sampleRate == 96000 ) {
415+ int current = 0 ;
416+ for (int i = 0 ; i < 2048 ; i += 4 ) {
417+ newSamples[current] = newSamples_buff[i];
418+ current++;
419+ }
420+ num_samples /= 4 ; // back to 512 samples
421+ }
422+
411423 // Store samples in sample buffer and update DC offset
412424 for (int i = 0 ; i < num_samples; i++) {
413425
0 commit comments