@@ -20,6 +20,7 @@ import {arrayFastResample, arraySeed} from "../utils/arrays";
2020import { SimpleObservable } from "matrix-widget-api" ;
2121import { IDestroyable } from "../utils/IDestroyable" ;
2222import { PlaybackClock } from "./PlaybackClock" ;
23+ import { clamp } from "../utils/numbers" ;
2324
2425export 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