Skip to content

Commit

Permalink
PCMDriver forward tracks being played (#282)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Raffaello authored Nov 12, 2023
1 parent db7430b commit 822e477
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 13 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ namespace HyperSonicDrivers::audio
const std::shared_ptr<int16_t[]> &data
);

bool append(const PCMSound* sound2) noexcept;

const mixer::eChannelGroup group;
const bool stereo;
const uint32_t freq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PCMSound> PCMStream::getSound() const noexcept
{
return m_sound;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PCMSound> getSound() const noexcept;
private:
std::shared_ptr<PCMSound> m_sound;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <algorithm>
#include <HyperSonicDrivers/drivers/PCMDriver.hpp>
#include <HyperSonicDrivers/utils/sound.hpp>

namespace HyperSonicDrivers::drivers
{
Expand Down Expand Up @@ -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();)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ namespace HyperSonicDrivers::drivers
void stop(const std::shared_ptr<audio::PCMSound>& 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<audio::IMixer> m_mixer;
Expand Down
12 changes: 11 additions & 1 deletion sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/utils/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<audio::PCMSound>& sound)
{
uint32_t res = ms * sound->freq / 1000;

if (sound->stereo)
res <<= 1;

return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ namespace HyperSonicDrivers::utils
const std::shared_ptr<audio::PCMSound>& sound2);

uint32_t duration_ms(const std::shared_ptr<audio::PCMSound>& sound);
uint32_t ms_toPos(const uint32_t, const std::shared_ptr<audio::PCMSound>& sound);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<int16_t[]>(size);

auto s1 = std::make_shared<PCMSound>(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<PCMSound>(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)
Expand Down

0 comments on commit 822e477

Please sign in to comment.