|
| 1 | +/* |
| 2 | +Copyright 2021 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React from "react"; |
| 18 | +import {IRecordingUpdate, VoiceRecorder} from "../../../voice/VoiceRecorder"; |
| 19 | +import {replaceableComponent} from "../../../utils/replaceableComponent"; |
| 20 | +import {arrayFastResample, arraySeed} from "../../../utils/arrays"; |
| 21 | +import {percentageOf} from "../../../utils/numbers"; |
| 22 | +import Waveform from "./Waveform"; |
| 23 | + |
| 24 | +interface IProps { |
| 25 | + recorder: VoiceRecorder; |
| 26 | +} |
| 27 | + |
| 28 | +interface IState { |
| 29 | + heights: number[]; |
| 30 | +} |
| 31 | + |
| 32 | +const DOWNSAMPLE_TARGET = 35; // number of bars we want |
| 33 | + |
| 34 | +/** |
| 35 | + * A waveform which shows the waveform of a live recording |
| 36 | + */ |
| 37 | +@replaceableComponent("views.voice_messages.LiveRecordingWaveform") |
| 38 | +export default class LiveRecordingWaveform extends React.PureComponent<IProps, IState> { |
| 39 | + public constructor(props) { |
| 40 | + super(props); |
| 41 | + |
| 42 | + this.state = {heights: arraySeed(0, DOWNSAMPLE_TARGET)}; |
| 43 | + this.props.recorder.liveData.onUpdate(this.onRecordingUpdate); |
| 44 | + } |
| 45 | + |
| 46 | + private onRecordingUpdate = (update: IRecordingUpdate) => { |
| 47 | + // The waveform and the downsample target are pretty close, so we should be fine to |
| 48 | + // do this, despite the docs on arrayFastResample. |
| 49 | + const bars = arrayFastResample(Array.from(update.waveform), DOWNSAMPLE_TARGET); |
| 50 | + this.setState({ |
| 51 | + // The incoming data is between zero and one, but typically even screaming into a |
| 52 | + // microphone won't send you over 0.6, so we artificially adjust the gain for the |
| 53 | + // waveform. This results in a slightly more cinematic/animated waveform for the |
| 54 | + // user. |
| 55 | + heights: bars.map(b => percentageOf(b, 0, 0.50)), |
| 56 | + }); |
| 57 | + }; |
| 58 | + |
| 59 | + public render() { |
| 60 | + return <Waveform relHeights={this.state.heights} />; |
| 61 | + } |
| 62 | +} |
0 commit comments