Skip to content

Commit 3abbb4e

Browse files
committed
Target: Add API for sound effects
1 parent 7b7ba4e commit 3abbb4e

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

include/scratchcpp/target.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "global.h"
88
#include "spimpl.h"
99
#include "rect.h"
10+
#include "sound.h"
1011

1112
namespace libscratchcpp
1213
{
@@ -88,6 +89,8 @@ class LIBSCRATCHCPP_EXPORT Target
8889
double volume() const;
8990
void setVolume(double newVolume);
9091

92+
virtual void setSoundEffect(Sound::Effect effect, double value);
93+
9194
virtual Rect boundingRect() const;
9295
virtual Rect fastBoundingRect() const;
9396

src/scratch/target.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ int Target::addSound(std::shared_ptr<Sound> sound)
359359
if (sound) {
360360
sound->setTarget(this);
361361
sound->setVolume(impl->volume);
362+
363+
for (const auto &[effect, value] : impl->soundEffects)
364+
sound->setEffect(effect, value);
362365
}
363366

364367
impl->sounds.push_back(sound);
@@ -419,6 +422,17 @@ void Target::setVolume(double newVolume)
419422
}
420423
}
421424

425+
/*! Sets the value of the given sound effect. */
426+
void Target::setSoundEffect(Sound::Effect effect, double value)
427+
{
428+
impl->soundEffects[effect] = value;
429+
430+
for (auto sound : impl->sounds) {
431+
if (sound)
432+
sound->setEffect(effect, value);
433+
}
434+
}
435+
422436
/*! Returns the bounding rectangle of the sprite. */
423437
Rect Target::boundingRect() const
424438
{

src/scratch/target_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct TargetPrivate
3636
std::vector<std::shared_ptr<Sound>> sounds;
3737
int layerOrder = 0;
3838
double volume = 100;
39+
std::unordered_map<Sound::Effect, double> soundEffects;
3940
std::unordered_map<IGraphicsEffect *, double> graphicsEffects;
4041
Target::BubbleType bubbleType = Target::BubbleType::Say;
4142
std::string bubbleText;

test/mocks/targetmock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class TargetMock : public Target
1717

1818
MOCK_METHOD(void, setLayerOrder, (int), (override));
1919

20+
MOCK_METHOD(void, setSoundEffect, (Sound::Effect, double), (override));
21+
2022
MOCK_METHOD(Rect, boundingRect, (), (const, override));
2123
MOCK_METHOD(Rect, fastBoundingRect, (), (const, override));
2224

test/scratch_classes/target_test.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <graphicseffectmock.h>
1414
#include <audiooutputmock.h>
1515
#include <audioplayermock.h>
16+
#include <soundmock.h>
1617

1718
#include "../common.h"
1819

@@ -511,6 +512,38 @@ TEST(TargetTest, Volume)
511512
SoundPrivate::audioOutput = nullptr;
512513
}
513514

515+
TEST(TargetTest, SetSoundEffect)
516+
{
517+
Target target;
518+
519+
auto s1 = std::make_shared<SoundMock>();
520+
auto s2 = std::make_shared<SoundMock>();
521+
auto s3 = std::make_shared<SoundMock>();
522+
523+
EXPECT_CALL(*s1, setVolume);
524+
EXPECT_CALL(*s2, setVolume);
525+
EXPECT_CALL(*s3, setVolume);
526+
target.addSound(s1);
527+
target.addSound(s2);
528+
target.addSound(s3);
529+
530+
EXPECT_CALL(*s1, setEffect(Sound::Effect::Pitch, 12.5));
531+
EXPECT_CALL(*s2, setEffect(Sound::Effect::Pitch, 12.5));
532+
EXPECT_CALL(*s3, setEffect(Sound::Effect::Pitch, 12.5));
533+
target.setSoundEffect(Sound::Effect::Pitch, 12.5);
534+
535+
EXPECT_CALL(*s1, setEffect(Sound::Effect::Pan, -56.7));
536+
EXPECT_CALL(*s2, setEffect(Sound::Effect::Pan, -56.7));
537+
EXPECT_CALL(*s3, setEffect(Sound::Effect::Pan, -56.7));
538+
target.setSoundEffect(Sound::Effect::Pan, -56.7);
539+
540+
auto s4 = std::make_shared<SoundMock>();
541+
EXPECT_CALL(*s4, setVolume);
542+
EXPECT_CALL(*s4, setEffect(Sound::Effect::Pitch, 12.5));
543+
EXPECT_CALL(*s4, setEffect(Sound::Effect::Pan, -56.7));
544+
target.addSound(s4);
545+
}
546+
514547
TEST(TargetTest, CurrentCostumeWidth)
515548
{
516549
Target target;

0 commit comments

Comments
 (0)