Skip to content

Commit 71095b6

Browse files
committed
Implement sound_seteffectto block
1 parent 3abbb4e commit 71095b6

File tree

3 files changed

+120
-2
lines changed

3 files changed

+120
-2
lines changed

src/blocks/soundblocks.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <scratchcpp/compiler.h>
55
#include <scratchcpp/target.h>
66
#include <scratchcpp/input.h>
7+
#include <scratchcpp/field.h>
78
#include <scratchcpp/sound.h>
89

910
#include "soundblocks.h"
@@ -33,6 +34,7 @@ void SoundBlocks::registerBlocks(IEngine *engine)
3334
engine->addCompileFunction(this, "sound_play", &compilePlay);
3435
engine->addCompileFunction(this, "sound_playuntildone", &compilePlayUntilDone);
3536
engine->addCompileFunction(this, "sound_stopallsounds", &compileStopAllSounds);
37+
engine->addCompileFunction(this, "sound_seteffectto", &compileSetEffectTo);
3638
engine->addCompileFunction(this, "sound_changevolumeby", &compileChangeVolumeBy);
3739
engine->addCompileFunction(this, "sound_setvolumeto", &compileSetVolumeTo);
3840
engine->addCompileFunction(this, "sound_volume", &compileVolume);
@@ -42,7 +44,15 @@ void SoundBlocks::registerBlocks(IEngine *engine)
4244

4345
// Inputs
4446
engine->addInput(this, "SOUND_MENU", SOUND_MENU);
47+
engine->addInput(this, "VALUE", VALUE);
4548
engine->addInput(this, "VOLUME", VOLUME);
49+
50+
// Fields
51+
engine->addField(this, "EFFECT", EFFECT);
52+
53+
// Field values
54+
engine->addFieldValue(this, "PITCH", PITCH);
55+
engine->addFieldValue(this, "PAN", PAN);
4656
}
4757

4858
void SoundBlocks::onInit(IEngine *engine)
@@ -118,6 +128,26 @@ void SoundBlocks::compileStopAllSounds(Compiler *compiler)
118128
compiler->addFunctionCall(&stopAllSounds);
119129
}
120130

131+
void SoundBlocks::compileSetEffectTo(Compiler *compiler)
132+
{
133+
compiler->addInput(VALUE);
134+
int option = compiler->field(EFFECT)->specialValueId();
135+
136+
switch (option) {
137+
case PITCH:
138+
compiler->addFunctionCall(&setPitchEffectTo);
139+
break;
140+
141+
case PAN:
142+
compiler->addFunctionCall(&setPanEffectTo);
143+
break;
144+
145+
default:
146+
assert(false);
147+
break;
148+
}
149+
}
150+
121151
void SoundBlocks::compileChangeVolumeBy(Compiler *compiler)
122152
{
123153
compiler->addInput(VOLUME);
@@ -296,6 +326,22 @@ unsigned int SoundBlocks::stopAllSounds(VirtualMachine *vm)
296326
return 0;
297327
}
298328

329+
unsigned int SoundBlocks::setPitchEffectTo(VirtualMachine *vm)
330+
{
331+
if (Target *target = vm->target())
332+
target->setSoundEffect(Sound::Effect::Pitch, vm->getInput(0, 1)->toDouble());
333+
334+
return 1;
335+
}
336+
337+
unsigned int SoundBlocks::setPanEffectTo(VirtualMachine *vm)
338+
{
339+
if (Target *target = vm->target())
340+
target->setSoundEffect(Sound::Effect::Pan, vm->getInput(0, 1)->toDouble());
341+
342+
return 1;
343+
}
344+
299345
unsigned int SoundBlocks::changeVolumeBy(VirtualMachine *vm)
300346
{
301347
if (Target *target = vm->target())

src/blocks/soundblocks.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@ class SoundBlocks : public IBlockSection
2020
enum Inputs
2121
{
2222
SOUND_MENU,
23+
VALUE,
2324
VOLUME
2425
};
2526

2627
enum Fields
2728
{
28-
29+
EFFECT
2930
};
3031

3132
enum FieldValues
3233
{
33-
34+
PITCH,
35+
PAN
3436
};
3537

3638
std::string name() const override;
@@ -42,6 +44,7 @@ class SoundBlocks : public IBlockSection
4244
static void compilePlay(Compiler *compiler);
4345
static void compilePlayUntilDone(Compiler *compiler);
4446
static void compileStopAllSounds(Compiler *compiler);
47+
static void compileSetEffectTo(Compiler *compiler);
4548
static void compileChangeVolumeBy(Compiler *compiler);
4649
static void compileSetVolumeTo(Compiler *compiler);
4750
static void compileVolume(Compiler *compiler);
@@ -62,6 +65,9 @@ class SoundBlocks : public IBlockSection
6265

6366
static unsigned int stopAllSounds(VirtualMachine *vm);
6467

68+
static unsigned int setPitchEffectTo(VirtualMachine *vm);
69+
static unsigned int setPanEffectTo(VirtualMachine *vm);
70+
6571
static unsigned int changeVolumeBy(VirtualMachine *vm);
6672
static unsigned int setVolumeTo(VirtualMachine *vm);
6773
static unsigned int volume(VirtualMachine *vm);

test/blocks/sound_blocks_test.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ TEST_F(SoundBlocksTest, RegisterBlocks)
102102
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_play", &SoundBlocks::compilePlay));
103103
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_playuntildone", &SoundBlocks::compilePlayUntilDone));
104104
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_stopallsounds", &SoundBlocks::compileStopAllSounds));
105+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_seteffectto", &SoundBlocks::compileSetEffectTo));
105106
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_changevolumeby", &SoundBlocks::compileChangeVolumeBy));
106107
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_setvolumeto", &SoundBlocks::compileSetVolumeTo));
107108
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_volume", &SoundBlocks::compileVolume));
@@ -111,8 +112,16 @@ TEST_F(SoundBlocksTest, RegisterBlocks)
111112

112113
// Inputs
113114
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "SOUND_MENU", SoundBlocks::SOUND_MENU));
115+
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "VALUE", SoundBlocks::VALUE));
114116
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "VOLUME", SoundBlocks::VOLUME));
115117

