Update text-to-speech extension to use new audio engine

This commit is contained in:
Andrew Sliwinski committed 2018-07-09 14:40:13 -04:00
1 parent 6c118cf8e0
commit ace5ae40d0
2 files changed
+32 -19

No files matched your search

+2 -2
View File
@@ -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
@@ -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);
});
});
}