Skip to content

Commit c9d7148

Browse files
authored
Merge pull request scratchfoundation#1311 from thisandagain/feature/polly
Update text-to-speech extension to use new audio engine
2 parents 5a0815b + ace5ae4 commit c9d7148

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

src/extension-support/extension-manager.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const Scratch3PenBlocks = require('../extensions/scratch3_pen');
1111
const Scratch3WeDo2Blocks = require('../extensions/scratch3_wedo2');
1212
const Scratch3MusicBlocks = require('../extensions/scratch3_music');
1313
const Scratch3MicroBitBlocks = require('../extensions/scratch3_microbit');
14-
const Scratch3SpeakBlocks = require('../extensions/scratch3_speak');
14+
const Scratch3Text2SpeechBlocks = require('../extensions/scratch3_text2speech');
1515
const Scratch3TranslateBlocks = require('../extensions/scratch3_translate');
1616
const Scratch3VideoSensingBlocks = require('../extensions/scratch3_video_sensing');
1717
const Scratch3SpeechBlocks = require('../extensions/scratch3_speech');
@@ -22,7 +22,7 @@ const builtinExtensions = {
2222
wedo2: Scratch3WeDo2Blocks,
2323
music: Scratch3MusicBlocks,
2424
microbit: Scratch3MicroBitBlocks,
25-
speak: Scratch3SpeakBlocks,
25+
text2speech: Scratch3Text2SpeechBlocks,
2626
translate: Scratch3TranslateBlocks,
2727
videoSensing: Scratch3VideoSensingBlocks,
2828
speech: Scratch3SpeechBlocks,

src/extensions/scratch3_speak/index.js renamed to src/extensions/scratch3_text2speech/index.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,27 @@ const SERVER_HOST = 'https://synthesis-service.scratch.mit.edu';
1616
* How long to wait in ms before timing out requests to synthesis server.
1717
* @type {int}
1818
*/
19-
const SERVER_TIMEOUT = 10000; // 10 seconds (chosen arbitrarily).
19+
const SERVER_TIMEOUT = 10000; // 10 seconds
2020

2121
/**
2222
* Class for the synthesis block in Scratch 3.0.
2323
* @constructor
2424
*/
2525
class Scratch3SpeakBlocks {
26-
constructor () {
26+
constructor (runtime) {
27+
/**
28+
* The runtime instantiating this block package.
29+
* @type {Runtime}
30+
*/
31+
this.runtime = runtime;
32+
33+
// Clear sound effects on green flag and stop button events.
34+
// this._clearEffectsForAllTargets = this._clearEffectsForAllTargets.bind(this);
35+
if (this.runtime) {
36+
// @todo
37+
// this.runtime.on('PROJECT_STOP_ALL', this._clearEffectsForAllTargets);
38+
}
39+
2740
/**
2841
* Locale code of the viewer
2942
* @type {string}
@@ -37,16 +50,16 @@ class Scratch3SpeakBlocks {
3750
* @return {string} The key.
3851
*/
3952
static get STATE_KEY () {
40-
return 'Scratch.speak';
53+
return 'Scratch.text2speech';
4154
}
4255

4356
/**
4457
* @returns {object} metadata for this extension and its blocks.
4558
*/
4659
getInfo () {
4760
return {
48-
id: 'speak',
49-
name: 'Amazon Polly',
61+
id: 'text2speech',
62+
name: 'Text-to-Speech',
5063
menuIconURI: '', // @todo Add the final icons.
5164
blockIconURI: '',
5265
blocks: [
@@ -132,19 +145,19 @@ class Scratch3SpeakBlocks {
132145
}
133146

134147
// Play the sound
135-
const ctx = new (window.AudioContext || window.webkitAudioContext)();
136-
const source = ctx.createBufferSource();
137-
ctx.decodeAudioData(body.buffer, buffer => {
138-
source.buffer = buffer;
139-
source.connect(ctx.destination);
140-
source.loop = false;
141-
}, e => {
142-
log.warn(e.err);
143-
});
144-
source.addEventListener('ended', () => {
145-
resolve();
148+
const sound = {
149+
// md5: 'test',
150+
// name: 'test',
151+
// format: 'audio/mpg',
152+
data: {
153+
buffer: body.buffer
154+
}
155+
};
156+
this.runtime.audioEngine.decodeSoundPlayer(sound).then(soundPlayer => {
157+
soundPlayer.connect(this.runtime.audioEngine);
158+
soundPlayer.play();
159+
soundPlayer.on('stop', resolve);
146160
});
147-
source.start(0);
148161
});
149162
});
150163
}

0 commit comments

Comments
 (0)