@@ -16,14 +16,27 @@ const SERVER_HOST = 'https://synthesis-service.scratch.mit.edu';
16
16
* How long to wait in ms before timing out requests to synthesis server.
17
17
* @type {int }
18
18
*/
19
- const SERVER_TIMEOUT = 10000 ; // 10 seconds (chosen arbitrarily).
19
+ const SERVER_TIMEOUT = 10000 ; // 10 seconds
20
20
21
21
/**
22
22
* Class for the synthesis block in Scratch 3.0.
23
23
* @constructor
24
24
*/
25
25
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
+
27
40
/**
28
41
* Locale code of the viewer
29
42
* @type {string }
@@ -37,16 +50,16 @@ class Scratch3SpeakBlocks {
37
50
* @return {string } The key.
38
51
*/
39
52
static get STATE_KEY ( ) {
40
- return 'Scratch.speak ' ;
53
+ return 'Scratch.text2speech ' ;
41
54
}
42
55
43
56
/**
44
57
* @returns {object } metadata for this extension and its blocks.
45
58
*/
46
59
getInfo ( ) {
47
60
return {
48
- id : 'speak ' ,
49
- name : 'Amazon Polly ' ,
61
+ id : 'text2speech ' ,
62
+ name : 'Text-to-Speech ' ,
50
63
menuIconURI : '' , // @todo Add the final icons.
51
64
blockIconURI : '' ,
52
65
blocks : [
@@ -132,19 +145,19 @@ class Scratch3SpeakBlocks {
132
145
}
133
146
134
147
// 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 ) ;
146
160
} ) ;
147
- source . start ( 0 ) ;
148
161
} ) ;
149
162
} ) ;
150
163
}
0 commit comments