getWaveformPeaks
getWaveformPeaks(
audioBuffer,options?):object
Defined in: packages/pleco-xa/src/scripts/analysis/WaveformData.ts:64
Extracts waveform peaks suitable for visualization
Parameters
Section titled “Parameters”audioBuffer
Section titled “audioBuffer”any
Web Audio API AudioBuffer
options?
Section titled “options?”Extraction options
Returns
Section titled “Returns”object
Waveform data for visualization
data:
Float32Array<any>
duration
Section titled “duration”duration:
any=audioBuffer.duration
length
Section titled “length”length:
any=data.length
metadata
Section titled “metadata”metadata:
object
metadata.method
Section titled “metadata.method”method:
string=opts.type
metadata.originalLength
Section titled “metadata.originalLength”originalLength:
any=channelData.length
metadata.samplesPerPeak
Section titled “metadata.samplesPerPeak”samplesPerPeak:
number
peaks:
Float32Array<any>
sampleRate
Section titled “sampleRate”sampleRate:
any=audioBuffer.sampleRate
Example
Section titled “Example”import { getWaveformPeaks } from './analysis/WaveformData.ts';
const peaks = getWaveformPeaks(audioBuffer, { width: 1000, type: 'peaks', normalize: true});
// Use with canvasconst canvas = document.getElementById('waveform');const ctx = canvas.getContext('2d');
peaks.data.forEach((peak, i) => { const x = (i / peaks.length) * canvas.width; const y = canvas.height / 2; const height = peak * canvas.height / 2; ctx.fillRect(x, y - height, 1, height * 2);});