waveframe 0.1.2 → 0.2.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.
- package/README.md +50 -15
- package/dist/src/components/WaveframePlayer.d.ts +66 -3
- package/dist/src/core/PeakAnalyzer.d.ts +41 -0
- package/dist/src/core/PlayerCore.d.ts +92 -0
- package/dist/src/core/WaveframeEngine.d.ts +122 -0
- package/dist/src/hooks/useWaveframe.d.ts +51 -0
- package/dist/src/hooks/useWaveframeStore.d.ts +27 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/molecules/ArtworkOverlay.d.ts +12 -2
- package/dist/src/organisms/SettingsPanel.d.ts +12 -1
- package/dist/src/types/index.d.ts +57 -1
- package/dist/src/utils/audio.d.ts +24 -6
- package/dist/waveframe.css +1 -1
- package/dist/waveframe.es.js +355 -187
- package/dist/waveframe.umd.js +3 -2
- package/package.json +6 -2
- package/dist/src/hooks/useAudioPlayer.d.ts +0 -16
package/README.md
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
# Waveframe
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Waveframe is a soundcloud-embed inspired web audio player for React. It handles audio playback and waveform visualization with a modular engine.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
7
|
+
- Canvas-based waveform rendering.
|
|
8
|
+
- Automatic peak analysis for URLs and Blobs.
|
|
9
|
+
- Customizable colors via a theme object or CSS variables.
|
|
10
|
+
- A hook-based API for building custom layouts.
|
|
11
|
+
- Support for local audio and artwork file uploads.
|
|
12
|
+
- Responsive scaling to fit different container sizes.
|
|
11
13
|
|
|
12
14
|
## Installation
|
|
13
15
|
|
|
@@ -15,26 +17,59 @@ A customizable React audio player component with SoundCloud-style waveforms.
|
|
|
15
17
|
pnpm add waveframe
|
|
16
18
|
```
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
Note: Waveframe requires React 19 or newer.
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
The simplest way to use Waveframe is to provide an audio source via the `media` prop.
|
|
19
25
|
|
|
20
26
|
```tsx
|
|
21
27
|
import { WaveframePlayer } from 'waveframe';
|
|
28
|
+
import 'waveframe/style.css';
|
|
22
29
|
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
function App() {
|
|
30
|
+
const App = () => {
|
|
26
31
|
return (
|
|
27
32
|
<WaveframePlayer
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
artist="Artist Name"
|
|
33
|
+
title="Electronic Sunset"
|
|
34
|
+
artist="Digital Nomad"
|
|
35
|
+
media="https://example.com/audio.mp3"
|
|
36
|
+
artwork="https://example.com/cover.jpg"
|
|
33
37
|
/>
|
|
34
38
|
);
|
|
35
|
-
}
|
|
39
|
+
};
|
|
36
40
|
```
|
|
37
41
|
|
|
42
|
+
## Component Reference
|
|
43
|
+
|
|
44
|
+
### Primary Props
|
|
45
|
+
|
|
46
|
+
| Prop | Type | Description |
|
|
47
|
+
| :--- | :--- | :--- |
|
|
48
|
+
| `media` | `string \| Blob` | Audio source (URL or local File/Blob). |
|
|
49
|
+
| `peaks` | `number[]` | Optional pre-computed peaks to skip analysis. |
|
|
50
|
+
| `artwork`| `string \| Blob` | Artwork image source (URL or local File/Blob). |
|
|
51
|
+
| `title` | `string` | Track title. |
|
|
52
|
+
| `artist` | `string` | Artist name. |
|
|
53
|
+
| `theme` | `WaveframeTheme` | Object for color customization. |
|
|
54
|
+
|
|
55
|
+
## Architecture
|
|
56
|
+
|
|
57
|
+
Waveframe separates playback logic from the UI to allow for custom implementations.
|
|
58
|
+
|
|
59
|
+
- **useWaveframe**: A hook for controlling the player state and analysis.
|
|
60
|
+
- **WaveframeEngine**: The core logic class.
|
|
61
|
+
- **Waveform**: A component for rendering peaks.
|
|
62
|
+
|
|
63
|
+
For technical guides on building custom layouts or handling file workflows, refer to the full documentation.
|
|
64
|
+
|
|
65
|
+
## Documentation and Playground
|
|
66
|
+
|
|
67
|
+
### Interactive Playground
|
|
68
|
+
`pnpm run dev`
|
|
69
|
+
|
|
70
|
+
### API Reference
|
|
71
|
+
`pnpm run docs`
|
|
72
|
+
|
|
38
73
|
## License
|
|
39
74
|
|
|
40
75
|
MIT
|
|
@@ -1,20 +1,83 @@
|
|
|
1
1
|
import { default as React } from 'react';
|
|
2
2
|
import { WaveframeTheme } from '../types';
|
|
3
|
+
import { WaveframeEngine } from '../core/WaveframeEngine';
|
|
4
|
+
/**
|
|
5
|
+
* Props for the WaveframePlayer component
|
|
6
|
+
*/
|
|
3
7
|
export interface WaveframePlayerProps {
|
|
4
|
-
|
|
8
|
+
/**
|
|
9
|
+
* The audio source to play. Can be a URL string or a Blob/File object.
|
|
10
|
+
*/
|
|
11
|
+
media?: string | Blob;
|
|
12
|
+
/**
|
|
13
|
+
* Optional pre-generated peaks for the waveform (0-1 range).
|
|
14
|
+
* If omitted or empty, the player will automatically analyze the media.
|
|
15
|
+
*/
|
|
5
16
|
peaks?: number[];
|
|
6
|
-
|
|
17
|
+
/**
|
|
18
|
+
* The artwork source to display. Can be a URL string or a Blob/File object.
|
|
19
|
+
*/
|
|
20
|
+
artwork?: string | Blob;
|
|
21
|
+
/**
|
|
22
|
+
* The title of the track
|
|
23
|
+
*/
|
|
7
24
|
title?: string;
|
|
25
|
+
/**
|
|
26
|
+
* The artist of the track
|
|
27
|
+
*/
|
|
8
28
|
artist?: string;
|
|
29
|
+
/**
|
|
30
|
+
* The base color of the waveform bars
|
|
31
|
+
* @default "#e5e7eb" (light) or "#374151" (dark)
|
|
32
|
+
*/
|
|
9
33
|
waveColor?: string;
|
|
34
|
+
/**
|
|
35
|
+
* The color of the played progress part of the waveform
|
|
36
|
+
* @default theme.primary or "#3b82f6"
|
|
37
|
+
*/
|
|
10
38
|
progressColor?: string;
|
|
39
|
+
/**
|
|
40
|
+
* The height of the waveform in pixels
|
|
41
|
+
* @default 80
|
|
42
|
+
*/
|
|
11
43
|
height?: number;
|
|
44
|
+
/**
|
|
45
|
+
* Additional CSS classes for the container
|
|
46
|
+
*/
|
|
12
47
|
className?: string;
|
|
48
|
+
/**
|
|
49
|
+
* Inline styles for the container
|
|
50
|
+
*/
|
|
13
51
|
style?: React.CSSProperties;
|
|
52
|
+
/**
|
|
53
|
+
* The number of bars to render. Use 'auto' to fit the container width.
|
|
54
|
+
* @default "auto"
|
|
55
|
+
*/
|
|
14
56
|
resolution?: number | 'auto';
|
|
57
|
+
/**
|
|
58
|
+
* The width of each bar in pixels (if resolution is 'auto')
|
|
59
|
+
* @default 2
|
|
60
|
+
*/
|
|
15
61
|
barWidth?: number;
|
|
62
|
+
/**
|
|
63
|
+
* The gap between bars in pixels (if resolution is 'auto')
|
|
64
|
+
* @default 1
|
|
65
|
+
*/
|
|
16
66
|
barGap?: number;
|
|
67
|
+
/**
|
|
68
|
+
* Custom theme configuration
|
|
69
|
+
*/
|
|
17
70
|
theme?: WaveframeTheme;
|
|
18
|
-
|
|
71
|
+
/**
|
|
72
|
+
* Optional WaveframeEngine instance for external control.
|
|
73
|
+
* If provided, the player will sync with this engine instead of creating its own.
|
|
74
|
+
*/
|
|
75
|
+
engine?: WaveframeEngine;
|
|
19
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* The standard "all-in-one" Waveframe player component.
|
|
79
|
+
*
|
|
80
|
+
* This component features a SoundCloud-inspired layout with a prominent
|
|
81
|
+
* play/pause button positioned next to the track metadata.
|
|
82
|
+
*/
|
|
20
83
|
export declare const WaveframePlayer: React.FC<WaveframePlayerProps>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A specialized class for decoding audio data and generating waveform peaks.
|
|
3
|
+
*
|
|
4
|
+
* It leverages the Web Audio API (`AudioContext`) to process audio buffers
|
|
5
|
+
* and extract amplitude data for visualization.
|
|
6
|
+
*/
|
|
7
|
+
export declare class PeakAnalyzer {
|
|
8
|
+
private audioCtx;
|
|
9
|
+
/**
|
|
10
|
+
* Initializes the analyzer. AudioContext creation is deferred to the first use
|
|
11
|
+
* to comply with browser autoplay and resource management policies.
|
|
12
|
+
*/
|
|
13
|
+
constructor();
|
|
14
|
+
/**
|
|
15
|
+
* Lazily creates or returns the existing AudioContext.
|
|
16
|
+
*/
|
|
17
|
+
private getContext;
|
|
18
|
+
/**
|
|
19
|
+
* Processes media (URL or Blob) and generates a set of normalized peaks.
|
|
20
|
+
*
|
|
21
|
+
* @param media The URL string or Blob object to analyze.
|
|
22
|
+
* @param samples The number of peaks (bars) to generate. Defaults to 512.
|
|
23
|
+
* @returns A promise resolving to an array of normalized peak values (0 to 1).
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const analyzer = new PeakAnalyzer();
|
|
28
|
+
*
|
|
29
|
+
* // Analyze from URL
|
|
30
|
+
* const peaksFromUrl = await analyzer.generatePeaks('https://example.com/audio.mp3');
|
|
31
|
+
*
|
|
32
|
+
* // Analyze from Blob
|
|
33
|
+
* const peaksFromBlob = await analyzer.generatePeaks(myAudioBlob);
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
generatePeaks(media: string | Blob, samples?: number): Promise<number[]>;
|
|
37
|
+
/**
|
|
38
|
+
* Closes the AudioContext and releases system audio resources.
|
|
39
|
+
*/
|
|
40
|
+
dispose(): void;
|
|
41
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the low-level playback state of the audio element.
|
|
3
|
+
*/
|
|
4
|
+
export type PlayerState = {
|
|
5
|
+
/** Whether the audio is currently playing */
|
|
6
|
+
isPlaying: boolean;
|
|
7
|
+
/** The current playback time in seconds */
|
|
8
|
+
currentTime: number;
|
|
9
|
+
/** The total duration of the track in seconds */
|
|
10
|
+
duration: number;
|
|
11
|
+
/** The current volume level (0 to 1) */
|
|
12
|
+
volume: number;
|
|
13
|
+
/** Whether the audio is currently muted */
|
|
14
|
+
muted: boolean;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* A callback function that receives the latest PlayerState.
|
|
18
|
+
*/
|
|
19
|
+
export type PlayerListener = (state: PlayerState) => void;
|
|
20
|
+
/**
|
|
21
|
+
* The internal core class responsible for managing the HTMLAudioElement.
|
|
22
|
+
*
|
|
23
|
+
* It handles raw playback logic, volume control, and synchronizes the
|
|
24
|
+
* internal `PlayerState` with DOM events from the underlying `Audio` instance.
|
|
25
|
+
*/
|
|
26
|
+
export declare class PlayerCore {
|
|
27
|
+
private audio;
|
|
28
|
+
private listeners;
|
|
29
|
+
private _state;
|
|
30
|
+
/**
|
|
31
|
+
* Initializes a new PlayerCore instance and sets up event listeners on a new Audio object.
|
|
32
|
+
*/
|
|
33
|
+
constructor();
|
|
34
|
+
/**
|
|
35
|
+
* Subscribes to various HTMLMediaElement events to keep the internal state in sync.
|
|
36
|
+
*/
|
|
37
|
+
private initListeners;
|
|
38
|
+
/**
|
|
39
|
+
* Updates the internal state and notifies subscribers.
|
|
40
|
+
*/
|
|
41
|
+
private updateState;
|
|
42
|
+
/**
|
|
43
|
+
* Triggers all registered listener callbacks.
|
|
44
|
+
*/
|
|
45
|
+
private notify;
|
|
46
|
+
/**
|
|
47
|
+
* Registers a listener for state updates.
|
|
48
|
+
* @param listener The callback function.
|
|
49
|
+
* @returns An unsubscribe function.
|
|
50
|
+
*/
|
|
51
|
+
subscribe(listener: PlayerListener): () => boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Returns the current playback state.
|
|
54
|
+
*/
|
|
55
|
+
get state(): PlayerState;
|
|
56
|
+
/**
|
|
57
|
+
* Updates the source URL of the underlying audio element.
|
|
58
|
+
* @param url The audio source URL.
|
|
59
|
+
*/
|
|
60
|
+
setSource(url: string): void;
|
|
61
|
+
/**
|
|
62
|
+
* Starts playback. Returns a promise that resolves when playback begins.
|
|
63
|
+
*/
|
|
64
|
+
play(): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Pauses playback.
|
|
67
|
+
*/
|
|
68
|
+
pause(): void;
|
|
69
|
+
/**
|
|
70
|
+
* Toggles between play and pause states.
|
|
71
|
+
*/
|
|
72
|
+
togglePlay(): void;
|
|
73
|
+
/**
|
|
74
|
+
* Seeks to a specific time.
|
|
75
|
+
* @param time Time in seconds.
|
|
76
|
+
*/
|
|
77
|
+
seek(time: number): void;
|
|
78
|
+
/**
|
|
79
|
+
* Sets the volume level.
|
|
80
|
+
* @param volume Level from 0 to 1.
|
|
81
|
+
*/
|
|
82
|
+
setVolume(volume: number): void;
|
|
83
|
+
/**
|
|
84
|
+
* Mutes or unmutes the audio element.
|
|
85
|
+
* @param muted Mute status.
|
|
86
|
+
*/
|
|
87
|
+
setMuted(muted: boolean): void;
|
|
88
|
+
/**
|
|
89
|
+
* Cleans up the audio element and removes all listeners.
|
|
90
|
+
*/
|
|
91
|
+
dispose(): void;
|
|
92
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { PlayerState } from './PlayerCore';
|
|
2
|
+
/**
|
|
3
|
+
* Represents the complete state of the Waveframe engine, combining playback and analysis.
|
|
4
|
+
*/
|
|
5
|
+
export type EngineState = PlayerState & {
|
|
6
|
+
/** The current set of generated or provided waveform peaks (0-1 range) */
|
|
7
|
+
peaks: number[];
|
|
8
|
+
/** Whether an audio analysis process is currently in progress */
|
|
9
|
+
isAnalyzing: boolean;
|
|
10
|
+
/** Any error message encountered during playback or analysis */
|
|
11
|
+
error: string | null;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* A callback function that receives the latest EngineState.
|
|
15
|
+
*/
|
|
16
|
+
export type EngineListener = (state: EngineState) => void;
|
|
17
|
+
/**
|
|
18
|
+
* The orchestrator class for Waveframe.
|
|
19
|
+
*
|
|
20
|
+
* It manages the lifecycle of audio playback and waveform analysis, providing a unified
|
|
21
|
+
* store-like interface that can be easily consumed by React or other frameworks.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const engine = new WaveframeEngine();
|
|
26
|
+
*
|
|
27
|
+
* // Load from URL (automatic analysis if peaks omitted)
|
|
28
|
+
* engine.load('https://example.com/audio.mp3');
|
|
29
|
+
*
|
|
30
|
+
* // Load from Blob with pre-computed peaks
|
|
31
|
+
* engine.load(myBlob, [0.1, 0.5, 0.8]);
|
|
32
|
+
*
|
|
33
|
+
* // Subscription
|
|
34
|
+
* const unsubscribe = engine.subscribe((state) => {
|
|
35
|
+
* console.log('Current time:', state.currentTime);
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare class WaveframeEngine {
|
|
40
|
+
private player;
|
|
41
|
+
private analyzer;
|
|
42
|
+
private listeners;
|
|
43
|
+
private _state;
|
|
44
|
+
private _media;
|
|
45
|
+
private _objectUrl;
|
|
46
|
+
/**
|
|
47
|
+
* Creates a new instance of the WaveframeEngine.
|
|
48
|
+
* Initializes internal PlayerCore and PeakAnalyzer.
|
|
49
|
+
*/
|
|
50
|
+
constructor();
|
|
51
|
+
/**
|
|
52
|
+
* Internal method to update the state and notify all subscribers.
|
|
53
|
+
*/
|
|
54
|
+
private updateState;
|
|
55
|
+
/**
|
|
56
|
+
* Notifies all registered listeners of a state change.
|
|
57
|
+
*/
|
|
58
|
+
private notify;
|
|
59
|
+
/**
|
|
60
|
+
* Registers a listener to be called whenever the engine state changes.
|
|
61
|
+
* @param listener The callback function.
|
|
62
|
+
* @returns An unsubscribe function.
|
|
63
|
+
*/
|
|
64
|
+
subscribe(listener: EngineListener): () => boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Returns a snapshot of the current engine state.
|
|
67
|
+
* Useful for `useSyncExternalStore`.
|
|
68
|
+
*/
|
|
69
|
+
getSnapshot(): EngineState;
|
|
70
|
+
/**
|
|
71
|
+
* Revokes any existing Object URLs to prevent memory leaks.
|
|
72
|
+
*/
|
|
73
|
+
private revokeOldSource;
|
|
74
|
+
/**
|
|
75
|
+
* Loads media (URL or Blob) into the player.
|
|
76
|
+
*
|
|
77
|
+
* If a string is passed, it's treated as a URL and used directly for playback.
|
|
78
|
+
* If a Blob is passed, an Object URL is created for playback.
|
|
79
|
+
*
|
|
80
|
+
* If `peaks` are not provided, it automatically triggers an analysis.
|
|
81
|
+
*
|
|
82
|
+
* @param media The audio source (URL string or Blob/File object).
|
|
83
|
+
* @param peaks Optional pre-generated peaks for the waveform.
|
|
84
|
+
*/
|
|
85
|
+
load(media: string | Blob, peaks?: number[]): void;
|
|
86
|
+
/**
|
|
87
|
+
* Analyzes the current media to generate waveform peaks.
|
|
88
|
+
* @param samples The number of peaks to generate. Defaults to 512.
|
|
89
|
+
*/
|
|
90
|
+
analyze(samples?: number): Promise<void>;
|
|
91
|
+
/**
|
|
92
|
+
* Toggles playback between playing and paused.
|
|
93
|
+
*/
|
|
94
|
+
togglePlay(): void;
|
|
95
|
+
/**
|
|
96
|
+
* Starts audio playback.
|
|
97
|
+
*/
|
|
98
|
+
play(): void;
|
|
99
|
+
/**
|
|
100
|
+
* Pauses audio playback.
|
|
101
|
+
*/
|
|
102
|
+
pause(): void;
|
|
103
|
+
/**
|
|
104
|
+
* Seeks to a specific position in the track.
|
|
105
|
+
* @param percentage The seek position as a decimal (0 to 1).
|
|
106
|
+
*/
|
|
107
|
+
seek(percentage: number): void;
|
|
108
|
+
/**
|
|
109
|
+
* Sets the playback volume.
|
|
110
|
+
* @param volume The volume level (0 to 1).
|
|
111
|
+
*/
|
|
112
|
+
setVolume(volume: number): void;
|
|
113
|
+
/**
|
|
114
|
+
* Mutes or unmutes the audio.
|
|
115
|
+
* @param muted Whether the audio should be muted.
|
|
116
|
+
*/
|
|
117
|
+
setMuted(muted: boolean): void;
|
|
118
|
+
/**
|
|
119
|
+
* Disposes of the engine, pausing playback and clearing all listeners and resources.
|
|
120
|
+
*/
|
|
121
|
+
dispose(): void;
|
|
122
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { WaveframeEngine, EngineState } from '../core/WaveframeEngine';
|
|
2
|
+
/**
|
|
3
|
+
* Configuration options for the `useWaveframe` hook.
|
|
4
|
+
*/
|
|
5
|
+
export interface UseWaveframeOptions {
|
|
6
|
+
/** Optional pre-computed peaks to skip automatic analysis */
|
|
7
|
+
peaks?: number[];
|
|
8
|
+
/** Optional external engine instance for shared playback across components */
|
|
9
|
+
engine?: WaveframeEngine;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A headless hook that provides full control over the Waveframe engine.
|
|
13
|
+
*
|
|
14
|
+
* It manages the engine's lifecycle, loads the provided media, and returns
|
|
15
|
+
* the current state along with playback controls.
|
|
16
|
+
*
|
|
17
|
+
* @param media The audio source (URL string or Blob/File object).
|
|
18
|
+
* @param options Additional configuration and an optional external engine.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```tsx
|
|
22
|
+
* const { state, togglePlay, seek } = useWaveframe('https://example.com/audio.mp3');
|
|
23
|
+
*
|
|
24
|
+
* return (
|
|
25
|
+
* <div>
|
|
26
|
+
* <button onClick={togglePlay}>{state.isPlaying ? 'Pause' : 'Play'}</button>
|
|
27
|
+
* <div onClick={(e) => seek(0.5)}>Seek to Middle</div>
|
|
28
|
+
* </div>
|
|
29
|
+
* );
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare const useWaveframe: (media: string | Blob | undefined, options?: UseWaveframeOptions) => {
|
|
33
|
+
/** The current reactive state of the engine */
|
|
34
|
+
state: EngineState;
|
|
35
|
+
/** The raw WaveframeEngine instance for advanced usage */
|
|
36
|
+
engine: WaveframeEngine;
|
|
37
|
+
/** Toggles playback between playing and paused */
|
|
38
|
+
togglePlay: () => void;
|
|
39
|
+
/** Starts audio playback */
|
|
40
|
+
play: () => void;
|
|
41
|
+
/** Pauses audio playback */
|
|
42
|
+
pause: () => void;
|
|
43
|
+
/** Seeks to a specific percentage (0-1) */
|
|
44
|
+
seek: (percentage: number) => void;
|
|
45
|
+
/** Sets the playback volume (0-1) */
|
|
46
|
+
setVolume: (v: number) => void;
|
|
47
|
+
/** Mutes or unmutes the audio */
|
|
48
|
+
setMuted: (m: boolean) => void;
|
|
49
|
+
/** Manually triggers a re-analysis of the current media */
|
|
50
|
+
analyze: (samples?: number) => Promise<void>;
|
|
51
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { WaveframeEngine, EngineState } from '../core/WaveframeEngine';
|
|
2
|
+
/**
|
|
3
|
+
* A React hook that synchronizes a WaveframeEngine's state with a React component.
|
|
4
|
+
*
|
|
5
|
+
* It uses `useSyncExternalStore` for high-performance updates, ensuring that
|
|
6
|
+
* the component only re-renders when the engine's state snapshot actually changes.
|
|
7
|
+
*
|
|
8
|
+
* @param engine The WaveframeEngine instance to subscribe to.
|
|
9
|
+
* @returns The current EngineState (isPlaying, currentTime, peaks, etc.).
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* const MyPlayer = ({ engine }: { engine: WaveframeEngine }) => {
|
|
14
|
+
* const { isPlaying, currentTime, duration } = useWaveframeStore(engine);
|
|
15
|
+
*
|
|
16
|
+
* return (
|
|
17
|
+
* <div>
|
|
18
|
+
* <button onClick={() => engine.togglePlay()}>
|
|
19
|
+
* {isPlaying ? 'Pause' : 'Play'}
|
|
20
|
+
* </button>
|
|
21
|
+
* <p>{currentTime.toFixed(2)} / {duration.toFixed(2)}</p>
|
|
22
|
+
* </div>
|
|
23
|
+
* );
|
|
24
|
+
* };
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare const useWaveframeStore: (engine: WaveframeEngine) => EngineState;
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import { default as React } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Props for the ArtworkOverlay component.
|
|
4
|
+
*/
|
|
2
5
|
interface ArtworkOverlayProps {
|
|
6
|
+
/** The URL or Object URL of the artwork image */
|
|
3
7
|
artworkUrl?: string;
|
|
8
|
+
/** The title of the track (used for alt text) */
|
|
4
9
|
title?: string;
|
|
5
|
-
|
|
6
|
-
onToggle: (e: React.MouseEvent) => void;
|
|
10
|
+
/** Whether the artwork is currently being processed or the audio is analyzing */
|
|
7
11
|
isLoading?: boolean;
|
|
8
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* A purely visual component for displaying track artwork.
|
|
15
|
+
*
|
|
16
|
+
* It handles loading states with a blur effect and provides a consistent
|
|
17
|
+
* container for the track image.
|
|
18
|
+
*/
|
|
9
19
|
export declare const ArtworkOverlay: React.FC<ArtworkOverlayProps>;
|
|
10
20
|
export {};
|
|
@@ -5,7 +5,12 @@ interface SettingsPanelProps {
|
|
|
5
5
|
trackInfo: TrackInfo;
|
|
6
6
|
config: WaveformConfig;
|
|
7
7
|
scale: number;
|
|
8
|
-
|
|
8
|
+
engineState: {
|
|
9
|
+
isPlaying: boolean;
|
|
10
|
+
volume: number;
|
|
11
|
+
muted: boolean;
|
|
12
|
+
isAnalyzing: boolean;
|
|
13
|
+
};
|
|
9
14
|
onAnalyze: () => void;
|
|
10
15
|
onThemeChange: (theme: Partial<WaveframeTheme>) => void;
|
|
11
16
|
onTrackChange: (track: Partial<TrackInfo>) => void;
|
|
@@ -13,6 +18,12 @@ interface SettingsPanelProps {
|
|
|
13
18
|
onScaleChange: (scale: number) => void;
|
|
14
19
|
onTogglePreset: (type: 'light' | 'dark') => void;
|
|
15
20
|
onClearPeaks: () => void;
|
|
21
|
+
onFileUpload: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
|
22
|
+
onArtworkUpload: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
|
23
|
+
onReset: () => void;
|
|
24
|
+
onTogglePlay: () => void;
|
|
25
|
+
onSetVolume: (v: number) => void;
|
|
26
|
+
onSetMuted: (m: boolean) => void;
|
|
16
27
|
}
|
|
17
28
|
export declare const SettingsPanel: React.FC<SettingsPanelProps>;
|
|
18
29
|
export {};
|
|
@@ -1,19 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Defines the core color palette for the Waveframe component.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```tsx
|
|
6
|
+
* const darkTheme: WaveframeTheme = {
|
|
7
|
+
* bg: '#111827',
|
|
8
|
+
* primary: '#ec4899',
|
|
9
|
+
* text: '#f9fafb',
|
|
10
|
+
* border: '#1f2937'
|
|
11
|
+
* };
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
1
14
|
export interface WaveframeTheme {
|
|
15
|
+
/** The main background color of the player */
|
|
2
16
|
bg: string;
|
|
17
|
+
/** The primary accent color (used for play button and progress) */
|
|
3
18
|
primary: string;
|
|
19
|
+
/** The color of the text elements (title, artist, time) */
|
|
4
20
|
text: string;
|
|
21
|
+
/** The color of borders and dividers */
|
|
5
22
|
border: string;
|
|
6
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Metadata associated with an audio track.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```tsx
|
|
29
|
+
* const track: TrackInfo = {
|
|
30
|
+
* title: 'Electronic Sunset',
|
|
31
|
+
* artist: 'Digital Nomad',
|
|
32
|
+
* artwork: 'https://example.com/art.jpg', // or a Blob object
|
|
33
|
+
* audioUrl: 'https://example.com/audio.mp3'
|
|
34
|
+
* };
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
7
37
|
export interface TrackInfo {
|
|
38
|
+
/** The title of the track */
|
|
8
39
|
title: string;
|
|
40
|
+
/** The artist of the track */
|
|
9
41
|
artist: string;
|
|
10
|
-
|
|
42
|
+
/** The artwork source (URL string or Blob/File object) */
|
|
43
|
+
artwork: string | Blob;
|
|
44
|
+
/** The URL to the actual audio file */
|
|
11
45
|
audioUrl: string;
|
|
12
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Defines the resolution of the waveform.
|
|
49
|
+
* - `number`: A fixed number of bars to render.
|
|
50
|
+
* - `'auto'`: Compute the number of bars dynamically based on the container width.
|
|
51
|
+
*/
|
|
13
52
|
export type Resolution = number | 'auto';
|
|
53
|
+
/**
|
|
54
|
+
* Configuration options for how the waveform is rendered visually.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```tsx
|
|
58
|
+
* const config: WaveformConfig = {
|
|
59
|
+
* resolution: 'auto',
|
|
60
|
+
* barWidth: 2,
|
|
61
|
+
* barGap: 1,
|
|
62
|
+
* height: 100
|
|
63
|
+
* };
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
14
66
|
export interface WaveformConfig {
|
|
67
|
+
/** The number of bars to render, or 'auto' to compute based on container width */
|
|
15
68
|
resolution: Resolution;
|
|
69
|
+
/** The width of each individual bar in pixels */
|
|
16
70
|
barWidth: number;
|
|
71
|
+
/** The spacing between each bar in pixels */
|
|
17
72
|
barGap: number;
|
|
73
|
+
/** The total height of the waveform in pixels */
|
|
18
74
|
height: number;
|
|
19
75
|
}
|
|
@@ -1,15 +1,33 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
2
|
+
* Loads audio from a URL, decodes it, and generates a specific number of peaks (samples).
|
|
3
|
+
*
|
|
4
|
+
* This is a high-level utility function that internally manages a `PeakAnalyzer` instance.
|
|
5
|
+
*
|
|
6
|
+
* @param audioUrl The URL of the audio file to analyze.
|
|
7
|
+
* @param samples The number of peaks (bars) to generate. Defaults to 512.
|
|
8
|
+
* @returns A promise resolving to an array of normalized peak values (0 to 1).
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const peaks = await generatePeaks('https://example.com/audio.mp3', 256);
|
|
13
|
+
* ```
|
|
6
14
|
*/
|
|
7
15
|
export declare const generatePeaks: (audioUrl: string, samples?: number) => Promise<number[]>;
|
|
8
16
|
/**
|
|
9
|
-
* Loads audio into memory as a Blob and returns a temporary Object URL
|
|
17
|
+
* Loads audio into memory as a Blob and returns a temporary Object URL.
|
|
18
|
+
*
|
|
19
|
+
* Useful for ensuring audio data is fully loaded locally before starting
|
|
20
|
+
* playback or analysis, which can help with CORS issues or slow networks.
|
|
21
|
+
*
|
|
22
|
+
* @param url The URL of the remote audio file.
|
|
23
|
+
* @returns A promise resolving to a temporary `blob:` URL.
|
|
10
24
|
*/
|
|
11
25
|
export declare const loadAudioToMemory: (url: string) => Promise<string>;
|
|
12
26
|
/**
|
|
13
|
-
* Cleanup function to prevent memory leaks from Object URLs
|
|
27
|
+
* Cleanup function to prevent memory leaks from Object URLs.
|
|
28
|
+
*
|
|
29
|
+
* Call this when a `blob:` URL is no longer needed (e.g., when the component unmounts).
|
|
30
|
+
*
|
|
31
|
+
* @param url The Object URL to revoke.
|
|
14
32
|
*/
|
|
15
33
|
export declare const revokeAudioMemory: (url: string) => void;
|