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

Commit e523ce6

Browse files
committed
Fix float operations to make a little more sense.
1 parent a848feb commit e523ce6

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

src/components/views/voice_messages/LiveRecordingWaveform.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,14 @@ export default class LiveRecordingWaveform extends React.PureComponent<IProps, I
4949
const bars = arrayFastResample(Array.from(update.waveform), DOWNSAMPLE_TARGET);
5050
this.setState({
5151
// 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 "cap" the graph at about 0.50 for a
53-
// point where the average user can still see feedback and be perceived as peaking
54-
// when talking "loudly".
55-
//
56-
// We multiply by 100 because the Waveform component wants values in 0-100 (percentages)
57-
heights: bars.map(b => percentageOf(b, 0, 0.50) * 100),
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)),
5856
});
5957
};
6058

6159
public render() {
62-
return <Waveform heights={this.state.heights} />;
60+
return <Waveform relHeights={this.state.heights} />;
6361
}
6462
}

src/components/views/voice_messages/Waveform.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import React from "react";
1818
import {replaceableComponent} from "../../../utils/replaceableComponent";
1919

2020
interface IProps {
21-
heights: number[]; // percentages as integers (0-100)
21+
relHeights: number[]; // relative heights (0-1)
2222
}
2323

2424
interface IState {
@@ -37,8 +37,8 @@ export default class Waveform extends React.PureComponent<IProps, IState> {
3737

3838
public render() {
3939
return <div className='mx_Waveform'>
40-
{this.props.heights.map((h, i) => {
41-
return <span key={i} style={{height: h + '%'}} className='mx_Waveform_bar' />;
40+
{this.props.relHeights.map((h, i) => {
41+
return <span key={i} style={{height: (h * 100) + '%'}} className='mx_Waveform_bar' />;
4242
})}
4343
</div>;
4444
}

src/voice/VoiceRecorder.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import encoderPath from 'opus-recorder/dist/encoderWorker.min.js';
1919
import {MatrixClient} from "matrix-js-sdk/src/client";
2020
import CallMediaHandler from "../CallMediaHandler";
2121
import {SimpleObservable} from "matrix-widget-api";
22+
import {percentageOf} from "../utils/numbers";
2223

2324
const CHANNELS = 1; // stereo isn't important
2425
const SAMPLE_RATE = 48000; // 48khz is what WebRTC uses. 12khz is where we lose quality.
@@ -133,23 +134,18 @@ export class VoiceRecorder {
133134
// The time domain is the input to the FFT, which means we use an array of the same
134135
// size. The time domain is also known as the audio waveform. We're ignoring the
135136
// output of the FFT here (frequency data) because we're not interested in it.
136-
//
137-
// We use bytes out of the analyser because floats have weird precision problems
138-
// and are slightly more difficult to work with. The bytes are easy to work with,
139-
// which is why we pick them (they're also more precise, but we care less about that).
140-
const data = new Uint8Array(this.recorderFFT.fftSize);
141-
this.recorderFFT.getByteTimeDomainData(data);
142-
143-
// Because we're dealing with a uint array we need to do math a bit differently.
144-
// If we just `Array.from()` the uint array, we end up with 1s and 0s, which aren't
145-
// what we're after. Instead, we have to use a bit of manual looping to correctly end
146-
// up with the right values
137+
const data = new Float32Array(this.recorderFFT.fftSize);
138+
this.recorderFFT.getFloatTimeDomainData(data);
139+
140+
// We can't just `Array.from()` the array because we're dealing with 32bit floats
141+
// and the built-in function won't consider that when converting between numbers.
142+
// However, the runtime will convert the float32 to a float64 during the math operations
143+
// which is why the loop works below. Note that a `.map()` call also doesn't work
144+
// and will instead return a Float32Array still.
147145
const translatedData: number[] = [];
148146
for (let i = 0; i < data.length; i++) {
149-
// All we're doing here is inverting the amplitude and putting the metric somewhere
150-
// between zero and one. Without the inversion, lower values are "louder", which is
151-
// not super helpful.
152-
translatedData.push(1 - (data[i] / 128.0));
147+
// We're clamping the values so we can do that math operation mentioned above.
148+
translatedData.push(percentageOf(data[i], 0, 1));
153149
}
154150

155151
this.observable.update({

0 commit comments

Comments
 (0)