Methods
(static) decodeSampleArrayBuffer(arrayBuffer) → {Promise.<{audioBuffer: AudioBuffer, blobUrl: string, peaks: Array.<number>}>}
Decodes an audio ArrayBuffer and returns an AudioBuffer, a 120-bar peaks array, and a blobUrl suitable for re-upload. The ArrayBuffer is consumed by decodeAudioData; the returned blobUrl is re-encoded from the decoded data.
Parameters:
| Name | Type | Description |
|---|---|---|
arrayBuffer | ArrayBuffer | raw audio data to decode |
- Source
Returns:
- Type:
- Promise.<{audioBuffer: AudioBuffer, blobUrl: string, peaks: Array.<number>}>
(static) encodeMonoWav(samples, sampleRate) → {Blob}
Encodes a Float32Array of mono PCM samples as a 16-bit WAV Blob.
Parameters:
| Name | Type | Description |
|---|---|---|
samples | Float32Array | mono PCM data in the range [-1, 1] |
sampleRate | number | sample rate in Hz for the WAV header |
- Source
Returns:
WAV blob with audio/wav MIME type
- Type:
- Blob
(static) extractPeaksFromUrl(url, optsopt) → {Promise.<({peaks: Array.<Float32Array>, sampleRate: number, duration: number}|null)>}
Extracts waveform peaks from a remote audio URL by fetching it in CHUNK_SIZE_BYTES slices via HTTP Range requests and decoding each chunk independently. Peak memory stays under ~100 MB regardless of file length. Returns null if the server does not advertise a Content-Length (chunking is impossible without knowing the total size) or rejects Range requests, so callers should fall back to the standard WaveSurfer decode path.
Parameters:
| Name | Type | Attributes | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
url | string | URL to an audio resource that supports Range requests | |||||||||
opts | object | <optional> | optional configuration objectProperties
|
- Source
Returns:
- Type:
- Promise.<({peaks: Array.<Float32Array>, sampleRate: number, duration: number}|null)>
(static) isAudioFile(file) → {boolean}
Returns true if the File appears to be a supported audio file. Checks the MIME type first; if absent or video/mp4, falls back to the file extension to handle OS/browser inconsistencies.
Parameters:
| Name | Type | Description |
|---|---|---|
file | File | the file to test |
- Source
Returns:
- Type:
- boolean
(static) loadAudioFile(file, optsopt) → {Promise.<{url: string, sampleRate: number, peaks: Array.<Float32Array>, duration: number, filename: string}>}
Reads an audio file, decodes it via the Web Audio API, mixes all channels down to mono, extracts waveform peaks via a Web Worker, and creates a streamable object URL (mono WAV) for playback. Has no knowledge of AppState and can be used independently of the wider application. For files larger than LARGE_FILE_THRESHOLD_BYTES the audio is decoded in small chunks so that peak memory never exceeds ~100 MB regardless of file length (see _extractPeaksChunked). The caller is responsible for revoking the returned URL when it is no longer needed: `URL.revokeObjectURL(url)`.
Parameters:
| Name | Type | Attributes | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
file | File | audio file to load; any format supported by the browser | |||||||||
opts | object | <optional> | optional configuration objectProperties
|
- Source
Returns:
url - object URL suitable for passing to WaveSurfer.load() sampleRate - the sample rate of the decoded audio in Hz peaks - one Float32Array of absolute peak values (mono mix) duration - total duration of the audio in seconds filename - the original file name
- Type:
- Promise.<{url: string, sampleRate: number, peaks: Array.<Float32Array>, duration: number, filename: string}>
(async, inner) _extractPeaksChunked(file, onProgressopt, knownDurationopt) → {Promise.<{url, sampleRate, peaks, duration, filename}>}
Extracts waveform peaks from a large audio file by decoding it in CHUNK_SIZE_BYTES slices. Only one decoded buffer is held in memory at a time, so peak memory stays well under 100 MB regardless of file length. Each slice is passed to decodeAudioData independently. MP3 frames are self-contained so boundary splits cause at most a few milliseconds of silence; visually this is invisible on a waveform. Slices that fail to decode (e.g. unsupported container fragments) contribute zero peaks. WAV files are handled specially: each PCM chunk is prepended with the original file's RIFF header (with sizes patched) before being passed to decodeAudioData. This avoids the headerless-chunk problem and delegates all format-specific decoding (PCM, float, extensible, etc.) to the browser's native decoder rather than reimplementing it in JavaScript.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
file | File | audio file to decode in chunks | |
onProgress | function | <optional> | called after each chunk with fraction 0–1 |
knownDuration | number | <optional> | duration in seconds if already known, skips a metadata fetch |
- Source
Returns:
- Type:
- Promise.<{url, sampleRate, peaks, duration, filename}>
(inner) _getDurationFromUrl(url) → {Promise.<number>}
Returns the duration of a media URL by loading it into a temporary Audio element and waiting for 'loadedmetadata'. No full decode is performed.
Parameters:
| Name | Type | Description |
|---|---|---|
url | string | the audio URL to measure |
- Source
Returns:
duration in seconds (0 on error)
- Type:
- Promise.<number>
(async, inner) extractPeaks(channelBuffers, peakCount) → {Promise.<Array.<Float32Array>>}
Extracts per-channel waveform peaks from raw PCM channel buffers using an inline Web Worker, keeping the UI thread free during the computation. Each channel is downsampled to peakCount blocks, with each block's value being the maximum absolute amplitude of the samples within it.
Parameters:
| Name | Type | Description |
|---|---|---|
channelBuffers | Array.<ArrayBuffer> | raw PCM data, one ArrayBuffer per channel |
peakCount | number | number of peak samples to extract per channel |
- Source
Returns:
one Float32Array of peak values per channel
- Type:
- Promise.<Array.<Float32Array>>