Skip to content

Commit 7af93e1

Browse files
committed
Implement sound_changeeffectby block
1 parent e6e1448 commit 7af93e1

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

src/blocks/soundblocks.cpp

Lines changed: 31 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
}
3940

4041
void SoundBlocks::onInit(IEngine *engine)
@@ -100,6 +101,26 @@ CompilerValue *SoundBlocks::compileSetEffectTo(Compiler *compiler)
100101
return nullptr;
101102
}
102103

104+
CompilerValue *SoundBlocks::compileChangeEffectBy(Compiler *compiler)
105+
{
106+
Field *field = compiler->field("EFFECT");
107+
108+
if (!field)
109+
return nullptr;
110+
111+
const std::string &option = field->value().toString();
112+
113+
if (option == "PITCH") {
114+
auto value = compiler->addInput("VALUE");
115+
compiler->addTargetFunctionCall("sound_change_pitch_effect", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { value });
116+
} else if (option == "PAN") {
117+
auto value = compiler->addInput("VALUE");
118+
compiler->addTargetFunctionCall("sound_change_pan_effect", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { value });
119+
}
120+
121+
return nullptr;
122+
}
123+
103124
int sound_wrap_clamp_index(Target *target, int index)
104125
{
105126
const long soundCount = target->sounds().size();
@@ -179,3 +200,13 @@ extern "C" void sound_set_pan_effect(Target *target, double value)
179200
{
180201
target->setSoundEffectValue(Sound::Effect::Pan, value);
181202
}
203+
204+
extern "C" void sound_change_pitch_effect(Target *target, double value)
205+
{
206+
target->setSoundEffectValue(Sound::Effect::Pitch, target->soundEffectValue(Sound::Effect::Pitch) + value);
207+
}
208+
209+
extern "C" void sound_change_pan_effect(Target *target, double value)
210+
{
211+
target->setSoundEffectValue(Sound::Effect::Pan, target->soundEffectValue(Sound::Effect::Pan) + value);
212+
}

src/blocks/soundblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class SoundBlocks : public IExtension
2424
static CompilerValue *compilePlayUntilDone(Compiler *compiler);
2525
static CompilerValue *compileStopAllSounds(Compiler *compiler);
2626
static CompilerValue *compileSetEffectTo(Compiler *compiler);
27+
static CompilerValue *compileChangeEffectBy(Compiler *compiler);
2728
};
2829

2930
} // namespace libscratchcpp

test/blocks/sound_blocks_test.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,3 +946,66 @@ TEST_F(SoundBlocksTest, SetEffectTo_Pan)
946946
EXPECT_CALL(*target, setSoundEffectValue(Sound::Effect::Pan, -23.8));
947947
thread.run();
948948
}
949+
950+
TEST_F(SoundBlocksTest, ChangeEffectBy_Invalid)
951+
{
952+
auto target = std::make_shared<TargetMock>();
953+
954+
ScriptBuilder builder(m_extension.get(), m_engine, target);
955+
builder.addBlock("sound_changeeffectby");
956+
builder.addDropdownField("EFFECT", "abc");
957+
builder.addValueInput("VALUE", 1);
958+
auto block = builder.currentBlock();
959+
960+
Compiler compiler(&m_engineMock, target.get());
961+
auto code = compiler.compile(block);
962+
Script script(target.get(), block, &m_engineMock);
963+
script.setCode(code);
964+
Thread thread(target.get(), &m_engineMock, &script);
965+
966+
EXPECT_CALL(*target, soundEffectValue).Times(0);
967+
EXPECT_CALL(*target, setSoundEffectValue).Times(0);
968+
thread.run();
969+
}
970+
971+
TEST_F(SoundBlocksTest, ChangeEffectBy_Pitch)
972+
{
973+
auto target = std::make_shared<TargetMock>();
974+
975+
ScriptBuilder builder(m_extension.get(), m_engine, target);
976+
builder.addBlock("sound_changeeffectby");
977+
builder.addDropdownField("EFFECT", "PITCH");
978+
builder.addValueInput("VALUE", 75.2);
979+
auto block = builder.currentBlock();
980+
981+
Compiler compiler(&m_engineMock, target.get());
982+
auto code = compiler.compile(block);
983+
Script script(target.get(), block, &m_engineMock);
984+
script.setCode(code);
985+
Thread thread(target.get(), &m_engineMock, &script);
986+
987+
EXPECT_CALL(*target, soundEffectValue(Sound::Effect::Pitch)).WillOnce(Return(-28.6));
988+
EXPECT_CALL(*target, setSoundEffectValue(Sound::Effect::Pitch, 46.6));
989+
thread.run();
990+
}
991+
992+
TEST_F(SoundBlocksTest, ChangeEffectBy_Pan)
993+
{
994+
auto target = std::make_shared<TargetMock>();
995+
996+
ScriptBuilder builder(m_extension.get(), m_engine, target);
997+
builder.addBlock("sound_changeeffectby");
998+
builder.addDropdownField("EFFECT", "PAN");
999+
builder.addValueInput("VALUE", -23.8);
1000+
auto block = builder.currentBlock();
1001+
1002+
Compiler compiler(&m_engineMock, target.get());
1003+
auto code = compiler.compile(block);
1004+
Script script(target.get(), block, &m_engineMock);
1005+
script.setCode(code);
1006+
Thread thread(target.get(), &m_engineMock, &script);
1007+
1008+
EXPECT_CALL(*target, soundEffectValue(Sound::Effect::Pan)).WillOnce(Return(12.5));
1009+
EXPECT_CALL(*target, setSoundEffectValue(Sound::Effect::Pan, -11.3));
1010+
thread.run();
1011+
}

0 commit comments

Comments
 (0)