Audio/Music: Fix GetPlayingOffset and SetPlayingOffset
Former-commit-id: 71d19338612e84105723e28b4fc38510472e89ef
This commit is contained in:
parent
2415002fc5
commit
6d146e30c8
|
|
@ -6,6 +6,7 @@
|
||||||
#include <Nazara/Audio/Audio.hpp>
|
#include <Nazara/Audio/Audio.hpp>
|
||||||
#include <Nazara/Audio/OpenAL.hpp>
|
#include <Nazara/Audio/OpenAL.hpp>
|
||||||
#include <Nazara/Audio/SoundStream.hpp>
|
#include <Nazara/Audio/SoundStream.hpp>
|
||||||
|
#include <Nazara/Core/Mutex.hpp>
|
||||||
#include <Nazara/Core/Thread.hpp>
|
#include <Nazara/Core/Thread.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
@ -23,7 +24,9 @@ namespace Nz
|
||||||
ALenum audioFormat;
|
ALenum audioFormat;
|
||||||
std::unique_ptr<SoundStream> stream;
|
std::unique_ptr<SoundStream> stream;
|
||||||
std::vector<Int16> chunkSamples;
|
std::vector<Int16> chunkSamples;
|
||||||
|
Mutex bufferLock;
|
||||||
Thread thread;
|
Thread thread;
|
||||||
|
UInt64 processedSamples;
|
||||||
bool loop = false;
|
bool loop = false;
|
||||||
bool streaming = false;
|
bool streaming = false;
|
||||||
unsigned int sampleRate;
|
unsigned int sampleRate;
|
||||||
|
|
@ -36,15 +39,9 @@ namespace Nz
|
||||||
|
|
||||||
bool Music::Create(SoundStream* soundStream)
|
bool Music::Create(SoundStream* soundStream)
|
||||||
{
|
{
|
||||||
Destroy();
|
NazaraAssert(soundStream, "Invalid stream");
|
||||||
|
|
||||||
#if NAZARA_AUDIO_SAFE
|
Destroy();
|
||||||
if (!soundStream)
|
|
||||||
{
|
|
||||||
NazaraError("Sound stream must be valid");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
AudioFormat format = soundStream->GetFormat();
|
AudioFormat format = soundStream->GetFormat();
|
||||||
|
|
||||||
|
|
@ -54,6 +51,8 @@ namespace Nz
|
||||||
m_impl->chunkSamples.resize(format * m_impl->sampleRate); // Une seconde de samples
|
m_impl->chunkSamples.resize(format * m_impl->sampleRate); // Une seconde de samples
|
||||||
m_impl->stream.reset(soundStream);
|
m_impl->stream.reset(soundStream);
|
||||||
|
|
||||||
|
SetPlayingOffset(0);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -116,9 +115,14 @@ namespace Nz
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Prevent music thread from enqueing new buffers while we're getting the count
|
||||||
|
Nz::LockGuard lock(m_impl->bufferLock);
|
||||||
|
|
||||||
///TODO
|
ALint samples = 0;
|
||||||
return 0;
|
alGetSourcei(m_source, AL_SAMPLE_OFFSET, &samples);
|
||||||
|
|
||||||
|
return (1000ULL * (samples + (m_impl->processedSamples / m_impl->stream->GetFormat()))) / m_impl->sampleRate;
|
||||||
}
|
}
|
||||||
|
|
||||||
UInt32 Music::GetSampleCount() const
|
UInt32 Music::GetSampleCount() const
|
||||||
|
|
@ -144,7 +148,7 @@ namespace Nz
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return m_impl->stream->GetSampleRate();
|
return m_impl->sampleRate;
|
||||||
}
|
}
|
||||||
|
|
||||||
SoundStatus Music::GetStatus() const
|
SoundStatus Music::GetStatus() const
|
||||||
|
|
@ -209,22 +213,29 @@ namespace Nz
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Nous sommes déjà en train de jouer
|
// Maybe we are already playing
|
||||||
if (m_impl->streaming)
|
if (m_impl->streaming)
|
||||||
{
|
{
|
||||||
// Peut-être sommes-nous en pause
|
switch (GetStatus())
|
||||||
if (GetStatus() != SoundStatus_Playing)
|
{
|
||||||
alSourcePlay(m_source);
|
case SoundStatus_Playing:
|
||||||
|
SetPlayingOffset(0);
|
||||||
|
break;
|
||||||
|
|
||||||
return;
|
case SoundStatus_Paused:
|
||||||
|
alSourcePlay(m_source);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break; // We shouldn't be stopped
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Starting streaming's thread
|
||||||
|
m_impl->streaming = true;
|
||||||
|
m_impl->thread = Thread(&Music::MusicThread, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lancement du thread de streaming
|
|
||||||
m_impl->stream->Seek(0);
|
|
||||||
m_impl->streaming = true;
|
|
||||||
m_impl->thread = Thread(&Music::MusicThread, this);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Music::SetPlayingOffset(UInt32 offset)
|
void Music::SetPlayingOffset(UInt32 offset)
|
||||||
|
|
@ -237,7 +248,16 @@ namespace Nz
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
///TODO
|
bool isPlaying = m_impl->streaming;
|
||||||
|
|
||||||
|
if (isPlaying)
|
||||||
|
Stop();
|
||||||
|
|
||||||
|
m_impl->stream->Seek(offset);
|
||||||
|
m_impl->processedSamples = UInt64(offset) * m_impl->sampleRate * m_impl->stream->GetFormat() / 1000ULL;
|
||||||
|
|
||||||
|
if (isPlaying)
|
||||||
|
Play();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Music::Stop()
|
void Music::Stop()
|
||||||
|
|
@ -312,18 +332,29 @@ namespace Nz
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Nz::LockGuard lock(m_impl->bufferLock);
|
||||||
|
|
||||||
// On traite les buffers lus
|
// On traite les buffers lus
|
||||||
ALint processedCount = 0;
|
ALint processedCount = 0;
|
||||||
alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &processedCount);
|
alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &processedCount);
|
||||||
|
|
||||||
ALuint buffer;
|
|
||||||
while (processedCount--)
|
while (processedCount--)
|
||||||
{
|
{
|
||||||
|
ALuint buffer;
|
||||||
alSourceUnqueueBuffers(m_source, 1, &buffer);
|
alSourceUnqueueBuffers(m_source, 1, &buffer);
|
||||||
|
|
||||||
|
ALint bits, size;
|
||||||
|
alGetBufferi(buffer, AL_BITS, &bits);
|
||||||
|
alGetBufferi(buffer, AL_SIZE, &size);
|
||||||
|
|
||||||
|
if (bits != 0)
|
||||||
|
m_impl->processedSamples += (8 * size) / bits;
|
||||||
|
|
||||||
if (FillAndQueueBuffer(buffer))
|
if (FillAndQueueBuffer(buffer))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lock.Unlock();
|
||||||
|
|
||||||
// On retourne dormir un peu
|
// On retourne dormir un peu
|
||||||
Thread::Sleep(50);
|
Thread::Sleep(50);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue