Skip to content

Commit e6e1448

Browse files
committed
Implement sound_seteffectto block
1 parent 0086d8b commit e6e1448

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/blocks/soundblocks.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <scratchcpp/iengine.h>
44
#include <scratchcpp/compiler.h>
55
#include <scratchcpp/compilerconstant.h>
6+
#include <scratchcpp/field.h>
67
#include <scratchcpp/target.h>
78
#include <scratchcpp/thread.h>
89
#include <scratchcpp/value.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
}
3739

3840
void SoundBlocks::onInit(IEngine *engine)
@@ -78,6 +80,26 @@ CompilerValue *SoundBlocks::compileStopAllSounds(Compiler *compiler)
7880
return nullptr;
7981
}
8082

83+
CompilerValue *SoundBlocks::compileSetEffectTo(Compiler *compiler)
84+
{
85+
Field *field = compiler->field("EFFECT");
86+
87+
if (!field)
88+
return nullptr;
89+
90+
const std::string &option = field->value().toString();
91+
92+
if (option == "PITCH") {
93+
auto value = compiler->addInput("VALUE");
94+
compiler->addTargetFunctionCall("sound_set_pitch_effect", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { value });
95+
} else if (option == "PAN") {
96+
auto value = compiler->addInput("VALUE");
97+
compiler->addTargetFunctionCall("sound_set_pan_effect", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { value });
98+
}
99+
100+
return nullptr;
101+
}
102+
81103
int sound_wrap_clamp_index(Target *target, int index)
82104
{
83105
const long soundCount = target->sounds().size();
@@ -147,3 +169,13 @@ extern "C" void sound_stopallsounds(ExecutionContext *ctx)
147169
{
148170
ctx->engine()->stopSounds();
149171
}
172+
173+
extern "C" void sound_set_pitch_effect(Target *target, double value)
174+
{
175+
target->setSoundEffectValue(Sound::Effect::Pitch, value);
176+
}
177+
178+
extern "C" void sound_set_pan_effect(Target *target, double value)
179+
{
180+
target->setSoundEffectValue(Sound::Effect::Pan, value);
181+
}

src/blocks/soundblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class SoundBlocks : public IExtension
2323
static CompilerValue *compilePlay(Compiler *compiler);
2424
static CompilerValue *compilePlayUntilDone(Compiler *compiler);
2525
static CompilerValue *compileStopAllSounds(Compiler *compiler);
26+
static CompilerValue *compileSetEffectTo(Compiler *compiler);
2627
};
2728

2829
} // namespace libscratchcpp

test/blocks/sound_blocks_test.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <scratchcpp/test/scriptbuilder.h>
88
#include <scratch/sound_p.h>
99
#include <enginemock.h>
10+
#include <targetmock.h>
1011
#include <audiooutputmock.h>
1112
#include <audioplayermock.h>
1213

@@ -885,3 +886,63 @@ TEST_F(SoundBlocksTest, StopAllSounds_Stage)
885886
EXPECT_CALL(m_engineMock, stopSounds());
886887
thread.run();
887888
}
889+
890+
TEST_F(SoundBlocksTest, SetEffectTo_Invalid)
891+
{
892+
auto target = std::make_shared<TargetMock>();
893+
894+
ScriptBuilder builder(m_extension.get(), m_engine, target);
895+
builder.addBlock("sound_seteffectto");
896+
builder.addDropdownField("EFFECT", "abc");
897+
builder.addValueInput("VALUE", 0);
898+
auto block = builder.currentBlock();
899+
900+
Compiler compiler(&m_engineMock, target.get());
901+
auto code = compiler.compile(block);
902+
Script script(target.get(), block, &m_engineMock);
903+
script.setCode(code);
904+
Thread thread(target.get(), &m_engineMock, &script);
905+
906+
EXPECT_CALL(*target, setSoundEffectValue).Times(0);
907+
thread.run();
908+
}
909+
910+
TEST_F(SoundBlocksTest, SetEffectTo_Pitch)
911+
{
912+
auto target = std::make_shared<TargetMock>();
913+
914+
ScriptBuilder builder(m_extension.get(), m_engine, target);
915+
builder.addBlock("sound_seteffectto");
916+
builder.addDropdownField("EFFECT", "PITCH");
917+
builder.addValueInput("VALUE", 75.2);
918+
auto block = builder.currentBlock();
919+
920+
Compiler compiler(&m_engineMock, target.get());
921+
auto code = compiler.compile(block);
922+
Script script(target.get(), block, &m_engineMock);
923+
script.setCode(code);
924+
Thread thread(target.get(), &m_engineMock, &script);
925+
926+
EXPECT_CALL(*target, setSoundEffectValue(Sound::Effect::Pitch, 75.2));
927+
thread.run();
928+
}
929+
930+
TEST_F(SoundBlocksTest, SetEffectTo_Pan)
931+
{
932+
auto target = std::make_shared<TargetMock>();
933+
934+
ScriptBuilder builder(m_extension.get(), m_engine, target);
935+
builder.addBlock("sound_seteffectto");
936+
builder.addDropdownField("EFFECT", "PAN");
937+
builder.addValueInput("VALUE", -23.8);
938+
auto block = builder.currentBlock();
939+
940+
Compiler compiler(&m_engineMock, target.get());
941+
auto code = compiler.compile(block);
942+
Script script(target.get(), block, &m_engineMock);
943+
script.setCode(code);
944+
Thread thread(target.get(), &m_engineMock, &script);
945+
946+
EXPECT_CALL(*target, setSoundEffectValue(Sound::Effect::Pan, -23.8));
947+
thread.run();
948+
}

0 commit comments

Comments
 (0)