@@ -5,7 +5,7 @@ import WavEncoder from 'wav-encoder';
5
5
6
6
import { connect } from 'react-redux' ;
7
7
8
- import { computeChunkedRMS } from '../lib/audio/audio-util.js' ;
8
+ import { computeChunkedRMS , SOUND_BYTE_LIMIT } from '../lib/audio/audio-util.js' ;
9
9
import AudioEffects from '../lib/audio/audio-effects.js' ;
10
10
import SoundEditorComponent from '../components/sound-editor/sound-editor.jsx' ;
11
11
import AudioBufferPlayer from '../lib/audio/audio-buffer-player.js' ;
@@ -65,32 +65,45 @@ class SoundEditor extends React.Component {
65
65
} ) ;
66
66
}
67
67
submitNewSamples ( samples , sampleRate , skipUndo ) {
68
- if ( ! skipUndo ) {
69
- this . redoStack = [ ] ;
70
- if ( this . undoStack . length >= UNDO_STACK_SIZE ) {
71
- this . undoStack . shift ( ) ; // Drop the first element off the array
72
- }
73
- this . undoStack . push ( this . copyCurrentBuffer ( ) ) ;
74
- }
75
68
// Encode the new sound into a wav so that it can be stored
76
69
let wavBuffer = null ;
77
70
try {
78
71
wavBuffer = WavEncoder . encode . sync ( {
79
72
sampleRate : sampleRate ,
80
73
channelData : [ samples ]
81
74
} ) ;
75
+
76
+ if ( wavBuffer . byteLength > SOUND_BYTE_LIMIT ) {
77
+ // Cancel the sound update by setting to null
78
+ wavBuffer = null ;
79
+ log . error ( `Refusing to encode sound larger than ${ SOUND_BYTE_LIMIT } bytes` ) ;
80
+ }
82
81
} catch ( e ) {
83
82
// This error state is mostly for the mock sounds used during testing.
84
83
// Any incorrect sound buffer trying to get interpretd as a Wav file
85
84
// should yield this error.
85
+ // This can also happen if the sound is too be allocated in memory.
86
86
log . error ( `Encountered error while trying to encode sound update: ${ e } ` ) ;
87
87
}
88
88
89
- this . resetState ( samples , sampleRate ) ;
90
- this . props . vm . updateSoundBuffer (
91
- this . props . soundIndex ,
92
- this . audioBufferPlayer . buffer ,
93
- wavBuffer ? new Uint8Array ( wavBuffer ) : new Uint8Array ( ) ) ;
89
+ // Do not submit sound if it could not be encoded (i.e. if too large)
90
+ if ( wavBuffer ) {
91
+ if ( ! skipUndo ) {
92
+ this . redoStack = [ ] ;
93
+ if ( this . undoStack . length >= UNDO_STACK_SIZE ) {
94
+ this . undoStack . shift ( ) ; // Drop the first element off the array
95
+ }
96
+ this . undoStack . push ( this . copyCurrentBuffer ( ) ) ;
97
+ }
98
+ this . resetState ( samples , sampleRate ) ;
99
+ this . props . vm . updateSoundBuffer (
100
+ this . props . soundIndex ,
101
+ this . audioBufferPlayer . buffer ,
102
+ new Uint8Array ( wavBuffer ) ) ;
103
+
104
+ return true ; // Update succeeded
105
+ }
106
+ return false ; // Update failed
94
107
}
95
108
handlePlay ( ) {
96
109
this . audioBufferPlayer . play (
@@ -147,8 +160,8 @@ class SoundEditor extends React.Component {
147
160
effects . process ( ( { renderedBuffer} ) => {
148
161
const samples = renderedBuffer . getChannelData ( 0 ) ;
149
162
const sampleRate = renderedBuffer . sampleRate ;
150
- this . submitNewSamples ( samples , sampleRate ) ;
151
- this . handlePlay ( ) ;
163
+ const success = this . submitNewSamples ( samples , sampleRate ) ;
164
+ if ( success ) this . handlePlay ( ) ;
152
165
} ) ;
153
166
}
154
167
handleUndo ( ) {
0 commit comments