Skip to content

Commit 66c57d8

Browse files
committed
Set sound volume from Target
1 parent 4451a49 commit 66c57d8

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/scratch/target.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ int Target::addSound(std::shared_ptr<Sound> sound)
349349
if (it != impl->sounds.end())
350350
return it - impl->sounds.begin();
351351

352+
assert(sound);
353+
354+
if (sound)
355+
sound->setVolume(impl->volume);
356+
352357
impl->sounds.push_back(sound);
353358
return impl->sounds.size() - 1;
354359
}
@@ -398,7 +403,7 @@ double Target::volume() const
398403
return impl->volume;
399404
}
400405

401-
/*! Sets the volume. */
406+
/*! Sets the volume of all sounds of this target. */
402407
void Target::setVolume(double newVolume)
403408
{
404409
if (newVolume >= 100)
@@ -407,6 +412,11 @@ void Target::setVolume(double newVolume)
407412
impl->volume = 0;
408413
else
409414
impl->volume = newVolume;
415+
416+
for (auto sound : impl->sounds) {
417+
if (sound)
418+
sound->setVolume(impl->volume);
419+
}
410420
}
411421

412422
/*! Returns the value of the given graphics effect. */

test/scratch_classes/target_test.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
#include <scratchcpp/comment.h>
66
#include <scratchcpp/costume.h>
77
#include <scratchcpp/sound.h>
8+
#include <scratch/sound_p.h>
89
#include <enginemock.h>
910
#include <targetmock.h>
1011
#include <graphicseffectmock.h>
12+
#include <audioplayerfactorymock.h>
13+
#include <audioplayermock.h>
1114

1215
#include "../common.h"
1316

@@ -385,13 +388,22 @@ TEST(TargetTest, Costumes)
385388

386389
TEST(TargetTest, Sounds)
387390
{
391+
AudioPlayerFactoryMock factory;
392+
auto player1 = std::make_shared<AudioPlayerMock>();
393+
auto player2 = std::make_shared<AudioPlayerMock>();
394+
auto player3 = std::make_shared<AudioPlayerMock>();
395+
SoundPrivate::playerFactory = &factory;
396+
EXPECT_CALL(factory, create()).WillOnce(Return(player1)).WillOnce(Return(player2)).WillOnce(Return(player3));
388397
auto s1 = std::make_shared<Sound>("sound1", "", "mp3");
389398
auto s2 = std::make_shared<Sound>("sound2", "", "wav");
390399
auto s3 = std::make_shared<Sound>("sound3", "", "mp3");
391400

392401
TargetMock target;
393402
EXPECT_CALL(target, dataSource()).Times(14).WillRepeatedly(Return(nullptr));
394403

404+
EXPECT_CALL(*player1, setVolume(1));
405+
EXPECT_CALL(*player2, setVolume(1));
406+
EXPECT_CALL(*player3, setVolume(1));
395407
ASSERT_EQ(target.addSound(s1), 0);
396408
ASSERT_EQ(target.addSound(s2), 1);
397409
ASSERT_EQ(target.addSound(s3), 2);
@@ -419,6 +431,9 @@ TEST(TargetTest, Sounds)
419431
TargetMock target2;
420432
EXPECT_CALL(target2, dataSource()).Times(15).WillRepeatedly(Return(&source));
421433

434+
EXPECT_CALL(*player1, setVolume(1));
435+
EXPECT_CALL(*player2, setVolume(1));
436+
EXPECT_CALL(*player3, setVolume(1));
422437
ASSERT_EQ(target2.addSound(s1), 0);
423438
ASSERT_EQ(target2.addSound(s2), 1);
424439
ASSERT_EQ(target2.addSound(s3), 2);
@@ -438,7 +453,11 @@ TEST(TargetTest, Sounds)
438453

439454
ASSERT_EQ(target2.sounds(), source.sounds());
440455

456+
auto player4 = std::make_shared<AudioPlayerMock>();
457+
EXPECT_CALL(factory, create()).WillOnce(Return(player4));
441458
auto s4 = std::make_shared<Sound>("sound4", "", "wav");
459+
460+
EXPECT_CALL(*player4, setVolume(1));
442461
ASSERT_EQ(source.addSound(s4), 3);
443462

444463
EXPECT_CALL(target2, dataSource()).WillOnce(Return(&source));
@@ -457,6 +476,8 @@ TEST(TargetTest, Sounds)
457476
ASSERT_EQ(source.findSound("sound2"), 1);
458477
ASSERT_EQ(source.findSound("sound3"), 2);
459478
ASSERT_EQ(source.findSound("sound4"), 3);
479+
480+
SoundPrivate::playerFactory = nullptr;
460481
}
461482

462483
TEST(TargetTest, LayerOrder)
@@ -480,6 +501,45 @@ TEST(TargetTest, Volume)
480501

481502
target.setVolume(-4.2);
482503
ASSERT_EQ(target.volume(), 0);
504+
505+
target.setVolume(64.9);
506+
ASSERT_EQ(target.volume(), 64.9);
507+
508+
AudioPlayerFactoryMock factory;
509+
auto player1 = std::make_shared<AudioPlayerMock>();
510+
auto player2 = std::make_shared<AudioPlayerMock>();
511+
auto player3 = std::make_shared<AudioPlayerMock>();
512+
SoundPrivate::playerFactory = &factory;
513+
EXPECT_CALL(factory, create()).WillOnce(Return(player1)).WillOnce(Return(player2)).WillOnce(Return(player3));
514+
auto s1 = std::make_shared<Sound>("", "", "");
515+
auto s2 = std::make_shared<Sound>("", "", "");
516+
auto s3 = std::make_shared<Sound>("", "", "");
517+
518+
EXPECT_CALL(*player1, setVolume(0.649));
519+
target.addSound(s1);
520+
521+
EXPECT_CALL(*player2, setVolume(0.649));
522+
target.addSound(s2);
523+
524+
EXPECT_CALL(*player3, setVolume(0.649));
525+
target.addSound(s3);
526+
527+
EXPECT_CALL(*player1, setVolume(0.7692));
528+
EXPECT_CALL(*player2, setVolume(0.7692));
529+
EXPECT_CALL(*player3, setVolume(0.7692));
530+
target.setVolume(76.92);
531+
532+
EXPECT_CALL(*player1, setVolume(1));
533+
EXPECT_CALL(*player2, setVolume(1));
534+
EXPECT_CALL(*player3, setVolume(1));
535+
target.setVolume(102);
536+
537+
EXPECT_CALL(*player1, setVolume(0));
538+
EXPECT_CALL(*player2, setVolume(0));
539+
EXPECT_CALL(*player3, setVolume(0));
540+
target.setVolume(-0.5);
541+
542+
SoundPrivate::playerFactory = nullptr;
483543
}
484544

485545
TEST(TargetTest, GraphicsEffects)

0 commit comments

Comments
 (0)