Replace float/UInt64 durations by a more precise Time class (#388)

Improve Clock class with atomic RestartIfOver method and allows to choose required precision
This commit is contained in:
Jérôme Leclercq
2022-12-29 21:31:46 +01:00
committed by GitHub
parent 1de5f65536
commit dd421a6385
84 changed files with 1278 additions and 663 deletions

View File

@@ -15,9 +15,9 @@ namespace Nz
return m_format;
}
UInt32 DummyAudioBuffer::GetDuration() const
Time DummyAudioBuffer::GetDuration() const
{
return SafeCast<UInt32>((1000ULL * m_sampleCount / (GetChannelCount(m_format) * m_sampleRate)));
return Time::Microseconds((1'000'000LL * m_sampleCount / (GetChannelCount(m_format) * m_sampleRate)));
}
UInt64 DummyAudioBuffer::GetSampleCount() const

View File

@@ -39,6 +39,27 @@ namespace Nz
return m_pitch;
}
Time DummyAudioSource::GetPlayingOffset() const
{
if (m_status == SoundStatus::Stopped)
return Time::Zero(); //< Always return 0 when stopped, to mimic OpenAL behavior
Time bufferTime = UpdateTime();
Time playingOffset = Time::Zero();
// All processed buffers count
for (const auto& processedBuffer : m_processedBuffers)
playingOffset += processedBuffer->GetDuration();
if (!m_queuedBuffers.empty())
{
auto& frontBuffer = m_queuedBuffers.front();
playingOffset += std::min(bufferTime, frontBuffer->GetDuration());
}
return playingOffset;
}
Vector3f DummyAudioSource::GetPosition() const
{
return m_position;
@@ -49,7 +70,7 @@ namespace Nz
if (m_status == SoundStatus::Stopped)
return 0; //< Always return 0 when stopped, to mimic OpenAL behavior
UInt64 bufferTime = UpdateTime();
Time bufferTime = UpdateTime();
UInt64 sampleOffset = 0;
// All processed buffers count in sample offset
@@ -59,7 +80,7 @@ namespace Nz
if (!m_queuedBuffers.empty())
{
auto& frontBuffer = m_queuedBuffers.front();
UInt64 bufferOffset = bufferTime * frontBuffer->GetSampleRate() / 1000;
UInt64 bufferOffset = bufferTime.AsMicroseconds() * frontBuffer->GetSampleRate() / 1'000'000ll;
UInt64 bufferDuration = frontBuffer->GetSampleCount() / GetChannelCount(frontBuffer->GetAudioFormat());
sampleOffset += std::min(bufferOffset, bufferDuration);
@@ -72,7 +93,7 @@ namespace Nz
{
OffsetWithLatency info;
info.sampleOffset = GetSampleOffset() * 1000;
info.sourceLatency = 0;
info.sourceLatency = Time::Zero();
return info;
}
@@ -120,19 +141,19 @@ namespace Nz
void DummyAudioSource::Play()
{
if (m_status == SoundStatus::Paused)
m_playClock.Unpause();
else
if (m_status != SoundStatus::Paused)
{
// playing or stopped, restart
RequeueBuffers();
// special case, we are stopped but SetSampleOffset has been called
if (m_status == SoundStatus::Stopped && m_playClock.GetMilliseconds() != 0)
if (m_status == SoundStatus::Stopped && m_playClock.GetElapsedTime() != Time::Zero())
m_playClock.Unpause();
else
m_playClock.Restart(); //< already playing or stopped, restart from beginning
}
else
m_playClock.Unpause();
m_status = SoundStatus::Playing;
}
@@ -161,6 +182,13 @@ namespace Nz
m_pitch = pitch;
}
void DummyAudioSource::SetPlayingOffset(Time offset)
{
// Next UpdateTime call will handle this properly
RequeueBuffers();
m_playClock.Restart(offset, m_playClock.IsPaused());
}
void DummyAudioSource::SetPosition(const Vector3f& position)
{
m_position = position;
@@ -187,7 +215,7 @@ namespace Nz
if (!m_queuedBuffers.empty())
{
UInt64 timeOffset = 1'000'000ULL * offset / m_queuedBuffers.front()->GetSampleRate();
Time timeOffset = Time::Microseconds(1'000'000ll * offset / m_queuedBuffers.front()->GetSampleRate());
m_playClock.Restart(timeOffset, m_playClock.IsPaused());
}
else
@@ -206,7 +234,7 @@ namespace Nz
void DummyAudioSource::Stop()
{
m_playClock.Restart(0, true);
m_playClock.Restart(Time::Zero(), true);
m_status = SoundStatus::Stopped;
}
@@ -246,9 +274,10 @@ namespace Nz
}
}
UInt64 DummyAudioSource::UpdateTime() const
Time DummyAudioSource::UpdateTime() const
{
UInt64 currentTime = m_playClock.GetMilliseconds();
Time currentTime = m_playClock.GetElapsedTime();
bool isPaused = m_playClock.IsPaused();
while (!m_queuedBuffers.empty() && currentTime >= m_queuedBuffers.front()->GetDuration())
{
@@ -278,10 +307,14 @@ namespace Nz
}
}
else
{
m_status = SoundStatus::Stopped;
currentTime = Time::Zero();
isPaused = m_playClock.IsPaused();
}
}
m_playClock.Restart(currentTime * 1000, m_playClock.IsPaused()); //< Adjust time
m_playClock.Restart(currentTime, isPaused); //< Adjust time
return currentTime;
}
}

View File

@@ -105,7 +105,7 @@ namespace Nz
drwav_uninit(&m_decoder);
}
UInt32 GetDuration() const override
Time GetDuration() const override
{
return m_duration;
}
@@ -172,7 +172,7 @@ namespace Nz
m_format = *formatOpt;
m_duration = static_cast<UInt32>(1000ULL * m_decoder.totalPCMFrameCount / m_decoder.sampleRate);
m_duration = Time::Microseconds(1'000'000LL * m_decoder.totalPCMFrameCount / m_decoder.sampleRate);
m_sampleCount = m_decoder.totalPCMFrameCount * m_decoder.channels;
m_sampleRate = m_decoder.sampleRate;
@@ -230,7 +230,7 @@ namespace Nz
std::vector<Int16> m_mixBuffer;
AudioFormat m_format;
drwav m_decoder;
UInt32 m_duration;
Time m_duration;
UInt32 m_sampleRate;
UInt64 m_readSampleCount;
UInt64 m_sampleCount;

View File

@@ -265,7 +265,7 @@ namespace Nz
}
}
UInt32 GetDuration() const override
Time GetDuration() const override
{
return m_duration;
}
@@ -333,7 +333,7 @@ namespace Nz
m_sampleCount = frameCount * m_channelCount;
m_sampleRate = meta->data.stream_info.sample_rate;
m_duration = UInt32(1000ULL * frameCount / m_sampleRate);
m_duration = Time::Microseconds(1'000'000LL * frameCount / m_sampleRate);
};
FLAC__StreamDecoderInitStatus status = FLAC__stream_decoder_init_stream(decoder, &FlacReadCallback, &FlacSeekCallback, &FlacTellCallback, &FlacLengthCallback, &FlacEofCallback, &WriteCallback, &MetadataCallback, &ErrorCallback, &m_userData);
@@ -477,8 +477,8 @@ namespace Nz
FLAC__StreamDecoder* m_decoder;
AudioFormat m_format;
FlacUserdata m_userData;
Time m_duration;
UInt32 m_channelCount;
UInt32 m_duration;
UInt32 m_sampleRate;
UInt64 m_readSampleCount;
UInt64 m_sampleCount;

View File

