Skip to content

Commit 6414985

Browse files
committed
Calc tooLoud with chunkLevels, disable louder btn
1 parent 059d3f7 commit 6414985

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

src/components/sound-editor/sound-editor.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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
};

src/containers/sound-editor.jsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import log from '../lib/log.js';
1414

1515
const UNDO_STACK_SIZE = 99;
1616

17+
const MAX_RMS = 1.2;
18+
1719
class 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.floor(this.state.trimEnd * numChunks);
276+
const trimChunks = this.state.chunkLevels.slice(startIndex, endIndex + 1);
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}

src/lib/audio/audio-effects.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import RobotEffect from './effects/robot-effect.js';
33
import VolumeEffect from './effects/volume-effect.js';
44
import FadeEffect from './effects/fade-effect.js';
55
import MuteEffect from './effects/mute-effect.js';
6-
import {computeChunkedRMS} from './audio-util.js';
76

87
const effectTypes = {
98
ROBOT: 'robot',
@@ -97,21 +96,6 @@ class AudioEffects {
9796
this.buffer = buffer;
9897
}
9998

100-
// If the sound is louder than an RMS limit, do not apply gain.
101-
// This helps reduce distortion due to clipping.
102-
this.louderGain = 1.25;
103-
this.rmsLimit = 1.2;
104-
if (name === effectTypes.LOUDER) {
105-
const trimStartSamples = Math.floor(trimStart * buffer.length);
106-
const trimEndSamples = Math.floor(trimEnd * buffer.length);
107-
const slicedBuffer = buffer.getChannelData(0).slice(trimStartSamples, trimEndSamples);
108-
const rmsChunks = computeChunkedRMS(slicedBuffer);
109-
const maxRMS = Math.max(...rmsChunks);
110-
if (maxRMS > this.rmsLimit) {
111-
this.louderGain = 1;
112-
}
113-
}
114-
11599
this.source = this.audioContext.createBufferSource();
116100
this.source.buffer = this.buffer;
117101
this.name = name;
@@ -127,7 +111,7 @@ class AudioEffects {
127111
this.source.playbackRate.setValueAtTime(1.0, this.adjustedTrimEndSeconds);
128112
break;
129113
case effectTypes.LOUDER:
130-
({input, output} = new VolumeEffect(this.audioContext, this.louderGain,
114+
({input, output} = new VolumeEffect(this.audioContext, 1.25,
131115
this.adjustedTrimStartSeconds, this.adjustedTrimEndSeconds));
132116
break;
133117
case effectTypes.SOFTER:

0 commit comments

Comments
 (0)