From 822e47743e76f489f92441b9f9ed8a1f29612ff6 Mon Sep 17 00:00:00 2001 From: Raffaello Bertini Date: Sun, 12 Nov 2023 22:48:55 +0000 Subject: [PATCH] PCMDriver forward tracks being played (#282) * small improvement * ms to sound byte position utility function * pcmDriver forward functionality * code rev * update version * sonarcloud ci rev * sonarcloud code rev * PCMDriver forward channel group * PCMDriver forward channel id --- .github/workflows/sonarcloud.yml | 18 ++++++------ CMakeLists.txt | 2 +- .../src/HyperSonicDrivers/audio/PCMSound.hpp | 2 -- .../audio/streams/PCMStream.cpp | 6 ++++ .../audio/streams/PCMStream.hpp | 2 ++ .../HyperSonicDrivers/drivers/PCMDriver.cpp | 28 +++++++++++++++++++ .../HyperSonicDrivers/drivers/PCMDriver.hpp | 4 +++ .../src/HyperSonicDrivers/utils/sound.cpp | 12 +++++++- .../src/HyperSonicDrivers/utils/sound.hpp | 1 + .../HyperSonicDrivers/utils/TestSound.cpp | 22 +++++++++++++++ 10 files changed, 84 insertions(+), 13 deletions(-) diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index 167cf656..54918cff 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -18,15 +18,15 @@ on: #branches: # - master - #pull_request: - ## branches: [ master ] - # paths-ignore: - # - 'doc/**' - # - '.gitignore' - # - '.gitattributes' - # - 'README.md' - # - 'LICENSE' - # - 'wave_generators.r' + pull_request: + # branches: [ master ] + paths-ignore: + - 'doc/**' + - '.gitignore' + - '.gitattributes' + - 'README.md' + - 'LICENSE' + - 'wave_generators.r' #permissions: read-all diff --git a/CMakeLists.txt b/CMakeLists.txt index c2bf284e..e552cd02 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE) endif() -project ("sdl2-hyper-sonic-drivers" VERSION 0.16.0 DESCRIPTION "SDL2 based Hyper-Sonic Drivers for emulating old soundcards") +project ("sdl2-hyper-sonic-drivers" VERSION 0.17.0 DESCRIPTION "SDL2 based Hyper-Sonic Drivers for emulating old soundcards") include (TestBigEndian) TEST_BIG_ENDIAN(IS_BIG_ENDIAN) if(IS_BIG_ENDIAN) diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/PCMSound.hpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/PCMSound.hpp index 2b5f3b4e..fbccb374 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/PCMSound.hpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/PCMSound.hpp @@ -27,8 +27,6 @@ namespace HyperSonicDrivers::audio const std::shared_ptr &data ); - bool append(const PCMSound* sound2) noexcept; - const mixer::eChannelGroup group; const bool stereo; const uint32_t freq; diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/streams/PCMStream.cpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/streams/PCMStream.cpp index 343b59c0..78e4c358 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/streams/PCMStream.cpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/streams/PCMStream.cpp @@ -38,6 +38,12 @@ namespace HyperSonicDrivers::audio::streams return m_curPos == m_sound->dataSize; } + void PCMStream::forward(const uint32_t bytes) noexcept + { + m_curPos += bytes; + m_curPos = std::min(m_curPos, m_sound->dataSize); + } + std::shared_ptr PCMStream::getSound() const noexcept { return m_sound; diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/streams/PCMStream.hpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/streams/PCMStream.hpp index 9c8901f4..905dc6a6 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/streams/PCMStream.hpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/streams/PCMStream.hpp @@ -18,6 +18,8 @@ namespace HyperSonicDrivers::audio::streams uint32_t getRate() const override; bool endOfData() const override; + void forward(const uint32_t bytes) noexcept; + std::shared_ptr getSound() const noexcept; private: std::shared_ptr m_sound; diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/PCMDriver.cpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/PCMDriver.cpp index 37110ddc..b68b6ef8 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/PCMDriver.cpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/PCMDriver.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace HyperSonicDrivers::drivers { @@ -98,6 +99,33 @@ namespace HyperSonicDrivers::drivers releaseStreams_(); } + void PCMDriver::forward(const uint32_t ms) const noexcept + { + for (const auto& [stream, _] : m_PCMStreams_channels) + stream->forward(utils::ms_toPos(ms, stream->getSound())); + } + + void PCMDriver::forward(const uint32_t ms, const audio::mixer::eChannelGroup group) const noexcept + { + for (const auto& [stream, _] : m_PCMStreams_channels) + { + const auto& s = stream->getSound(); + if (s->group == group) + stream->forward(utils::ms_toPos(ms, s)); + } + } + + void PCMDriver::forward(const uint32_t ms, const uint8_t channel_id) const noexcept + { + for (const auto& [stream, ch_id] : m_PCMStreams_channels) + { + if (ch_id != channel_id) + continue; + + stream->forward(utils::ms_toPos(ms, stream->getSound())); + } + } + void PCMDriver::releaseEndedStreams_() noexcept { for (auto it = m_PCMStreams_channels.begin(); it != m_PCMStreams_channels.end();) diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/PCMDriver.hpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/PCMDriver.hpp index 0bcb4820..994b4479 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/PCMDriver.hpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/PCMDriver.hpp @@ -34,6 +34,10 @@ namespace HyperSonicDrivers::drivers void stop(const std::shared_ptr& sound, const bool releaseEndedStreams = true); void stop() noexcept; + void forward(const uint32_t ms) const noexcept; + void forward(const uint32_t ms, const audio::mixer::eChannelGroup group) const noexcept; + void forward(const uint32_t ms, const uint8_t channel_id) const noexcept; + const uint8_t max_streams; private: std::shared_ptr m_mixer; diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/utils/sound.cpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/utils/sound.cpp index 99505659..0aad8302 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/utils/sound.cpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/utils/sound.cpp @@ -67,8 +67,18 @@ namespace HyperSonicDrivers::utils uint32_t ms = sound->dataSize; if (sound->stereo) - ms /= 2; + ms >>= 1; return ms * 1000 / sound->freq; } + + uint32_t ms_toPos(const uint32_t ms, const std::shared_ptr& sound) + { + uint32_t res = ms * sound->freq / 1000; + + if (sound->stereo) + res <<= 1; + + return res; + } } diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/utils/sound.hpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/utils/sound.hpp index 2d3a27d5..8a5bc9fe 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/utils/sound.hpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/utils/sound.hpp @@ -26,4 +26,5 @@ namespace HyperSonicDrivers::utils const std::shared_ptr& sound2); uint32_t duration_ms(const std::shared_ptr& sound); + uint32_t ms_toPos(const uint32_t, const std::shared_ptr& sound); } diff --git a/sdl2-hyper-sonic-drivers/test/HyperSonicDrivers/utils/TestSound.cpp b/sdl2-hyper-sonic-drivers/test/HyperSonicDrivers/utils/TestSound.cpp index 33add3fc..45154fc1 100644 --- a/sdl2-hyper-sonic-drivers/test/HyperSonicDrivers/utils/TestSound.cpp +++ b/sdl2-hyper-sonic-drivers/test/HyperSonicDrivers/utils/TestSound.cpp @@ -81,6 +81,28 @@ namespace HyperSonicDrivers::utils EXPECT_EQ(duration_ms(s4), 11025); EXPECT_EQ(duration_ms(s5), 5512); } + + TEST(utils, ms_toPos) + { + const uint32_t size = 44100 * 2; + auto data = std::make_shared(size); + + auto s1 = std::make_shared(eChannelGroup::Plain, true, 44100, size, data); + + EXPECT_EQ(ms_toPos(0, s1), 0); + EXPECT_EQ(ms_toPos(1000, s1), 44100 * 2); + EXPECT_EQ(ms_toPos(500, s1), 22050 * 2); + EXPECT_EQ(ms_toPos(250, s1), 11025 * 2); + EXPECT_EQ(ms_toPos(100, s1), 4410 * 2); + + auto s2 = std::make_shared(eChannelGroup::Plain, false, 44100, size, data); + + EXPECT_EQ(ms_toPos(0, s2), 0); + EXPECT_EQ(ms_toPos(1000, s2), 44100); + EXPECT_EQ(ms_toPos(500, s2), 22050); + EXPECT_EQ(ms_toPos(250, s2), 11025); + EXPECT_EQ(ms_toPos(100, s2), 4410); + } } int main(int argc, char** argv)