Timing of the Audio library

Status
Not open for further replies.

mumes

New member
Hi,

I am using the Audio-library and was wondering about its timing.

Lets say I want to repeatedly record a tone of 1000Hz (continuously played by a speaker). I am using a microphone connected to the teensy ADC. I want to create a loop, where I always record 50ms and then pause for a while (another 50ms). However, each of the repeating 50ms recordings is supposed to beginn at more or less (+- 10us) the same position of the 1000Hz-sine-wave. I do not care which position though and also the length of the recording does not to be accurate. It just has to be the same value of the wave.

The figure describes the timing:

Unbenannt.png

My code does not give the desired result. It is off by up to +-1.2ms.

Code:
#include "Audio.h"
#include "Wire.h"
#include "SPI.h"
#include "SD.h"


AudioInputAnalog         adc1;
AudioRecordQueue         queue1;
AudioConnection          patchCord1(adc1, queue1);  


const uint32_t Tcycle = 100000;
const uint32_t Trecording = 50000;
const int nAudiop = 128;

void setup() {

  AudioMemory(60);
}

elapsedMicros sinceRec;

void loop() {

  if (sinceRec >= Tcycle) {

    sinceRec -= Tcycle;

    queue1.begin();
    elapsedMicros us;
    while ( us < Trecording ) { };
    queue1.end();

   int npac = queue1.available();      

    if (queue1.available() >= 1) {

      int16_t  buffer[npac * nAudiop];         

      for (int i = 0; i < npac; i++) {  
        memcpy(buffer + i * nAudiop , queue1.readBuffer(), 256);  
        queue1.freeBuffer();
   }
    queue1.clear()
  }
 }
}

I was wondering how I can improve the timing?

Thanks!
 
Status
Not open for further replies.
Back
Top