我想制作一个简单的语音聊天程序并使用OpenAL。一方面我捕获语音并通过互联网发送(使用Boost.Asio),所以在另一方面我只需要播放捕获的声音。但我的问题是,我设法捕获语音说,5秒,但在另一边(我的意思是另一个客户端,计算机)播放5秒语音只是第一秒而不是整个数据。我不明白我的代码有什么问题,请帮助我,你的任何建议对我来说都非常重要。 提前谢谢!!!
captureDev = alcCaptureOpenDevice(NULL, 8000, AL_FORMAT_MONO16, 800);
// ... //...
lastTime = currentTime;
// Capture (roughly) five seconds of audio
ALubyte captureBuffer[104857];
ALubyte *captureBufPtr;
alcCaptureStart(captureDev);
samplesCaptured = 0;
captureBufPtr = captureBuffer;
while (currentTime < (lastTime + 5))
{
// Get the number of samples available
alcGetIntegerv(captureDev, ALC_CAPTURE_SAMPLES, 1, &samplesAvailable);
// Copy the samples to our capture buffer
if (samplesAvailable > 0)
{
alcCaptureSamples(captureDev, captureBufPtr, samplesAvailable);
samplesCaptured += samplesAvailable;
printf("Captured %d samples (adding %d)\r", samplesCaptured,
samplesAvailable);
fflush(stdout);
// Advance the buffer (two bytes per sample * number of samples)
captureBufPtr += samplesAvailable * 2;
}
// Wait for a bit
std::this_thread::sleep_for(std::chrono::seconds(1));
// Update the clock
currentTime = time(NULL);
}
所以我发送捕获的数据 captureBuffer 与 samplesCaptured ,另一方面(客户端)我收到它们并想玩。
// Generate an OpenAL buffer for the captured data
alGenBuffers(1, &buffer);
alGenSources(1, &source);
alBufferData(buffer, AL_FORMAT_MONO16, captureBuffer, samplesCaptured * 2, 8000);
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(source);
// Wait for the source to stop playing
playState = AL_PLAYING;
while (playState == AL_PLAYING)
{
printf(" source %d is playing...\r", source);
fflush(stdout);
alGetSourcei(source, AL_SOURCE_STATE, &playState);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
正如我上面所说,它不是整个数据。