Audio capture problem


#1

Hi,

It seems that I have a problem with my audio callback function. In fact, some data is missing in the audio file created, I mean that at certain point of the file, the audio is like cut, like some data hasn’t been written into the file.
Here’s my audio class:

[code]Audio::Audio(PageInfo* info) : mpInfo(info)
{
mpDevice = NULL;
}

Audio::~Audio()
{
delete mpDevice;
}

bool Audio::LaunchProcess(bool createCapture)
{
mCreateCapture = createCapture;

if (mpInfo->IsDeviceChanged() && mpDevice)
{
	mpDevice->close();
	mpInfo->SetDeviceChanged(false);
	delete mpDevice;
	mpDevice = NULL;
}

if (!mpDevice || !mpDevice->isOpen())
{
	String str = mpInfo->GetInputDevice();
	mpDevice = new AudioIODevice(str);

	String error = mpDevice->open(BitArray(3),BitArray(0),mpDevice->getSampleRate(0),mpDevice->getBufferSizeSamples(0));  

	if (!error.isEmpty())
	{
		Error er;
		er.Display("Error while opening device");
		return false;
	}
}

SF_INFO* sfinfo = new SF_INFO;

sfinfo->channels = 2;
sfinfo->format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
sfinfo->samplerate = (int)mpDevice->getSampleRate(0);
sfinfo->sections = 1;
sfinfo->seekable = 1;

String path = mpInfo->GetDir() + mpInfo->GetSeparator() + "Capture.wav";

if (!(mpSndfile = sf_open(toCString(path), SFM_WRITE, sfinfo)))
	return false;

mpDevice->start(this);  // mpCallBack is created in the constructor of this class

return true;

}

void Audio::StopProcess()
{
sf_close(mpSndfile);
mpDevice->stop();
}

void Audio::audioDeviceIOCallback(const float **inputChannelData, int totalNumInputChannels, float *outputChannelData, int totalNumOutputChannels, int numSamples)
{
float
samples = new float[numSamples*2];

if (mCreateCapture)
{
	for (int i = 0, j = 0; j < numSamples; i+=2, j++)
	{
		samples[i] = inputChannelData[0][j];
		samples[i+1] = inputChannelData[1][j];
	}

	sf_write_float(mpSndfile, samples, numSamples*2);
}

delete[] samples;

}

void Audio::audioDeviceAboutToStart(double sampleRate, int numSamplesPerBlock)
{

}

void Audio::audioDeviceStopped()
{
}[/code]

Did someone already have a problem like this? Thanks for your help.

Leskimo


#2

Well I don’t know anything about the file-writing code you’re using, but you really shouldn’t do any disk access in the callback - that’s a high-priority interrupt and if it blocks, then the audio device could skip blocks. Always buffer the data and use a background thread to actually write it to disk.


#3