From ace5ae40d0b968645ea35bbe3dc2ec581c3024e0 Mon Sep 17 00:00:00 2001 From: Andrew Sliwinski Date: Mon, 9 Jul 2018 14:34:31 -0400 Subject: [PATCH] Update text-to-speech extension to use new audio engine --- src/extension-support/extension-manager.js | 4 +- .../index.js | 47 ++++++++++++------- 2 files changed, 32 insertions(+), 19 deletions(-) rename src/extensions/{scratch3_speak => scratch3_text2speech}/index.js (77%) diff --git a/src/extension-support/extension-manager.js b/src/extension-support/extension-manager.js index cff9f4f0..9c5177f0 100644 --- a/src/extension-support/extension-manager.js +++ b/src/extension-support/extension-manager.js @@ -11,7 +11,7 @@ const Scratch3PenBlocks = require('../extensions/scratch3_pen'); const Scratch3WeDo2Blocks = require('../extensions/scratch3_wedo2'); const Scratch3MusicBlocks = require('../extensions/scratch3_music'); const Scratch3MicroBitBlocks = require('../extensions/scratch3_microbit'); -const Scratch3SpeakBlocks = require('../extensions/scratch3_speak'); +const Scratch3Text2SpeechBlocks = require('../extensions/scratch3_text2speech'); const Scratch3TranslateBlocks = require('../extensions/scratch3_translate'); const Scratch3VideoSensingBlocks = require('../extensions/scratch3_video_sensing'); const Scratch3SpeechBlocks = require('../extensions/scratch3_speech'); @@ -21,7 +21,7 @@ const builtinExtensions = { wedo2: Scratch3WeDo2Blocks, music: Scratch3MusicBlocks, microbit: Scratch3MicroBitBlocks, - speak: Scratch3SpeakBlocks, + text2speech: Scratch3Text2SpeechBlocks, translate: Scratch3TranslateBlocks, videoSensing: Scratch3VideoSensingBlocks, speech: Scratch3SpeechBlocks diff --git a/src/extensions/scratch3_speak/index.js b/src/extensions/scratch3_text2speech/index.js similarity index 77% rename from src/extensions/scratch3_speak/index.js rename to src/extensions/scratch3_text2speech/index.js index 9a64228f..fe223a74 100644 --- a/src/extensions/scratch3_speak/index.js +++ b/src/extensions/scratch3_text2speech/index.js @@ -16,14 +16,27 @@ const SERVER_HOST = 'https://synthesis-service.scratch.mit.edu'; * How long to wait in ms before timing out requests to synthesis server. * @type {int} */ -const SERVER_TIMEOUT = 10000; // 10 seconds (chosen arbitrarily). +const SERVER_TIMEOUT = 10000; // 10 seconds /** * Class for the synthesis block in Scratch 3.0. * @constructor */ class Scratch3SpeakBlocks { - constructor () { + constructor (runtime) { + /** + * The runtime instantiating this block package. + * @type {Runtime} + */ + this.runtime = runtime; + + // Clear sound effects on green flag and stop button events. + // this._clearEffectsForAllTargets = this._clearEffectsForAllTargets.bind(this); + if (this.runtime) { + // @todo + // this.runtime.on('PROJECT_STOP_ALL', this._clearEffectsForAllTargets); + } + /** * Locale code of the viewer * @type {string} @@ -37,7 +50,7 @@ class Scratch3SpeakBlocks { * @return {string} The key. */ static get STATE_KEY () { - return 'Scratch.speak'; + return 'Scratch.text2speech'; } /** @@ -45,8 +58,8 @@ class Scratch3SpeakBlocks { */ getInfo () { return { - id: 'speak', - name: 'Amazon Polly', + id: 'text2speech', + name: 'Text-to-Speech', menuIconURI: '', // @todo Add the final icons. blockIconURI: '', blocks: [ @@ -132,19 +145,19 @@ class Scratch3SpeakBlocks { } // Play the sound - const ctx = new (window.AudioContext || window.webkitAudioContext)(); - const source = ctx.createBufferSource(); - ctx.decodeAudioData(body.buffer, buffer => { - source.buffer = buffer; - source.connect(ctx.destination); - source.loop = false; - }, e => { - log.warn(e.err); + const sound = { + // md5: 'test', + // name: 'test', + // format: 'audio/mpg', + data: { + buffer: body.buffer + } + }; + this.runtime.audioEngine.decodeSoundPlayer(sound).then(soundPlayer => { + soundPlayer.connect(this.runtime.audioEngine); + soundPlayer.play(); + soundPlayer.on('stop', resolve); }); - source.addEventListener('ended', () => { - resolve(); - }); - source.start(0); }); }); }