UNPKG

@omnimedia/omnitool

Version:

open source video processing tools

51 lines 1.8 kB
import WaveSurfer from "wavesurfer.js"; import { context } from "../../context.js"; export class Waveform { wavesurfer; constructor(peaks, container, duration) { this.wavesurfer = WaveSurfer.create({ container, waveColor: 'rgb(200, 0, 200)', progressColor: 'rgb(100, 0, 100)', barWidth: 10, barRadius: 10, barGap: 2, peaks: [peaks], duration }); } static async init(source, container) { const driver = await context.driver; const reader = driver.decode({ source }).audio.getReader(); const peaks = []; let buffer = []; const samplesPerPeak = 1024; const duration = await driver.getAudioDuration(source); while (true) { const { done, value: audioData } = await reader.read(); if (done) break; const frames = audioData.numberOfFrames; const plane = new Float32Array(frames); audioData.copyTo(plane, { planeIndex: 0 }); // Use left channel only for (let i = 0; i < plane.length; i++) { buffer.push(plane[i]); if (buffer.length >= samplesPerPeak) { const chunk = buffer.splice(0, samplesPerPeak); const min = Math.min(...chunk); const max = Math.max(...chunk); peaks.push(min, max); } } audioData.close(); } return new Waveform(peaks, container, duration ?? 0); } // set zoom(value: number) { // this.wavesurfer.zoom(value) // } set width(value) { this.wavesurfer.setOptions({ width: value }); } } //# sourceMappingURL=waveform.js.map