@@ -184,7 +184,7 @@ namespace Nz
ov_clear(&m_decoder);
}
UInt32 GetDuration() const override
Time GetDuration() const override
{
return m_duration;
}
@@ -258,7 +258,7 @@ namespace Nz
UInt64 frameCount = UInt64(ov_pcm_total(&m_decoder, -1));
m_channelCount = info->channels;
m_duration = UInt32(1000ULL * frameCount / info->rate);
m_duration = Time::Microseconds(1'000'000LL * frameCount / info->rate);
m_sampleCount = UInt64(frameCount * info->channels);
m_sampleRate = info->rate;
@@ -319,8 +319,8 @@ namespace Nz
std::vector<Int16> m_mixBuffer;
AudioFormat m_format;
OggVorbis_File m_decoder;
Time m_duration;
UInt32 m_channelCount;
UInt32 m_duration;
UInt32 m_sampleRate;
UInt64 m_sampleCount;
bool m_mixToMono;

View File

@@ -130,7 +130,7 @@ namespace Nz
mp3dec_ex_close(&m_decoder);
}
UInt32 GetDuration() const override
Time GetDuration() const override
{
return m_duration;
}
@@ -214,7 +214,7 @@ namespace Nz
m_format = *formatOpt;
m_duration = static_cast<UInt32>(1000ULL * m_decoder.samples / (m_decoder.info.hz * m_decoder.info.channels));
m_duration = Time::Microseconds(1'000'000LL * m_decoder.samples / (m_decoder.info.hz * m_decoder.info.channels));
m_sampleCount = m_decoder.samples;
m_sampleRate = m_decoder.info.hz;
@@ -275,7 +275,7 @@ namespace Nz
AudioFormat m_format;
mp3dec_ex_t m_decoder;
mp3dec_io_t m_io;
UInt32 m_duration;
Time m_duration;
UInt32 m_sampleRate;
UInt64 m_readSampleCount;
UInt64 m_sampleCount;

View File

