Skip to content

Commit

Permalink
code rev
Browse files Browse the repository at this point in the history
  • Loading branch information
Raffaello committed Nov 5, 2023
1 parent 12985da commit 66656a5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,7 @@ namespace HyperSonicDrivers::audio::sdl2
// we store stereo, 16-bit samples (div 2 for stereo and 2 from 8 to 16 bits)
assert(len % 4 == 0);
// zero the buf (size of 2ch stereo: len*2 of 16 bits)
memset(buf, 0, len /** sizeof(int16_t)*/);
//len >>= 1;
//len >>= 1; // size of the stereo 16 bits buffer.
memset(buf, 0, len);
len >>= 2;
// mix all channels
size_t res = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@ namespace HyperSonicDrivers::drivers
PCMDriver::PCMDriver(const std::shared_ptr<audio::IMixer>& mixer, const uint8_t max_channels) :
max_streams(std::min(mixer->max_channels, max_channels)), m_mixer(mixer)
{
m_PCMStreams.resize(max_streams);
}

bool PCMDriver::isPlaying() const noexcept
{
/*for(const auto& ss: m_PCMStreams)
{
if (isPCMStreamPlaying_(ss))
return true;
}*/

for (const auto& ss : m_PCMStreams_channels)
{
if (isPCMStreamPlaying_(ss.first))
Expand All @@ -30,26 +23,6 @@ namespace HyperSonicDrivers::drivers

bool PCMDriver::isPlaying(const std::shared_ptr<audio::PCMSound>& sound) const noexcept
{
// TODO:
// should map channelId to check directly in the mixer?
// how to find a free slot then?
// does we need to really track it?
// probably using a map instead of a vector is ok,
// no need to define nether max-channels.
// but that is because if wanting to reserve some channels for something
// else that is not PCM related...
// anyway... it could be achieved having the mixer a "lock or reserved channel"
// feature or something that that one won't be used unless
// it is for the resources that has been reserved for.....
/*for(const auto& ss : m_PCMStreams)
{
if (ss == nullptr)
continue;
if (ss->getSound() == sound)
return isPCMStreamPlaying_(ss);
}*/

for (const auto& ss : m_PCMStreams_channels)
{
if (ss.first->getSound() == sound)
Expand All @@ -61,28 +34,21 @@ namespace HyperSonicDrivers::drivers

std::optional<uint8_t> PCMDriver::play(const std::shared_ptr<audio::PCMSound>& sound, const uint8_t volume, const int8_t pan)
{
// find first free slot
auto it = std::ranges::find_if_not(m_PCMStreams, isPCMStreamPlaying_);
if (it == m_PCMStreams.end())
releaseEndedStreams_();
if (m_PCMStreams_channels.size() == max_streams)
return std::nullopt;

//releaseEndedStreams_();
//if (m_PCMStreams_channels.size() == max_streams)
// return std::nullopt;

*it = std::make_shared<PCMStream>(sound);
auto s = std::make_shared<PCMStream>(sound);

auto channelId = m_mixer->play(
sound->group,
*it,
s,
volume,
pan
);

if (!channelId.has_value())
*it = nullptr;
else
m_PCMStreams_channels[*it] = channelId.value();
if (channelId.has_value())
m_PCMStreams_channels[s] = channelId.value();

return channelId;
}
Expand All @@ -104,14 +70,6 @@ namespace HyperSonicDrivers::drivers

if (releaseEndedStreams)
{
for (int i = 0; i < m_PCMStreams.size(); i++)
{
if (m_PCMStreams[i] == it->first)
{
m_PCMStreams[i] = nullptr;
break;
}
}
m_PCMStreams_channels.erase(it);
releaseEndedStreams_();
}
Expand Down Expand Up @@ -144,22 +102,17 @@ namespace HyperSonicDrivers::drivers

void PCMDriver::releaseEndedStreams_() noexcept
{
for (int i = 0; i < m_PCMStreams.size(); i++)
for (auto it = m_PCMStreams_channels.begin(); it != m_PCMStreams_channels.end();)
{
if (!isPCMStreamPlaying_(m_PCMStreams[i]))
{
if (m_PCMStreams_channels.contains(m_PCMStreams[i]))
m_PCMStreams_channels.erase(m_PCMStreams[i]);

m_PCMStreams[i] = nullptr;
}
if (!isPCMStreamPlaying_(it->first))
it = m_PCMStreams_channels.erase(it);
else
++it;
}
}

void PCMDriver::releaseStreams_() noexcept
{
for (int i = 0; i < m_PCMStreams.size(); i++)
m_PCMStreams[i] = nullptr;
m_PCMStreams_channels.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <cstdint>
#include <memory>
#include <map>
#include <vector>
#include <optional>
#include <HyperSonicDrivers/audio/IMixer.hpp>
#include <HyperSonicDrivers/audio/streams/PCMStream.hpp>
Expand Down Expand Up @@ -39,12 +38,7 @@ namespace HyperSonicDrivers::drivers
const uint8_t max_streams;
private:
std::shared_ptr<audio::IMixer> m_mixer;
// TODO: probably better having a fixed vector to map all mixer channels, for tracking purposes
// so the vector index is the channel_id of the mixer when needed to stop it.
// otherwise better store in PCMStream the mixer channel_id,
// as the map should be based on PCMSound and not PCMStream
std::vector<std::shared_ptr<audio::streams::PCMStream>> m_PCMStreams; // TODO: is this still required?
std::map<std::shared_ptr<audio::streams::PCMStream>, uint8_t> m_PCMStreams_channels; // TODO: does this improve things?
std::map<std::shared_ptr<audio::streams::PCMStream>, uint8_t> m_PCMStreams_channels;

void releaseEndedStreams_() noexcept;
void releaseStreams_() noexcept;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace HyperSonicDrivers::audio
{
public:
int rate = 44100;
uint8_t cur_ch = 255;

IMixerMock() : IMixer(32, 44100, 1024) {};
explicit IMixerMock(const int freq) : IMixer(32, freq, 1024) {};
Expand All @@ -24,7 +25,7 @@ namespace HyperSonicDrivers::audio
const uint8_t vol,
const int8_t pan
) override {
return std::make_optional(0);
return std::make_optional((++cur_ch) % max_channels);
};

void suspend() noexcept override {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@ namespace HyperSonicDrivers::drivers
EXPECT_FALSE(drv.isPlaying(sound));
EXPECT_FALSE(drv.isPlaying());
}

TEST(PCMDriver, play_stop_complex)
{
auto mixer = audio::make_mixer<audio::IMixerMock>();
auto drv = PCMDriver(mixer);

EXPECT_EQ(drv.max_streams, mixer->max_channels);

auto sound_data = std::make_shared<int16_t[]>(1);
auto sound = std::make_shared<audio::PCMSound>(audio::mixer::eChannelGroup::Plain, true, 44100, 1, sound_data);
auto sound_data2 = std::make_shared<int16_t[]>(1);
auto sound2 = std::make_shared<audio::PCMSound>(audio::mixer::eChannelGroup::Unknown, true, 22050, 1, sound_data2);

auto ch_id = drv.play(sound);
EXPECT_TRUE(drv.play(sound2).has_value());
EXPECT_TRUE(drv.play(sound2).has_value());
EXPECT_TRUE(drv.play(sound2).has_value());

ASSERT_TRUE(drv.isPlaying(sound));
ASSERT_TRUE(drv.isPlaying());
ASSERT_TRUE(ch_id.has_value());
EXPECT_EQ(ch_id.value(), 0);

drv.stop(ch_id.value());
EXPECT_FALSE(drv.isPlaying(sound));
EXPECT_TRUE(drv.isPlaying());
}
}

int main(int argc, char** argv)
Expand Down

0 comments on commit 66656a5

Please sign in to comment.