@@ -132,45 +132,34 @@ class SoundEditor extends React.Component {
132
132
} ) ;
133
133
}
134
134
submitNewSamples ( samples , sampleRate , skipUndo ) {
135
- // Encode the new sound into a wav so that it can be stored
136
- let wavBuffer = null ;
137
- try {
138
- wavBuffer = WavEncoder . encode . sync ( {
139
- sampleRate : sampleRate ,
140
- channelData : [ samples ]
141
- } ) ;
142
-
143
- if ( wavBuffer . byteLength > SOUND_BYTE_LIMIT ) {
144
- // Cancel the sound update by setting to null
145
- wavBuffer = null ;
146
- log . error ( `Refusing to encode sound larger than ${ SOUND_BYTE_LIMIT } bytes` ) ;
147
- }
148
- } catch ( e ) {
149
- // This error state is mostly for the mock sounds used during testing.
150
- // Any incorrect sound buffer trying to get interpretd as a Wav file
151
- // should yield this error.
152
- // This can also happen if the sound is too be allocated in memory.
153
- log . error ( `Encountered error while trying to encode sound update: ${ e } ` ) ;
154
- }
155
-
156
- // Do not submit sound if it could not be encoded (i.e. if too large)
157
- if ( wavBuffer ) {
158
- if ( ! skipUndo ) {
159
- this . redoStack = [ ] ;
160
- if ( this . undoStack . length >= UNDO_STACK_SIZE ) {
161
- this . undoStack . shift ( ) ; // Drop the first element off the array
135
+ return WavEncoder . encode ( {
136
+ sampleRate : sampleRate ,
137
+ channelData : [ samples ]
138
+ } )
139
+ . then ( wavBuffer => {
140
+ if ( wavBuffer . byteLength > SOUND_BYTE_LIMIT ) {
141
+ log . error ( `Refusing to encode sound larger than ${ SOUND_BYTE_LIMIT } bytes` ) ;
142
+ return Promise . reject ( ) ;
162
143
}
163
- this . undoStack . push ( this . getUndoItem ( ) ) ;
164
- }
165
- this . resetState ( samples , sampleRate ) ;
166
- this . props . vm . updateSoundBuffer (
167
- this . props . soundIndex ,
168
- this . audioBufferPlayer . buffer ,
169
- new Uint8Array ( wavBuffer ) ) ;
170
-
171
- return true ; // Update succeeded
172
- }
173
- return false ; // Update failed
144
+ if ( ! skipUndo ) {
145
+ this . redoStack = [ ] ;
146
+ if ( this . undoStack . length >= UNDO_STACK_SIZE ) {
147
+ this . undoStack . shift ( ) ; // Drop the first element off the array
148
+ }
149
+ this . undoStack . push ( this . getUndoItem ( ) ) ;
150
+ }
151
+ this . resetState ( samples , sampleRate ) ;
152
+ this . props . vm . updateSoundBuffer (
153
+ this . props . soundIndex ,
154
+ this . audioBufferPlayer . buffer ,
155
+ new Uint8Array ( wavBuffer ) ) ;
156
+ return true ; // Edit was successful
157
+ } )
158
+ . catch ( e => {
159
+ // Encoding failed, or the sound was too large to save so edit is rejected
160
+ log . error ( `Encountered error while trying to encode sound update: ${ e } ` ) ;
161
+ return false ; // Edit was not applied
162
+ } ) ;
174
163
}
175
164
handlePlay ( ) {
176
165
this . audioBufferPlayer . stop ( ) ;
@@ -209,11 +198,13 @@ class SoundEditor extends React.Component {
209
198
newSamples . set ( firstPart , 0 ) ;
210
199
newSamples . set ( secondPart , firstPart . length ) ;
211
200
}
212
- this . submitNewSamples ( newSamples , sampleRate ) ;
213
- this . setState ( {
214
- trimStart : null ,
215
- trimEnd : null
201
+ this . submitNewSamples ( newSamples , sampleRate ) . then ( ( ) => {
202
+ this . setState ( {
203
+ trimStart : null ,
204
+ trimEnd : null
205
+ } ) ;
216
206
} ) ;
207
+
217
208
}
218
209
handleDeleteInverse ( ) {
219
210
const { samples, sampleRate} = this . copyCurrentBuffer ( ) ;
@@ -224,10 +215,13 @@ class SoundEditor extends React.Component {
224
215
if ( clippedSamples . length === 0 ) {
225
216
clippedSamples = new Float32Array ( 1 ) ;
226
217
}
227
- this . submitNewSamples ( clippedSamples , sampleRate ) ;
228
- this . setState ( {
229
- trimStart : null ,
230
- trimEnd : null
218
+ this . submitNewSamples ( clippedSamples , sampleRate ) . then ( success => {
219
+ if ( success ) {
220
+ this . setState ( {
221
+ trimStart : null ,
222
+ trimEnd : null
223
+ } ) ;
224
+ }
231
225
} ) ;
232
226
}
233
227
handleUpdateTrim ( trimStart , trimEnd ) {
@@ -257,14 +251,15 @@ class SoundEditor extends React.Component {
257
251
effects . process ( ( renderedBuffer , adjustedTrimStart , adjustedTrimEnd ) => {
258
252
const samples = renderedBuffer . getChannelData ( 0 ) ;
259
253
const sampleRate = renderedBuffer . sampleRate ;
260
- const success = this . submitNewSamples ( samples , sampleRate ) ;
261
- if ( success ) {
262
- if ( this . state . trimStart === null ) {
263
- this . handlePlay ( ) ;
264
- } else {
265
- this . setState ( { trimStart : adjustedTrimStart , trimEnd : adjustedTrimEnd } , this . handlePlay ) ;
254
+ this . submitNewSamples ( samples , sampleRate ) . then ( success => {
255
+ if ( success ) {
256
+ if ( this . state . trimStart === null ) {
257
+ this . handlePlay ( ) ;
258
+ } else {
259
+ this . setState ( { trimStart : adjustedTrimStart , trimEnd : adjustedTrimEnd } , this . handlePlay ) ;
260
+ }
266
261
}
267
- }
262
+ } ) ;
268
263
} ) ;
269
264
}
270
265
tooLoud ( ) {
@@ -287,16 +282,22 @@ class SoundEditor extends React.Component {
287
282
this . redoStack . push ( this . getUndoItem ( ) ) ;
288
283
const { samples, sampleRate, trimStart, trimEnd} = this . undoStack . pop ( ) ;
289
284
if ( samples ) {
290
- this . submitNewSamples ( samples , sampleRate , true ) ;
291
- this . setState ( { trimStart : trimStart , trimEnd : trimEnd } , this . handlePlay ) ;
285
+ return this . submitNewSamples ( samples , sampleRate , true ) . then ( success => {
286
+ if ( success ) {
287
+ this . setState ( { trimStart : trimStart , trimEnd : trimEnd } , this . handlePlay ) ;
288
+ }
289
+ } ) ;
292
290
}
293
291
}
294
292
handleRedo ( ) {
295
293
const { samples, sampleRate, trimStart, trimEnd} = this . redoStack . pop ( ) ;
296
294
if ( samples ) {
297
295
this . undoStack . push ( this . getUndoItem ( ) ) ;
298
- this . submitNewSamples ( samples , sampleRate , true ) ;
299
- this . setState ( { trimStart : trimStart , trimEnd : trimEnd } , this . handlePlay ) ;
296
+ return this . submitNewSamples ( samples , sampleRate , true ) . then ( success => {
297
+ if ( success ) {
298
+ this . setState ( { trimStart : trimStart , trimEnd : trimEnd } , this . handlePlay ) ;
299
+ }
300
+ } ) ;
300
301
}
301
302
}
302
303
handleCopy ( ) {
@@ -351,8 +352,11 @@ class SoundEditor extends React.Component {
351
352
const newSamples = new Float32Array ( newLength ) ;
352
353
newSamples . set ( samples , 0 ) ;
353
354
newSamples . set ( this . state . copyBuffer . samples , samples . length ) ;
354
- this . submitNewSamples ( newSamples , this . props . sampleRate , false ) ;
355
- this . handlePlay ( ) ;
355
+ this . submitNewSamples ( newSamples , this . props . sampleRate , false ) . then ( success => {
356
+ if ( success ) {
357
+ this . handlePlay ( ) ;
358
+ }
359
+ } ) ;
356
360
} else {
357
361
// else replace the selection with the pasted sound
358
362
const trimStartSamples = this . state . trimStart * samples . length ;
@@ -371,11 +375,14 @@ class SoundEditor extends React.Component {
371
375
const newDurationSeconds = newSamples . length / this . state . copyBuffer . sampleRate ;
372
376
const adjustedTrimStart = trimStartSeconds / newDurationSeconds ;
373
377
const adjustedTrimEnd = trimEndSeconds / newDurationSeconds ;
374
- this . submitNewSamples ( newSamples , this . props . sampleRate , false ) ;
375
- this . setState ( {
376
- trimStart : adjustedTrimStart ,
377
- trimEnd : adjustedTrimEnd
378
- } , this . handlePlay ) ;
378
+ this . submitNewSamples ( newSamples , this . props . sampleRate , false ) . then ( success => {
379
+ if ( success ) {
380
+ this . setState ( {
381
+ trimStart : adjustedTrimStart ,
382
+ trimEnd : adjustedTrimEnd
383
+ } , this . handlePlay ) ;
384
+ }
385
+ } ) ;
379
386
}
380
387
}
381
388
handlePaste ( ) {
0 commit comments