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

@@ -10,6 +10,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Audio/Config.hpp>
#include <Nazara/Audio/Enums.hpp>
#include <Nazara/Core/Time.hpp>
#include <Nazara/Math/Vector3.hpp>
namespace Nz
@@ -34,6 +35,7 @@ namespace Nz
virtual float GetAttenuation() const = 0;
virtual float GetMinDistance() const = 0;
virtual float GetPitch() const = 0;
virtual Time GetPlayingOffset() const = 0;
virtual Vector3f GetPosition() const = 0;
virtual UInt32 GetSampleOffset() const = 0;
virtual OffsetWithLatency GetSampleOffsetAndLatency() const = 0;
@@ -53,6 +55,7 @@ namespace Nz
virtual void SetBuffer(std::shared_ptr<AudioBuffer> audioBuffer) = 0;
virtual void SetMinDistance(float minDistance) = 0;
virtual void SetPitch(float pitch) = 0;
virtual void SetPlayingOffset(Time offset) = 0;
virtual void SetPosition(const Vector3f& position) = 0;
virtual void SetSampleOffset(UInt32 offset) = 0;
virtual void SetVelocity(const Vector3f& velocity) = 0;
@@ -70,7 +73,7 @@ namespace Nz
struct OffsetWithLatency
{
UInt64 sampleOffset;
UInt64 sourceLatency;
Time sourceLatency;
};
private:

View File

@@ -11,6 +11,7 @@
#include <Nazara/Audio/AudioBuffer.hpp>
#include <Nazara/Audio/Config.hpp>
#include <Nazara/Audio/Enums.hpp>
#include <Nazara/Core/Time.hpp>
namespace Nz
{
@@ -23,7 +24,7 @@ namespace Nz
~DummyAudioBuffer() = default;
AudioFormat GetAudioFormat() const;
UInt32 GetDuration() const;
Time GetDuration() const;
UInt64 GetSampleCount() const override;
UInt64 GetSize() const override;
UInt32 GetSampleRate() const override;

View File

@@ -30,6 +30,7 @@ namespace Nz
float GetAttenuation() const override;
float GetMinDistance() const override;
float GetPitch() const override;
Time GetPlayingOffset() const override;
Vector3f GetPosition() const override;
UInt32 GetSampleOffset() const override;
OffsetWithLatency GetSampleOffsetAndLatency() const override;
@@ -49,6 +50,7 @@ namespace Nz
void SetBuffer(std::shared_ptr<AudioBuffer> audioBuffer) override;
void SetMinDistance(float minDistance) override;
void SetPitch(float pitch) override;
void SetPlayingOffset(Time offset) override;
void SetPosition(const Vector3f& position) override;
void SetSampleOffset(UInt32 offset) override;
void SetVelocity(const Vector3f& velocity) override;
@@ -65,11 +67,11 @@ namespace Nz
private:
void RequeueBuffers();
UInt64 UpdateTime() const;
Time UpdateTime() const;
mutable std::vector<std::shared_ptr<DummyAudioBuffer>> m_queuedBuffers;
mutable std::vector<std::shared_ptr<DummyAudioBuffer>> m_processedBuffers;
mutable Clock m_playClock;
mutable MillisecondClock m_playClock;
mutable SoundStatus m_status;
Vector3f m_position;
Vector3f m_velocity;

View File

@@ -9,7 +9,7 @@ namespace Nz
{
inline DummyAudioSource::DummyAudioSource(std::shared_ptr<AudioDevice> device) :
AudioSource(std::move(device)),
m_playClock(0, true),
m_playClock(Time::Zero(), true),
m_status(SoundStatus::Stopped),
m_position(Vector3f::Zero()),
m_velocity(Vector3f::Zero()),

View File

@@ -22,7 +22,7 @@ namespace Nz
{
class AudioBuffer;
class NAZARA_AUDIO_API Music : public Resource, public SoundEmitter
class NAZARA_AUDIO_API Music final : public Resource, public SoundEmitter
{
public:
Music();
@@ -36,11 +36,12 @@ namespace Nz
void EnableLooping(bool loop) override;
UInt32 GetDuration() const override;
Time GetDuration() const override;
AudioFormat GetFormat() const;
UInt32 GetPlayingOffset() const override;
Time GetPlayingOffset() const;
UInt64 GetSampleCount() const;
UInt32 GetSampleRate() const;
UInt64 GetSampleOffset() const override;
UInt32 GetSampleRate() const override;
SoundStatus GetStatus() const override;
bool IsLooping() const override;
@@ -52,7 +53,7 @@ namespace Nz
void Pause() override;
void Play() override;
void SetPlayingOffset(UInt32 offset);
void SeekToSampleOffset(UInt64 offset);
void Stop() override;

View File

@@ -34,6 +34,7 @@ namespace Nz
float GetAttenuation() const override;
float GetMinDistance() const override;
float GetPitch() const override;
Time GetPlayingOffset() const override;
Vector3f GetPosition() const override;
UInt32 GetSampleOffset() const override;
OffsetWithLatency GetSampleOffsetAndLatency() const override;
@@ -53,6 +54,7 @@ namespace Nz
void SetBuffer(std::shared_ptr<AudioBuffer> audioBuffer) override;
void SetMinDistance(float minDistance) override;
void SetPitch(float pitch) override;
void SetPlayingOffset(Time offset) override;
void SetPosition(const Vector3f& position) override;
void SetSampleOffset(UInt32 offset) override;
void SetVelocity(const Vector3f& velocity) override;

View File

@@ -14,7 +14,7 @@
namespace Nz
{
class NAZARA_AUDIO_API Sound : public SoundEmitter
class NAZARA_AUDIO_API Sound final : public SoundEmitter
{
public:
using SoundEmitter::SoundEmitter;
@@ -27,8 +27,10 @@ namespace Nz
void EnableLooping(bool loop) override;
const std::shared_ptr<SoundBuffer>& GetBuffer() const;
UInt32 GetDuration() const override;
UInt32 GetPlayingOffset() const override;
Time GetDuration() const override;
Time GetPlayingOffset() const override;
UInt64 GetSampleOffset() const override;
UInt32 GetSampleRate() const override;
SoundStatus GetStatus() const override;
bool IsLooping() const override;
@@ -42,7 +44,8 @@ namespace Nz
void Play() override;
void SetBuffer(std::shared_ptr<SoundBuffer> soundBuffer);
void SetPlayingOffset(UInt32 offset);
void SeekToSampleOffset(UInt64 offset) override;
void Stop() override;

View File

@@ -16,6 +16,7 @@
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceManager.hpp>
#include <Nazara/Core/ResourceParameters.hpp>
#include <Nazara/Core/Time.hpp>
#include <memory>
#include <unordered_map>
@@ -52,7 +53,7 @@ namespace Nz
const std::shared_ptr<AudioBuffer>& GetAudioBuffer(AudioDevice* device);
inline UInt32 GetDuration() const;
inline Time GetDuration() const;
inline AudioFormat GetFormat() const;
inline const Int16* GetSamples() const;
inline UInt64 GetSampleCount() const;
@@ -76,7 +77,7 @@ namespace Nz
std::unordered_map<AudioDevice*, AudioDeviceEntry> m_audioBufferByDevice;
std::unique_ptr<Int16[]> m_samples;
AudioFormat m_format;
UInt32 m_duration;
Time m_duration;
UInt32 m_sampleRate;
UInt64 m_sampleCount;
};

View File

@@ -10,10 +10,8 @@ namespace Nz
/*!
* \brief Gets the duration of the sound buffer
* \return Duration of the sound buffer in milliseconds
*
* \remark Produces a NazaraError if there is no sound buffer with NAZARA_AUDIO_SAFE defined
*/
inline UInt32 SoundBuffer::GetDuration() const
inline Time SoundBuffer::GetDuration() const
{
return m_duration;
}

View File

@@ -10,6 +10,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Audio/Config.hpp>
#include <Nazara/Audio/Enums.hpp>
#include <Nazara/Core/Time.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <limits>
@@ -32,11 +33,13 @@ namespace Nz
void EnableSpatialization(bool spatialization);
float GetAttenuation() const;
virtual UInt32 GetDuration() const = 0;
virtual Time GetDuration() const = 0;
float GetMinDistance() const;
float GetPitch() const;
virtual UInt32 GetPlayingOffset() const = 0;
virtual Time GetPlayingOffset() const = 0;
Vector3f GetPosition() const;
virtual UInt64 GetSampleOffset() const = 0;
virtual UInt32 GetSampleRate() const = 0;
Vector3f GetVelocity() const;
virtual SoundStatus GetStatus() const = 0;
float GetVolume() const;
@@ -48,6 +51,9 @@ namespace Nz
virtual void Pause() = 0;
virtual void Play() = 0;
virtual void SeekToPlayingOffset(Time offset);
virtual void SeekToSampleOffset(UInt64 offset) = 0;
void SetAttenuation(float attenuation);
void SetMinDistance(float minDistance);
void SetPitch(float pitch);

View File

@@ -13,6 +13,7 @@
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceParameters.hpp>
#include <Nazara/Core/Time.hpp>
#include <mutex>
namespace Nz
@@ -35,7 +36,7 @@ namespace Nz
SoundStream() = default;
virtual ~SoundStream();
virtual UInt32 GetDuration() const = 0;
virtual Time GetDuration() const = 0;
virtual AudioFormat GetFormat() const = 0;
virtual std::mutex& GetMutex() = 0;
virtual UInt64 GetSampleCount() const = 0;