Calc tooLoud with chunkLevels, disable louder btn

This commit is contained in:
Eric Rosenbaum committed 2019-09-05 13:49:22 -04:00
1 parent 059d3f709b
commit 6414985461
3 files changed
+15 -17

No files matched your search

@@ -265,6 +265,7 @@ const SoundEditor = props => (
onClick={props.onSlower}
/>
<IconButton
disabled={props.tooLoud}
className={classNames(styles.effectButton, styles.flipInRtl)}
img={louderIcon}
title={<FormattedMessage {...messages.louder} />}
@@ -340,6 +341,7 @@ SoundEditor.propTypes = {
onUndo: PropTypes.func.isRequired,
playhead: PropTypes.number,
setRef: PropTypes.func,
tooLoud: PropTypes.bool.isRequired,
trimEnd: PropTypes.number,
trimStart: PropTypes.number
};
+12
View File
@@ -14,6 +14,8 @@ import log from '../lib/log.js';
const UNDO_STACK_SIZE = 99;
const MAX_RMS = 1.2;
class SoundEditor extends React.Component {
constructor (props) {
super(props);
@@ -265,6 +267,15 @@ class SoundEditor extends React.Component {
}
});
}
tooLoud () {
const numChunks = this.state.chunkLevels.length;
const startIndex = this.state.trimStart === null ?
0 : Math.floor(this.state.trimStart * numChunks);
const endIndex = this.state.trimEnd === null ?
numChunks - 1 : Math.floor(this.state.trimEnd * numChunks);
const trimChunks = this.state.chunkLevels.slice(startIndex, endIndex + 1);
return Math.max(...trimChunks) > MAX_RMS;
}
getUndoItem () {
return {
...this.copyCurrentBuffer(),
@@ -399,6 +410,7 @@ class SoundEditor extends React.Component {
name={this.props.name}
playhead={this.state.playhead}
setRef={this.setRef}
tooLoud={this.tooLoud()}
trimEnd={this.state.trimEnd}
trimStart={this.state.trimStart}
onChangeName={this.handleChangeName}
+1 -17
View File
@@ -3,7 +3,6 @@ import RobotEffect from './effects/robot-effect.js';
import VolumeEffect from './effects/volume-effect.js';
import FadeEffect from './effects/fade-effect.js';
import MuteEffect from './effects/mute-effect.js';
import {computeChunkedRMS} from './audio-util.js';
const effectTypes = {
ROBOT: 'robot',
@@ -97,21 +96,6 @@ class AudioEffects {
this.buffer = buffer;
}
// If the sound is louder than an RMS limit, do not apply gain.
// This helps reduce distortion due to clipping.
this.louderGain = 1.25;
this.rmsLimit = 1.2;
if (name === effectTypes.LOUDER) {
const trimStartSamples = Math.floor(trimStart * buffer.length);
const trimEndSamples = Math.floor(trimEnd * buffer.length);
const slicedBuffer = buffer.getChannelData(0).slice(trimStartSamples, trimEndSamples);
const rmsChunks = computeChunkedRMS(slicedBuffer);
const maxRMS = Math.max(...rmsChunks);
if (maxRMS > this.rmsLimit) {
this.louderGain = 1;
}
}
this.source = this.audioContext.createBufferSource();
this.source.buffer = this.buffer;
this.name = name;
@@ -127,7 +111,7 @@ class AudioEffects {
this.source.playbackRate.setValueAtTime(1.0, this.adjustedTrimEndSeconds);
break;
case effectTypes.LOUDER:
({input, output} = new VolumeEffect(this.audioContext, this.louderGain,
({input, output} = new VolumeEffect(this.audioContext, 1.25,
this.adjustedTrimStartSeconds, this.adjustedTrimEndSeconds));
break;
case effectTypes.SOFTER: