Early concept for rendering the frequency waveform

This commit is contained in:
Travis Ralston 2021-03-22 20:54:09 -06:00
parent da7d31aeb6
commit 8ddd14e252
6 changed files with 136 additions and 8 deletions

View file

@ -21,6 +21,7 @@ import {VoiceRecorder} from "../../../voice/VoiceRecorder";
import {Room} from "matrix-js-sdk/src/models/room";
import {MatrixClientPeg} from "../../../MatrixClientPeg";
import classNames from "classnames";
import FrequencyBars from "../voice_messages/FrequencyBars";
interface IProps {
room: Room;
@ -57,10 +58,6 @@ export default class VoiceRecordComposerTile extends React.PureComponent<IProps,
const recorder = new VoiceRecorder(MatrixClientPeg.get());
await recorder.start();
this.props.onRecording(true);
// TODO: @@ TravisR: Run through EQ component
// recorder.frequencyData.onUpdate((freq) => {
// console.log('@@ UPDATE', freq);
// });
this.setState({recorder});
};
@ -71,18 +68,21 @@ export default class VoiceRecordComposerTile extends React.PureComponent<IProps,
'mx_VoiceRecordComposerTile_stop': !!this.state.recorder,
});
let bars = null;
let tooltip = _t("Record a voice message");
if (!!this.state.recorder) {
// TODO: @@ TravisR: Change to match behaviour
tooltip = _t("Stop & send recording");
bars = <FrequencyBars recorder={this.state.recorder} />;
}
return (
return (<>
{bars}
<AccessibleTooltipButton
className={classes}
onClick={this.onStartStopVoiceMessage}
title={tooltip}
/>
);
</>);
}
}

View file

@ -0,0 +1,58 @@
/*
Copyright 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React from "react";
import {IFrequencyPackage, VoiceRecorder} from "../../../voice/VoiceRecorder";
import {replaceableComponent} from "../../../utils/replaceableComponent";
import {arrayFastResample, arraySeed} from "../../../utils/arrays";
import {percentageOf} from "../../../utils/numbers";
interface IProps {
recorder: VoiceRecorder
}
interface IState {
heights: number[];
}
const DOWNSAMPLE_TARGET = 35; // number of bars
@replaceableComponent("views.voice_messages.FrequencyBars")
export default class FrequencyBars extends React.PureComponent<IProps, IState> {
public constructor(props) {
super(props);
this.state = {heights: arraySeed(0, DOWNSAMPLE_TARGET)};
this.props.recorder.frequencyData.onUpdate(this.onFrequencyData);
}
private onFrequencyData = (freq: IFrequencyPackage) => {
// We're downsampling from about 1024 points to about 35, so this function is fine (see docs/impl)
const bars = arrayFastResample(Array.from(freq.dbBars), DOWNSAMPLE_TARGET);
this.setState({
// Values are somewhat arbitrary, but help decide what shape the graph should be
heights: bars.map(b => percentageOf(b, -150, -70) * 100),
});
};
public render() {
return <div className='mx_FrequencyBars'>
{this.state.heights.map((h, i) => {
return <span key={i} style={{height: h + '%'}} className='mx_FrequencyBars_bar' />;
})}
</div>;
}
}