-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PCMSound utility functions | makeMono | makeStereo | append (#272)
* update README * PCMSound makeMono, makeStereo, append * code rev * add test * code rev * code rev * update version
- Loading branch information
Showing
11 changed files
with
208 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/utils/sound.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <cstdint> | ||
#include <HyperSonicDrivers/utils/sound.hpp> | ||
#include <HyperSonicDrivers/audio/converters/LinearRateConverter.hpp> | ||
|
||
namespace HyperSonicDrivers::utils | ||
{ | ||
std::shared_ptr<audio::PCMSound> makeMono(const std::shared_ptr<audio::PCMSound>& sound) | ||
{ | ||
if (!sound->stereo) | ||
return sound; | ||
|
||
const uint32_t monoDataSize = sound->dataSize / 2; | ||
|
||
auto monoData = std::make_shared<int16_t[]>(monoDataSize); | ||
for (uint32_t i = 0, j = 0; i < sound->dataSize; i += 2, j++) | ||
{ | ||
monoData[j] = (sound->data[i] + sound->data[i + 1]) / 2; | ||
} | ||
|
||
return std::make_shared<audio::PCMSound>(sound->group, false, sound->freq, monoDataSize, monoData); | ||
} | ||
|
||
std::shared_ptr<audio::PCMSound> makeStereo(const std::shared_ptr<audio::PCMSound>& sound) | ||
{ | ||
if (sound->stereo) | ||
return sound; | ||
|
||
const uint32_t stereoDataSize = sound->dataSize * 2; | ||
auto stereoData = std::make_shared<int16_t[]>(stereoDataSize); | ||
for (uint32_t i = 0, j = 0; i < sound->dataSize; i++, j += 2) | ||
{ | ||
stereoData[j] = stereoData[j + 1] = sound->data[i]; | ||
} | ||
|
||
return std::make_shared<audio::PCMSound>(sound->group, true, sound->freq, stereoDataSize, stereoData); | ||
} | ||
|
||
std::shared_ptr<audio::PCMSound> append( | ||
const std::shared_ptr<audio::PCMSound>& sound1, | ||
const std::shared_ptr<audio::PCMSound>& sound2) | ||
{ | ||
std::shared_ptr<audio::PCMSound> s2; | ||
|
||
if (sound1->stereo) | ||
s2 = makeStereo(sound2); | ||
else | ||
s2 = makeMono(sound2); | ||
|
||
if (sound1->freq != sound2->freq) | ||
{ | ||
// TODO: | ||
// use covert frequency/ sample rate libraries for this ? | ||
utils::throwLogC<std::runtime_error>("different frequency, not implemented yet"); | ||
} | ||
|
||
const uint32_t dataSize = sound1->dataSize + s2->dataSize; | ||
auto data = std::make_shared<int16_t[]>(dataSize); | ||
|
||
memcpy(data.get(), sound1->data.get(), sound1->dataSize * sizeof(int16_t)); | ||
memcpy(&data[sound1->dataSize], s2->data.get(), s2->dataSize * sizeof(int16_t)); | ||
|
||
return std::make_shared<audio::PCMSound>(sound1->group, sound1->stereo, sound1->freq, dataSize, data); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/utils/sound.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <HyperSonicDrivers/audio/PCMSound.hpp> | ||
|
||
namespace HyperSonicDrivers::utils | ||
{ | ||
/** | ||
* @returns a new mono PCMSound if the parameter is stereo | ||
* otherwise returns the parameter itself. | ||
**/ | ||
std::shared_ptr<audio::PCMSound> makeMono(const std::shared_ptr<audio::PCMSound>& sound); | ||
|
||
/** | ||
* @returns a new stereo PCMSound if the parameter is mono | ||
* otherwise returns the parameter itself. | ||
**/ | ||
std::shared_ptr<audio::PCMSound> makeStereo(const std::shared_ptr<audio::PCMSound>& sound); | ||
|
||
/** | ||
* @returns sound1 + sound2. | ||
* it converts sound2 to the same freq and mono/stereo of sound1 | ||
**/ | ||
std::shared_ptr<audio::PCMSound> append( | ||
const std::shared_ptr<audio::PCMSound>& sound1, | ||
const std::shared_ptr<audio::PCMSound>& sound2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
sdl2-hyper-sonic-drivers/test/HyperSonicDrivers/utils/TestSound.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <gtest/gtest.h> | ||
#include <gmock/gmock.h> | ||
#include <cstdint> | ||
#include <memory> | ||
#include <HyperSonicDrivers/utils/sound.hpp> | ||
#include <HyperSonicDrivers/audio/PCMSound.hpp> | ||
|
||
namespace HyperSonicDrivers::utils | ||
{ | ||
using audio::mixer::eChannelGroup; | ||
using audio::PCMSound; | ||
|
||
TEST(utils, makeMono0) | ||
{ | ||
const uint32_t size = 100; | ||
auto data = std::make_shared<int16_t[]>(size); | ||
|
||
auto s1 = std::make_shared<PCMSound>(eChannelGroup::Plain, true, 44100, size, data); | ||
auto s2 = makeMono(s1); | ||
|
||
EXPECT_TRUE(s1->stereo); | ||
EXPECT_FALSE(s2->stereo); | ||
EXPECT_EQ(s1->group, s2->group); | ||
EXPECT_EQ(s1->freq, s2->freq); | ||
EXPECT_EQ(s1->dataSize, size); | ||
EXPECT_EQ(s2->dataSize, size / 2); | ||
} | ||
|
||
TEST(utils, makeMono1) | ||
{ | ||
const uint32_t size = 100; | ||
auto data = std::make_shared<int16_t[]>(size); | ||
|
||
auto s1 = std::make_shared<PCMSound>(eChannelGroup::Plain, false, 44100, size, data); | ||
auto s2 = makeMono(s1); | ||
|
||
ASSERT_EQ(s1, s2); | ||
} | ||
|
||
TEST(utils, makeStereo0) | ||
{ | ||
const uint32_t size = 100; | ||
auto data = std::make_shared<int16_t[]>(size); | ||
|
||
auto s1 = std::make_shared<PCMSound>(eChannelGroup::Plain, false, 44100, size, data); | ||
auto s2 = makeStereo(s1); | ||
|
||
EXPECT_FALSE(s1->stereo); | ||
EXPECT_TRUE(s2->stereo); | ||
EXPECT_EQ(s1->group, s2->group); | ||
EXPECT_EQ(s1->freq, s2->freq); | ||
EXPECT_EQ(s1->dataSize, size); | ||
EXPECT_EQ(s2->dataSize, size * 2); | ||
} | ||
|
||
TEST(utils, makeStereo1) | ||
{ | ||
const uint32_t size = 100; | ||
auto data = std::make_shared<int16_t[]>(size); | ||
|
||
auto s1 = std::make_shared<PCMSound>(eChannelGroup::Plain, true, 44100, size, data); | ||
auto s2 = makeStereo(s1); | ||
|
||
ASSERT_EQ(s1, s2); | ||
} | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |