Skip to content

Commit 91c7655

Browse files
committed
Implement sound_changeeffectby block
1 parent ca3c402 commit 91c7655

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

src/blocks/soundblocks.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void SoundBlocks::registerBlocks(IEngine *engine)
3535
engine->addCompileFunction(this, "sound_playuntildone", &compilePlayUntilDone);
3636
engine->addCompileFunction(this, "sound_stopallsounds", &compileStopAllSounds);
3737
engine->addCompileFunction(this, "sound_seteffectto", &compileSetEffectTo);
38+
engine->addCompileFunction(this, "sound_changeeffectby", &compileChangeEffectBy);
3839
engine->addCompileFunction(this, "sound_changevolumeby", &compileChangeVolumeBy);
3940
engine->addCompileFunction(this, "sound_setvolumeto", &compileSetVolumeTo);
4041
engine->addCompileFunction(this, "sound_volume", &compileVolume);
@@ -148,6 +149,26 @@ void SoundBlocks::compileSetEffectTo(Compiler *compiler)
148149
}
149150
}
150151

152+
void SoundBlocks::compileChangeEffectBy(Compiler *compiler)
153+
{
154+
compiler->addInput(VALUE);
155+
int option = compiler->field(EFFECT)->specialValueId();
156+
157+
switch (option) {
158+
case PITCH:
159+
compiler->addFunctionCall(&changePitchEffectBy);
160+
break;
161+
162+
case PAN:
163+
compiler->addFunctionCall(&changePanEffectBy);
164+
break;
165+
166+
default:
167+
assert(false);
168+
break;
169+
}
170+
}
171+
151172
void SoundBlocks::compileChangeVolumeBy(Compiler *compiler)
152173
{
153174
compiler->addInput(VOLUME);
@@ -342,6 +363,22 @@ unsigned int SoundBlocks::setPanEffectTo(VirtualMachine *vm)
342363
return 1;
343364
}
344365

366+
unsigned int SoundBlocks::changePitchEffectBy(VirtualMachine *vm)
367+
{
368+
if (Target *target = vm->target())
369+
target->setSoundEffect(Sound::Effect::Pitch, target->soundEffect(Sound::Effect::Pitch) + vm->getInput(0, 1)->toDouble());
370+
371+
return 1;
372+
}
373+
374+
unsigned int SoundBlocks::changePanEffectBy(VirtualMachine *vm)
375+
{
376+
if (Target *target = vm->target())
377+
target->setSoundEffect(Sound::Effect::Pan, target->soundEffect(Sound::Effect::Pan) + vm->getInput(0, 1)->toDouble());
378+
379+
return 1;
380+
}
381+
345382
unsigned int SoundBlocks::changeVolumeBy(VirtualMachine *vm)
346383
{
347384
if (Target *target = vm->target())

src/blocks/soundblocks.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class SoundBlocks : public IBlockSection
4545
static void compilePlayUntilDone(Compiler *compiler);
4646
static void compileStopAllSounds(Compiler *compiler);
4747
static void compileSetEffectTo(Compiler *compiler);
48+
static void compileChangeEffectBy(Compiler *compiler);
4849
static void compileChangeVolumeBy(Compiler *compiler);
4950
static void compileSetVolumeTo(Compiler *compiler);
5051
static void compileVolume(Compiler *compiler);
@@ -67,6 +68,8 @@ class SoundBlocks : public IBlockSection
6768

6869
static unsigned int setPitchEffectTo(VirtualMachine *vm);
6970
static unsigned int setPanEffectTo(VirtualMachine *vm);
71+
static unsigned int changePitchEffectBy(VirtualMachine *vm);
72+
static unsigned int changePanEffectBy(VirtualMachine *vm);
7073

7174
static unsigned int changeVolumeBy(VirtualMachine *vm);
7275
static unsigned int setVolumeTo(VirtualMachine *vm);

test/blocks/sound_blocks_test.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ TEST_F(SoundBlocksTest, RegisterBlocks)
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));
105105
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_seteffectto", &SoundBlocks::compileSetEffectTo));
106+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_changeeffectby", &SoundBlocks::compileChangeEffectBy));
106107
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_changevolumeby", &SoundBlocks::compileChangeVolumeBy));
107108
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_setvolumeto", &SoundBlocks::compileSetVolumeTo));
108109
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_volume", &SoundBlocks::compileVolume));
@@ -655,6 +656,65 @@ TEST_F(SoundBlocksTest, SetEffectToImpl)
655656
ASSERT_EQ(vm.registerCount(), 0);
656657
}
657658

659+
TEST_F(SoundBlocksTest, ChangeEffectBy)
660+
{
661+
Compiler compiler(&m_engineMock);
662+
663+
// change [pitch] effect by (5.3)
664+
auto block1 = std::make_shared<Block>("a", "sound_changeeffectby");
665+
addDropdownField(block1, "EFFECT", SoundBlocks::EFFECT, "PITCH", SoundBlocks::PITCH);
666+
addValueInput(block1, "VALUE", SoundBlocks::VALUE, 5.3);
667+
668+
// change [pan] effect by (-79.52)
669+
auto block2 = std::make_shared<Block>("b", "sound_changeeffectby");
670+
addDropdownField(block2, "EFFECT", SoundBlocks::EFFECT, "PAN", SoundBlocks::PAN);
671+
addValueInput(block2, "VALUE", SoundBlocks::VALUE, -79.52);
672+
673+
compiler.init();
674+
675+
EXPECT_CALL(m_engineMock, functionIndex(&SoundBlocks::changePitchEffectBy)).WillOnce(Return(0));
676+
compiler.setBlock(block1);
677+
SoundBlocks::compileChangeEffectBy(&compiler);
678+
679+
EXPECT_CALL(m_engineMock, functionIndex(&SoundBlocks::changePanEffectBy)).WillOnce(Return(1));
680+
compiler.setBlock(block2);
681+
SoundBlocks::compileChangeEffectBy(&compiler);
682+
683+
compiler.end();
684+
685+
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 }));
686+
ASSERT_EQ(compiler.constValues(), std::vector<Value>({ 5.3, -79.52 }));
687+
}
688+
689+
TEST_F(SoundBlocksTest, ChangeEffectByImpl)
690+
{
691+
static unsigned int bytecode1[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT };
692+
static unsigned int bytecode2[] = { vm::OP_START, vm::OP_CONST, 1, vm::OP_EXEC, 1, vm::OP_HALT };
693+
static BlockFunc functions[] = { &SoundBlocks::changePitchEffectBy, &SoundBlocks::changePanEffectBy };
694+
static Value constValues[] = { -20.7, 12.53 };
695+
696+
TargetMock target;
697+
VirtualMachine vm(&target, nullptr, nullptr);
698+
699+
vm.setBytecode(bytecode1);
700+
vm.setFunctions(functions);
701+
vm.setConstValues(constValues);
702+
703+
EXPECT_CALL(target, soundEffect(Sound::Effect::Pitch)).WillOnce(Return(56));
704+
EXPECT_CALL(target, setSoundEffect(Sound::Effect::Pitch, 35.3));
705+
vm.run();
706+
707+
ASSERT_EQ(vm.registerCount(), 0);
708+
709+
EXPECT_CALL(target, soundEffect(Sound::Effect::Pan)).WillOnce(Return(-2.5));
710+
EXPECT_CALL(target, setSoundEffect(Sound::Effect::Pan, 10.03));
711+
vm.reset();
712+
vm.setBytecode(bytecode2);
713+
vm.run();
714+
715+
ASSERT_EQ(vm.registerCount(), 0);
716+
}
717+
658718
TEST_F(SoundBlocksTest, ChangeVolumeBy)
659719
{
660720
Compiler compiler(&m_engineMock);

0 commit comments

Comments
 (0)