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

Commit 34c735e

Browse files
authored
Merge pull request #5956 from matrix-org/travis/voicemessages/waveform2
Calculate the real waveform in the Playback class for voice messages
2 parents 9b76654 + 7f56ec7 commit 34c735e

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/voice/Playback.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {arrayFastResample, arraySeed} from "../utils/arrays";
2020
import {SimpleObservable} from "matrix-widget-api";
2121
import {IDestroyable} from "../utils/IDestroyable";
2222
import {PlaybackClock} from "./PlaybackClock";
23+
import {clamp} from "../utils/numbers";
2324

2425
export enum PlaybackState {
2526
Decoding = "decoding",
@@ -52,8 +53,6 @@ export class Playback extends EventEmitter implements IDestroyable {
5253
this.resampledWaveform = arrayFastResample(seedWaveform, PLAYBACK_WAVEFORM_SAMPLES);
5354
this.waveformObservable.update(this.resampledWaveform);
5455
this.clock = new PlaybackClock(this.context);
55-
56-
// TODO: @@ TR: Calculate real waveform
5756
}
5857

5958
public get waveform(): number[] {
@@ -93,6 +92,13 @@ export class Playback extends EventEmitter implements IDestroyable {
9392

9493
public async prepare() {
9594
this.audioBuf = await this.context.decodeAudioData(this.buf);
95+
96+
// Update the waveform to the real waveform once we have channel data to use. We don't
97+
// exactly trust the user-provided waveform to be accurate...
98+
const waveform = Array.from(this.audioBuf.getChannelData(0)).map(v => clamp(v, 0, 1));
99+
this.resampledWaveform = arrayFastResample(waveform, PLAYBACK_WAVEFORM_SAMPLES);
100+
this.waveformObservable.update(this.resampledWaveform);
101+
96102
this.emit(PlaybackState.Stopped); // signal that we're not decoding anymore
97103
this.clock.durationSeconds = this.audioBuf.duration;
98104
}

0 commit comments

Comments
 (0)