@waveform-playlist/webaudio-peaks 12.0.0 → 12.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +107 -0
- package/package.json +5 -5
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# @waveform-playlist/webaudio-peaks
|
|
2
|
+
|
|
3
|
+
Small library to extract min/max peaks from an array of audio samples or a Web Audio `AudioBuffer` — the data waveform renderers draw from. Standalone and framework-agnostic; no dependency on the rest of waveform-playlist beyond a shared types package.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @waveform-playlist/webaudio-peaks
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import extractPeaksFromBuffer from '@waveform-playlist/webaudio-peaks';
|
|
15
|
+
|
|
16
|
+
// From a decoded AudioBuffer
|
|
17
|
+
const audioContext = new AudioContext();
|
|
18
|
+
const response = await fetch('/audio/track.mp3');
|
|
19
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
20
|
+
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
|
|
21
|
+
|
|
22
|
+
const peakData = extractPeaksFromBuffer(
|
|
23
|
+
audioBuffer,
|
|
24
|
+
1000, // samplesPerPixel
|
|
25
|
+
true, // isMono - merge all channels into one
|
|
26
|
+
0, // cueIn - start sample
|
|
27
|
+
undefined, // cueOut - end sample (defaults to full length)
|
|
28
|
+
16 // bits - 8 or 16
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
console.log(peakData.length); // number of peak pairs
|
|
32
|
+
console.log(peakData.data); // Peaks[] - one entry per channel (or one, if isMono)
|
|
33
|
+
console.log(peakData.bits); // 16
|
|
34
|
+
|
|
35
|
+
// From a raw Float32Array (e.g. one channel pulled from an AudioBuffer,
|
|
36
|
+
// or samples produced by a worker/AudioWorklet)
|
|
37
|
+
const channelData = audioBuffer.getChannelData(0);
|
|
38
|
+
const monoPeaks = extractPeaksFromBuffer(channelData, 1000);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## API
|
|
42
|
+
|
|
43
|
+
### `extractPeaksFromBuffer(source, samplesPerPixel?, isMono?, cueIn?, cueOut?, bits?)`
|
|
44
|
+
|
|
45
|
+
Default export. Extracts peaks from an `AudioBuffer` or a `Float32Array`.
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
function extractPeaksFromBuffer(
|
|
49
|
+
source: AudioBuffer | Float32Array,
|
|
50
|
+
samplesPerPixel?: number, // samples per peak pair (default: 1000)
|
|
51
|
+
isMono?: boolean, // merge multi-channel input to mono (default: true)
|
|
52
|
+
cueIn?: number, // start sample index (default: 0)
|
|
53
|
+
cueOut?: number, // end sample index (default: source.length)
|
|
54
|
+
bits?: 8 | 16 // peak bit depth (default: 16)
|
|
55
|
+
): PeakData;
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
- **`source`** — an `AudioBuffer` (all channels are read via `getChannelData`) or a single `Float32Array` of samples.
|
|
59
|
+
- **`samplesPerPixel`** — how many source samples are collapsed into one min/max peak pair. Higher values produce fewer, coarser peaks (typical for zoomed-out views); lower values produce more detail.
|
|
60
|
+
- **`isMono`** — when `true` and the source has multiple channels, all channels are averaged down to a single merged peak channel. When `false`, each channel's peaks are kept separate.
|
|
61
|
+
- **`cueIn` / `cueOut`** — restrict extraction to a sample range within the source, useful for extracting peaks from a trimmed region without decoding a new buffer.
|
|
62
|
+
- **`bits`** — `8` or `16`. Throws if any other value is passed. Peak values are quantized to `Int8Array` or `Int16Array` accordingly, which keeps pre-computed peaks compact to store or transmit.
|
|
63
|
+
|
|
64
|
+
Throws an `Error` if `bits` is not `8` or `16`.
|
|
65
|
+
|
|
66
|
+
### Return value: `PeakData`
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
interface PeakData {
|
|
70
|
+
length: number; // number of peak pairs (per channel)
|
|
71
|
+
data: Peaks[]; // one entry per channel; each is an interleaved [min, max, min, max, ...] typed array
|
|
72
|
+
bits: Bits; // 8 or 16 — the bit depth used to quantize the values
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
type Peaks = Int8Array | Int16Array;
|
|
76
|
+
type Bits = 8 | 16;
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
`Peaks`, `Bits`, and `PeakData` are re-exported from `@waveform-playlist/core` for convenience — you don't need to install `core` separately just to reference these types.
|
|
80
|
+
|
|
81
|
+
### Lower-level helpers
|
|
82
|
+
|
|
83
|
+
The min/max extraction and quantization primitives that `extractPeaksFromBuffer` is built from are also exported directly, for callers assembling their own pipeline (e.g. inside an `AudioWorklet` or a Web Worker where you already have raw channel data):
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
import { extractPeaks, makeMono, findMinMax, convert, makeTypedArray } from '@waveform-playlist/webaudio-peaks';
|
|
87
|
+
|
|
88
|
+
// Extract peaks for a single channel you already have as a Float32Array
|
|
89
|
+
const channelPeaks = extractPeaks(channelData, 1000, 16);
|
|
90
|
+
|
|
91
|
+
// Merge several per-channel Peaks arrays into one mono Peaks array
|
|
92
|
+
const [mono] = makeMono([leftPeaks, rightPeaks], 16);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
- `extractPeaks(channel: Float32Array, samplesPerPixel: number, bits: Bits): Peaks` — extracts interleaved min/max peaks from a single channel.
|
|
96
|
+
- `makeMono(channelPeaks: Peaks[], bits: Bits): Peaks[]` — averages multiple channels' peaks into a single-entry array.
|
|
97
|
+
- `findMinMax(array: Float32Array): { min: number; max: number }` — plain min/max over a typed array segment.
|
|
98
|
+
- `convert(n: number, bits: Bits): number` — quantizes a float peak value (-1..1) to the integer range for the given bit depth.
|
|
99
|
+
- `makeTypedArray(bits: Bits, length: number): Peaks` — allocates an `Int8Array` or `Int16Array`.
|
|
100
|
+
|
|
101
|
+
## Examples & Documentation
|
|
102
|
+
|
|
103
|
+
Guides and full API reference: [naomiaro.github.io/waveform-playlist](https://naomiaro.github.io/waveform-playlist/)
|
|
104
|
+
|
|
105
|
+
## License
|
|
106
|
+
|
|
107
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@waveform-playlist/webaudio-peaks",
|
|
3
|
-
"version": "12.0.
|
|
3
|
+
"version": "12.0.1",
|
|
4
4
|
"description": "Small library to extract peaks from an array of audio samples or an AudioBuffer from the WebAudio API.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -36,12 +36,12 @@
|
|
|
36
36
|
"README.md"
|
|
37
37
|
],
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@waveform-playlist/core": "12.
|
|
39
|
+
"@waveform-playlist/core": "12.6.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"tsup": "^8.
|
|
43
|
-
"typescript": "^5.
|
|
44
|
-
"vitest": "^3.
|
|
42
|
+
"tsup": "^8.5.1",
|
|
43
|
+
"typescript": "^5.9.3",
|
|
44
|
+
"vitest": "^3.2.6"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "tsup",
|