diff --git a/include/Nazara/Audio/Music.hpp b/include/Nazara/Audio/Music.hpp index 766b23514..2191003b8 100644 --- a/include/Nazara/Audio/Music.hpp +++ b/include/Nazara/Audio/Music.hpp @@ -52,7 +52,7 @@ class NAZARA_API NzMusic : public NzSoundEmitter bool OpenFromStream(NzInputStream& stream, const NzMusicParams& params = NzMusicParams()); void Pause(); - bool Play(); + void Play(); void SetPlayingOffset(nzUInt32 offset); diff --git a/include/Nazara/Audio/Sound.hpp b/include/Nazara/Audio/Sound.hpp index 1b2ac77e6..d4c670839 100644 --- a/include/Nazara/Audio/Sound.hpp +++ b/include/Nazara/Audio/Sound.hpp @@ -34,7 +34,7 @@ class NAZARA_API NzSound : public NzSoundEmitter bool LoadFromStream(NzInputStream& stream, const NzSoundBufferParams& params = NzSoundBufferParams()); void Pause(); - bool Play(); + void Play(); void SetBuffer(const NzSoundBuffer* buffer); void SetPlayingOffset(nzUInt32 offset); diff --git a/include/Nazara/Audio/SoundEmitter.hpp b/include/Nazara/Audio/SoundEmitter.hpp index 7d4d41d6a..84d9d5f9b 100644 --- a/include/Nazara/Audio/SoundEmitter.hpp +++ b/include/Nazara/Audio/SoundEmitter.hpp @@ -35,7 +35,7 @@ class NAZARA_API NzSoundEmitter bool IsSpatialized() const; virtual void Pause() = 0; - virtual bool Play() = 0; + virtual void Play() = 0; void SetAttenuation(float attenuation); void SetMinDistance(float minDistance); diff --git a/src/Nazara/Audio/Music.cpp b/src/Nazara/Audio/Music.cpp index ac2d856b8..d098970c8 100644 --- a/src/Nazara/Audio/Music.cpp +++ b/src/Nazara/Audio/Music.cpp @@ -22,7 +22,7 @@ struct NzMusicImpl NzSoundStream* stream; NzThread thread; bool loop = false; - bool playing = false; + bool streaming = false; bool paused = false; unsigned int sampleRate; }; @@ -118,7 +118,7 @@ nzSoundStatus NzMusic::GetStatus() const nzSoundStatus status = GetInternalStatus(); - if (m_impl->playing && status == nzSoundStatus_Stopped) + if (m_impl->streaming && status == nzSoundStatus_Stopped) status = nzSoundStatus_Playing; return status; @@ -154,34 +154,32 @@ bool NzMusic::OpenFromStream(NzInputStream& stream, const NzMusicParams& params) void NzMusic::Pause() { - return; + alSourcePause(m_source); } -bool NzMusic::Play() +void NzMusic::Play() { #if NAZARA_AUDIO_SAFE if (!m_impl) { NazaraError("Music not created"); - return false; + return; } #endif - /*if (m_impl->playing) + if (m_impl->streaming) { - if (m_impl->paused) + if (GetStatus() != nzSoundStatus_Playing) alSourcePlay(m_source); - else - // On repositionne au début - m_impl->stream->Seek(0); - return true; - }*/ + return; + } - m_impl->playing = true; + m_impl->stream->Seek(0); + m_impl->streaming = true; m_impl->thread = NzThread(&NzMusic::MusicThread, this); - return true; + return; } void NzMusic::Stop() @@ -194,20 +192,31 @@ void NzMusic::Stop() } #endif - if (m_impl->playing) + if (m_impl->streaming) { - m_impl->playing = false; + m_impl->streaming = false; m_impl->thread.Join(); } } bool NzMusic::FillBuffer(unsigned int buffer) { - unsigned int sampleRead = m_impl->stream->Read(&m_impl->chunkSamples[0], m_impl->chunkSamples.size()); + unsigned int sampleCount = m_impl->chunkSamples.size(); + unsigned int sampleRead = 0; + + for (;;) + { + sampleRead += m_impl->stream->Read(&m_impl->chunkSamples[sampleRead], sampleCount-sampleRead); + if (sampleRead != sampleCount && m_impl->loop) + m_impl->stream->Seek(0); + else + break; + } + if (sampleRead > 0) alBufferData(buffer, m_impl->audioFormat, &m_impl->chunkSamples[0], sampleRead*sizeof(nzInt16), m_impl->sampleRate); - return sampleRead != m_impl->chunkSamples.size(); // Fin du fichier + return sampleRead != sampleCount; // Fin du fichier (N'arrive pas en cas de loop) } void NzMusic::MusicThread() @@ -217,52 +226,40 @@ void NzMusic::MusicThread() for (unsigned int i = 0; i < NAZARA_AUDIO_STREAMEDBUFFERCOUNT; ++i) { - FillBuffer(buffers[i]); + bool eof = FillBuffer(buffers[i]); alSourceQueueBuffers(m_source, 1, &buffers[i]); + + if (eof) + break; // Nous avons fini, nous ne continuons pas } alSourcePlay(m_source); - while (m_impl->playing) + while (m_impl->streaming) { nzSoundStatus status = GetInternalStatus(); if (status == nzSoundStatus_Stopped) { - NazaraError("Stopped !"); - if (m_impl->loop) - { - m_impl->stream->Seek(0); - for (unsigned int i = 0; i < NAZARA_AUDIO_STREAMEDBUFFERCOUNT; ++i) - { - FillBuffer(buffers[i]); - alSourceQueueBuffers(m_source, 1, &buffers[i]); - } - - alSourcePlay(m_source); - } - else - m_impl->playing = false; - + m_impl->streaming = false; break; } ALint processedCount = 0; alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &processedCount); - if (processedCount > 0) - NazaraWarning(NzString::Number(processedCount)); - ALuint buffer; while (processedCount--) { alSourceUnqueueBuffers(m_source, 1, &buffer); - FillBuffer(buffer); - alSourceQueueBuffers(m_source, 1, &buffer); + if (!FillBuffer(buffer)) // Fin du fichier ? + alSourceQueueBuffers(m_source, 1, &buffer); } - NzThread::Sleep(NAZARA_AUDIO_STREAMEDBUFFERCOUNT*500); + NzThread::Sleep(50); } + alSourceStop(m_source); + ALint queuedBufferCount; alGetSourcei(m_source, AL_BUFFERS_QUEUED, &queuedBufferCount); diff --git a/src/Nazara/Audio/Sound.cpp b/src/Nazara/Audio/Sound.cpp index 9f21e3b57..009957a40 100644 --- a/src/Nazara/Audio/Sound.cpp +++ b/src/Nazara/Audio/Sound.cpp @@ -133,19 +133,17 @@ void NzSound::Pause() alSourcePause(m_source); } -bool NzSound::Play() +void NzSound::Play() { #if NAZARA_AUDIO_SAFE if (!m_buffer) { NazaraError("No sound buffer to play"); - return false; + return; } #endif alSourcePlay(m_source); - - return true; } void NzSound::SetBuffer(const NzSoundBuffer* buffer)