Skip to content

Commit c95da82

Browse files
committed
Clamp sound effect values in Target
1 parent 8edd51a commit c95da82

File tree

4 files changed

+37
-26
lines changed

4 files changed

+37
-26
lines changed

src/scratch/sound.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111

1212
using namespace libscratchcpp;
1313

14-
static std::unordered_map<Sound::Effect, std::pair<double, double>> EFFECT_RANGE = {
15-
{ Sound::Effect::Pitch, { -360, 360 } }, // -3 to 3 octaves
16-
{ Sound::Effect::Pan, { -100, 100 } } // // 100% left to 100% right
17-
};
18-
1914
/*! Constructs Sound. */
2015
Sound::Sound(const std::string &name, const std::string &id, const std::string &format) :
2116
Asset(name, id, format),
@@ -56,13 +51,6 @@ void Sound::setVolume(double volume)
5651
/*! Sets the value of the given sound effect. */
5752
void Sound::setEffect(Effect effect, double value)
5853
{
59-
auto it = EFFECT_RANGE.find(effect);
60-
61-
if (it == EFFECT_RANGE.cend())
62-
return;
63-
64-
value = std::clamp(value, it->second.first, it->second.second);
65-
6654
switch (effect) {
6755
case Effect::Pitch: {
6856
// Convert from linear

src/scratch/target.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <scratchcpp/block.h>
88
#include <scratchcpp/comment.h>
99
#include <scratchcpp/iengine.h>
10+
#include <scratchcpp/igraphicseffect.h>
1011

1112
#include <unordered_set>
1213

@@ -16,6 +17,11 @@ using namespace libscratchcpp;
1617

1718
static const std::unordered_set<std::string> RESERVED_NAMES = { "_mouse_", "_stage_", "_edge_", "_myself_", "_random_" };
1819

20+
static std::unordered_map<Sound::Effect, std::pair<double, double>> SOUND_EFFECT_RANGE = {
21+
{ Sound::Effect::Pitch, { -360, 360 } }, // -3 to 3 octaves
22+
{ Sound::Effect::Pan, { -100, 100 } } // // 100% left to 100% right
23+
};
24+
1925
/*! Constructs target. */
2026
Target::Target() :
2127
impl(spimpl::make_unique_impl<TargetPrivate>())
@@ -436,6 +442,12 @@ double Target::soundEffectValue(Sound::Effect effect) const
436442
/*! Sets the value of the given sound effect. */
437443
void Target::setSoundEffectValue(Sound::Effect effect, double value)
438444
{
445+
auto it = SOUND_EFFECT_RANGE.find(effect);
446+
447+
if (it == SOUND_EFFECT_RANGE.cend())
448+
return;
449+
450+
value = std::clamp(value, it->second.first, it->second.second);
439451
impl->soundEffects[effect] = value;
440452

441453
for (auto sound : impl->sounds) {

test/assets/sound_test.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ TEST_F(SoundTest, SetEffect)
8484
Sound sound("sound1", "a", "wav");
8585

8686
// Pitch
87-
EXPECT_CALL(*m_player, setPitch(0.125));
88-
sound.setEffect(Sound::Effect::Pitch, -400);
89-
9087
EXPECT_CALL(*m_player, setPitch(0.125));
9188
sound.setEffect(Sound::Effect::Pitch, -360);
9289

@@ -102,13 +99,7 @@ TEST_F(SoundTest, SetEffect)
10299
EXPECT_CALL(*m_player, setPitch(8));
103100
sound.setEffect(Sound::Effect::Pitch, 360);
104101

105-
EXPECT_CALL(*m_player, setPitch(8));
106-
sound.setEffect(Sound::Effect::Pitch, 400);
107-
108102
// Pan
109-
EXPECT_CALL(*m_player, setPan(-1));
110-
sound.setEffect(Sound::Effect::Pan, -150);
111-
112103
EXPECT_CALL(*m_player, setPan(-1));
113104
sound.setEffect(Sound::Effect::Pan, -100);
114105

@@ -123,9 +114,6 @@ TEST_F(SoundTest, SetEffect)
123114

124115
EXPECT_CALL(*m_player, setPan(1));
125116
sound.setEffect(Sound::Effect::Pan, 100);
126-
127-
EXPECT_CALL(*m_player, setPan(1));
128-
sound.setEffect(Sound::Effect::Pan, 150);
129117
}
130118

131119
TEST_F(SoundTest, Start)

test/scratch_classes/target_test.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,36 @@ TEST(TargetTest, SoundEffects)
519519
ASSERT_EQ(target.soundEffectValue(Sound::Effect::Pan), 0);
520520

521521
auto s1 = std::make_shared<SoundMock>();
522+
EXPECT_CALL(*s1, setVolume);
523+
target.addSound(s1);
524+
525+
EXPECT_CALL(*s1, setEffect(Sound::Effect::Pitch, -360));
526+
target.setSoundEffectValue(Sound::Effect::Pitch, -400);
527+
ASSERT_EQ(target.soundEffectValue(Sound::Effect::Pitch), -360);
528+
529+
EXPECT_CALL(*s1, setEffect(Sound::Effect::Pitch, 360));
530+
target.setSoundEffectValue(Sound::Effect::Pitch, 400);
531+
ASSERT_EQ(target.soundEffectValue(Sound::Effect::Pitch), 360);
532+
533+
EXPECT_CALL(*s1, setEffect(Sound::Effect::Pan, -100));
534+
target.setSoundEffectValue(Sound::Effect::Pan, -200);
535+
ASSERT_EQ(target.soundEffectValue(Sound::Effect::Pan), -100);
536+
537+
EXPECT_CALL(*s1, setEffect(Sound::Effect::Pan, 100));
538+
target.setSoundEffectValue(Sound::Effect::Pan, 200);
539+
ASSERT_EQ(target.soundEffectValue(Sound::Effect::Pan), 100);
540+
541+
EXPECT_CALL(*s1, setEffect(Sound::Effect::Pitch, 0));
542+
EXPECT_CALL(*s1, setEffect(Sound::Effect::Pan, 0));
543+
target.clearSoundEffects();
544+
ASSERT_EQ(target.soundEffectValue(Sound::Effect::Pitch), 0);
545+
ASSERT_EQ(target.soundEffectValue(Sound::Effect::Pan), 0);
546+
522547
auto s2 = std::make_shared<SoundMock>();
523548
auto s3 = std::make_shared<SoundMock>();
524549

525-
EXPECT_CALL(*s1, setVolume);
526550
EXPECT_CALL(*s2, setVolume);
527551
EXPECT_CALL(*s3, setVolume);
528-
target.addSound(s1);
529552
target.addSound(s2);
530553
target.addSound(s3);
531554

0 commit comments

Comments
 (0)