@waveform-playlist/browser 13.1.3 → 14.0.0

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.
@@ -0,0 +1,429 @@
1
+ import { TrackEffectsFunction as TrackEffectsFunction$1, Fade, WaveformDataObject, RenderMode, SpectrogramConfig, ColorMapValue, ClipTrack } from '@waveform-playlist/core';
2
+ import * as React$1 from 'react';
3
+ import React__default from 'react';
4
+ import { EffectsFunction, TrackEffectsFunction as TrackEffectsFunction$2 } from '@waveform-playlist/playout';
5
+ import { Analyser, ToneAudioNode, InputNode } from 'tone';
6
+
7
+ /**
8
+ * Configuration for a single audio track to load
9
+ *
10
+ * Audio can be provided in three ways:
11
+ * 1. `src` - URL to fetch and decode (standard loading)
12
+ * 2. `audioBuffer` - Pre-loaded AudioBuffer (skip fetch/decode)
13
+ * 3. `waveformData` only - Peaks-first rendering (audio loads later)
14
+ *
15
+ * For peaks-first rendering, just provide `waveformData` - the sample rate
16
+ * and duration are derived from the waveform data automatically.
17
+ */
18
+ interface AudioTrackConfig {
19
+ /** URL to audio file - used if audioBuffer not provided */
20
+ src?: string;
21
+ /** Pre-loaded AudioBuffer - skips fetch/decode if provided */
22
+ audioBuffer?: AudioBuffer;
23
+ name?: string;
24
+ muted?: boolean;
25
+ soloed?: boolean;
26
+ volume?: number;
27
+ pan?: number;
28
+ color?: string;
29
+ effects?: TrackEffectsFunction$1;
30
+ startTime?: number;
31
+ duration?: number;
32
+ offset?: number;
33
+ fadeIn?: Fade;
34
+ fadeOut?: Fade;
35
+ waveformData?: WaveformDataObject;
36
+ /** Visualization render mode: 'waveform' | 'spectrogram' | 'both'. Default: 'waveform' */
37
+ renderMode?: RenderMode;
38
+ /** Spectrogram configuration (FFT size, window, frequency scale, etc.) */
39
+ spectrogramConfig?: SpectrogramConfig;
40
+ /** Spectrogram color map name or custom color array */
41
+ spectrogramColorMap?: ColorMapValue;
42
+ }
43
+ /**
44
+ * Options for useAudioTracks hook
45
+ */
46
+ interface UseAudioTracksOptions {
47
+ /**
48
+ * When true, all tracks render immediately as placeholders with clip geometry
49
+ * from the config. Audio fills in progressively as files decode, and peaks
50
+ * render as each buffer becomes available. Use with `deferEngineRebuild={loading}`
51
+ * on the provider for a single engine build when all tracks are ready.
52
+ *
53
+ * Requires `duration` or `waveformData` in each config so clip dimensions are known upfront.
54
+ * Default: false
55
+ */
56
+ immediate?: boolean;
57
+ /** @deprecated Use `immediate` instead. */
58
+ progressive?: boolean;
59
+ }
60
+ /**
61
+ * Hook to load audio from URLs and convert to ClipTrack format
62
+ *
63
+ * This hook fetches audio files, decodes them, and creates ClipTrack objects
64
+ * with a single clip per track. Supports custom positioning for multi-clip arrangements.
65
+ *
66
+ * @param configs - Array of audio track configurations
67
+ * @param options - Optional configuration for loading behavior
68
+ * @returns Object with tracks array, loading state, and progress info
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * // Basic usage (clips positioned at start)
73
+ * const { tracks, loading, error } = useAudioTracks([
74
+ * { src: 'audio/vocals.mp3', name: 'Vocals' },
75
+ * { src: 'audio/drums.mp3', name: 'Drums' },
76
+ * ]);
77
+ *
78
+ * // Immediate rendering with deferred engine build (recommended for multi-track)
79
+ * const { tracks, loading } = useAudioTracks(
80
+ * [
81
+ * { src: 'audio/vocals.mp3', name: 'Vocals', duration: 30 },
82
+ * { src: 'audio/drums.mp3', name: 'Drums', duration: 30 },
83
+ * ],
84
+ * { immediate: true }
85
+ * );
86
+ * // All tracks render instantly as placeholders, peaks fill in as files load
87
+ * return (
88
+ * <WaveformPlaylistProvider tracks={tracks} deferEngineRebuild={loading}>
89
+ * ...
90
+ * </WaveformPlaylistProvider>
91
+ * );
92
+ *
93
+ * // Pre-loaded AudioBuffer (skip fetch/decode)
94
+ * const { tracks } = useAudioTracks([
95
+ * { audioBuffer: myPreloadedBuffer, name: 'Pre-loaded' },
96
+ * ]);
97
+ *
98
+ * // Peaks-first rendering (instant visual, audio loads later)
99
+ * const { tracks } = useAudioTracks([
100
+ * { waveformData: preloadedPeaks, name: 'Peaks Only' }, // Renders immediately
101
+ * ]);
102
+ * ```
103
+ */
104
+ declare function useAudioTracks(configs: AudioTrackConfig[], options?: UseAudioTracksOptions): {
105
+ tracks: ClipTrack[];
106
+ loading: boolean;
107
+ error: string | null;
108
+ loadedCount: number;
109
+ totalCount: number;
110
+ };
111
+
112
+ /**
113
+ * Hook for master effects with frequency analyzer
114
+ * Returns the analyser ref and the effects function to pass to WaveformPlaylistProvider
115
+ *
116
+ * For more advanced effects (reverb, delay, filters, etc.), use useDynamicEffects instead.
117
+ */
118
+ declare const useMasterAnalyser: (fftSize?: number) => {
119
+ analyserRef: React$1.MutableRefObject<Analyser | null>;
120
+ masterEffects: EffectsFunction;
121
+ };
122
+
123
+ /**
124
+ * Effect definitions for all available Tone.js effects
125
+ * Each effect has parameters with min/max/default values for UI controls
126
+ */
127
+ type ParameterType = 'number' | 'select' | 'boolean';
128
+ interface EffectParameter {
129
+ name: string;
130
+ label: string;
131
+ type: ParameterType;
132
+ min?: number;
133
+ max?: number;
134
+ step?: number;
135
+ default: number | string | boolean;
136
+ unit?: string;
137
+ options?: {
138
+ value: string | number;
139
+ label: string;
140
+ }[];
141
+ }
142
+ interface EffectDefinition {
143
+ id: string;
144
+ name: string;
145
+ category: 'delay' | 'reverb' | 'modulation' | 'distortion' | 'filter' | 'dynamics' | 'spatial';
146
+ description: string;
147
+ parameters: EffectParameter[];
148
+ }
149
+ declare const effectDefinitions: EffectDefinition[];
150
+ declare const getEffectDefinition: (id: string) => EffectDefinition | undefined;
151
+ declare const getEffectsByCategory: (category: EffectDefinition["category"]) => EffectDefinition[];
152
+ declare const effectCategories: {
153
+ id: EffectDefinition['category'];
154
+ name: string;
155
+ }[];
156
+
157
+ interface ActiveEffect {
158
+ instanceId: string;
159
+ effectId: string;
160
+ definition: EffectDefinition;
161
+ params: Record<string, number | string | boolean>;
162
+ bypassed: boolean;
163
+ }
164
+ interface UseDynamicEffectsReturn {
165
+ activeEffects: ActiveEffect[];
166
+ availableEffects: EffectDefinition[];
167
+ addEffect: (effectId: string) => void;
168
+ removeEffect: (instanceId: string) => void;
169
+ updateParameter: (instanceId: string, paramName: string, value: number | string | boolean) => void;
170
+ toggleBypass: (instanceId: string) => void;
171
+ reorderEffects: (fromIndex: number, toIndex: number) => void;
172
+ clearAllEffects: () => void;
173
+ masterEffects: EffectsFunction;
174
+ /**
175
+ * Creates a fresh effects function for offline rendering.
176
+ * This creates new effect instances that work in the offline AudioContext.
177
+ */
178
+ createOfflineEffectsFunction: () => EffectsFunction | undefined;
179
+ analyserRef: React.RefObject<Analyser | null>;
180
+ }
181
+ /**
182
+ * Hook for managing a dynamic chain of audio effects with real-time parameter updates
183
+ */
184
+ declare function useDynamicEffects(fftSize?: number): UseDynamicEffectsReturn;
185
+
186
+ interface TrackActiveEffect {
187
+ instanceId: string;
188
+ effectId: string;
189
+ definition: EffectDefinition;
190
+ params: Record<string, number | string | boolean>;
191
+ bypassed: boolean;
192
+ }
193
+ interface TrackEffectsState {
194
+ trackId: string;
195
+ activeEffects: TrackActiveEffect[];
196
+ }
197
+ interface UseTrackDynamicEffectsReturn {
198
+ trackEffectsState: Map<string, TrackActiveEffect[]>;
199
+ addEffectToTrack: (trackId: string, effectId: string) => void;
200
+ removeEffectFromTrack: (trackId: string, instanceId: string) => void;
201
+ updateTrackEffectParameter: (trackId: string, instanceId: string, paramName: string, value: number | string | boolean) => void;
202
+ toggleBypass: (trackId: string, instanceId: string) => void;
203
+ clearTrackEffects: (trackId: string) => void;
204
+ getTrackEffectsFunction: (trackId: string) => TrackEffectsFunction$2 | undefined;
205
+ /**
206
+ * Creates a fresh effects function for a track for offline rendering.
207
+ * This creates new effect instances that work in the offline AudioContext.
208
+ */
209
+ createOfflineTrackEffectsFunction: (trackId: string) => TrackEffectsFunction$2 | undefined;
210
+ availableEffects: EffectDefinition[];
211
+ }
212
+ /**
213
+ * Hook for managing dynamic effects per track with real-time parameter updates
214
+ */
215
+ declare function useTrackDynamicEffects(): UseTrackDynamicEffectsReturn;
216
+
217
+ /**
218
+ * WAV file encoder
219
+ * Converts AudioBuffer to WAV format Blob
220
+ */
221
+ interface WavEncoderOptions {
222
+ /** Bit depth: 16 or 32. Default: 16 */
223
+ bitDepth?: 16 | 32;
224
+ }
225
+
226
+ /** Function type for per-track effects (same as in @waveform-playlist/core) */
227
+ type TrackEffectsFunction = (graphEnd: unknown, destination: unknown, isOffline: boolean) => void | (() => void);
228
+ interface ExportOptions extends WavEncoderOptions {
229
+ /** Filename for download (without extension) */
230
+ filename?: string;
231
+ /** Export mode: 'master' for full mixdown, 'individual' for single track */
232
+ mode?: 'master' | 'individual';
233
+ /** Track index for individual export (only used when mode is 'individual') */
234
+ trackIndex?: number;
235
+ /** Whether to trigger automatic download */
236
+ autoDownload?: boolean;
237
+ /** Whether to apply effects (fades, etc.) - defaults to true */
238
+ applyEffects?: boolean;
239
+ /**
240
+ * Optional Tone.js effects function for master effects. When provided, export renders
241
+ * through the effects chain. The function receives isOffline=true.
242
+ */
243
+ effectsFunction?: EffectsFunction;
244
+ /**
245
+ * Optional function to create offline track effects.
246
+ * Takes a trackId and returns a TrackEffectsFunction for offline rendering.
247
+ * This is used instead of track.effects to avoid AudioContext mismatch issues.
248
+ */
249
+ createOfflineTrackEffects?: (trackId: string) => TrackEffectsFunction | undefined;
250
+ /** Progress callback (0-1) */
251
+ onProgress?: (progress: number) => void;
252
+ }
253
+ interface ExportResult {
254
+ /** The rendered audio buffer */
255
+ audioBuffer: AudioBuffer;
256
+ /** The WAV file as a Blob */
257
+ blob: Blob;
258
+ /** Duration in seconds */
259
+ duration: number;
260
+ }
261
+ interface UseExportWavReturn {
262
+ /** Export the playlist to WAV */
263
+ exportWav: (tracks: ClipTrack[], trackStates: TrackState[], options?: ExportOptions) => Promise<ExportResult>;
264
+ /** Whether export is in progress */
265
+ isExporting: boolean;
266
+ /** Export progress (0-1) */
267
+ progress: number;
268
+ /** Error message if export failed */
269
+ error: string | null;
270
+ }
271
+ interface TrackState {
272
+ muted: boolean;
273
+ soloed: boolean;
274
+ volume: number;
275
+ pan: number;
276
+ }
277
+ /**
278
+ * Hook for exporting the waveform playlist to WAV format.
279
+ * Uses Tone.Offline for non-real-time rendering, mirroring the live playback graph.
280
+ */
281
+ declare function useExportWav(): UseExportWavReturn;
282
+
283
+ /**
284
+ * useDynamicTracks — imperative hook for runtime track additions.
285
+ *
286
+ * Complements `useAudioTracks` (declarative, configs-driven) with an
287
+ * imperative `addTracks()` API for dynamic loading (drag-and-drop, file pickers).
288
+ *
289
+ * Placeholder tracks appear instantly while audio decodes in parallel.
290
+ */
291
+
292
+ /** A source that can be decoded into a track. */
293
+ type TrackSource = File | Blob | string | {
294
+ src: string;
295
+ name?: string;
296
+ };
297
+ /** Info about a track that failed to load. */
298
+ interface TrackLoadError {
299
+ /** Display name of the source that failed. */
300
+ name: string;
301
+ /** The underlying error. */
302
+ error: Error;
303
+ }
304
+ interface UseDynamicTracksReturn {
305
+ /**
306
+ * Current tracks array (placeholders + loaded). Pass to WaveformPlaylistProvider.
307
+ * Placeholder tracks have `clips: []` and names ending with " (loading...)".
308
+ */
309
+ tracks: ClipTrack[];
310
+ /** Add one or more sources — creates placeholder tracks immediately. */
311
+ addTracks: (sources: TrackSource[]) => void;
312
+ /** Remove a track by its id. Aborts in-flight fetch/decode if still loading. */
313
+ removeTrack: (trackId: string) => void;
314
+ /** Number of sources currently decoding. */
315
+ loadingCount: number;
316
+ /** True when any source is still decoding. */
317
+ isLoading: boolean;
318
+ /** Tracks that failed to load (removed from `tracks` automatically). */
319
+ errors: TrackLoadError[];
320
+ }
321
+ declare function useDynamicTracks(): UseDynamicTracksReturn;
322
+
323
+ /**
324
+ * Hook for monitoring master output levels
325
+ *
326
+ * Connects an AudioWorklet meter processor to the Destination node for
327
+ * real-time output level monitoring. Computes sample-accurate peak and
328
+ * RMS via the meter worklet — no transient is missed.
329
+ *
330
+ * IMPORTANT: Uses getGlobalContext() from playout to ensure the meter
331
+ * is created on the same AudioContext as the audio engine. Tone.js's
332
+ * getContext()/getDestination() return the DEFAULT context, which is
333
+ * replaced when getGlobalContext() calls setContext() on first audio init.
334
+ */
335
+ interface UseOutputMeterOptions {
336
+ /**
337
+ * Number of channels to meter.
338
+ * Default: 2 (stereo output)
339
+ */
340
+ channelCount?: number;
341
+ /**
342
+ * How often to update the levels (in Hz).
343
+ * Default: 60 (60fps)
344
+ */
345
+ updateRate?: number;
346
+ /**
347
+ * Whether audio is currently playing. When this transitions to false,
348
+ * all levels (current, peak, RMS) and smoothed state are reset to zero.
349
+ * Without this, the browser's tail-time optimization stops calling the
350
+ * worklet's process() when no audio flows, leaving the last non-zero
351
+ * levels frozen in state.
352
+ * Default: false
353
+ */
354
+ isPlaying?: boolean;
355
+ }
356
+ interface UseOutputMeterReturn {
357
+ /** Per-channel peak output levels (0-1) */
358
+ levels: number[];
359
+ /** Per-channel held peak levels (0-1) */
360
+ peakLevels: number[];
361
+ /** Per-channel RMS output levels (0-1) */
362
+ rmsLevels: number[];
363
+ /** Reset all held peak levels to 0 */
364
+ resetPeak: () => void;
365
+ /** Error from meter setup (worklet load failure, context issues, etc.) */
366
+ error: Error | null;
367
+ }
368
+ declare function useOutputMeter(options?: UseOutputMeterOptions): UseOutputMeterReturn;
369
+
370
+ /**
371
+ * Factory for creating Tone.js effect instances from effect definitions
372
+ */
373
+
374
+ interface EffectInstance {
375
+ effect: ToneAudioNode;
376
+ id: string;
377
+ instanceId: string;
378
+ dispose: () => void;
379
+ setParameter: (name: string, value: number | string | boolean) => void;
380
+ getParameter: (name: string) => number | string | boolean | undefined;
381
+ connect: (destination: InputNode) => void;
382
+ disconnect: () => void;
383
+ }
384
+ /**
385
+ * Create an effect instance from a definition with initial parameter values
386
+ */
387
+ declare function createEffectInstance(definition: EffectDefinition, initialParams?: Record<string, number | string | boolean>): EffectInstance;
388
+ /**
389
+ * Create a chain of effects connected in series
390
+ */
391
+ declare function createEffectChain(effects: EffectInstance[]): {
392
+ input: ToneAudioNode;
393
+ output: ToneAudioNode;
394
+ dispose: () => void;
395
+ };
396
+
397
+ interface ExportWavButtonProps {
398
+ /** Button label */
399
+ label?: string;
400
+ /** Filename for the downloaded file (without extension) */
401
+ filename?: string;
402
+ /** Export mode: 'master' for stereo mix, 'individual' for single track */
403
+ mode?: 'master' | 'individual';
404
+ /** Track index for individual export */
405
+ trackIndex?: number;
406
+ /** Bit depth: 16 or 32 */
407
+ bitDepth?: 16 | 32;
408
+ /** Whether to apply effects (fades, etc.) - defaults to true */
409
+ applyEffects?: boolean;
410
+ /**
411
+ * Optional Tone.js effects function for master effects. When provided, export will use Tone.Offline
412
+ * to render through the effects chain. The function receives isOffline=true.
413
+ */
414
+ effectsFunction?: EffectsFunction;
415
+ /**
416
+ * Optional function to create offline track effects.
417
+ * Takes a trackId and returns a TrackEffectsFunction for offline rendering.
418
+ */
419
+ createOfflineTrackEffects?: (trackId: string) => TrackEffectsFunction | undefined;
420
+ /** CSS class name */
421
+ className?: string;
422
+ /** Callback when export completes */
423
+ onExportComplete?: (blob: Blob) => void;
424
+ /** Callback when export fails */
425
+ onExportError?: (error: Error) => void;
426
+ }
427
+ declare const ExportWavButton: React__default.FC<ExportWavButtonProps>;
428
+
429
+ export { type ActiveEffect, type AudioTrackConfig, type EffectDefinition, type EffectInstance, type EffectParameter, type ExportOptions, type ExportResult, ExportWavButton, type ExportWavButtonProps, type ParameterType, type TrackActiveEffect, type TrackEffectsState, type TrackLoadError, type TrackSource, type UseDynamicEffectsReturn, type UseDynamicTracksReturn, type UseExportWavReturn, type UseOutputMeterOptions, type UseOutputMeterReturn, type UseTrackDynamicEffectsReturn, createEffectChain, createEffectInstance, effectCategories, effectDefinitions, getEffectDefinition, getEffectsByCategory, useAudioTracks, useDynamicEffects, useDynamicTracks, useExportWav, useMasterAnalyser, useOutputMeter, useTrackDynamicEffects };