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,7 +34,10 @@ 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);
38+ engine->addCompileFunction (this , " sound_changeeffectby" , &compileChangeEffectBy);
3639 engine->addCompileFunction (this , " sound_changevolumeby" , &compileChangeVolumeBy);
40+ engine->addCompileFunction (this , " sound_cleareffects" , &compileClearEffects);
3741 engine->addCompileFunction (this , " sound_setvolumeto" , &compileSetVolumeTo);
3842 engine->addCompileFunction (this , " sound_volume" , &compileVolume);
3943
@@ -42,7 +46,15 @@ void SoundBlocks::registerBlocks(IEngine *engine)
4246
4347 // Inputs
4448 engine->addInput (this , " SOUND_MENU" , SOUND_MENU);
49+ engine->addInput (this , " VALUE" , VALUE);
4550 engine->addInput (this , " VOLUME" , VOLUME);
51+
52+ // Fields
53+ engine->addField (this , " EFFECT" , EFFECT);
54+
55+ // Field values
56+ engine->addFieldValue (this , " PITCH" , PITCH);
57+ engine->addFieldValue (this , " PAN" , PAN);
4658}
4759
4860void SoundBlocks::onInit (IEngine *engine)
@@ -118,6 +130,51 @@ void SoundBlocks::compileStopAllSounds(Compiler *compiler)
118130 compiler->addFunctionCall (&stopAllSounds);
119131}
120132
133+ void SoundBlocks::compileSetEffectTo (Compiler *compiler)
134+ {
135+ compiler->addInput (VALUE);
136+ int option = compiler->field (EFFECT)->specialValueId ();
137+
138+ switch (option) {
139+ case PITCH:
140+ compiler->addFunctionCall (&setPitchEffectTo);
141+ break ;
142+
143+ case PAN:
144+ compiler->addFunctionCall (&setPanEffectTo);
145+ break ;
146+
147+ default :
148+ assert (false );
149+ break ;
150+ }
151+ }
152+
153+ void SoundBlocks::compileChangeEffectBy (Compiler *compiler)
154+ {
155+ compiler->addInput (VALUE);
156+ int option = compiler->field (EFFECT)->specialValueId ();
157+
158+ switch (option) {
159+ case PITCH:
160+ compiler->addFunctionCall (&changePitchEffectBy);
161+ break ;
162+
163+ case PAN:
164+ compiler->addFunctionCall (&changePanEffectBy);
165+ break ;
166+
167+ default :
168+ assert (false );
169+ break ;
170+ }
171+ }
172+
173+ void SoundBlocks::compileClearEffects (Compiler *compiler)
174+ {
175+ compiler->addFunctionCall (&clearEffects);
176+ }
177+
121178void SoundBlocks::compileChangeVolumeBy (Compiler *compiler)
122179{
123180 compiler->addInput (VOLUME);
@@ -296,6 +353,46 @@ unsigned int SoundBlocks::stopAllSounds(VirtualMachine *vm)
296353 return 0 ;
297354}
298355
356+ unsigned int SoundBlocks::setPitchEffectTo (VirtualMachine *vm)
357+ {
358+ if (Target *target = vm->target ())
359+ target->setSoundEffect (Sound::Effect::Pitch, vm->getInput (0 , 1 )->toDouble ());
360+
361+ return 1 ;
362+ }
363+
364+ unsigned int SoundBlocks::setPanEffectTo (VirtualMachine *vm)
365+ {
366+ if (Target *target = vm->target ())
367+ target->setSoundEffect (Sound::Effect::Pan, vm->getInput (0 , 1 )->toDouble ());
368+
369+ return 1 ;
370+ }
371+
372+ unsigned int SoundBlocks::changePitchEffectBy (VirtualMachine *vm)
373+ {
374+ if (Target *target = vm->target ())
375+ target->setSoundEffect (Sound::Effect::Pitch, target->soundEffect (Sound::Effect::Pitch) + vm->getInput (0 , 1 )->toDouble ());
376+
377+ return 1 ;
378+ }
379+
380+ unsigned int SoundBlocks::changePanEffectBy (VirtualMachine *vm)
381+ {
382+ if (Target *target = vm->target ())
383+ target->setSoundEffect (Sound::Effect::Pan, target->soundEffect (Sound::Effect::Pan) + vm->getInput (0 , 1 )->toDouble ());
384+
385+ return 1 ;
386+ }
387+
388+ unsigned int SoundBlocks::clearEffects (VirtualMachine *vm)
389+ {
390+ if (Target *target = vm->target ())
391+ target->clearSoundEffects ();
392+
393+ return 0 ;
394+ }
395+
299396unsigned int SoundBlocks::changeVolumeBy (VirtualMachine *vm)
300397{
301398 if (Target *target = vm->target ())
0 commit comments