Nano 33 Ble sense recording to wav file

Hi,
I m trying to record my voice using the PDM libray and save it as a wav file on my sd card. When a button is pressed it will start recording and stop when pressing again. I m using the PDMSerialPlotter sketch a callback function is called when there are samples

void on_PDM_data() 
{
  // Query the number of available bytes
  int bytesAvailable = PDM.available();
  // Read into the sample buffer
  PDM.read(sample_buffer, bytesAvailable);
  // 16-bit, 2 bytes per sample
  samples_read = bytesAvailable / 2;
}

and in a loop I m checking the buffer and storing it to sd card

void loop() {

  if (samples_read) 
  {
    for (int i = 0; i < samples_read; i++) 
    {
      cpbuffer[2*i]= (sampleBuffer[i] >> 8) & 0xFF;
      cpbuffer[2*i+1]= (sampleBuffer[i]  & 0xFF);
    
    }
    rec_byte_saved+=rec.write(cpbuffer,samples_read*2); //save data
    samples_read = 0;
  }

}

The problem is when I play the wav file on my pc it is to fast, and the recording time is not the same. For example if I record for 10 seconds , I have 5 seconds when playing the wav file.
The wavheader I think is ok sample-rate 16khz, channels 1, 16 bits per sample...

I count the samples that I stored on the sd card and the number of samples that I read in the callback function, and its the same number of samples.
Still I think I m missing samples. I checked The PDM library and there is a doubleBuffer. Does anyone know How the PDM librray works. I think it is like a circular buffer but Im not sure.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.