jeudi 5 mars 2020

Capturing audio of fixed number of samples using openAL

For a Voice Activity Detection program (which uses webRTC's VAD), I want to record audio from microphone. To do this I am using openAL. Below is my program to record audio:

const int SRATE = 16000;
const int SSIZE = 320;

ALbyte buffer[SSIZE];
ALint sample;

void OpenALPOC::recordAudio()
{
    const ALCchar* s=NULL;
    alGetError();

    if (alcIsExtensionPresent(NULL, "ALC_enumeration_EXT") == AL_TRUE)
    {
        s = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
    }

    ALCdevice* device = alcCaptureOpenDevice(s, SRATE, AL_FORMAT_MONO16, SSIZE);
    if (alGetError() != AL_NO_ERROR) {
        cout << "error" << endl;
    }
    alcCaptureStart(device);

    if (true) {
        alcGetIntegerv(device, ALC_CAPTURE_SAMPLES, (ALCsizei)sizeof(buffer), &sample);
        alcCaptureSamples(device, (ALCvoid*)buffer, sample);

        //Below this webRTC's VAD detection on received buffer ll be performed
    }

    alcCaptureStop(device);
    alcCaptureCloseDevice(device);
}

webRTC's VAD requires audio frame of mono 16 bit PCM, with 16 KHz sample rate and is must be of duration 10, 20, 20 milliseconds. Assumes audio frame is 10 ms i.e 320 byts of audio at 16000 Hz.

1 sample = 2B = 16 bits  
SampleRate = 16000 sample/sec = 16 samples/ms  
For 10 ms, no of samples    =   160  = 320 bytes

So in the above program, SSIZE I set to 320 and the buffer is of the same size. But when I check value of sample after function call alcGetIntegerv it's 1600, 800, 960 it's random random at every run. I suppose that If I specified buffer size in function alcCaptureOpenDevice() and alcGetIntegerv() then only that size of bytes will be returned into the data after calling alcGetIntegerv().

So, how can I ask openAL to record audio for fixed number of samples or for 10 milliseconds of duration?

Aucun commentaire:

Enregistrer un commentaire