Fix some streaming behaviour in XAudio2 driver

This commit is contained in:
kaetemi 2014-02-16 20:36:58 +01:00
parent bb156457c0
commit 3b38848903

View file

@ -242,9 +242,13 @@ void CSourceXAudio2::updateState()
if (!_AdpcmUtility->getSourceData()) if (!_AdpcmUtility->getSourceData())
{ {
_SoundDriver->getXAudio2()->CommitChanges(_OperationSet); _SoundDriver->getXAudio2()->CommitChanges(_OperationSet);
if (FAILED(_SourceVoice->Stop(0))) if (!_BufferStreaming)
nlwarning(NLSOUND_XAUDIO2_PREFIX "FAILED Stop"); {
_IsPlaying = false; nlinfo(NLSOUND_XAUDIO2_PREFIX "Stop");
if (FAILED(_SourceVoice->Stop(0)))
nlwarning(NLSOUND_XAUDIO2_PREFIX "FAILED Stop");
_IsPlaying = false;
}
} }
} }
else else
@ -254,9 +258,13 @@ void CSourceXAudio2::updateState()
if (!voice_state.BuffersQueued) if (!voice_state.BuffersQueued)
{ {
_SoundDriver->getXAudio2()->CommitChanges(_OperationSet); _SoundDriver->getXAudio2()->CommitChanges(_OperationSet);
if (FAILED(_SourceVoice->Stop(0))) if (!_BufferStreaming)
nlwarning(NLSOUND_XAUDIO2_PREFIX "FAILED Stop"); {
_IsPlaying = false; nlinfo(NLSOUND_XAUDIO2_PREFIX "Stop");
if (FAILED(_SourceVoice->Stop(0)))
nlwarning(NLSOUND_XAUDIO2_PREFIX "FAILED Stop");
_IsPlaying = false;
}
} }
} }
} }
@ -265,6 +273,8 @@ void CSourceXAudio2::updateState()
/// (Internal) Submit a buffer to the XAudio2 source voice. /// (Internal) Submit a buffer to the XAudio2 source voice.
void CSourceXAudio2::submitBuffer(CBufferXAudio2 *ibuffer) void CSourceXAudio2::submitBuffer(CBufferXAudio2 *ibuffer)
{ {
// nldebug(NLSOUND_XAUDIO2_PREFIX "submitBuffer %u", (uint32)(void *)this);
nlassert(_SourceVoice); nlassert(_SourceVoice);
nlassert(ibuffer->getFormat() == _Format nlassert(ibuffer->getFormat() == _Format
&& ibuffer->getChannels() == _Channels && ibuffer->getChannels() == _Channels
@ -278,9 +288,9 @@ void CSourceXAudio2::submitBuffer(CBufferXAudio2 *ibuffer)
{ {
XAUDIO2_BUFFER buffer; XAUDIO2_BUFFER buffer;
buffer.AudioBytes = ibuffer->getSize(); buffer.AudioBytes = ibuffer->getSize();
buffer.Flags = _IsLooping || _BufferStreaming ? 0 : XAUDIO2_END_OF_STREAM; buffer.Flags = (_IsLooping || _BufferStreaming) ? 0 : XAUDIO2_END_OF_STREAM;
buffer.LoopBegin = 0; buffer.LoopBegin = 0;
buffer.LoopCount = _IsLooping ? XAUDIO2_LOOP_INFINITE : 0; buffer.LoopCount = (_IsLooping && !_BufferStreaming) ? XAUDIO2_LOOP_INFINITE : 0;
buffer.LoopLength = 0; buffer.LoopLength = 0;
buffer.pAudioData = const_cast<BYTE *>(ibuffer->getData()); buffer.pAudioData = const_cast<BYTE *>(ibuffer->getData());
buffer.pContext = ibuffer; buffer.pContext = ibuffer;
@ -336,7 +346,25 @@ void CSourceXAudio2::setupVoiceSends()
void CSourceXAudio2::setStreaming(bool streaming) void CSourceXAudio2::setStreaming(bool streaming)
{ {
nlassert(!_IsPlaying); nlassert(!_IsPlaying);
// nldebug(NLSOUND_XAUDIO2_PREFIX "setStreaming %i", (uint32)streaming);
if (_SourceVoice)
{
XAUDIO2_VOICE_STATE voice_state;
_SourceVoice->GetState(&voice_state);
if (!voice_state.BuffersQueued)
{
nlwarning(NLSOUND_XAUDIO2_PREFIX "Switched streaming mode while buffer still queued!?! Flush");
_SoundDriver->getXAudio2()->CommitChanges(_OperationSet);
if (FAILED(_SourceVoice->FlushSourceBuffers()))
nlwarning(NLSOUND_XAUDIO2_PREFIX "FAILED FlushSourceBuffers");
}
}
_BufferStreaming = streaming; _BufferStreaming = streaming;
// nldebug(NLSOUND_XAUDIO2_PREFIX "setStreaming done %i", (uint32)streaming);
} }
/// Set the buffer that will be played (no streaming) /// Set the buffer that will be played (no streaming)
@ -344,6 +372,8 @@ void CSourceXAudio2::setStaticBuffer(IBuffer *buffer)
{ {
nlassert(!_BufferStreaming); nlassert(!_BufferStreaming);
// nldebug(NLSOUND_XAUDIO2_PREFIX "setStaticBuffer");
// if (buffer) // nldebug(NLSOUND_XAUDIO2_PREFIX "setStaticBuffer %s", _SoundDriver->getStringMapper()->unmap(buffer->getName()).c_str()); // if (buffer) // nldebug(NLSOUND_XAUDIO2_PREFIX "setStaticBuffer %s", _SoundDriver->getStringMapper()->unmap(buffer->getName()).c_str());
// else // nldebug(NLSOUND_XAUDIO2_PREFIX "setStaticBuffer NULL"); // else // nldebug(NLSOUND_XAUDIO2_PREFIX "setStaticBuffer NULL");
@ -364,6 +394,8 @@ IBuffer *CSourceXAudio2::getStaticBuffer()
/// Should be called by a thread which checks countStreamingBuffers every 100ms. /// Should be called by a thread which checks countStreamingBuffers every 100ms.
void CSourceXAudio2::submitStreamingBuffer(IBuffer *buffer) void CSourceXAudio2::submitStreamingBuffer(IBuffer *buffer)
{ {
// nldebug(NLSOUND_XAUDIO2_PREFIX "submitStreamingBuffer");
nlassert(_BufferStreaming); nlassert(_BufferStreaming);
IBuffer::TBufferFormat bufferFormat; IBuffer::TBufferFormat bufferFormat;
@ -414,9 +446,10 @@ uint CSourceXAudio2::countStreamingBuffers() const
/// Set looping on/off for future playbacks (default: off) /// Set looping on/off for future playbacks (default: off)
void CSourceXAudio2::setLooping(bool l) void CSourceXAudio2::setLooping(bool l)
{ {
// nldebug(NLSOUND_XAUDIO2_PREFIX "setLooping %u", (uint32)l);
nlassert(!_BufferStreaming); nlassert(!_BufferStreaming);
// nldebug(NLSOUND_XAUDIO2_PREFIX "setLooping %u", (uint32)l);
if (_IsLooping != l) if (_IsLooping != l)
{ {
_IsLooping = l; _IsLooping = l;
@ -455,7 +488,7 @@ void CSourceXAudio2::setLooping(bool l)
_SourceVoice->GetState(&voice_state); _SourceVoice->GetState(&voice_state);
if (voice_state.BuffersQueued) if (voice_state.BuffersQueued)
{ {
nlwarning(NLSOUND_XAUDIO2_PREFIX "not playing but buffer already queued???"); nlwarning(NLSOUND_XAUDIO2_PREFIX "Not playing but buffer already queued while switching loop mode!?! Flush and requeue");
if (FAILED(_SourceVoice->FlushSourceBuffers())) if (FAILED(_SourceVoice->FlushSourceBuffers()))
nlwarning(NLSOUND_XAUDIO2_PREFIX "FAILED FlushSourceBuffers"); nlwarning(NLSOUND_XAUDIO2_PREFIX "FAILED FlushSourceBuffers");
// queue buffer with correct looping parameters // queue buffer with correct looping parameters
@ -580,7 +613,7 @@ bool CSourceXAudio2::preparePlay(IBuffer::TBufferFormat bufferFormat, uint8 chan
/// Play the static buffer (or stream in and play). /// Play the static buffer (or stream in and play).
bool CSourceXAudio2::play() bool CSourceXAudio2::play()
{ {
// nldebug(NLSOUND_XAUDIO2_PREFIX "play"); // nldebug(NLSOUND_XAUDIO2_PREFIX "play");
// Commit 3D changes before starting play // Commit 3D changes before starting play