I am trying to create a voice chat program using OpenAL. The networking side of things seems to be ok but I'll take advice on that =]
The problem I am having at the moment is trying to playback captured audio data from the mic.
I followed the code found here to get mic input and echo it to the speakers.
This works OK but I can't seem to playback the fully captured audio after the capturing has finished.
When some sound has been captured I store it on a list like so…
for (int i = 0; i < CAP_SIZE; i++)
{
playbackBuffer.push_back( buffer[i] );
}
and then after the capturing has finished I have tried (and failed) to use the following code to play that audio back
ALuint playbackSource;
alGenSources(1, &playbackSource);
errorCode = alGetError();
ALuint tempPlayback;
alBufferData(tempPlayback, AL_FORMAT_MONO16, &playbackBuffer.front(), playbackBuffer.size()*sizeof(ALuint), FREQ);
// Attach the playback buffer to the new playback source
alSourcei(playbackSource, AL_BUFFER, tempPlayback);
alSourcePlay(playbackSource);
ALint sState = 0;
do
{
alGetSourcei(playbackSource, AL_SOURCE_STATE, &sState);
}
while ((sState == AL_PLAYING));
When debugging I can see that playbackBuffer has a lot of data in it and there is a split second of noise played at this point but nowhere near that which was captured.
EDIT: I have added the line
alGenBuffers(1, &tempPlayback);
after creating the tempPlayback and before the call to alBufferData(…) and this now plays for the correct amount of time but all I hear is a high-pitched noise the whole way through.
Best Answer
Here's a really simple program that captures audio for 5 seconds, pauses, then captures 5 more seconds of audio, and finally plays it all back (I used it to debug a problem with capture one time).
It worked on mint 13 linux laptop. Mint is like ubuntu 12.04
Compile with this: