Skip to content

Commit 3c3428d

Browse files
committed
Implement sound_setvolumeto block
1 parent e68170e commit 3c3428d

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/blocks/soundblocks.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ void SoundBlocks::registerBlocks(IEngine *engine)
1717
{
1818
// Blocks
1919
engine->addCompileFunction(this, "sound_changevolumeby", &compileChangeVolumeBy);
20+
engine->addCompileFunction(this, "sound_setvolumeto", &compileSetVolumeTo);
2021

2122
// Inputs
2223
engine->addInput(this, "VOLUME", VOLUME);
@@ -28,10 +29,24 @@ void SoundBlocks::compileChangeVolumeBy(Compiler *compiler)
2829
compiler->addFunctionCall(&changeVolumeBy);
2930
}
3031

32+
void SoundBlocks::compileSetVolumeTo(Compiler *compiler)
33+
{
34+
compiler->addInput(VOLUME);
35+
compiler->addFunctionCall(&setVolumeTo);
36+
}
37+
3138
unsigned int SoundBlocks::changeVolumeBy(VirtualMachine *vm)
3239
{
3340
if (Target *target = vm->target())
3441
target->setVolume(target->volume() + vm->getInput(0, 1)->toDouble());
3542

3643
return 1;
3744
}
45+
46+
unsigned int SoundBlocks::setVolumeTo(VirtualMachine *vm)
47+
{
48+
if (Target *target = vm->target())
49+
target->setVolume(vm->getInput(0, 1)->toDouble());
50+
51+
return 1;
52+
}

src/blocks/soundblocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ class SoundBlocks : public IBlockSection
3131
void registerBlocks(IEngine *engine) override;
3232

3333
static void compileChangeVolumeBy(Compiler *compiler);
34+
static void compileSetVolumeTo(Compiler *compiler);
3435

3536
static unsigned int changeVolumeBy(VirtualMachine *vm);
37+
static unsigned int setVolumeTo(VirtualMachine *vm);
3638
};
3739

3840
} // namespace libscratchcpp

test/blocks/sound_blocks_test.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ TEST_F(SoundBlocksTest, RegisterBlocks)
9595
{
9696
// Blocks
9797
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_changevolumeby", &SoundBlocks::compileChangeVolumeBy));
98+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_setvolumeto", &SoundBlocks::compileSetVolumeTo));
9899

99100
// Inputs
100101
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "VOLUME", SoundBlocks::VOLUME));
@@ -157,3 +158,43 @@ TEST_F(SoundBlocksTest, ChangeVolumeByImpl)
157158
ASSERT_EQ(vm.registerCount(), 0);
158159
ASSERT_EQ(std::round(target.volume() * 100) / 100, 93.32);
159160
}
161+
162+
TEST_F(SoundBlocksTest, SetVolumeTo)
163+
{
164+
Compiler compiler(&m_engineMock);
165+
166+
// set volume to (43.409) %
167+
auto block = std::make_shared<Block>("a", "sound_setvolumeto");
168+
addValueInput(block, "VOLUME", SoundBlocks::VOLUME, 43.409);
169+
170+
EXPECT_CALL(m_engineMock, functionIndex(&SoundBlocks::setVolumeTo)).WillOnce(Return(0));
171+
172+
compiler.init();
173+
compiler.setBlock(block);
174+
SoundBlocks::compileSetVolumeTo(&compiler);
175+
compiler.end();
176+
177+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT }));
178+
ASSERT_EQ(compiler.constValues().size(), 1);
179+
ASSERT_EQ(compiler.constValues()[0].toDouble(), 43.409);
180+
}
181+
182+
TEST_F(SoundBlocksTest, SetVolumeToImpl)
183+
{
184+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT };
185+
static BlockFunc functions[] = { &SoundBlocks::setVolumeTo };
186+
static Value constValues[] = { 43.409 };
187+
188+
Target target;
189+
target.setVolume(42.4);
190+
191+
VirtualMachine vm(&target, nullptr, nullptr);
192+
193+
vm.setBytecode(bytecode);
194+
vm.setFunctions(functions);
195+
vm.setConstValues(constValues);
196+
vm.run();
197+
198+
ASSERT_EQ(vm.registerCount(), 0);
199+
ASSERT_EQ(target.volume(), 43.409);
200+
}

0 commit comments

Comments
 (0)