@@ -67,7 +67,7 @@ namespace Nz
m_chunkSamples.resize(GetChannelCount(format) * m_sampleRate); // One second of samples
m_stream = std::move(soundStream);
SetPlayingOffset(0);
SeekToSampleOffset(0);
return true;
}
@@ -102,7 +102,7 @@ namespace Nz
*
* \remark Music must be valid when calling this function
*/
UInt32 Music::GetDuration() const
Time Music::GetDuration() const
{
NazaraAssert(m_stream, "Music not created");
@@ -123,30 +123,30 @@ namespace Nz
}
/*!
* \brief Gets the current offset in the music
* \return Offset in milliseconds (works with entire seconds)
*
* \remark Music must be valid when calling this function
* \brief Gets the current playing offset of the music
* \return Time offset
*/
UInt32 Music::GetPlayingOffset() const
Time Music::GetPlayingOffset() const
{
NazaraAssert(m_stream, "Music not created");
if (!m_streaming)
return 0;
return Time::Zero();
// Prevent music thread from enqueuing new buffers while we're getting the count
std::lock_guard<std::recursive_mutex> lock(m_sourceLock);
UInt32 sampleOffset = m_source->GetSampleOffset();
UInt32 playingOffset = SafeCast<UInt32>((1000ULL * (sampleOffset + (m_processedSamples / GetChannelCount(m_stream->GetFormat())))) / m_sampleRate);
UInt32 duration = m_stream->GetDuration();
if (playingOffset > duration)
Time playingOffset = m_source->GetPlayingOffset();
Time processedTime = Time::Microseconds(1'000'000ll * m_processedSamples / (GetChannelCount(m_stream->GetFormat()) * m_sampleRate));
playingOffset += processedTime;
Time sampleCount = m_stream->GetDuration();
if (playingOffset > sampleCount)
{
if (m_looping)
playingOffset %= duration;
playingOffset %= sampleCount;
else
playingOffset = 0; //< stopped
playingOffset = Time::Zero(); //< stopped
}
return playingOffset;
@@ -165,6 +165,33 @@ namespace Nz
return m_stream->GetSampleCount();
}
/*!
* \brief Gets the current offset in the music
* \return Offset in samples
*/
UInt64 Music::GetSampleOffset() const
{
NazaraAssert(m_stream, "Music not created");
if (!m_streaming)
return 0;
// Prevent music thread from enqueuing new buffers while we're getting the count
std::lock_guard<std::recursive_mutex> lock(m_sourceLock);
UInt64 sampleOffset = m_processedSamples + m_source->GetSampleOffset();
UInt64 sampleCount = m_stream->GetSampleCount();
if (sampleOffset > sampleCount)
{
if (m_looping)
sampleOffset %= sampleCount;
else
sampleOffset = 0; //< stopped
}
return sampleOffset;
}
/*!
* \brief Gets the rates of sample in the music
* \return Rate of sample in Hertz (Hz)
@@ -296,7 +323,7 @@ namespace Nz
switch (GetStatus())
{
case SoundStatus::Playing:
SetPlayingOffset(0);
SeekToSampleOffset(0);
break;
case SoundStatus::Paused:
@@ -325,11 +352,11 @@ namespace Nz
*
* If the music is not playing, this sets the playing offset for the next Play call
*
* \param offset The offset in milliseconds
* \param offset The offset in samples
*
* \remark Music must be valid when calling this function
*/
void Music::SetPlayingOffset(UInt32 offset)
void Music::SeekToSampleOffset(UInt64 offset)
{
NazaraAssert(m_stream, "Music not created");
@@ -339,7 +366,7 @@ namespace Nz
if (isPlaying)
StopThread();
UInt64 sampleOffset = UInt64(offset) * m_sampleRate * GetChannelCount(m_stream->GetFormat()) / 1000ULL;
UInt64 sampleOffset = offset * GetChannelCount(m_stream->GetFormat());
m_processedSamples = sampleOffset;
m_streamOffset = sampleOffset;
@@ -356,7 +383,7 @@ namespace Nz
void Music::Stop()
{
StopThread();
SetPlayingOffset(0);
SeekToSampleOffset(0);
}
bool Music::FillAndQueueBuffer(std::shared_ptr<AudioBuffer> buffer)

View File

@@ -62,6 +62,29 @@ namespace Nz
return pitch;
}
Time OpenALSource::GetPlayingOffset() const
{
GetDevice().MakeContextCurrent();
#ifdef AL_SOFT_source_latency
if (GetDevice().IsExtensionSupported(OpenALExtension::SourceLatency))
{
// alGetSourcedvSOFT has extra precision thanks to double
ALdouble playingOffset;
m_library.alGetSourcedvSOFT(m_sourceId, AL_SEC_OFFSET, &playingOffset);
return Time::Seconds(playingOffset);
}
else
#endif
{
ALfloat playingOffset;
m_library.alGetSourcefv(m_sourceId, AL_SEC_OFFSET, &playingOffset);
return Time::Seconds(playingOffset);
}
}
Vector3f OpenALSource::GetPosition() const
{
GetDevice().MakeContextCurrent();
@@ -94,14 +117,14 @@ namespace Nz
m_library.alGetSourcei64vSOFT(m_sourceId, AL_SAMPLE_OFFSET_LATENCY_SOFT, values.data());
offsetWithLatency.sampleOffset = ((values[0] & 0xFFFFFFFF00000000) >> 32) * 1'000;
offsetWithLatency.sourceLatency = values[1] / 1'000;
offsetWithLatency.sourceLatency = Time::Nanoseconds(values[1] / 1'000);
}
else
#endif
{
offsetWithLatency.sampleOffset = GetSampleOffset() * 1'000;
offsetWithLatency.sourceLatency = 0;
offsetWithLatency.sourceLatency = Time::Zero();
}
return offsetWithLatency;
@@ -239,6 +262,19 @@ namespace Nz
m_library.alSourcef(m_sourceId, AL_PITCH, pitch);
}
void OpenALSource::SetPlayingOffset(Time offset)
{
GetDevice().MakeContextCurrent();
#ifdef AL_SOFT_source_latency
if (GetDevice().IsExtensionSupported(OpenALExtension::SourceLatency))
// alGetSourcedvSOFT has extra precision thanks to double
m_library.alSourcedSOFT(m_sourceId, AL_SEC_OFFSET, offset.AsSeconds<ALdouble>());
else
#endif
m_library.alSourcef(m_sourceId, AL_SEC_OFFSET, offset.AsSeconds<ALfloat>());
}
void OpenALSource::SetPosition(const Vector3f& position)
{
GetDevice().MakeContextCurrent();

View File

@@ -72,7 +72,7 @@ namespace Nz
*
* \remark Produces a NazaraError if there is no buffer
*/
UInt32 Sound::GetDuration() const
Time Sound::GetDuration() const
{
NazaraAssert(m_buffer, "Invalid sound buffer");
@@ -80,13 +80,30 @@ namespace Nz
}
/*!
* \brief Gets the current offset in the sound
* \return Offset in milliseconds (works with entire seconds)
* \brief Gets the current playing offset of the sound
* \return Offset
*/
UInt32 Sound::GetPlayingOffset() const
Time Sound::GetPlayingOffset() const
{
UInt32 sampleCount = m_source->GetSampleOffset();
return SafeCast<UInt32>(1000ULL * sampleCount / m_buffer->GetSampleRate());
return m_source->GetPlayingOffset();
}
/*!
* \brief Gets the current sample offset of the sound
* \return Offset
*/
UInt64 Sound::GetSampleOffset() const
{
return m_source->GetSampleOffset();
}
/*!
* \brief Gets the sample rate of the sound
* \return Offset
*/
UInt32 Sound::GetSampleRate() const
{
return m_buffer->GetSampleRate();
}
/*!
@@ -220,19 +237,17 @@ namespace Nz
}
/*!
* \brief Sets the playing offset for the sound
* \brief Sets the source to a sample offset
*
* \param offset Offset in the sound in milliseconds
* \param offset Sample offset
*/
void Sound::SetPlayingOffset(UInt32 offset)
void Sound::SeekToSampleOffset(UInt64 offset)
{
m_source->SetSampleOffset(SafeCast<UInt32>(UInt64(offset) * m_buffer->GetSampleRate() / 1000));
m_source->SetSampleOffset(SafeCast<UInt32>(offset));
}
/*!
* \brief Stops the sound
*
* \remark This is one of the only function that can be called on a moved sound (and does nothing)
*/
void Sound::Stop()
{

View File

@@ -47,7 +47,7 @@ namespace Nz
NazaraAssert(sampleRate > 0, "sample rate must be different from zero");
NazaraAssert(samples, "invalid samples");
m_duration = SafeCast<UInt32>((1000ULL*sampleCount / (GetChannelCount(format) * sampleRate)));
m_duration = Time::Microseconds((1'000'000LL * sampleCount / (GetChannelCount(format) * sampleRate)));
m_format = format;
m_sampleCount = sampleCount;
m_sampleRate = sampleRate;

View File

@@ -105,6 +105,17 @@ namespace Nz
return m_source->IsSpatializationEnabled();
}
/*!
* \brief Seek the sound to a point in time
*
* \param offset Time offset to seek
*/
void SoundEmitter::SeekToPlayingOffset(Time offset)
{
UInt64 microseconds = static_cast<UInt64>(std::max(offset.AsMicroseconds(), Int64(0)));
SeekToSampleOffset(SafeCast<UInt32>(microseconds * GetSampleRate() / 1'000'000));
}
/*!
* \brief Sets the attenuation
*