forked from bbc/peaks.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peaks.js.d.ts
332 lines (301 loc) · 10 KB
/
peaks.js.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* Peaks.js TypeScript Definitions
* @author Evan Louie <[email protected]> (https://evanlouie.com)
*/
declare module 'peaks.js' {
interface SegmentAddOptions {
startTime: number;
endTime: number;
editable?: boolean;
color?: string;
labelText?: string;
id?: string;
}
interface SegmentUpdateOptions {
startTime?: number;
endTime?: number;
editable?: boolean;
color?: string;
labelText?: string;
}
interface Segment extends SegmentAddOptions {
update: (options: SegmentUpdateOptions) => void;
}
interface PointAddOptions {
time: number;
editable?: boolean;
color?: string;
labelText?: string;
id?: string;
}
interface PointUpdateOptions {
time?: number;
editable?: boolean;
color?: string;
labelText?: string;
}
interface Point extends PointAddOptions {
update: (options: PointUpdateOptions) => void;
}
interface RequiredOptions {
/** HTML5 Media element containing an audio track */
mediaElement: Element;
}
interface SingleContainerOptions {
/** Container element for the waveform views */
container: HTMLElement;
}
interface ViewContainerOptions {
containers: {
/** Container element for the overview (non-zoomable) waveform view */
overview?: HTMLElement | null;
/** Container element for the zoomable waveform view */
zoomview?: HTMLElement | null;
}
}
type ContainerOptions = SingleContainerOptions | ViewContainerOptions;
interface PreGeneratedWaveformOptions {
/** URI to waveform data file in binary or JSON */
dataUri?: {
arraybuffer?: string;
json?: string;
}
/** raw waveform data file in binary or JSON */
waveformData?: {
arraybuffer?: ArrayBuffer;
json?: JsonWaveformData;
}
}
interface WebAudioOptions {
webAudio: {
/**
* A Web Audio AudioContext instance which can be used
* to render the waveform if dataUri is not provided
*/
audioContext?: AudioContext;
/**
* Alternatively, provide an AudioBuffer containing the decoded audio
* samples. In this case, an AudioContext is not needed
*/
audioBuffer?: AudioBuffer;
scale?: number;
multiChannel?: boolean;
}
}
type AudioOptions = WebAudioOptions | PreGeneratedWaveformOptions;
interface PointMarker {
init: (group: object) => void; // TODO: group: Konva.Group
fitToView: () => void;
timeUpdated?: (time: number) => void;
destroy?: () => void;
}
interface SegmentMarker {
init: (group: object) => void; // TODO: group: Konva.Group
fitToView: () => void;
timeUpdated?: (time: number) => void;
destroy?: () => void;
}
interface Layer {
getHeight: () => number;
draw: () => void;
}
interface CreatePointMarkerOptions {
point: Point;
view: string;
draggable: boolean;
color: string;
layer: Layer;
}
interface CreateSegmentMarkerOptions {
segment: Segment;
view: string;
draggable: boolean;
color: string;
layer: Layer;
startMarker: boolean;
}
interface CreateSegmentLabelOptions {
segment: Segment;
view: string;
layer: Layer;
}
interface OptionalOptions {
/**
* If true, peaks will send credentials with all network requests
* - i.e. when fetching waveform data.
*/
withCredentials?: boolean;
/** async logging function */
logger?: (...args: any[]) => void;
/** default height of the waveform canvases in pixels */
height?: number;
/** Array of zoom levels in samples per pixel (big >> small) */
zoomLevels?: number[];
/** Bind keyboard controls */
keyboard?: boolean;
/** Keyboard nudge increment in seconds (left arrow/right arrow) */
nudgeIncrement?: number;
/** Color for segment start marker handles */
segmentStartMarkerColor?: string;
/** Color for segment end marker handles */
segmentEndMarkerColor?: string;
/** Color for the zoomed in waveform */
zoomWaveformColor?: string;
/** Color for the overview waveform */
overviewWaveformColor?: string;
/**
* Color for the overview waveform rectangle
* that shows what the zoom view shows
*/
overviewHighlightColor?: string;
/**
* The default number of pixels from the top and bottom of the canvas
* that the overviewHighlight takes up
*/
overviewHighlightOffset?: number;
/** Color for segments on the waveform */
segmentColor?: string;
/** Color of the play head */
playheadColor?: string;
/** Color of the play head text */
playheadTextColor?: string;
/**
* Show current time next to the play head
* - (zoom view only)
*/
showPlayheadTime?: boolean;
/** The color of a point marker */
pointMarkerColor?: string;
/** Color of the axis gridlines */
axisGridlineColor?: string;
/** Color of the axis labels */
axisLabelColor?: string;
/** Random color per segment (overrides segmentColor) */
randomizeSegmentColor?: boolean;
/**
* Zoom view adapter to use.
* - Valid adapters are: `'animated'` (default) and `'static'`
*/
zoomAdapter?: string;
/** Array of initial segment objects */
segments?: Segment[];
/** Array of initial point objects */
points?: Point[];
/** Emit cue events when playing */
emitCueEvents?: boolean;
/** Custom segment marker factory function */
createSegmentMarker?: (options: CreateSegmentMarkerOptions) => SegmentMarker;
/** Custom segment label factory function */
createSegmentLabel?: (options: CreateSegmentLabelOptions) => object; // Konva.Node;
/** Custom point marker factory function */
createPointMarker?: (options: CreatePointMarkerOptions) => PointMarker;
}
interface SetSourceRequiredOptions {
mediaUrl: string;
withCredentials?: boolean;
}
type SetSourceOptions = SetSourceRequiredOptions & AudioOptions;
type SetSourceCallback = (error: Error) => void;
interface InstanceEvents {
'peaks.ready': () => void;
'points.add': (points: Point[]) => void;
'points.dblclick': (point: Point) => void;
'points.dragend': (point: Point) => void;
'points.dragmove': (point: Point) => void;
'points.dragstart': (point: Point) => void;
'points.mouseenter': (point: Point) => void;
'points.mouseleave': (point: Point) => void;
'points.remove_all': () => void;
'points.remove': (points: Point[]) => void;
'points.enter': (point: Point) => void;
'segments.add': (segments: Segment[]) => void;
'segments.dragstart': (segment: Segment, startMarker: boolean) => void;
'segments.dragged': (segment: Segment, startMarker: boolean) => void;
'segments.dragend': (segment: Segment, startMarker: boolean) => void;
'segments.remove_all': () => void;
'segments.remove': (segments: Segment[]) => void;
'segments.mouseenter': (segment: Segment) => void;
'segments.mouseleave': (segment: Segment) => void;
'segments.click': (segment: Segment) => void;
'segments.enter': (segment: Segment) => void;
'segments.exit': (segment: Segment) => void;
'overview.dblclick': (time: number) => void;
'zoomview.dblclick': (time: number) => void;
'zoom.update': (currentZoomLevel: number, previousZoomLevel: number) => void;
player_seek: (time: number) => void;
user_seek: (time: number) => void;
}
interface WaveformView {
setAmplitudeScale: (scale: number) => void;
setWaveformColor: (color: string) => void;
showPlayheadTime: (show: boolean) => void;
enableAutoScroll: (enable: boolean) => void;
enableMarkerEditing: (enable: boolean) => void;
fitToContainer: () => void;
setZoom: (options: XOR<{ scale: number | 'auto' }, { seconds: number | 'auto' }>) => void;
setStartTime: (time: number) => void;
}
type Without<T> = { [K in keyof T]?: undefined };
type XOR<T, U> = (Without<T> & U) | (T & Without<U>);
interface PeaksInstance {
setSource: (options: SetSourceOptions, callback: SetSourceCallback) => void;
destroy: () => void;
/** Player API */
player: {
play: () => void;
pause: () => void;
getCurrentTime: () => number;
getDuration: () => number;
seek: (time: number) => void;
playSegment: (segment: Segment) => void;
};
/** Views API */
views: {
createOverview: (container: HTMLElement) => WaveformView;
createZoomview: (container: HTMLElement) => WaveformView;
destroyOverview: () => void;
destroyZoomview: () => void;
getView: (name?: 'overview' | 'zoomview' ) => WaveformView | null;
};
/** Zoom API */
zoom: {
zoomOut: () => void;
zoomIn: () => void;
setZoom: (index: number) => void;
getZoom: () => number;
};
/** Segments API */
segments: {
add: (segments: SegmentAddOptions | SegmentAddOptions[]) => void;
getSegments: () => Segment[];
getSegment: (id: string) => Segment | null;
removeByTime: (startTime: number, endTime?: number) => Segment[];
removeById: (segmentId: string) => Segment[];
removeAll: () => void;
};
/** Points API */
points: {
add: (points: PointAddOptions | PointAddOptions[]) => void;
getPoints: () => Point[];
getPoint: (id: string) => Point | null;
removeByTime: (time: number) => Point[];
removeById: (id: string) => Point[];
removeAll: () => void;
};
/** Events */
on: <E extends keyof InstanceEvents>(event: E, listener: InstanceEvents[E]) => void;
}
type PeaksInitCallback = (error: Error, peaks?: PeaksInstance) => void;
interface PeaksOptionsWithoutAudioOptions extends RequiredOptions, OptionalOptions {}
export interface JsonWaveformData {
version: number;
channels: number;
sample_rate: number;
samples_per_pixel: number;
bits: number;
length: number;
data: number[];
}
export type PeaksOptions = PeaksOptionsWithoutAudioOptions & AudioOptions & ContainerOptions;
export function init(options: PeaksOptions, callback?: PeaksInitCallback): PeaksInstance;
}