Skip to content

Commit 7b7ba4e

Browse files
committed
Sound: Implement pan effect
1 parent 93012ac commit 7b7ba4e

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

include/scratchcpp/sound.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class LIBSCRATCHCPP_EXPORT Sound : public Asset
1818
public:
1919
enum class Effect
2020
{
21-
Pitch
21+
Pitch,
22+
Pan
2223
};
2324

2425
Sound(const std::string &name, const std::string &id, const std::string &format);

src/scratch/sound.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
using namespace libscratchcpp;
1313

1414
static std::unordered_map<Sound::Effect, std::pair<double, double>> EFFECT_RANGE = {
15-
{ Sound::Effect::Pitch, { -360, 360 } } // -3 to 3 octaves
15+
{ Sound::Effect::Pitch, { -360, 360 } }, // -3 to 3 octaves
16+
{ Sound::Effect::Pan, { -100, 100 } } // // 100% left to 100% right
1617
};
1718

1819
/*! Constructs Sound. */
@@ -63,11 +64,16 @@ void Sound::setEffect(Effect effect, double value)
6364
value = std::clamp(value, it->second.first, it->second.second);
6465

6566
switch (effect) {
66-
case Effect::Pitch:
67+
case Effect::Pitch: {
6768
// Convert from linear
6869
const double root = std::pow(2, 1 / 12.0);
6970
impl->player->setPitch(std::pow(root, value / 10));
7071
break;
72+
}
73+
74+
case Effect::Pan:
75+
impl->player->setPan(value / 100);
76+
break;
7177
}
7278
}
7379

@@ -115,6 +121,7 @@ std::shared_ptr<Sound> Sound::clone() const
115121
sound->impl->cloneRoot = root;
116122
sound->impl->player->setVolume(impl->player->volume());
117123
sound->impl->player->setPitch(impl->player->pitch());
124+
sound->impl->player->setPan(impl->player->pan());
118125

119126
if (root->impl->player->isLoaded()) {
120127
sound->impl->player->loadCopy(root->impl->player.get());

test/assets/sound_test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,28 @@ TEST_F(SoundTest, SetEffect)
104104

105105
EXPECT_CALL(*m_player, setPitch(8));
106106
sound.setEffect(Sound::Effect::Pitch, 400);
107+
108+
// Pan
109+
EXPECT_CALL(*m_player, setPan(-1));
110+
sound.setEffect(Sound::Effect::Pan, -150);
111+
112+
EXPECT_CALL(*m_player, setPan(-1));
113+
sound.setEffect(Sound::Effect::Pan, -100);
114+
115+
EXPECT_CALL(*m_player, setPan(-0.5));
116+
sound.setEffect(Sound::Effect::Pan, -50);
117+
118+
EXPECT_CALL(*m_player, setPan(0));
119+
sound.setEffect(Sound::Effect::Pan, 0);
120+
121+
EXPECT_CALL(*m_player, setPan(0.5));
122+
sound.setEffect(Sound::Effect::Pan, 50);
123+
124+
EXPECT_CALL(*m_player, setPan(1));
125+
sound.setEffect(Sound::Effect::Pan, 100);
126+
127+
EXPECT_CALL(*m_player, setPan(1));
128+
sound.setEffect(Sound::Effect::Pan, 150);
107129
}
108130

109131
TEST_F(SoundTest, Start)
@@ -166,6 +188,8 @@ TEST_F(SoundTest, Clone)
166188
EXPECT_CALL(*clonePlayer, setVolume(0.45));
167189
EXPECT_CALL(*m_player, pitch()).WillOnce(Return(1.25));
168190
EXPECT_CALL(*clonePlayer, setPitch(1.25));
191+
EXPECT_CALL(*m_player, pan()).WillOnce(Return(-0.75));
192+
EXPECT_CALL(*clonePlayer, setPan(-0.75));
169193
EXPECT_CALL(*clonePlayer, isLoaded()).WillOnce(Return(true));
170194
auto clone = sound->clone();
171195
ASSERT_TRUE(clone);
@@ -183,6 +207,8 @@ TEST_F(SoundTest, Clone)
183207
EXPECT_CALL(*cloneClonePlayer, setVolume(0.62));
184208
EXPECT_CALL(*clonePlayer, pitch()).WillOnce(Return(0.5));
185209
EXPECT_CALL(*cloneClonePlayer, setPitch(0.5));
210+
EXPECT_CALL(*clonePlayer, pan()).WillOnce(Return(0.25));
211+
EXPECT_CALL(*cloneClonePlayer, setPan(0.25));
186212
EXPECT_CALL(*cloneClonePlayer, isLoaded()).WillOnce(Return(true));
187213
auto cloneClone = clone->clone();
188214
ASSERT_TRUE(cloneClone);
@@ -195,6 +221,8 @@ TEST_F(SoundTest, Clone)
195221
EXPECT_CALL(*anotherClonePlayer, setVolume(0.62));
196222
EXPECT_CALL(*clonePlayer, pitch()).WillOnce(Return(2));
197223
EXPECT_CALL(*anotherClonePlayer, setPitch(2));
224+
EXPECT_CALL(*clonePlayer, pan()).WillOnce(Return(1));
225+
EXPECT_CALL(*anotherClonePlayer, setPan(1));
198226
EXPECT_CALL(*anotherClonePlayer, isLoaded()).Times(0);
199227
auto anotherClone = clone->clone();
200228
ASSERT_TRUE(anotherClone);

0 commit comments

Comments
 (0)