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

@@ -9,6 +9,8 @@ std::filesystem::path GetAssetDir();
SCENARIO("Music", "[AUDIO][MUSIC]")
{
using namespace Nz::Literals;
GIVEN("A music")
{
Nz::Music music;
@@ -19,9 +21,9 @@ SCENARIO("Music", "[AUDIO][MUSIC]")
THEN("We can ask the informations of the file")
{
CHECK(music.GetDuration() == 63059); // 1 min 03 = 63s = 63000ms
CHECK(music.GetDuration() == 63'059'591_us); // 1 min 03 = 63s = 63000ms
CHECK(music.GetFormat() == Nz::AudioFormat::I16_Stereo);
CHECK(music.GetPlayingOffset() == 0);
CHECK(music.GetPlayingOffset() == 0_ms);
CHECK(music.GetSampleCount() <= 64 * 44100 * 2); // * 2 (stereo)
CHECK(music.GetSampleCount() >= 63 * 44100 * 2); // * 2 (stereo)
CHECK(music.GetSampleRate() == 44100 /* Hz */);
@@ -30,7 +32,7 @@ SCENARIO("Music", "[AUDIO][MUSIC]")
CHECK(music.IsSpatializationEnabled());
CHECK(music.GetMinDistance() == 1.f);
CHECK(music.GetPitch() == 1.f);
CHECK(music.GetPlayingOffset() == 0);
CHECK(music.GetPlayingOffset() == 0_ms);
CHECK(music.GetPosition() == Nz::Vector3f::Zero());
CHECK(music.GetVelocity() == Nz::Vector3f::Zero());
CHECK(music.GetVolume() == 1.f);
@@ -42,53 +44,54 @@ SCENARIO("Music", "[AUDIO][MUSIC]")
music.Play();
CHECK(music.GetStatus() == Nz::SoundStatus::Playing);
std::this_thread::sleep_for(std::chrono::seconds(1));
CHECK(music.GetPlayingOffset() >= 950);
std::this_thread::sleep_for(std::chrono::seconds(1));
Nz::Time t = music.GetPlayingOffset();
CHECK(music.GetPlayingOffset() >= 950_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
CHECK(music.GetPlayingOffset() <= 1500);
CHECK(music.GetPlayingOffset() <= 1500_ms);
music.SetPlayingOffset(4200);
music.SeekToPlayingOffset(4200_ms);
CHECK(music.GetStatus() == Nz::SoundStatus::Playing);
CHECK(music.GetPlayingOffset() >= 4150);
CHECK(music.GetPlayingOffset() < 4500);
CHECK(music.GetPlayingOffset() >= 4150_ms);
CHECK(music.GetPlayingOffset() < 4500_ms);
CHECK(music.GetStatus() == Nz::SoundStatus::Playing);
music.Pause();
Nz::UInt32 playingOffset = music.GetPlayingOffset();
Nz::Time playingOffset = music.GetPlayingOffset();
CHECK(music.GetStatus() == Nz::SoundStatus::Paused);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
CHECK(music.GetStatus() == Nz::SoundStatus::Paused);
CHECK(music.GetPlayingOffset() == playingOffset);
music.SetPlayingOffset(3500);
music.SeekToPlayingOffset(3500_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
CHECK(music.GetPlayingOffset() == 3500);
CHECK(music.GetPlayingOffset() == 3500_ms);
music.Play();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
CHECK(music.GetStatus() == Nz::SoundStatus::Playing);
CHECK(music.GetPlayingOffset() >= 3650);
CHECK(music.GetPlayingOffset() >= 3650_ms);
AND_WHEN("We let the sound stop by itself")
{
REQUIRE(music.GetDuration() == 63059);
REQUIRE(music.GetDuration() == 63'059'591_us);
music.SetPlayingOffset(62900);
music.SeekToPlayingOffset(62900_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(300));
CHECK(music.GetStatus() == Nz::SoundStatus::Stopped);
CHECK(music.GetPlayingOffset() == 0);
CHECK(music.GetPlayingOffset() == 0_ms);
music.SetPlayingOffset(64000);
music.SeekToPlayingOffset(64000_ms);
music.Play();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
CHECK(music.GetStatus() == Nz::SoundStatus::Playing);
CHECK(music.GetPlayingOffset() < 100);
CHECK(music.GetPlayingOffset() < 100_ms);
music.Stop();
music.SetPlayingOffset(62900);
music.SeekToPlayingOffset(62900_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
CHECK(music.GetStatus() == Nz::SoundStatus::Stopped);
CHECK(music.GetPlayingOffset() == 0); //< playing offset has no effect until Play()
CHECK(music.GetPlayingOffset() == 0_ms); //< playing offset has no effect until Play()
AND_WHEN("We enable looping")
{
@@ -96,10 +99,10 @@ SCENARIO("Music", "[AUDIO][MUSIC]")
CHECK(music.IsLooping());
music.Play();
CHECK(music.GetStatus() == Nz::SoundStatus::Playing);
CHECK(music.GetPlayingOffset() >= 62900);
CHECK(music.GetPlayingOffset() >= 62900_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(300));
CHECK(music.GetStatus() == Nz::SoundStatus::Playing);
CHECK(music.GetPlayingOffset() < 300);
CHECK(music.GetPlayingOffset() < 300_ms);
}
}
Nz::Audio::Instance()->GetDefaultDevice()->SetGlobalVolume(100.f);

View File

@@ -6,6 +6,8 @@ std::filesystem::path GetAssetDir();
SCENARIO("SoundBuffer", "[AUDIO][SOUNDBUFFER]")
{
using namespace Nz::Literals;
GIVEN("A sound buffer")
{
WHEN("We load a .flac file")
@@ -15,7 +17,7 @@ SCENARIO("SoundBuffer", "[AUDIO][SOUNDBUFFER]")
THEN("We can ask the informations of the file")
{
CHECK(soundBuffer->GetDuration() == 8192);
CHECK(soundBuffer->GetDuration() == 8192_ms);
CHECK(soundBuffer->GetFormat() == Nz::AudioFormat::I16_Stereo);
CHECK(soundBuffer->GetSampleRate() == 96000);
}
@@ -28,7 +30,7 @@ SCENARIO("SoundBuffer", "[AUDIO][SOUNDBUFFER]")
THEN("We can ask the informations of the file")
{
CHECK(soundBuffer->GetDuration() == 27193);
CHECK(soundBuffer->GetDuration() == 27'193'468_us);
CHECK(soundBuffer->GetFormat() == Nz::AudioFormat::I16_Stereo);
CHECK(soundBuffer->GetSampleRate() == 32000);
}
@@ -41,7 +43,7 @@ SCENARIO("SoundBuffer", "[AUDIO][SOUNDBUFFER]")
THEN("We can ask the informations of the file")
{
CHECK(soundBuffer->GetDuration() == 63059);
CHECK(soundBuffer->GetDuration() == 63'059'591_us);
CHECK(soundBuffer->GetFormat() == Nz::AudioFormat::I16_Stereo);
CHECK(soundBuffer->GetSampleRate() == 44100);
}
@@ -54,7 +56,7 @@ SCENARIO("SoundBuffer", "[AUDIO][SOUNDBUFFER]")
THEN("We can ask the informations of the file")
{
CHECK(soundBuffer->GetDuration() == 2490);
CHECK(soundBuffer->GetDuration() == 2'490'340_us);
CHECK(soundBuffer->GetFormat() == Nz::AudioFormat::I16_Mono);
CHECK(soundBuffer->GetSampleRate() == 44100);
}

View File

@@ -6,6 +6,8 @@ std::filesystem::path GetAssetDir();
SCENARIO("SoundStream", "[AUDIO][SoundStream]")
{
using namespace Nz::Literals;
GIVEN("A sound buffer")
{
WHEN("We load a .flac file")
@@ -15,7 +17,7 @@ SCENARIO("SoundStream", "[AUDIO][SoundStream]")
THEN("We can ask the informations of the file")
{
CHECK(soundStream->GetDuration() == 8192);
CHECK(soundStream->GetDuration() == 8192_ms);
CHECK(soundStream->GetFormat() == Nz::AudioFormat::I16_Stereo);
CHECK(soundStream->GetSampleRate() == 96000);
}
@@ -28,7 +30,7 @@ SCENARIO("SoundStream", "[AUDIO][SoundStream]")
THEN("We can ask the informations of the file")
{
CHECK(soundStream->GetDuration() == 27193);
CHECK(soundStream->GetDuration() == 27'193'468_us);
CHECK(soundStream->GetFormat() == Nz::AudioFormat::I16_Stereo);
CHECK(soundStream->GetSampleRate() == 32000);
}
@@ -41,7 +43,7 @@ SCENARIO("SoundStream", "[AUDIO][SoundStream]")
THEN("We can ask the informations of the file")
{
CHECK(soundStream->GetDuration() == 63059);
CHECK(soundStream->GetDuration() == 63'059'591_us);
CHECK(soundStream->GetFormat() == Nz::AudioFormat::I16_Stereo);
CHECK(soundStream->GetSampleRate() == 44100);
}
@@ -54,7 +56,7 @@ SCENARIO("SoundStream", "[AUDIO][SoundStream]")
THEN("We can ask the informations of the file")
{
CHECK(soundStream->GetDuration() == 2490);
CHECK(soundStream->GetDuration() == 2'490'340_us);
CHECK(soundStream->GetFormat() == Nz::AudioFormat::I16_Mono);
CHECK(soundStream->GetSampleRate() == 44100);
}

View File

@@ -3,12 +3,15 @@
#include <catch2/catch_approx.hpp>
#include <catch2/catch_test_macros.hpp>
#include <chrono>
#include <iostream>
#include <thread>
std::filesystem::path GetAssetDir();
SCENARIO("Sound", "[AUDIO][SOUND]")
{
using namespace Nz::Literals;
GIVEN("A sound")
{
Nz::Sound sound;
@@ -19,14 +22,14 @@ SCENARIO("Sound", "[AUDIO][SOUND]")
THEN("We can ask the informations of the file")
{
CHECK(sound.GetDuration() == 8192);
CHECK(sound.GetDuration() == 8192_ms);
CHECK(sound.GetStatus() == Nz::SoundStatus::Stopped);
CHECK_FALSE(sound.IsLooping());
CHECK(sound.IsPlayable());
CHECK(sound.IsSpatializationEnabled());
CHECK(sound.GetMinDistance() == 1.f);
CHECK(sound.GetPitch() == 1.f);
CHECK(sound.GetPlayingOffset() == 0);
CHECK(sound.GetPlayingOffset() == 0_ms);
CHECK(sound.GetPosition() == Nz::Vector3f::Zero());
CHECK(sound.GetVelocity() == Nz::Vector3f::Zero());
CHECK(sound.GetVolume() == 1.f);
@@ -38,42 +41,45 @@ SCENARIO("Sound", "[AUDIO][SOUND]")
sound.Play();
std::this_thread::sleep_for(std::chrono::seconds(1));
CHECK(sound.GetPlayingOffset() >= 950);
CHECK(sound.GetPlayingOffset() >= 950_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
CHECK(sound.GetPlayingOffset() <= 1500);
CHECK(sound.GetPlayingOffset() <= 1500_ms);
sound.Pause();
Nz::UInt32 playingOffset = sound.GetPlayingOffset();
Nz::Time playingOffset = sound.GetPlayingOffset();
Nz::UInt64 sampleOffset = sound.GetSampleOffset();
CHECK(sound.GetStatus() == Nz::SoundStatus::Paused);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
CHECK(sound.GetStatus() == Nz::SoundStatus::Paused);
CHECK(sound.GetPlayingOffset() == playingOffset);
CHECK(sound.GetSampleOffset() == sampleOffset);
sound.SetPlayingOffset(3500);
CHECK(sound.GetPlayingOffset() == 3500);
sound.SeekToPlayingOffset(3500_ms);
CHECK(sound.GetPlayingOffset() == 3500_ms);
sound.Play();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
CHECK(sound.GetPlayingOffset() >= 1650);
CHECK(sound.GetPlayingOffset() >= 1650_ms);
AND_WHEN("We let the sound stop by itself")
{
REQUIRE(sound.GetDuration() == 8192);
REQUIRE(sound.GetDuration() == 8192_ms);
sound.SetPlayingOffset(8000);
sound.SeekToPlayingOffset(8000_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
CHECK(sound.GetStatus() == Nz::SoundStatus::Stopped);
CHECK(sound.GetPlayingOffset() == 0);
CHECK(sound.GetPlayingOffset() == 0_ms);
sound.SetPlayingOffset(9000);
sound.SeekToPlayingOffset(9000_ms);
sound.Play();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
CHECK(sound.GetStatus() == Nz::SoundStatus::Playing);
sound.Stop();
sound.SetPlayingOffset(8000);
sound.SeekToPlayingOffset(8000_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
CHECK(sound.GetStatus() == Nz::SoundStatus::Stopped);
CHECK(sound.GetPlayingOffset() == 0); //< playing offset has no effect until Play()
CHECK(sound.GetPlayingOffset() == 0_ms); //< playing offset has no effect until Play()
AND_WHEN("We enable looping")
{
@@ -81,10 +87,10 @@ SCENARIO("Sound", "[AUDIO][SOUND]")
CHECK(sound.IsLooping());
sound.Play();
CHECK(sound.GetStatus() == Nz::SoundStatus::Playing);
CHECK(sound.GetPlayingOffset() >= 8000);
CHECK(sound.GetPlayingOffset() >= 8000_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(300));
CHECK(sound.GetStatus() == Nz::SoundStatus::Playing);
CHECK(sound.GetPlayingOffset() < 300);
CHECK(sound.GetPlayingOffset() < 300_ms);
}
}