118+
// Fields
119+
EXPECT_CALL(m_engineMock, addField(m_section.get(), "EFFECT", SoundBlocks::EFFECT));
120+
121+
// Field values
122+
EXPECT_CALL(m_engineMock, addFieldValue(m_section.get(), "PITCH", SoundBlocks::PITCH));
123+
EXPECT_CALL(m_engineMock, addFieldValue(m_section.get(), "PAN", SoundBlocks::PAN));
124+
116125
m_section->registerBlocks(&m_engineMock);
117126
}
118127

@@ -589,6 +598,63 @@ TEST_F(SoundBlocksTest, StopAllSoundsImpl)
589598
ASSERT_EQ(vm.registerCount(), 0);
590599
}
591600

601+
TEST_F(SoundBlocksTest, SetEffectTo)
602+
{
603+
Compiler compiler(&m_engineMock);
604+
605+
// set [pitch] effect to (5.3)
606+
auto block1 = std::make_shared<Block>("a", "sound_seteffectto");
607+
addDropdownField(block1, "EFFECT", SoundBlocks::EFFECT, "PITCH", SoundBlocks::PITCH);
608+
addValueInput(block1, "VALUE", SoundBlocks::VALUE, 5.3);
609+
610+
// set [pan] effect to (-79.52)
611+
auto block2 = std::make_shared<Block>("b", "sound_seteffectto");
612+
addDropdownField(block2, "EFFECT", SoundBlocks::EFFECT, "PAN", SoundBlocks::PAN);
613+
addValueInput(block2, "VALUE", SoundBlocks::VALUE, -79.52);
614+
615+
compiler.init();
616+
617+
EXPECT_CALL(m_engineMock, functionIndex(&SoundBlocks::setPitchEffectTo)).WillOnce(Return(0));
618+
compiler.setBlock(block1);
619+
SoundBlocks::compileSetEffectTo(&compiler);
620+
621+
EXPECT_CALL(m_engineMock, functionIndex(&SoundBlocks::setPanEffectTo)).WillOnce(Return(1));
622+
compiler.setBlock(block2);
623+
SoundBlocks::compileSetEffectTo(&compiler);
624+
625+
compiler.end();
626+
627+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_CONST, 1, vm::OP_EXEC, 1, vm::OP_HALT }));
628+
ASSERT_EQ(compiler.constValues(), std::vector<Value>({ 5.3, -79.52 }));
629+
}
630+
631+
TEST_F(SoundBlocksTest, SetEffectToImpl)
632+
{
633+
static unsigned int bytecode1[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT };
634+
static unsigned int bytecode2[] = { vm::OP_START, vm::OP_CONST, 1, vm::OP_EXEC, 1, vm::OP_HALT };
635+
static BlockFunc functions[] = { &SoundBlocks::setPitchEffectTo, &SoundBlocks::setPanEffectTo };
636+
static Value constValues[] = { -20.7, 12.53 };
637+
638+
TargetMock target;
639+
VirtualMachine vm(&target, nullptr, nullptr);
640+
641+
vm.setBytecode(bytecode1);
642+
vm.setFunctions(functions);
643+
vm.setConstValues(constValues);
644+
645+
EXPECT_CALL(target, setSoundEffect(Sound::Effect::Pitch, -20.7));
646+
vm.run();
647+
648+
ASSERT_EQ(vm.registerCount(), 0);
649+
650+
EXPECT_CALL(target, setSoundEffect(Sound::Effect::Pan, 12.53));
651+
vm.reset();
652+
vm.setBytecode(bytecode2);
653+
vm.run();
654+
655+
ASSERT_EQ(vm.registerCount(), 0);
656+
}
657+
592658
TEST_F(SoundBlocksTest, ChangeVolumeBy)
593659
{
594660
Compiler compiler(&m_engineMock);

0 commit comments

Comments
 (0)