File tree Expand file tree Collapse file tree 3 files changed +23
-1
lines changed Expand file tree Collapse file tree 3 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -265,6 +265,7 @@ const SoundEditor = props => (
265265 onClick = { props . onSlower }
266266 />
267267 < IconButton
268+ disabled = { props . tooLoud }
268269 className = { classNames ( styles . effectButton , styles . flipInRtl ) }
269270 img = { louderIcon }
270271 title = { < FormattedMessage { ...messages . louder } /> }
@@ -340,6 +341,7 @@ SoundEditor.propTypes = {
340341 onUndo : PropTypes . func . isRequired ,
341342 playhead : PropTypes . number ,
342343 setRef : PropTypes . func ,
344+ tooLoud : PropTypes . bool . isRequired ,
343345 trimEnd : PropTypes . number ,
344346 trimStart : PropTypes . number
345347} ;
Original file line number Diff line number Diff line change @@ -14,6 +14,8 @@ import log from '../lib/log.js';
1414
1515const UNDO_STACK_SIZE = 99 ;
1616
17+ const MAX_RMS = 1.2 ;
18+
1719class SoundEditor extends React . Component {
1820 constructor ( props ) {
1921 super ( props ) ;
@@ -265,6 +267,15 @@ class SoundEditor extends React.Component {
265267 }
266268 } ) ;
267269 }
270+ tooLoud ( ) {
271+ const numChunks = this . state . chunkLevels . length ;
272+ const startIndex = this . state . trimStart === null ?
273+ 0 : Math . floor ( this . state . trimStart * numChunks ) ;
274+ const endIndex = this . state . trimEnd === null ?
275+ numChunks - 1 : Math . ceil ( this . state . trimEnd * numChunks ) ;
276+ const trimChunks = this . state . chunkLevels . slice ( startIndex , endIndex ) ;
277+ return Math . max ( ...trimChunks ) > MAX_RMS ;
278+ }
268279 getUndoItem ( ) {
269280 return {
270281 ...this . copyCurrentBuffer ( ) ,
@@ -399,6 +410,7 @@ class SoundEditor extends React.Component {
399410 name = { this . props . name }
400411 playhead = { this . state . playhead }
401412 setRef = { this . setRef }
413+ tooLoud = { this . tooLoud ( ) }
402414 trimEnd = { this . state . trimEnd }
403415 trimStart = { this . state . trimStart }
404416 onChangeName = { this . handleChangeName }
Original file line number Diff line number Diff line change @@ -14,8 +14,16 @@ class VolumeEffect {
1414 this . gain . gain . setValueAtTime ( volume , endSeconds ) ;
1515 this . gain . gain . exponentialRampToValueAtTime ( 1.0 , endSeconds + this . rampLength ) ;
1616
17+ // Use a waveshaper node to prevent sample values from exceeding -1 or 1.
18+ // Without this, gain can cause samples to exceed this range, then they
19+ // are clipped on save, and the sound is distorted on load.
20+ this . waveShaper = this . audioContext . createWaveShaper ( ) ;
21+ this . waveShaper . curve = new Float32Array ( [ - 1 , 1 ] ) ;
22+ this . waveShaper . oversample = 'none' ;
23+
1724 this . input . connect ( this . gain ) ;
18- this . gain . connect ( this . output ) ;
25+ this . gain . connect ( this . waveShaper ) ;
26+ this . waveShaper . connect ( this . output ) ;
1927 }
2028}
2129
You can’t perform that action at this time.
0 commit comments