Skip to content

Commit c43ee3a

Browse files
committed
Implement sensing_loud block
1 parent 25686e6 commit c43ee3a

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

src/blocks/sensingblocks.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
5151
engine->addCompileFunction(this, "sensing_mousey", &compileMouseY);
5252
engine->addCompileFunction(this, "sensing_setdragmode", &compileSetDragMode);
5353
engine->addCompileFunction(this, "sensing_loudness", &compileLoudness);
54+
engine->addCompileFunction(this, "sensing_loud", &compileLoud);
5455
}
5556

5657
void SensingBlocks::onInit(IEngine *engine)
@@ -217,6 +218,13 @@ CompilerValue *SensingBlocks::compileLoudness(Compiler *compiler)
217218
return compiler->addFunctionCall("sensing_loudness", Compiler::StaticType::Number);
218219
}
219220

221+
CompilerValue *SensingBlocks::compileLoud(Compiler *compiler)
222+
{
223+
CompilerValue *treshold = compiler->addConstValue(10);
224+
CompilerValue *loudness = compiler->addFunctionCall("sensing_loudness", Compiler::StaticType::Number);
225+
return compiler->createCmpGT(loudness, treshold);
226+
}
227+
220228
void SensingBlocks::onAnswer(const std::string &answer)
221229
{
222230
// https://github.com/scratchfoundation/scratch-vm/blob/6055823f203a696165084b873e661713806583ec/src/blocks/scratch3_sensing.js#L99-L115

src/blocks/sensingblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class SensingBlocks : public IExtension
5757
static CompilerValue *compileMouseY(Compiler *compiler);
5858
static CompilerValue *compileSetDragMode(Compiler *compiler);
5959
static CompilerValue *compileLoudness(Compiler *compiler);
60+
static CompilerValue *compileLoud(Compiler *compiler);
6061

6162
static void onAnswer(const std::string &answer);
6263
static void enqueueAsk(const std::string &question, Thread *thread);

test/blocks/sensing_blocks_test.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,3 +1396,63 @@ TEST_F(SensingBlocksTest, Loudness)
13961396
ASSERT_EQ(value_toDouble(&value), 62);
13971397
value_free(&value);
13981398
}
1399+
1400+
TEST_F(SensingBlocksTest, Loud_Below)
1401+
{
1402+
auto targetMock = std::make_shared<TargetMock>();
1403+
1404+
ScriptBuilder builder(m_extension.get(), m_engine, targetMock);
1405+
builder.addBlock("sensing_loud");
1406+
Block *block = builder.currentBlock();
1407+
1408+
Compiler compiler(&m_engineMock, targetMock.get());
1409+
auto code = compiler.compile(block, Compiler::CodeType::Reporter);
1410+
Script script(targetMock.get(), block, &m_engineMock);
1411+
script.setCode(code);
1412+
Thread thread(targetMock.get(), &m_engineMock, &script);
1413+
1414+
EXPECT_CALL(*m_audioLoudness, getLoudness()).WillOnce(Return(9));
1415+
ValueData value = thread.runReporter();
1416+
ASSERT_FALSE(value_toBool(&value));
1417+
value_free(&value);
1418+
}
1419+
1420+
TEST_F(SensingBlocksTest, Loud_Equal)
1421+
{
1422+
auto targetMock = std::make_shared<TargetMock>();
1423+
1424+
ScriptBuilder builder(m_extension.get(), m_engine, targetMock);
1425+
builder.addBlock("sensing_loud");
1426+
Block *block = builder.currentBlock();
1427+
1428+
Compiler compiler(&m_engineMock, targetMock.get());
1429+
auto code = compiler.compile(block, Compiler::CodeType::Reporter);
1430+
Script script(targetMock.get(), block, &m_engineMock);
1431+
script.setCode(code);
1432+
Thread thread(targetMock.get(), &m_engineMock, &script);
1433+
1434+
EXPECT_CALL(*m_audioLoudness, getLoudness()).WillOnce(Return(10));
1435+
ValueData value = thread.runReporter();
1436+
ASSERT_FALSE(value_toBool(&value));
1437+
value_free(&value);
1438+
}
1439+
1440+
TEST_F(SensingBlocksTest, Loud_Above)
1441+
{
1442+
auto targetMock = std::make_shared<TargetMock>();
1443+
1444+
ScriptBuilder builder(m_extension.get(), m_engine, targetMock);
1445+
builder.addBlock("sensing_loud");
1446+
Block *block = builder.currentBlock();
1447+
1448+
Compiler compiler(&m_engineMock, targetMock.get());
1449+
auto code = compiler.compile(block, Compiler::CodeType::Reporter);
1450+
Script script(targetMock.get(), block, &m_engineMock);
1451+
script.setCode(code);
1452+
Thread thread(targetMock.get(), &m_engineMock, &script);
1453+
1454+
EXPECT_CALL(*m_audioLoudness, getLoudness()).WillOnce(Return(11));
1455+
ValueData value = thread.runReporter();
1456+
ASSERT_TRUE(value_toBool(&value));
1457+
value_free(&value);
1458+
}

0 commit comments

Comments
 (0)