From 237616490ffe8a2ace1b3dc52c79d9e42da9582f Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Tue, 27 Feb 2018 11:48:14 -0500 Subject: [PATCH 1/4] Add tempo property to rendered target --- src/sprites/rendered-target.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/sprites/rendered-target.js b/src/sprites/rendered-target.js index ae02b72bd..538cc576f 100644 --- a/src/sprites/rendered-target.js +++ b/src/sprites/rendered-target.js @@ -116,6 +116,12 @@ class RenderedTarget extends Target { * @type {!string} */ this.rotationStyle = RenderedTarget.ROTATION_STYLE_ALL_AROUND; + + /** + * Current tempo (used by the music extension) + * @type {number} + */ + this.tempo = 60; } /** From 7e5e43fbe16b41363a94ab7b471c8daf2561d0cb Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Tue, 27 Feb 2018 11:48:23 -0500 Subject: [PATCH 2/4] Load tempo from sb2 --- src/serialization/sb2.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/serialization/sb2.js b/src/serialization/sb2.js index 1102c005e..ada14f5f5 100644 --- a/src/serialization/sb2.js +++ b/src/serialization/sb2.js @@ -320,6 +320,9 @@ const parseScratchObject = function (object, runtime, extensions, topLevel) { target.rotationStyle = RenderedTarget.ROTATION_STYLE_ALL_AROUND; } } + if (object.hasOwnProperty('tempoBPM')) { + target.tempo = object.tempoBPM; + } target.isStage = topLevel; From db795abfbc0b4e9594b9e92790c55c7ae762016a Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Tue, 27 Feb 2018 11:48:49 -0500 Subject: [PATCH 3/4] Use tempo stored in stage --- src/extensions/scratch3_music/index.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/extensions/scratch3_music/index.js b/src/extensions/scratch3_music/index.js index 386c2c4a6..2c7e59b36 100644 --- a/src/extensions/scratch3_music/index.js +++ b/src/extensions/scratch3_music/index.js @@ -43,13 +43,6 @@ class Scratch3MusicBlocks { */ this.runtime = runtime; - /** - * The current tempo in beats per minute. The tempo is a global property of the project, - * not a property of each sprite, so it is not stored in the MusicState object. - * @type {number} - */ - this.tempo = 60; - /** * The number of drum and instrument sounds currently being played simultaneously. * @type {number} @@ -759,7 +752,7 @@ class Scratch3MusicBlocks { * @private */ _beatsToSec (beats) { - return (60 / this.tempo) * beats; + return (60 / this.getTempo()) * beats; } /** @@ -829,7 +822,7 @@ class Scratch3MusicBlocks { */ changeTempo (args) { const change = Cast.toNumber(args.TEMPO); - const tempo = change + this.tempo; + const tempo = change + this.getTempo(); this._updateTempo(tempo); } @@ -840,7 +833,10 @@ class Scratch3MusicBlocks { */ _updateTempo (tempo) { tempo = MathUtil.clamp(tempo, Scratch3MusicBlocks.TEMPO_RANGE.min, Scratch3MusicBlocks.TEMPO_RANGE.max); - this.tempo = tempo; + const stage = this.runtime.getTargetForStage(); + if (stage) { + stage.tempo = tempo; + } } /** @@ -848,7 +844,11 @@ class Scratch3MusicBlocks { * @return {number} - the current tempo, in beats per minute. */ getTempo () { - return this.tempo; + const stage = this.runtime.getTargetForStage(); + if (stage) { + return stage.tempo; + } + return 60; } } From a0525542fc5dabc7e816cf1ff69e86b5704196df Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Tue, 27 Feb 2018 15:38:34 -0500 Subject: [PATCH 4/4] Stub runtime to fix unit test --- test/unit/extension_music.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/unit/extension_music.js b/test/unit/extension_music.js index 93106297f..ad35b29a8 100644 --- a/test/unit/extension_music.js +++ b/test/unit/extension_music.js @@ -1,8 +1,11 @@ const test = require('tap').test; const Music = require('../../src/extensions/scratch3_music/index.js'); -const runtime = Object.create(null); -const blocks = new Music(runtime); +const fakeRuntime = { + getTargetForStage: () => ({tempo: 60}) +}; + +const blocks = new Music(fakeRuntime); const util = { stackFrame: Object.create(null),