From 45f0825a6ebb55a61f48cf054bc92d5fed70e269 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sat, 19 Mar 2022 12:16:52 +0100 Subject: [PATCH] Audio: Final fixes --- examples/DopplerEffect/main.cpp | 5 +-- examples/PlayMusic/main.cpp | 5 +-- src/Nazara/Audio/Audio.cpp | 29 +++++++++++++--- tests/Engine/Audio/SoundBufferTest.cpp | 46 ++++++++++++++++++++++++-- 4 files changed, 70 insertions(+), 15 deletions(-) diff --git a/examples/DopplerEffect/main.cpp b/examples/DopplerEffect/main.cpp index d5dd980dc..b051b6f09 100644 --- a/examples/DopplerEffect/main.cpp +++ b/examples/DopplerEffect/main.cpp @@ -24,10 +24,7 @@ int main() if (!std::filesystem::is_directory(resourceDir) && std::filesystem::is_directory(".." / resourceDir)) resourceDir = ".." / resourceDir; - Nz::Audio::Config config; - config.noAudio = true; - - Nz::Modules audio(config); + Nz::Modules audio; Nz::Sound sound; if (!sound.LoadFromFile(resourceDir / "siren.wav")) diff --git a/examples/PlayMusic/main.cpp b/examples/PlayMusic/main.cpp index 247422ef0..97c1048a7 100644 --- a/examples/PlayMusic/main.cpp +++ b/examples/PlayMusic/main.cpp @@ -18,10 +18,7 @@ int main() if (!std::filesystem::is_directory(resourceDir) && std::filesystem::is_directory(".." / resourceDir)) resourceDir = ".." / resourceDir; - Nz::Audio::Config config; - config.noAudio = true; - - Nz::Modules audio(config); + Nz::Modules audio; Nz::SoundStreamParams streamParams; streamParams.forceMono = false; diff --git a/src/Nazara/Audio/Audio.cpp b/src/Nazara/Audio/Audio.cpp index 3a0b35704..8bd3d5b0b 100644 --- a/src/Nazara/Audio/Audio.cpp +++ b/src/Nazara/Audio/Audio.cpp @@ -38,8 +38,16 @@ namespace Nz m_hasDummyDevice(config.allowDummyDevice) { // Load OpenAL - if (!config.noAudio && !s_openalLibrary.Load()) - throw std::runtime_error("failed to load OpenAL"); + if (!config.noAudio) + { + if (!s_openalLibrary.Load()) + { + if (!config.allowDummyDevice) + throw std::runtime_error("failed to load OpenAL"); + + NazaraError("failed to load OpenAL"); + } + } // Loaders m_soundBufferLoader.RegisterLoader(Loaders::GetSoundBufferLoader_drwav()); @@ -52,8 +60,21 @@ namespace Nz m_soundStreamLoader.RegisterLoader(Loaders::GetSoundStreamLoader_minimp3()); if (s_openalLibrary.IsLoaded()) - m_defaultDevice = s_openalLibrary.OpenDevice(); - else + { + try + { + m_defaultDevice = s_openalLibrary.OpenDevice(); + } + catch (const std::exception& e) + { + if (!config.allowDummyDevice) + throw; + + NazaraError(std::string("failed to open default OpenAL device: ") + e.what()); + } + } + + if (!m_defaultDevice) m_defaultDevice = std::make_shared(); } diff --git a/tests/Engine/Audio/SoundBufferTest.cpp b/tests/Engine/Audio/SoundBufferTest.cpp index 06cadd383..a8c0865bf 100644 --- a/tests/Engine/Audio/SoundBufferTest.cpp +++ b/tests/Engine/Audio/SoundBufferTest.cpp @@ -7,15 +7,55 @@ SCENARIO("SoundBuffer", "[AUDIO][SOUNDBUFFER]") { GIVEN("A sound buffer") { - WHEN("We load our sound") + WHEN("We load a .flac file") { std::shared_ptr soundBuffer = Nz::SoundBuffer::LoadFromFile(GetResourceDir() / "Engine/Audio/Cat.flac"); REQUIRE(soundBuffer); THEN("We can ask the informations of the file") { - REQUIRE(soundBuffer->GetDuration() <= 8500); // 8s = 8000ms - REQUIRE(soundBuffer->GetDuration() >= 8000); + CHECK(soundBuffer->GetDuration() == 8192); + CHECK(soundBuffer->GetFormat() == Nz::AudioFormat::I16_Stereo); + CHECK(soundBuffer->GetSampleRate() == 96000); + } + } + + WHEN("We load a .mp3 file") + { + std::shared_ptr soundBuffer = Nz::SoundBuffer::LoadFromFile(GetResourceDir() / "file_example_MP3_700KB.mp3"); + REQUIRE(soundBuffer); + + THEN("We can ask the informations of the file") + { + CHECK(soundBuffer->GetDuration() == 27193); + CHECK(soundBuffer->GetFormat() == Nz::AudioFormat::I16_Stereo); + CHECK(soundBuffer->GetSampleRate() == 32000); + } + } + + WHEN("We load a .ogg file") + { + std::shared_ptr soundBuffer = Nz::SoundBuffer::LoadFromFile(GetResourceDir() / "Engine/Audio/The_Brabanconne.ogg"); + REQUIRE(soundBuffer); + + THEN("We can ask the informations of the file") + { + CHECK(soundBuffer->GetDuration() == 63059); + CHECK(soundBuffer->GetFormat() == Nz::AudioFormat::I16_Stereo); + CHECK(soundBuffer->GetSampleRate() == 44100); + } + } + + WHEN("We load a .wav file") + { + std::shared_ptr soundBuffer = Nz::SoundBuffer::LoadFromFile(GetResourceDir() / "explosion.wav"); + REQUIRE(soundBuffer); + + THEN("We can ask the informations of the file") + { + CHECK(soundBuffer->GetDuration() == 2064); + CHECK(soundBuffer->GetFormat() == Nz::AudioFormat::I16_Stereo); + CHECK(soundBuffer->GetSampleRate() == 48000); } } }