Merge pull request from thisandagain/feature/polly

Update text-to-speech extension to use new audio engine
This commit is contained in:
Andrew Sliwinski 2018-07-10 08:30:52 -04:00 committed by GitHub
commit c9d71488c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 19 deletions
src
extension-support
extensions/scratch3_text2speech

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');
@ -22,7 +22,7 @@ const builtinExtensions = {
wedo2: Scratch3WeDo2Blocks,
music: Scratch3MusicBlocks,
microbit: Scratch3MicroBitBlocks,
speak: Scratch3SpeakBlocks,
text2speech: Scratch3Text2SpeechBlocks,
translate: Scratch3TranslateBlocks,
videoSensing: Scratch3VideoSensingBlocks,
speech: Scratch3SpeechBlocks,

View file

@ -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);
});
});
}