@checksub_team/peaks_timeline 1.4.17

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/peaks.js.d.ts ADDED
@@ -0,0 +1,332 @@
1
+ /**
2
+ * Peaks.js TypeScript Definitions
3
+ * @author Evan Louie <evan.louie@microsoft.com> (https://evanlouie.com)
4
+ */
5
+
6
+ declare module 'peaks.js' {
7
+ interface SegmentAddOptions {
8
+ startTime: number;
9
+ endTime: number;
10
+ editable?: boolean;
11
+ color?: string;
12
+ labelText?: string;
13
+ id?: string;
14
+ }
15
+
16
+ interface SegmentUpdateOptions {
17
+ startTime?: number;
18
+ endTime?: number;
19
+ editable?: boolean;
20
+ color?: string;
21
+ labelText?: string;
22
+ }
23
+
24
+ interface Segment extends SegmentAddOptions {
25
+ update: (options: SegmentUpdateOptions) => void;
26
+ }
27
+
28
+ interface PointAddOptions {
29
+ time: number;
30
+ editable?: boolean;
31
+ color?: string;
32
+ labelText?: string;
33
+ id?: string;
34
+ }
35
+
36
+ interface PointUpdateOptions {
37
+ time?: number;
38
+ editable?: boolean;
39
+ color?: string;
40
+ labelText?: string;
41
+ }
42
+
43
+ interface Point extends PointAddOptions {
44
+ update: (options: PointUpdateOptions) => void;
45
+ }
46
+
47
+ interface RequiredOptions {
48
+ /** HTML5 Media element containing an audio track */
49
+ mediaElement: Element;
50
+ }
51
+
52
+ interface SingleContainerOptions {
53
+ /** Container element for the waveform views */
54
+ container: HTMLElement;
55
+ }
56
+
57
+ interface ViewContainerOptions {
58
+ containers: {
59
+ /** Container element for the overview (non-zoomable) waveform view */
60
+ overview?: HTMLElement | null;
61
+ /** Container element for the zoomable waveform view */
62
+ zoomview?: HTMLElement | null;
63
+ }
64
+ }
65
+
66
+ type ContainerOptions = SingleContainerOptions | ViewContainerOptions;
67
+
68
+ interface PreGeneratedWaveformOptions {
69
+ /** URI to waveform data file in binary or JSON */
70
+ dataUri?: {
71
+ arraybuffer?: string;
72
+ json?: string;
73
+ }
74
+ /** raw waveform data file in binary or JSON */
75
+ waveformData?: {
76
+ arraybuffer?: ArrayBuffer;
77
+ json?: JsonWaveformData;
78
+ }
79
+ }
80
+
81
+ interface WebAudioOptions {
82
+ webAudio: {
83
+ /**
84
+ * A Web Audio AudioContext instance which can be used
85
+ * to render the waveform if dataUri is not provided
86
+ */
87
+ audioContext?: AudioContext;
88
+ /**
89
+ * Alternatively, provide an AudioBuffer containing the decoded audio
90
+ * samples. In this case, an AudioContext is not needed
91
+ */
92
+ audioBuffer?: AudioBuffer;
93
+ scale?: number;
94
+ multiChannel?: boolean;
95
+ }
96
+ }
97
+
98
+ type AudioOptions = WebAudioOptions | PreGeneratedWaveformOptions;
99
+
100
+ interface PointMarker {
101
+ init: (group: object) => void; // TODO: group: Konva.Group
102
+ fitToView: () => void;
103
+ timeUpdated?: (time: number) => void;
104
+ destroy?: () => void;
105
+ }
106
+
107
+ interface SegmentMarker {
108
+ init: (group: object) => void; // TODO: group: Konva.Group
109
+ fitToView: () => void;
110
+ timeUpdated?: (time: number) => void;
111
+ destroy?: () => void;
112
+ }
113
+
114
+ interface Layer {
115
+ getHeight: () => number;
116
+ draw: () => void;
117
+ }
118
+
119
+ interface CreatePointMarkerOptions {
120
+ point: Point;
121
+ view: string;
122
+ draggable: boolean;
123
+ color: string;
124
+ layer: Layer;
125
+ }
126
+
127
+ interface CreateSegmentMarkerOptions {
128
+ segment: Segment;
129
+ view: string;
130
+ draggable: boolean;
131
+ color: string;
132
+ layer: Layer;
133
+ startMarker: boolean;
134
+ }
135
+
136
+ interface CreateSegmentLabelOptions {
137
+ segment: Segment;
138
+ view: string;
139
+ layer: Layer;
140
+ }
141
+
142
+ interface OptionalOptions {
143
+ /**
144
+ * If true, peaks will send credentials with all network requests
145
+ * - i.e. when fetching waveform data.
146
+ */
147
+ withCredentials?: boolean;
148
+ /** async logging function */
149
+ logger?: (...args: any[]) => void;
150
+ /** default height of the waveform canvases in pixels */
151
+ height?: number;
152
+ /** Array of zoom levels in samples per pixel (big >> small) */
153
+ zoomLevels?: number[];
154
+ /** Bind keyboard controls */
155
+ keyboard?: boolean;
156
+ /** Keyboard nudge increment in seconds (left arrow/right arrow) */
157
+ nudgeIncrement?: number;
158
+ /** Color for segment start marker handles */
159
+ segmentStartMarkerColor?: string;
160
+ /** Color for segment end marker handles */
161
+ segmentEndMarkerColor?: string;
162
+ /** Color for the zoomed in waveform */
163
+ zoomWaveformColor?: string;
164
+ /** Color for the overview waveform */
165
+ overviewWaveformColor?: string;
166
+ /**
167
+ * Color for the overview waveform rectangle
168
+ * that shows what the zoom view shows
169
+ */
170
+ overviewHighlightColor?: string;
171
+ /**
172
+ * The default number of pixels from the top and bottom of the canvas
173
+ * that the overviewHighlight takes up
174
+ */
175
+ overviewHighlightOffset?: number;
176
+ /** Color for segments on the waveform */
177
+ segmentColor?: string;
178
+ /** Color of the play head */
179
+ playheadColor?: string;
180
+ /** Color of the play head text */
181
+ playheadTextColor?: string;
182
+ /**
183
+ * Show current time next to the play head
184
+ * - (zoom view only)
185
+ */
186
+ showPlayheadTime?: boolean;
187
+ /** The color of a point marker */
188
+ pointMarkerColor?: string;
189
+ /** Color of the axis gridlines */
190
+ axisGridlineColor?: string;
191
+ /** Color of the axis labels */
192
+ axisLabelColor?: string;
193
+ /** Random color per segment (overrides segmentColor) */
194
+ randomizeSegmentColor?: boolean;
195
+ /**
196
+ * Zoom view adapter to use.
197
+ * - Valid adapters are: `'animated'` (default) and `'static'`
198
+ */
199
+ zoomAdapter?: string;
200
+ /** Array of initial segment objects */
201
+ segments?: Segment[];
202
+ /** Array of initial point objects */
203
+ points?: Point[];
204
+ /** Emit cue events when playing */
205
+ emitCueEvents?: boolean;
206
+ /** Custom segment marker factory function */
207
+ createSegmentMarker?: (options: CreateSegmentMarkerOptions) => SegmentMarker;
208
+ /** Custom segment label factory function */
209
+ createSegmentLabel?: (options: CreateSegmentLabelOptions) => object; // Konva.Node;
210
+ /** Custom point marker factory function */
211
+ createPointMarker?: (options: CreatePointMarkerOptions) => PointMarker;
212
+ }
213
+
214
+ interface SetSourceRequiredOptions {
215
+ mediaUrl: string;
216
+ withCredentials?: boolean;
217
+ }
218
+
219
+ type SetSourceOptions = SetSourceRequiredOptions & AudioOptions;
220
+
221
+ type SetSourceCallback = (error: Error) => void;
222
+
223
+ interface InstanceEvents {
224
+ 'peaks.ready': () => void;
225
+ 'points.add': (points: Point[]) => void;
226
+ 'points.dblclick': (point: Point) => void;
227
+ 'points.dragend': (point: Point) => void;
228
+ 'points.dragmove': (point: Point) => void;
229
+ 'points.dragstart': (point: Point) => void;
230
+ 'points.mouseenter': (point: Point) => void;
231
+ 'points.mouseleave': (point: Point) => void;
232
+ 'points.remove_all': () => void;
233
+ 'points.remove': (points: Point[]) => void;
234
+ 'points.enter': (point: Point) => void;
235
+ 'segments.add': (segments: Segment[]) => void;
236
+ 'segments.dragstart': (segment: Segment, startMarker: boolean) => void;
237
+ 'segments.dragged': (segment: Segment, startMarker: boolean) => void;
238
+ 'segments.dragend': (segment: Segment, startMarker: boolean) => void;
239
+ 'segments.remove_all': () => void;
240
+ 'segments.remove': (segments: Segment[]) => void;
241
+ 'segments.mouseenter': (segment: Segment) => void;
242
+ 'segments.mouseleave': (segment: Segment) => void;
243
+ 'segments.click': (segment: Segment) => void;
244
+ 'segments.enter': (segment: Segment) => void;
245
+ 'segments.exit': (segment: Segment) => void;
246
+ 'overview.dblclick': (time: number) => void;
247
+ 'zoomview.dblclick': (time: number) => void;
248
+ 'zoom.update': (currentZoomLevel: number, previousZoomLevel: number) => void;
249
+ player_seek: (time: number) => void;
250
+ user_seek: (time: number) => void;
251
+ }
252
+
253
+ interface WaveformView {
254
+ setAmplitudeScale: (scale: number) => void;
255
+ setWaveformColor: (color: string) => void;
256
+ showPlayheadTime: (show: boolean) => void;
257
+ enableAutoScroll: (enable: boolean) => void;
258
+ enableMarkerEditing: (enable: boolean) => void;
259
+ fitToContainer: () => void;
260
+ setZoom: (options: XOR<{ scale: number | 'auto' }, { seconds: number | 'auto' }>) => void;
261
+ setStartTime: (time: number) => void;
262
+ }
263
+
264
+ type Without<T> = { [K in keyof T]?: undefined };
265
+ type XOR<T, U> = (Without<T> & U) | (T & Without<U>);
266
+
267
+ interface PeaksInstance {
268
+ setSource: (options: SetSourceOptions, callback: SetSourceCallback) => void;
269
+ destroy: () => void;
270
+ /** Player API */
271
+ player: {
272
+ play: () => void;
273
+ pause: () => void;
274
+ getCurrentTime: () => number;
275
+ getDuration: () => number;
276
+ seek: (time: number) => void;
277
+ playSegment: (segment: Segment) => void;
278
+ };
279
+ /** Views API */
280
+ views: {
281
+ createOverview: (container: HTMLElement) => WaveformView;
282
+ createZoomview: (container: HTMLElement) => WaveformView;
283
+ destroyOverview: () => void;
284
+ destroyZoomview: () => void;
285
+ getView: (name?: 'overview' | 'zoomview' ) => WaveformView | null;
286
+ };
287
+ /** Zoom API */
288
+ zoom: {
289
+ zoomOut: () => void;
290
+ zoomIn: () => void;
291
+ setZoom: (index: number) => void;
292
+ getZoom: () => number;
293
+ };
294
+ /** Segments API */
295
+ segments: {
296
+ add: (segments: SegmentAddOptions | SegmentAddOptions[]) => void;
297
+ getSegments: () => Segment[];
298
+ getSegment: (id: string) => Segment | null;
299
+ removeByTime: (startTime: number, endTime?: number) => Segment[];
300
+ removeById: (segmentId: string) => Segment[];
301
+ removeAll: () => void;
302
+ };
303
+ /** Points API */
304
+ points: {
305
+ add: (points: PointAddOptions | PointAddOptions[]) => void;
306
+ getPoints: () => Point[];
307
+ getPoint: (id: string) => Point | null;
308
+ removeByTime: (time: number) => Point[];
309
+ removeById: (id: string) => Point[];
310
+ removeAll: () => void;
311
+ };
312
+ /** Events */
313
+ on: <E extends keyof InstanceEvents>(event: E, listener: InstanceEvents[E]) => void;
314
+ }
315
+
316
+ type PeaksInitCallback = (error: Error, peaks?: PeaksInstance) => void;
317
+
318
+ interface PeaksOptionsWithoutAudioOptions extends RequiredOptions, OptionalOptions {}
319
+
320
+ export interface JsonWaveformData {
321
+ version: number;
322
+ channels: number;
323
+ sample_rate: number;
324
+ samples_per_pixel: number;
325
+ bits: number;
326
+ length: number;
327
+ data: number[];
328
+ }
329
+
330
+ export type PeaksOptions = PeaksOptionsWithoutAudioOptions & AudioOptions & ContainerOptions;
331
+ export function init(options: PeaksOptions, callback?: PeaksInitCallback): PeaksInstance;
332
+ }
@@ -0,0 +1,90 @@
1
+ /**
2
+ * @file
3
+ *
4
+ * Retrieving functions for external or internal media data.
5
+ *
6
+ * @module data-retriever
7
+ */
8
+
9
+ define([
10
+ './data'
11
+ ], function(Data) {
12
+ 'use strict';
13
+
14
+ /**
15
+ * Creates a DataRetriever that will look for and download sources data.
16
+ *
17
+ * @class
18
+ * @alias DataRetriever
19
+ *
20
+ * @param {Peaks} peaks
21
+ */
22
+
23
+ function DataRetriever(peaks) {
24
+ this._peaks = peaks;
25
+ this._data = {};
26
+ this._waitingForData = {};
27
+ }
28
+
29
+ /**
30
+ * Retrieves data for the given source.
31
+ *
32
+ * @param {Source} options
33
+ */
34
+
35
+ DataRetriever.prototype.retrieveData = function(source) {
36
+ this._retrieveDataByUrl(source, source.previewUrl);
37
+ this._retrieveDataByUrl(source, source.binaryUrl);
38
+ };
39
+
40
+ DataRetriever.prototype._retrieveDataByUrl = function(source, url) {
41
+ if (url && url !== '') {
42
+ if (!this._data[url]) {
43
+ this._retireveDataOnline(source, url);
44
+ }
45
+ else {
46
+ if (this._data[url].isComplete()) {
47
+ this._peaks.emit('data.retrieved', this._data[url], source, url);
48
+ }
49
+ else {
50
+ this._waitingForData[url].push(source);
51
+ }
52
+ }
53
+ }
54
+ };
55
+
56
+ DataRetriever.prototype._retireveDataOnline = function(source, url) {
57
+ var self = this;
58
+
59
+ var data = new Data();
60
+
61
+ this._data[url] = data;
62
+ this._waitingForData[url] = [source];
63
+
64
+ fetch(url)
65
+ .then(function(response) {
66
+ return response.blob();
67
+ })
68
+ .then(function(blob) {
69
+ var type = blob.type;
70
+
71
+ if (type) {
72
+ type = type.split('/')[0];
73
+ }
74
+ else {
75
+ type = 'other';
76
+ }
77
+
78
+ data.type = type;
79
+ data.content = URL.createObjectURL(blob);
80
+
81
+ if (self._waitingForData[url]) {
82
+ self._waitingForData[url].forEach(function(src) {
83
+ self._peaks.emit('data.retrieved', data, src, url);
84
+ });
85
+ }
86
+ });
87
+ };
88
+
89
+ return DataRetriever;
90
+ });
package/src/data.js ADDED
@@ -0,0 +1,56 @@
1
+ /**
2
+ * @file
3
+ *
4
+ * Defines the {@link data} class.
5
+ *
6
+ * @module data
7
+ */
8
+
9
+ define([
10
+ ], function() {
11
+ 'use strict';
12
+
13
+ /**
14
+ * A data object associated with a source.
15
+ *
16
+ * @class
17
+ * @alias Data
18
+ *
19
+ * @param {String} type The MIME type (image, video, audio...)
20
+ * @param {Object} content Content of the data retrieved.
21
+ */
22
+
23
+ function Data(type, content) {
24
+ this._type = type;
25
+ this._content = content;
26
+ }
27
+
28
+ Object.defineProperties(Data.prototype, {
29
+ type: {
30
+ enumerable: true,
31
+ get: function() {
32
+ return this._type;
33
+ },
34
+
35
+ set: function(type) {
36
+ this._type = type;
37
+ }
38
+ },
39
+ content: {
40
+ enumerable: true,
41
+ get: function() {
42
+ return this._content;
43
+ },
44
+
45
+ set: function(content) {
46
+ this._content = content;
47
+ }
48
+ }
49
+ });
50
+
51
+ Data.prototype.isComplete = function() {
52
+ return this._type && this._content;
53
+ };
54
+
55
+ return Data;
56
+ });
@@ -0,0 +1,132 @@
1
+ /**
2
+ * @file
3
+ *
4
+ * Defines the {@link DefaultSegmentMarker} class.
5
+ *
6
+ * @module default-segment-marker
7
+ */
8
+
9
+ define([
10
+ './utils',
11
+ 'konva'
12
+ ], function(
13
+ Utils,
14
+ Konva) {
15
+ 'use strict';
16
+
17
+ /**
18
+ * Creates a segment marker handle.
19
+ *
20
+ * @class
21
+ * @alias DefaultSegmentMarker
22
+ *
23
+ * @param {CreateSegmentMarkerOptions} options
24
+ */
25
+
26
+ function DefaultSegmentMarker(options) {
27
+ this._options = options;
28
+ }
29
+
30
+ DefaultSegmentMarker.prototype.init = function(group) {
31
+ this._handleWidth = this._options.segmentWidth;
32
+ this._handleHeight = this._options.segmentHeight;
33
+
34
+ this._group = group;
35
+
36
+ // var handleX = -(this._handleWidth / 2) + 0.5; // Place in the middle of the marker
37
+
38
+ var time = this._options.startMarker ? this._options.segment.startTime :
39
+ this._options.segment.endTime;
40
+
41
+ // Label - create with default y, the real value is set in fitToView().
42
+ this._label = new Konva.Text({
43
+ x: 0,
44
+ y: 0,
45
+ text: Utils.removeLineBreaks(Utils.formatTime(time, false)),
46
+ fontSize: 10,
47
+ fontFamily: 'sans-serif',
48
+ fontStyle: 'bold',
49
+ fill: this._options.segment.handleTextColor,
50
+ textAlign: 'center'
51
+ });
52
+
53
+ this._label.hide();
54
+
55
+ // Handle - create with default y, the real value is set in fitToView().
56
+ if (this._options.segment.editable && this._options.showSegmentMarkers) {
57
+ this._handle = new Konva.Rect({
58
+ x: 0,
59
+ y: this._options.y,
60
+ width: this._handleWidth,
61
+ height: this._handleHeight,
62
+ opacity: 0
63
+ });
64
+ }
65
+
66
+ var positionX = this._options.startMarker ? -this._label.width() - 1 : this._handleWidth + 1;
67
+
68
+ this._label.setX(positionX);
69
+
70
+ group.add(this._label);
71
+
72
+ if (this._handle) {
73
+ group.add(this._handle);
74
+ }
75
+
76
+ this.bindEventHandlers(group);
77
+ };
78
+
79
+ DefaultSegmentMarker.prototype.getHandleWidth = function() {
80
+ return this._handleWidth;
81
+ };
82
+
83
+ DefaultSegmentMarker.prototype.bindEventHandlers = function(group) {
84
+ var self = this;
85
+
86
+ if (self._options.draggable) {
87
+ group.on('dragstart', function() {
88
+ self._label.show();
89
+ self._options.group.draw();
90
+ });
91
+
92
+ group.on('dragend', function() {
93
+ self._label.hide();
94
+ self._options.group.draw();
95
+ });
96
+ }
97
+
98
+ if (self._handle) {
99
+ self._handle.on('mouseover touchstart', function() {
100
+ self._options.view.setCursor('ew-resize');
101
+ self._label.show();
102
+ self._group.moveToTop();
103
+ self._options.view.drawSourcesLayer();
104
+ });
105
+
106
+ self._handle.on('mouseout touchend', function() {
107
+ self._options.view.setCursor('default');
108
+ self._label.hide();
109
+ self._options.view.drawSourcesLayer();
110
+ });
111
+ }
112
+ };
113
+
114
+ DefaultSegmentMarker.prototype.getWidth = function() {
115
+ return this._handle ? this._handle.width() : 0;
116
+ };
117
+
118
+ DefaultSegmentMarker.prototype.setWidth = function(value) {
119
+ if (this._handle) {
120
+ this._handle.width(value);
121
+ if (!this._options.startMarker) {
122
+ this._label.setX(value + 1);
123
+ }
124
+ }
125
+ };
126
+
127
+ DefaultSegmentMarker.prototype.timeUpdated = function(time) {
128
+ this._label.setText(Utils.formatTime(time, false));
129
+ };
130
+
131
+ return DefaultSegmentMarker;
132
+ });