Skip to content

Commit

Permalink
pcm utility function: duration_ms (#275)
Browse files Browse the repository at this point in the history
* utils::duration_ms

* update version
  • Loading branch information
Raffaello authored Nov 10, 2023
1 parent a67ed5a commit 5940002
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
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.14.0 DESCRIPTION "SDL2 based Hyper-Sonic Drivers for emulating old soundcards")
project ("sdl2-hyper-sonic-drivers" VERSION 0.15.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
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 @@ -61,4 +61,14 @@ namespace HyperSonicDrivers::utils

return std::make_shared<audio::PCMSound>(sound1->group, sound1->stereo, sound1->freq, dataSize, data);
}

uint32_t duration_ms(const std::shared_ptr<audio::PCMSound>& sound)
{
uint32_t ms = sound->dataSize;

if (sound->stereo)
ms /= 2;

return ms * 1000 / sound->freq;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ namespace HyperSonicDrivers::utils
std::shared_ptr<audio::PCMSound> append(
const std::shared_ptr<audio::PCMSound>& sound1,
const std::shared_ptr<audio::PCMSound>& sound2);

uint32_t duration_ms(const std::shared_ptr<audio::PCMSound>& sound);
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ namespace HyperSonicDrivers::utils

ASSERT_EQ(s1, s2);
}

TEST(utils, duration_ms)
{
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);
auto s2 = std::make_shared<PCMSound>(eChannelGroup::Plain, false, 44100, size, data);
auto s3 = std::make_shared<PCMSound>(eChannelGroup::Plain, false, 44100, 11025 * 7, data);
auto s4 = std::make_shared<PCMSound>(eChannelGroup::Plain, false, 8000, size, data);
auto s5 = std::make_shared<PCMSound>(eChannelGroup::Plain, true, 8000, size, data);

EXPECT_EQ(duration_ms(s1), 1000);
EXPECT_EQ(duration_ms(s2), 2000);
EXPECT_EQ(duration_ms(s3), 1750);
EXPECT_EQ(duration_ms(s4), 11025);
EXPECT_EQ(duration_ms(s5), 5512);
}
}

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

0 comments on commit 5940002

Please sign in to comment.