Skip to content

Commit 822e477

Browse files
authored
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
1 parent db7430b commit 822e477

File tree

10 files changed

+84
-13
lines changed

10 files changed

+84
-13
lines changed

.github/workflows/sonarcloud.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ on:
1818
#branches:
1919
# - master
2020

21-
#pull_request:
22-
## branches: [ master ]
23-
# paths-ignore:
24-
# - 'doc/**'
25-
# - '.gitignore'
26-
# - '.gitattributes'
27-
# - 'README.md'
28-
# - 'LICENSE'
29-
# - 'wave_generators.r'
21+
pull_request:
22+
# branches: [ master ]
23+
paths-ignore:
24+
- 'doc/**'
25+
- '.gitignore'
26+
- '.gitattributes'
27+
- 'README.md'
28+
- 'LICENSE'
29+
- 'wave_generators.r'
3030

3131
#permissions: read-all
3232

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
55
endif()
66

77

8-
project ("sdl2-hyper-sonic-drivers" VERSION 0.16.0 DESCRIPTION "SDL2 based Hyper-Sonic Drivers for emulating old soundcards")
8+
project ("sdl2-hyper-sonic-drivers" VERSION 0.17.0 DESCRIPTION "SDL2 based Hyper-Sonic Drivers for emulating old soundcards")
99
include (TestBigEndian)
1010
TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
1111
if(IS_BIG_ENDIAN)

sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/PCMSound.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ namespace HyperSonicDrivers::audio
2727
const std::shared_ptr<int16_t[]> &data
2828
);
2929

30-
bool append(const PCMSound* sound2) noexcept;
31-
3230
const mixer::eChannelGroup group;
3331
const bool stereo;
3432
const uint32_t freq;

sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/streams/PCMStream.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ namespace HyperSonicDrivers::audio::streams
3838
return m_curPos == m_sound->dataSize;
3939
}
4040

41+
void PCMStream::forward(const uint32_t bytes) noexcept
42+
{
43+
m_curPos += bytes;
44+
m_curPos = std::min(m_curPos, m_sound->dataSize);
45+
}
46+
4147
std::shared_ptr<PCMSound> PCMStream::getSound() const noexcept
4248
{
4349
return m_sound;

sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/streams/PCMStream.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace HyperSonicDrivers::audio::streams
1818
uint32_t getRate() const override;
1919
bool endOfData() const override;
2020

21+
void forward(const uint32_t bytes) noexcept;
22+
2123
std::shared_ptr<PCMSound> getSound() const noexcept;
2224
private:
2325
std::shared_ptr<PCMSound> m_sound;

sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/PCMDriver.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <algorithm>
22
#include <HyperSonicDrivers/drivers/PCMDriver.hpp>
3+
#include <HyperSonicDrivers/utils/sound.hpp>
34

45
namespace HyperSonicDrivers::drivers
56
{
@@ -98,6 +99,33 @@ namespace HyperSonicDrivers::drivers
9899
releaseStreams_();
99100
}
100101

102+
void PCMDriver::forward(const uint32_t ms) const noexcept
103+
{
104+
for (const auto& [stream, _] : m_PCMStreams_channels)
105+
stream->forward(utils::ms_toPos(ms, stream->getSound()));
106+
}
107+
108+
void PCMDriver::forward(const uint32_t ms, const audio::mixer::eChannelGroup group) const noexcept
109+
{
110+
for (const auto& [stream, _] : m_PCMStreams_channels)
111+
{
112+
const auto& s = stream->getSound();
113+
if (s->group == group)
114+
stream->forward(utils::ms_toPos(ms, s));
115+
}
116+
}
117+
118+
void PCMDriver::forward(const uint32_t ms, const uint8_t channel_id) const noexcept
119+
{
120+
for (const auto& [stream, ch_id] : m_PCMStreams_channels)
121+
{
122+
if (ch_id != channel_id)
123+
continue;
124+
125+
stream->forward(utils::ms_toPos(ms, stream->getSound()));
126+
}
127+
}
128+
101129
void PCMDriver::releaseEndedStreams_() noexcept
102130
{
103131
for (auto it = m_PCMStreams_channels.begin(); it != m_PCMStreams_channels.end();)

sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/PCMDriver.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ namespace HyperSonicDrivers::drivers
3434
void stop(const std::shared_ptr<audio::PCMSound>& sound, const bool releaseEndedStreams = true);
3535
void stop() noexcept;
3636

37+
void forward(const uint32_t ms) const noexcept;
38+
void forward(const uint32_t ms, const audio::mixer::eChannelGroup group) const noexcept;
39+
void forward(const uint32_t ms, const uint8_t channel_id) const noexcept;
40+
3741
const uint8_t max_streams;
3842
private:
3943
std::shared_ptr<audio::IMixer> m_mixer;

sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/utils/sound.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,18 @@ namespace HyperSonicDrivers::utils
6767
uint32_t ms = sound->dataSize;
6868

6969
if (sound->stereo)
70-
ms /= 2;
70+
ms >>= 1;
7171

7272
return ms * 1000 / sound->freq;
7373
}
74+
75+
uint32_t ms_toPos(const uint32_t ms, const std::shared_ptr<audio::PCMSound>& sound)
76+
{
77+
uint32_t res = ms * sound->freq / 1000;
78+
79+
if (sound->stereo)
80+
res <<= 1;
81+
82+
return res;
83+
}
7484
}

sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/utils/sound.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ namespace HyperSonicDrivers::utils
2626
const std::shared_ptr<audio::PCMSound>& sound2);
2727

2828
uint32_t duration_ms(const std::shared_ptr<audio::PCMSound>& sound);
29+
uint32_t ms_toPos(const uint32_t, const std::shared_ptr<audio::PCMSound>& sound);
2930
}

sdl2-hyper-sonic-drivers/test/HyperSonicDrivers/utils/TestSound.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,28 @@ namespace HyperSonicDrivers::utils
8181
EXPECT_EQ(duration_ms(s4), 11025);
8282
EXPECT_EQ(duration_ms(s5), 5512);
8383
}
84+
85+
TEST(utils, ms_toPos)
86+
{
87+
const uint32_t size = 44100 * 2;
88+
auto data = std::make_shared<int16_t[]>(size);
89+
90+
auto s1 = std::make_shared<PCMSound>(eChannelGroup::Plain, true, 44100, size, data);
91+
92+
EXPECT_EQ(ms_toPos(0, s1), 0);
93+
EXPECT_EQ(ms_toPos(1000, s1), 44100 * 2);
94+
EXPECT_EQ(ms_toPos(500, s1), 22050 * 2);
95+
EXPECT_EQ(ms_toPos(250, s1), 11025 * 2);
96+
EXPECT_EQ(ms_toPos(100, s1), 4410 * 2);
97+
98+
auto s2 = std::make_shared<PCMSound>(eChannelGroup::Plain, false, 44100, size, data);
99+
100+
EXPECT_EQ(ms_toPos(0, s2), 0);
101+
EXPECT_EQ(ms_toPos(1000, s2), 44100);
102+
EXPECT_EQ(ms_toPos(500, s2), 22050);
103+
EXPECT_EQ(ms_toPos(250, s2), 11025);
104+
EXPECT_EQ(ms_toPos(100, s2), 4410);
105+
}
84106
}
85107

86108
int main(int argc, char** argv)

0 commit comments

Comments
 (0)