Skip to content

Commit

Permalink
ms to sound byte position utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
Raffaello committed Nov 12, 2023
1 parent 16cfd08 commit 88ff32e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/utils/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,14 @@ namespace HyperSonicDrivers::utils

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 88ff32e

Please sign in to comment.