-
Notifications
You must be signed in to change notification settings - Fork 103
/
waveform-data.d.ts
217 lines (153 loc) · 4.51 KB
/
waveform-data.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
declare module 'waveform-data' {
interface Options {
/**
* Number of audio samples per pixel. Default: 512
*/
scale?: number;
/**
* Number of bits per sample (8 or 16). Default: 8
*/
bits?: number;
/**
* Amplitude scale. Default: 1.0
*/
amplitude_scale?: number;
/**
* Create a single or multi-channel waveform. Default: false
*/
split_channels?: boolean;
}
export interface WaveformDataAudioContextOptions extends Options {
/**
* The Web Audio AudioContext to use
*/
audio_context: AudioContext;
/**
* An ArrayBuffer containing raw audio data (e.g., in WAV or MP3 format)
*/
array_buffer: ArrayBuffer;
}
export interface WaveformDataAudioBufferOptions extends Options {
/**
* A Web Audio AudioBuffer containing decoded audio data
*/
audio_buffer: AudioBuffer
}
export type WaveformDataFromAudioCallback = (
error: DOMException,
waveformData: WaveformData,
audioBuffer: AudioBuffer
) => void;
export interface JsonWaveformData {
version: number;
channels: number;
sample_rate: number;
samples_per_pixel: number;
bits: number;
length: number;
data: Array<number>;
}
export interface WaveformDataChannel {
/**
* Returns the waveform minimum at the given index position
*/
min_sample: (index: number) => number;
/**
* Returns the waveform minimum at the given index position
*/
max_sample: (index: number) => number;
/**
* Returns all the waveform minimum values as an array
*/
min_array: () => Array<number>;
/**
* Returns all the waveform maximum values as an array
*/
max_array: () => Array<number>;
}
export class WaveformData {
/**
* Creates and returns a WaveformData instance from the given data,
* which may be in binary (.dat) format in an ArrayBuffer, or a
* JavaScript object
*/
static create: (data: ArrayBuffer | JsonWaveformData) => WaveformData;
/**
* Creates a WaveformData object from audio using the Web Audio API
*/
static createFromAudio: (
options: WaveformDataAudioContextOptions | WaveformDataAudioBufferOptions,
callback: WaveformDataFromAudioCallback
) => void;
/**
* Returns the sample rate of the original audio, in Hz
*/
readonly sample_rate: number;
/**
* Returns the number of audio samples per pixel of the waveform data
*/
readonly scale: number;
/**
* Returns the amount of time (in seconds) represented by a single pixel
*/
readonly seconds_per_pixel: number;
/**
* Returns the number of pixels per second
*/
readonly pixels_per_second: number;
/**
* Returns the length of the waveform data, in pixels
*/
readonly length: number;
/**
* Returns the number of bits per sample, either 8 or 16
*/
readonly bits: number;
/**
* Returns the approximate duration of the audio file, in seconds.
*
* The duration is approximate because it is calculated based on the
* waveform length, number of samples per pixel, and audio sample rate
*/
readonly duration: number;
/**
* Returns the pixel index for a given time
*/
at_time: (time: number) => number;
/**
* Returns the time in seconds for a given pixel index
*/
time: (time: number) => number;
/**
* Returns the number of waveform channels
*/
readonly channels: number;
/**
* Returns a WaveformDataChannel object that provides access to the
* waveform data for the given channel index
*/
channel: (index: number) => WaveformDataChannel;
/**
* Creates and returns a new WaveformData object with resampled data.
* Use this method to create waveform data at different zoom levels
*/
resample: (
options: { width: number } | { scale: number }
) => WaveformData;
/**
* Concatenates the receiver with one or more other waveforms, returning
* a new WaveformData object. The waveforms must be compatible, i.e.,
* have the same sample rate, scale, and number of channels
*/
concat: (...args: Array<WaveformData>) => WaveformData;
/**
* Returns the waveform data as an object
*/
toJSON: () => JsonWaveformData;
/**
* Returns the waveform data in binary format as an ArrayBuffer
*/
toArrayBuffer: () => ArrayBuffer;
}
export default WaveformData;
}