From 63c855958fbafd451d9eba1518ee0e4fa563f44a Mon Sep 17 00:00:00 2001 From: sksum <55449862+sksum@users.noreply.github.com> Date: Wed, 24 Jun 2020 05:14:40 +0530 Subject: [PATCH 1/3] EveryBeat using setInterval to dispatch actions in a companion turtle. --- js/blocks.js | 14 +++++- js/blocks/MeterBlocks.js | 99 ++++++++++++++++++++++++++++++++++++++++ js/logo.js | 6 +++ 3 files changed, 117 insertions(+), 2 deletions(-) diff --git a/js/blocks.js b/js/blocks.js index 7837702457..33b58bdf90 100644 --- a/js/blocks.js +++ b/js/blocks.js @@ -7009,8 +7009,18 @@ function Blocks(activity) { if (turtle != null && turtleNotInTrash > 1) { console.debug("putting turtle " + turtle + " in the trash"); - this.turtles.turtleList[turtle].inTrash = true; - this.turtles.turtleList[turtle].container.visible = false; + let comp = this.turtles.turtleList[turtle].companionTurtle; + if (comp){ + if (turtleNotInTrash > 2){ + this.turtles.turtleList[comp].inTrash = true; + this.turtles.turtleList[comp].container.visible = false; + this.turtles.turtleList[turtle].inTrash = true; + this.turtles.turtleList[turtle].container.visible = false; + } + } else { + this.turtles.turtleList[turtle].inTrash = true; + this.turtles.turtleList[turtle].container.visible = false; + } } else { this.errorMsg( _("You must always have at least one start block.") diff --git a/js/blocks/MeterBlocks.js b/js/blocks/MeterBlocks.js index 847628de84..0262534d8e 100644 --- a/js/blocks/MeterBlocks.js +++ b/js/blocks/MeterBlocks.js @@ -554,7 +554,105 @@ function setupMeterBlocks() { } } } + class EveryBeatDoBlockNew extends FlowBlock { + constructor() { + // .TRANS: on every note played, do some action + super("everybeatdonew", _("on every beat do")); + this.setPalette("meter"); + this.beginnerBlock(true); + + this.setHelpString([ + _( + "The On-every-beat block let you specify actions to take on every beat." + ), + "documentation", + null, + "everybeathelp" + ]); + + + this.formBlock({ + args: 1, + argTypes: ["textin"], + defaults: [_("action")] + }); + + this.makeMacro((x, y) => { + return [ + [0, ["everybeatdonew",{}], x, y, [null, 1, null]], + [1, ["text", { "value": "action" }], 0, 0, [0]] + ] + }); + } + + flow(args, logo, turtle, blk, receivedArg, actionArgs, isflow) { + // Set up a listener for every beat for this turtle. + let orgTurtle =turtle ; + console.debug("used from :",orgTurtle) + if (!turtles.turtleList[orgTurtle].companionTurtle){ + turtle = logo.turtles.turtleList.length; + turtles.turtleList[orgTurtle].companionTurtle = turtle ; + logo.turtles.addTurtle( + logo.blocks.blockList[blk], + [] + ); + console.debug("beat Turtle : ",turtle); + } + turtle = turtles.turtleList[orgTurtle].companionTurtle; + + if (!(args[0] in logo.actions)) { + logo.errorMsg(NOACTIONERRORMSG, blk, args[1]); + } else { + let __listener = function(event) { + if (logo.turtles.turtleList[turtle].running) { + let queueBlock = new Queue( + logo.actions[args[0]], + 1, + blk + ); + logo.parentFlowQueue[turtle].push(blk); + logo.turtles.turtleList[turtle].queue.push(queueBlock); + } else { + // Since the turtle has stopped + // running, we need to run the stack + // from here. + if (isflow) { + logo._runFromBlockNow( + logo, + turtle, + logo.actions[args[0]], + isflow, + receivedArg + ); + } else { + logo._runFromBlock( + logo, + turtle, + logo.actions[args[0]], + isflow, + receivedArg + ); + } + } + }; + let eventName = "__everybeat_" + turtle + "__"; + logo.turtles.turtleList[turtle].queue = []; + logo.parentFlowQueue[turtle] = []; + logo.unhighlightQueue[turtle] = []; + logo.parameterQueue[turtle] = []; + logo.initTurtle(turtle); + logo._setListener(turtle, eventName, __listener); + if (logo.turtles.turtleList[turtle].interval !== undefined)clearInterval(this.interval); + logo.turtles.turtleList[turtle].interval = setInterval( + () => { + logo.stage.dispatchEvent(eventName); + } + ,666); + console.debug("set listener",eventName); + } + } + } class EveryBeatDoBlock extends FlowBlock { constructor() { // .TRANS: on every note played, do some action @@ -1080,6 +1178,7 @@ function setupMeterBlocks() { new DriftBlock().setup(); new OffBeatDoBlock().setup(); new OnBeatDoBlock().setup(); + new EveryBeatDoBlockNew().setup(); new EveryBeatDoBlock().setup(); new SetMasterBPM2Block().setup(); new SetMasterBPMBlock().setup(); diff --git a/js/logo.js b/js/logo.js index 89f7c6a9c3..c58004e90b 100644 --- a/js/logo.js +++ b/js/logo.js @@ -1881,6 +1881,12 @@ class Logo { logo.turtles.turtleList[turtle].running = false; } + let comp = logo.turtles.turtleList[turtle].companionTurtle; + if (comp){ + logo.turtles.turtleList[comp].running = false ; + let interval = logo.turtles.turtleList[comp].interval; + if (interval) clearInterval(interval); + } // Because flow can come from calc blocks, we are not // ensured that the turtle is really finished running // yet. Hence the timeout. From 81d1d952d7480f1f62a5e741fad458a924f3d0f2 Mon Sep 17 00:00:00 2001 From: sksum <55449862+sksum@users.noreply.github.com> Date: Wed, 24 Jun 2020 05:54:41 +0530 Subject: [PATCH 2/3] hard stop clear any intervals on hard stop --- js/logo.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/js/logo.js b/js/logo.js index c58004e90b..cf6ea9968a 100644 --- a/js/logo.js +++ b/js/logo.js @@ -713,6 +713,12 @@ class Logo { for (let instrumentName in instruments[turtle]) { this.synth.stopSound(turtle, instrumentName); } + let comp = this.turtles.turtleList[turtle].companionTurtle; + if (comp){ + this.turtles.turtleList[comp].running = false ; + let interval = logo.turtles.turtleList[comp].interval; + if (interval) clearInterval(interval); + } } this.synth.stop(); From abdc6be8226698facc29ca1db8555d96f2cd1e07 Mon Sep 17 00:00:00 2001 From: sksum <55449862+sksum@users.noreply.github.com> Date: Mon, 29 Jun 2020 05:11:07 +0530 Subject: [PATCH 3/3] useMeterBPM --- js/blocks/MeterBlocks.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/js/blocks/MeterBlocks.js b/js/blocks/MeterBlocks.js index 0262534d8e..2acd86d946 100644 --- a/js/blocks/MeterBlocks.js +++ b/js/blocks/MeterBlocks.js @@ -643,12 +643,18 @@ function setupMeterBlocks() { logo.parameterQueue[turtle] = []; logo.initTurtle(turtle); logo._setListener(turtle, eventName, __listener); + let duration ; + if (logo.bpm[orgTurtle].length > 0) { + duration = 60 / last(logo.bpm[orgTurtle]); + } else { + duration = 60 / logo._masterBPM; + } if (logo.turtles.turtleList[turtle].interval !== undefined)clearInterval(this.interval); logo.turtles.turtleList[turtle].interval = setInterval( () => { logo.stage.dispatchEvent(eventName); } - ,666); + ,duration*1000); console.debug("set listener",eventName); } }