Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 8ddd14e

Browse files
committed
Early concept for rendering the frequency waveform
1 parent da7d31a commit 8ddd14e

File tree

6 files changed

+136
-8
lines changed

6 files changed

+136
-8
lines changed

res/css/_components.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
@import "./views/toasts/_AnalyticsToast.scss";
247247
@import "./views/toasts/_NonUrgentEchoFailureToast.scss";
248248
@import "./views/verification/_VerificationShowSas.scss";
249+
@import "./views/voice_messages/_FrequencyBars.scss";
249250
@import "./views/voip/_CallContainer.scss";
250251
@import "./views/voip/_CallView.scss";
251252
@import "./views/voip/_DialPad.scss";
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
.mx_FrequencyBars {
18+
position: relative;
19+
height: 30px; // tallest bar can only be 30px
20+
21+
display: flex;
22+
align-items: center; // so the bars grow from the middle
23+
24+
.mx_FrequencyBars_bar {
25+
width: 2px;
26+
margin-left: 1px;
27+
margin-right: 1px;
28+
background-color: $muted-fg-color;
29+
display: inline-block;
30+
min-height: 2px;
31+
max-height: 100%;
32+
border-radius: 2px; // give them soft endcaps
33+
}
34+
}

src/components/views/rooms/VoiceRecordComposerTile.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {VoiceRecorder} from "../../../voice/VoiceRecorder";
2121
import {Room} from "matrix-js-sdk/src/models/room";
2222
import {MatrixClientPeg} from "../../../MatrixClientPeg";
2323
import classNames from "classnames";
24+
import FrequencyBars from "../voice_messages/FrequencyBars";
2425

2526
interface IProps {
2627
room: Room;
@@ -57,10 +58,6 @@ export default class VoiceRecordComposerTile extends React.PureComponent<IProps,
5758
const recorder = new VoiceRecorder(MatrixClientPeg.get());
5859
await recorder.start();
5960
this.props.onRecording(true);
60-
// TODO: @@ TravisR: Run through EQ component
61-
// recorder.frequencyData.onUpdate((freq) => {
62-
// console.log('@@ UPDATE', freq);
63-
// });
6461
this.setState({recorder});
6562
};
6663

@@ -71,18 +68,21 @@ export default class VoiceRecordComposerTile extends React.PureComponent<IProps,
7168
'mx_VoiceRecordComposerTile_stop': !!this.state.recorder,
7269
});
7370

71+
let bars = null;
7472
let tooltip = _t("Record a voice message");
7573
if (!!this.state.recorder) {
7674
// TODO: @@ TravisR: Change to match behaviour
7775
tooltip = _t("Stop & send recording");
76+
bars = <FrequencyBars recorder={this.state.recorder} />;
7877
}
7978

80-
return (
79+
return (<>
80+
{bars}
8181
<AccessibleTooltipButton
8282
className={classes}
8383
onClick={this.onStartStopVoiceMessage}
8484
title={tooltip}
8585
/>
86-
);
86+
</>);
8787
}
8888
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 {IFrequencyPackage, VoiceRecorder} from "../../../voice/VoiceRecorder";
19+
import {replaceableComponent} from "../../../utils/replaceableComponent";
20+
import {arrayFastResample, arraySeed} from "../../../utils/arrays";
21+
import {percentageOf} from "../../../utils/numbers";
22+
23+
interface IProps {
24+
recorder: VoiceRecorder
25+
}
26+
27+
interface IState {
28+
heights: number[];
29+
}
30+
31+
const DOWNSAMPLE_TARGET = 35; // number of bars
32+
33+
@replaceableComponent("views.voice_messages.FrequencyBars")
34+
export default class FrequencyBars extends React.PureComponent<IProps, IState> {
35+
public constructor(props) {
36+
super(props);
37+
38+
this.state = {heights: arraySeed(0, DOWNSAMPLE_TARGET)};
39+
this.props.recorder.frequencyData.onUpdate(this.onFrequencyData);
40+
}
41+
42+
private onFrequencyData = (freq: IFrequencyPackage) => {
43+
// We're downsampling from about 1024 points to about 35, so this function is fine (see docs/impl)
44+
const bars = arrayFastResample(Array.from(freq.dbBars), DOWNSAMPLE_TARGET);
45+
this.setState({
46+
// Values are somewhat arbitrary, but help decide what shape the graph should be
47+
heights: bars.map(b => percentageOf(b, -150, -70) * 100),
48+
});
49+
};
50+
51+
public render() {
52+
return <div className='mx_FrequencyBars'>
53+
{this.state.heights.map((h, i) => {
54+
return <span key={i} style={{height: h + '%'}} className='mx_FrequencyBars_bar' />;
55+
})}
56+
</div>;
57+
}
58+
}

src/utils/arrays.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,41 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
/**
18+
* Quickly resample an array to have less data points. This isn't a perfect representation,
19+
* though this does work best if given a large array to downsample to a much smaller array.
20+
* @param {number[]} input The input array to downsample.
21+
* @param {number} points The number of samples to end up with.
22+
* @returns {number[]} The downsampled array.
23+
*/
24+
export function arrayFastResample(input: number[], points: number): number[] {
25+
// Heavily inpired by matrix-media-repo (used with permission)
26+
// https://github.com/turt2live/matrix-media-repo/blob/abe72c87d2e29/util/util_audio/fastsample.go#L10
27+
const everyNth = Math.round(input.length / points);
28+
const samples: number[] = [];
29+
for (let i = 0; i < input.length; i += everyNth) {
30+
samples.push(input[i]);
31+
}
32+
while (samples.length < points) {
33+
samples.push(input[input.length - 1]);
34+
}
35+
return samples;
36+
}
37+
38+
/**
39+
* Creates an array of the given length, seeded with the given value.
40+
* @param {T} val The value to seed the array with.
41+
* @param {number} length The length of the array to create.
42+
* @returns {T[]} The array.
43+
*/
44+
export function arraySeed<T>(val: T, length: number): T[] {
45+
const a: T[] = [];
46+
for (let i = 0; i < length; i++) {
47+
a.push(val);
48+
}
49+
return a;
50+
}
51+
1752
/**
1853
* Clones an array as fast as possible, retaining references of the array's values.
1954
* @param a The array to clone. Must be defined.

src/voice/VoiceRecorder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {SimpleObservable} from "matrix-widget-api";
2323
const CHANNELS = 1; // stereo isn't important
2424
const SAMPLE_RATE = 48000; // 48khz is what WebRTC uses. 12khz is where we lose quality.
2525
const BITRATE = 24000; // 24kbps is pretty high quality for our use case in opus.
26-
const FREQ_SAMPLE_RATE = 4; // Target rate of frequency data (samples / sec). We don't need this super often.
26+
const FREQ_SAMPLE_RATE = 10; // Target rate of frequency data (samples / sec). We don't need this super often.
2727

2828
export interface IFrequencyPackage {
2929
dbBars: Float32Array;
@@ -60,7 +60,7 @@ export class VoiceRecorder {
6060
},
6161
});
6262
this.recorderContext = new AudioContext({
63-
latencyHint: "interactive",
63+
// latencyHint: "interactive", // we don't want a latency hint (this causes data smoothing)
6464
sampleRate: SAMPLE_RATE, // once again, the browser will resample for us
6565
});
6666
this.recorderSource = this.recorderContext.createMediaStreamSource(this.recorderStream);

0 commit comments

Comments
 (0)