From 0806af0f4d4f276ff38857ab7fb71b43ee923e9d Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 3 May 2021 15:07:52 -0600 Subject: [PATCH 1/2] Calculate the real waveform in the Playback class --- src/voice/Playback.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/voice/Playback.ts b/src/voice/Playback.ts index 99b1f62866..a927ad3104 100644 --- a/src/voice/Playback.ts +++ b/src/voice/Playback.ts @@ -20,6 +20,7 @@ import {arrayFastResample, arraySeed} from "../utils/arrays"; import {SimpleObservable} from "matrix-widget-api"; import {IDestroyable} from "../utils/IDestroyable"; import {PlaybackClock} from "./PlaybackClock"; +import {clamp} from "../utils/numbers"; export enum PlaybackState { Decoding = "decoding", @@ -52,8 +53,6 @@ export class Playback extends EventEmitter implements IDestroyable { this.resampledWaveform = arrayFastResample(seedWaveform, PLAYBACK_WAVEFORM_SAMPLES); this.waveformObservable.update(this.resampledWaveform); this.clock = new PlaybackClock(this.context); - - // TODO: @@ TR: Calculate real waveform } public get waveform(): number[] { @@ -93,6 +92,11 @@ export class Playback extends EventEmitter implements IDestroyable { public async prepare() { this.audioBuf = await this.context.decodeAudioData(this.buf); + + const waveform = Array.from(this.audioBuf.getChannelData(0)).map(v => clamp(v, 0, 1)); + this.resampledWaveform = arrayFastResample(waveform, PLAYBACK_WAVEFORM_SAMPLES); + this.waveformObservable.update(this.resampledWaveform); + this.emit(PlaybackState.Stopped); // signal that we're not decoding anymore this.clock.durationSeconds = this.audioBuf.duration; } From 7f56ec7bd263a67139e91583b102fc8a1c1bba99 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 3 May 2021 15:16:50 -0600 Subject: [PATCH 2/2] docs --- src/voice/Playback.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/voice/Playback.ts b/src/voice/Playback.ts index a927ad3104..954aa662c3 100644 --- a/src/voice/Playback.ts +++ b/src/voice/Playback.ts @@ -93,6 +93,8 @@ export class Playback extends EventEmitter implements IDestroyable { public async prepare() { this.audioBuf = await this.context.decodeAudioData(this.buf); + // Update the waveform to the real waveform once we have channel data to use. We don't + // exactly trust the user-provided waveform to be accurate... const waveform = Array.from(this.audioBuf.getChannelData(0)).map(v => clamp(v, 0, 1)); this.resampledWaveform = arrayFastResample(waveform, PLAYBACK_WAVEFORM_SAMPLES); this.waveformObservable.update(this.resampledWaveform);