[Openal] Recording and Playing Simultaneously

Spike7d5 spike7d5 at yahoo.com
Tue Jul 24 08:18:18 PDT 2007


I have finally recorded and played the sound at the same time. Thank you!
Unfortunately, there is one more problem: if the cpu usage is too high the
number of free buffers drops (the same behavior appears if I freeze the
program for a couple of seconds). So, what should I do to be sure that the
program always has some free buffers? Should I increase the number of
maximum buffers or does the code have some bugs? Here is the whole main loop
(BUFFERS is defined as 32):



int currentBuffer = 0, lastBuffer = 0;
int freeBuffers = BUFFERS, freeBuffersMin = BUFFERS;
char *bufferCapture[BUFFERS];

while (key != 27)
{
	ALCint samples;
	alcGetIntegerv(deviceCapture, ALC_CAPTURE_SAMPLES, 1, &samples);

	if (samples >= 1)
	{
		bufferCapture[currentBuffer] = (char *) malloc(samples * 2);
		alcCaptureSamples(deviceCapture, bufferCapture[currentBuffer], samples);

		if ((result = alcGetError(deviceCapture)) == AL_NO_ERROR)
		{
			alBufferData(buffer[currentBuffer], AL_FORMAT_MONO16,
bufferCapture[currentBuffer], samples * 2, 22050);
			if ((result = alGetError()) != AL_NO_ERROR) ERROR_MESSAGE

			alSourceQueueBuffers(source, 1, &buffer[currentBuffer]);
			if ((result = alGetError()) != AL_NO_ERROR) ERROR_MESSAGE

			ALint state;
			alGetSourcei(source, AL_SOURCE_STATE, &state);
			if ((result = alGetError()) != AL_NO_ERROR) ERROR_MESSAGE

			if (state != AL_PLAYING)
			{
				alSourcePlay(source);
				if ((result = alGetError()) != AL_NO_ERROR) ERROR_MESSAGE
			}

			freeBuffers--;
			if (freeBuffersMin > freeBuffers) freeBuffersMin = freeBuffers;

			ALint bufferProcessed;
			alGetSourcei(source, AL_BUFFER, &bufferProcessed);
			if ((result = alGetError()) != AL_NO_ERROR) ERROR_MESSAGE

			// unenque the buffers that aren't in the source queue anymore
			for (int i = lastBuffer; buffer[i] != bufferProcessed; i = (i + 1) %
BUFFERS)
			{
				alSourceUnqueueBuffers(source, 1, &buffer[i]);
				if ((result = alGetError()) != AL_NO_ERROR) ERROR_MESSAGE
				free(bufferCapture[i]);

				lastBuffer = (i + 1) % BUFFERS;
				freeBuffers++;
			}

			currentBuffer = (currentBuffer + 1) % BUFFERS;
			printf("%d / %d (%d)\n", freeBuffers, BUFFERS, freeBuffersMin);
		}
		else
		{
			printf("Error getting samples\n");
			return -1;
		}
	}

	if (_kbhit()) key = _getch();
}
-- 
View this message in context: http://www.nabble.com/Recording-and-Playing-Simultaneously-tf4132517.html#a11765463
Sent from the OpenAL - User mailing list archive at Nabble.com.



More information about the Openal mailing list