我第一次在C++中使用OpenAL生成編碼聲音。 我想要做的是產生無限的竇波成雙緩衝方式。 而問題是,聲音是閃爍/滯後。我認爲這是在緩衝之間,我不知道爲什麼它是這樣的。我的OpenAL C++音頻流緩衝區gliching
我的代碼:
void _OpenALEngine::play()
{
if(!m_running && !m_threadRunning)
{
ALfloat sourcePos[] = {0,0,0};
ALfloat sourceVel[] = {0,0,0};
ALfloat sourceOri[] = {0,0,0,0,0,0};
alGenSources(1, &FSourceID);
alSourcefv (FSourceID, AL_POSITION, sourcePos);
alSourcefv (FSourceID, AL_VELOCITY, sourceVel);
alSourcefv (FSourceID, AL_DIRECTION, sourceOri);
GetALError();
ALuint FBufferID[2];
alGenBuffers(2, &FBufferID[0]);
GetALError();
// Gain
ALfloat listenerPos[] = {0,0,0};
ALfloat listenerVel[] = {0,0,0};
ALfloat listenerOri[] = {0,0,0,0,0,0};
alListenerf(AL_GAIN, 1.0);
alListenerfv(AL_POSITION, listenerPos);
alListenerfv(AL_VELOCITY, listenerVel);
alListenerfv(AL_ORIENTATION, listenerOri);
GetALError();
alSourceQueueBuffers(FSourceID, 2, &FBufferID[0]);
GetALError();
alSourcePlay(FSourceID);
GetALError();
m_running = true;
m_threadRunning = true;
Threading::Thread thread(Threading::ThreadStart(this, &_OpenALEngine::threadPlaying));
thread.Start();
}
}
Void _OpenALEngine::threadPlaying()
{
while(m_running)
{
// Check how much data is processed in OpenAL's internal queue.
ALint Processed;
alGetSourcei(FSourceID, AL_BUFFERS_PROCESSED, &Processed);
GetALError();
// Add more buffers while we need them.
while (Processed--)
{
alSourceUnqueueBuffers(FSourceID, 1, &BufID);
runBuffer(); // <--- Generate the sinus wave and submit the Array to the submitBuffer method.
alSourceQueueBuffers(FSourceID, 1, &BufID);
ALint val;
alGetSourcei(FSourceID, AL_SOURCE_STATE, &val);
if(val != AL_PLAYING)
{
alSourcePlay(FSourceID);
}
}
// Don't kill the CPU.
Thread::Sleep(1);
}
m_threadRunning = false;
return Void();
}
void _OpenALEngine::submitBuffer(byte* buffer, int length)
{
// Submit more data to OpenAL
alBufferData(BufID, AL_FORMAT_MONO8, buffer, length * sizeof(byte), 44100);
}
我生成在runBuffer()方法的正弦波。而正弦發生器是正確的,因爲當我將緩衝區陣列從4096增加到40960時,閃爍/滯後聲音的間隔更大。非常感謝你,如果有人知道這個問題,並會分享它:)
讓我們知道如何爲解決 –
沒有抱歉,我沒有得到它的工作。現在我使用Windows多媒體庫,而不是現在: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4422&lngWId=3 但很快我會再試一次並解決它。 – Alexander