@micromag/element-audio 0.3.541 → 0.3.566

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.
Files changed (3) hide show
  1. package/es/index.js +38 -20
  2. package/package.json +5 -6
  3. package/lib/index.js +0 -623
package/es/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
2
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
2
3
  import classNames from 'classnames';
3
4
  import isFunction from 'lodash/isFunction';
4
5
  import isNumber from 'lodash/isNumber';
@@ -7,7 +8,6 @@ import React, { useState, useMemo, useCallback, useRef, useEffect } from 'react'
7
8
  import { PropTypes as PropTypes$1 } from '@micromag/core';
8
9
  import { useDimensionObserver, useMediaCurrentTime, useMediaReady, useMediaDuration, useMediaLoad, useProgressSteps } from '@micromag/core/hooks';
9
10
  import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
10
- import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
11
11
  import { useGesture } from '@use-gesture/react';
12
12
  import { useIntl } from 'react-intl';
13
13
  import { useSpring } from '@react-spring/core';
@@ -263,18 +263,36 @@ var Audio = function Audio(_ref) {
263
263
  onPlayError = _ref.onPlayError;
264
264
  var _ref2 = media || {},
265
265
  _ref2$url = _ref2.url,
266
- url = _ref2$url === void 0 ? null : _ref2$url;
267
- var _ref4 = useRef(null);
268
- var currentTime = useMediaCurrentTime(_ref4.current, {
269
- id: url,
266
+ url = _ref2$url === void 0 ? null : _ref2$url,
267
+ _ref2$files = _ref2.files,
268
+ files = _ref2$files === void 0 ? null : _ref2$files;
269
+ var srcUrl = useMemo(function () {
270
+ if (files) {
271
+ var filesAsEntries = Object.entries(files);
272
+ var _filesAsEntries$find = filesAsEntries.find(function (_ref3) {
273
+ var _ref4 = _slicedToArray(_ref3, 1),
274
+ key = _ref4[0];
275
+ return key === 'mp3';
276
+ }),
277
+ _filesAsEntries$find2 = _slicedToArray(_filesAsEntries$find, 2),
278
+ mp3File = _filesAsEntries$find2[1];
279
+ if (mp3File) {
280
+ return mp3File.url;
281
+ }
282
+ }
283
+ return url;
284
+ }, [files, url]);
285
+ var _ref6 = useRef(null);
286
+ var currentTime = useMediaCurrentTime(_ref6.current, {
287
+ id: srcUrl,
270
288
  disabled: paused || !withWave && onProgressStep === null,
271
289
  updateInterval: updateInterval
272
290
  });
273
- var ready = useMediaReady(_ref4.current, {
274
- id: url
291
+ var ready = useMediaReady(_ref6.current, {
292
+ id: srcUrl
275
293
  });
276
- var duration = useMediaDuration(_ref4.current, {
277
- id: url
294
+ var duration = useMediaDuration(_ref6.current, {
295
+ id: srcUrl
278
296
  });
279
297
 
280
298
  // const audioLevels = useMediaWaveform(media, {
@@ -282,7 +300,7 @@ var Audio = function Audio(_ref) {
282
300
  // reduceBufferFactor,
283
301
  // });
284
302
 
285
- useMediaLoad(_ref4.current, {
303
+ useMediaLoad(_ref6.current, {
286
304
  preload: preload,
287
305
  shouldLoad: shouldLoad
288
306
  });
@@ -293,7 +311,7 @@ var Audio = function Audio(_ref) {
293
311
  }
294
312
  }, [duration, customOnDurationChange]);
295
313
  var onVolumeChange = useCallback(function () {
296
- var _ref$current = _ref4.current,
314
+ var _ref$current = _ref6.current,
297
315
  element = _ref$current === void 0 ? null : _ref$current;
298
316
  if (element === null) {
299
317
  return;
@@ -301,17 +319,17 @@ var Audio = function Audio(_ref) {
301
319
  if (customOnVolumeChange !== null) {
302
320
  customOnVolumeChange(element.volume);
303
321
  }
304
- }, [_ref4.current, customOnVolumeChange]);
322
+ }, [_ref6.current, customOnVolumeChange]);
305
323
  var onWavePlay = useCallback(function () {
306
- var _ref$current2 = _ref4.current,
324
+ var _ref$current2 = _ref6.current,
307
325
  element = _ref$current2 === void 0 ? null : _ref$current2;
308
326
  if (element === null) {
309
327
  return;
310
328
  }
311
329
  element.play();
312
- }, [_ref4.current]);
330
+ }, [_ref6.current]);
313
331
  var onWaveSeek = useCallback(function (newTime) {
314
- var _ref$current3 = _ref4.current,
332
+ var _ref$current3 = _ref6.current,
315
333
  element = _ref$current3 === void 0 ? null : _ref$current3;
316
334
  if (element === null) {
317
335
  return;
@@ -319,14 +337,14 @@ var Audio = function Audio(_ref) {
319
337
  if (isNumber(newTime)) {
320
338
  element.currentTime = newTime;
321
339
  }
322
- }, [_ref4.current]);
340
+ }, [_ref6.current]);
323
341
  useEffect(function () {
324
342
  if (waveReady && onReady !== null) {
325
343
  onReady();
326
344
  }
327
345
  }, [waveReady, onReady]);
328
346
  useEffect(function () {
329
- var _ref$current4 = _ref4.current,
347
+ var _ref$current4 = _ref6.current,
330
348
  element = _ref$current4 === void 0 ? null : _ref$current4;
331
349
  if (element === null) {
332
350
  return;
@@ -352,16 +370,16 @@ var Audio = function Audio(_ref) {
352
370
  return /*#__PURE__*/React.createElement("div", {
353
371
  className: classNames([styles$1.container, _defineProperty({}, className, className !== null)])
354
372
  }, /*#__PURE__*/React.createElement("audio", {
355
- key: url,
373
+ key: srcUrl,
356
374
  ref: function ref(newRef) {
357
- _ref4.current = newRef;
375
+ _ref6.current = newRef;
358
376
  if (mediaRef !== null && isFunction(mediaRef)) {
359
377
  mediaRef(newRef);
360
378
  } else if (mediaRef !== null) {
361
379
  mediaRef.current = newRef;
362
380
  }
363
381
  },
364
- src: url,
382
+ src: srcUrl,
365
383
  autoPlay: autoPlay,
366
384
  muted: muted,
367
385
  loop: loop,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micromag/element-audio",
3
- "version": "0.3.541",
3
+ "version": "0.3.566",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "keywords": [
@@ -30,11 +30,10 @@
30
30
  }
31
31
  ],
32
32
  "license": "ISC",
33
- "main": "lib/index.js",
33
+ "type": "module",
34
34
  "module": "es/index.js",
35
35
  "exports": {
36
36
  ".": {
37
- "require": "./lib/index.js",
38
37
  "import": "./es/index.js"
39
38
  },
40
39
  "./assets/css/styles": "./assets/css/styles.css",
@@ -60,8 +59,8 @@
60
59
  },
61
60
  "dependencies": {
62
61
  "@babel/runtime": "^7.13.10",
63
- "@micromag/core": "^0.3.541",
64
- "@micromag/element-closed-captions": "^0.3.541",
62
+ "@micromag/core": "^0.3.547",
63
+ "@micromag/element-closed-captions": "^0.3.547",
65
64
  "@react-spring/core": "^9.6.1",
66
65
  "@react-spring/web": "^9.6.1",
67
66
  "@use-gesture/react": "^10.3.0",
@@ -75,5 +74,5 @@
75
74
  "access": "public",
76
75
  "registry": "https://registry.npmjs.org/"
77
76
  },
78
- "gitHead": "6c04a7e327b5fbc096785c11320b3fbd3c5751c8"
77
+ "gitHead": "7ee1998bfc00f5a841711346ac8ca21dba439df7"
79
78
  }
package/lib/index.js DELETED
@@ -1,623 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var _defineProperty = require('@babel/runtime/helpers/defineProperty');
6
- var classNames = require('classnames');
7
- var isFunction = require('lodash/isFunction');
8
- var isNumber = require('lodash/isNumber');
9
- var PropTypes = require('prop-types');
10
- var React = require('react');
11
- var core = require('@micromag/core');
12
- var hooks = require('@micromag/core/hooks');
13
- var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
14
- var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
15
- var react = require('@use-gesture/react');
16
- var reactIntl = require('react-intl');
17
- var core$1 = require('@react-spring/core');
18
- var web = require('@react-spring/web');
19
- var utils = require('@micromag/core/utils');
20
-
21
- var styles$2 = {"container":"micromag-element-audio-audio-bars-container","seekButton":"micromag-element-audio-audio-bars-seekButton"};
22
-
23
- var propTypes$2 = {
24
- progress: PropTypes.number,
25
- duration: PropTypes.number,
26
- playing: PropTypes.bool,
27
- seek: PropTypes.func,
28
- play: PropTypes.func,
29
- // onReady: PropTypes.func,
30
- className: PropTypes.string
31
- };
32
- var defaultProps$2 = {
33
- progress: 0,
34
- duration: 0,
35
- playing: false,
36
- seek: null,
37
- play: null,
38
- // onReady: null,
39
- className: null
40
- };
41
- function AudioBars(_ref) {
42
- var progress = _ref.progress,
43
- playing = _ref.playing,
44
- duration = _ref.duration,
45
- seek = _ref.seek,
46
- play = _ref.play,
47
- className = _ref.className;
48
- var intl = reactIntl.useIntl();
49
- var _useDimensionObserver = hooks.useDimensionObserver(),
50
- elRef = _useDimensionObserver.ref,
51
- _useDimensionObserver2 = _useDimensionObserver.width,
52
- elWidth = _useDimensionObserver2 === void 0 ? null : _useDimensionObserver2,
53
- elHeight = _useDimensionObserver.height;
54
- var barWidth = 1;
55
- var barGap = 1;
56
- var barSize = barWidth + barGap;
57
- var current = progress * elWidth;
58
- var _useState = React.useState(null),
59
- _useState2 = _slicedToArray(_useState, 2),
60
- currentSeek = _useState2[0],
61
- setCurrentSeek = _useState2[1];
62
- var items = React.useMemo(function () {
63
- var count = Math.floor(elWidth / barSize);
64
- return _toConsumableArray(Array(count).keys()).map(function () {
65
- return Math.floor(Math.random() * 100);
66
- });
67
- }, [elWidth, barSize]);
68
- var seekFromX = React.useCallback(function () {
69
- var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
70
- if (x === null) {
71
- return;
72
- }
73
- var _elRef$current$getBou = elRef.current.getBoundingClientRect(),
74
- elX = _elRef$current$getBou.left,
75
- width = _elRef$current$getBou.width;
76
- var distance = Math.max(0, Math.min(1, (x - elX) / width));
77
- if (seek !== null && duration !== null) {
78
- seek(parseFloat(distance * duration, 10));
79
- }
80
- if (!playing && play != null) {
81
- play();
82
- }
83
- }, [elRef.current, elWidth, duration, playing, seek, play]);
84
- var seekTemporary = React.useCallback(function (x) {
85
- var _elRef$current$getBou2 = elRef.current.getBoundingClientRect(),
86
- elX = _elRef$current$getBou2.left,
87
- width = _elRef$current$getBou2.width;
88
- var distance = Math.max(0, Math.min(1, (x - elX) / width));
89
- setCurrentSeek(distance * elWidth);
90
- }, [elRef.current, elWidth, setCurrentSeek]);
91
- var onClick = React.useCallback(function (e) {
92
- if (e) {
93
- seekFromX(e.clientX || null);
94
- setCurrentSeek(null);
95
- }
96
- }, [seekFromX, setCurrentSeek]);
97
- var bind = react.useGesture({
98
- onDrag: function onDrag(_ref2) {
99
- var _ref2$xy = _slicedToArray(_ref2.xy, 1),
100
- x = _ref2$xy[0],
101
- elapsedTime = _ref2.elapsedTime,
102
- active = _ref2.active;
103
- if (!active && elapsedTime > 300) {
104
- return;
105
- }
106
- seekTemporary(x);
107
- },
108
- onDragStart: function onDragStart(_ref3) {
109
- var _ref3$xy = _slicedToArray(_ref3.xy, 1),
110
- x = _ref3$xy[0],
111
- elapsedTime = _ref3.elapsedTime,
112
- active = _ref3.active;
113
- if (!active && elapsedTime > 300) {
114
- return;
115
- }
116
- seekTemporary(x);
117
- },
118
- onDragEnd: function onDragEnd(_ref4) {
119
- var _ref4$xy = _slicedToArray(_ref4.xy, 1),
120
- x = _ref4$xy[0];
121
- seekFromX(x);
122
- setTimeout(function () {
123
- setCurrentSeek(null);
124
- }, 1000);
125
- }
126
- }, {
127
- drag: {
128
- axis: 'x',
129
- filterTaps: true
130
- }
131
- });
132
- return /*#__PURE__*/React.createElement("div", {
133
- className: classNames([styles$2.container, _defineProperty({}, className, className !== null)]),
134
- ref: elRef
135
- }, /*#__PURE__*/React.createElement("svg", {
136
- version: "1.1",
137
- xmlns: "http://www.w3.org/2000/svg",
138
- xmlnsXlink: "http://www.w3.org/1999/xlink",
139
- x: "0",
140
- y: "0",
141
- width: "".concat(elWidth, "px"),
142
- height: "".concat(elHeight, "px"),
143
- viewBox: "0 0 ".concat(elWidth, " ").concat(elHeight),
144
- className: className,
145
- xmlSpace: "preserve"
146
- }, items.map(function (size, i) {
147
- return /*#__PURE__*/React.createElement("rect", {
148
- key: "bar-".concat(i + 1),
149
- width: barWidth,
150
- height: size,
151
- x: i * barSize,
152
- y: elHeight / 2 - size / 2,
153
- fill: (currentSeek || current) > i * barSize ? 'white' : 'gray',
154
- stroke: "black",
155
- strokeWidth: "0px",
156
- strokeLinejoin: "round",
157
- strokeLinecap: "round"
158
- });
159
- })), /*#__PURE__*/React.createElement("button", Object.assign({}, bind(), {
160
- onClick: onClick,
161
- type: "button",
162
- className: styles$2.seekButton,
163
- title: intl.formatMessage({
164
- id: "G1Gyjn",
165
- defaultMessage: [{
166
- "type": 0,
167
- "value": "Seek"
168
- }]
169
- }),
170
- "aria-label": intl.formatMessage({
171
- id: "G1Gyjn",
172
- defaultMessage: [{
173
- "type": 0,
174
- "value": "Seek"
175
- }]
176
- })
177
- })));
178
- }
179
- AudioBars.propTypes = propTypes$2;
180
- AudioBars.defaultProps = defaultProps$2;
181
-
182
- var styles$1 = {"container":"micromag-element-audio-container","wave":"micromag-element-audio-wave","withAutoHeight":"micromag-element-audio-withAutoHeight"};
183
-
184
- var propTypes$1 = {
185
- media: core.PropTypes.audioMedia,
186
- mediaRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
187
- current: PropTypes.any
188
- })]),
189
- muted: PropTypes.bool,
190
- autoPlay: PropTypes.bool,
191
- paused: PropTypes.bool,
192
- loop: PropTypes.bool,
193
- preload: PropTypes.oneOf(['auto', 'none', 'metadata']),
194
- shouldLoad: PropTypes.bool,
195
- waveFake: PropTypes.bool,
196
- // waveProps: PropTypes.shape({
197
- // sampleWidth: PropTypes.number,
198
- // sampleMargin: PropTypes.number,
199
- // minSampleHeight: PropTypes.number,
200
- // }),
201
- withWave: PropTypes.bool,
202
- autoWaveHeight: PropTypes.bool,
203
- // reduceBufferFactor: PropTypes.number,
204
- updateInterval: PropTypes.number,
205
- className: PropTypes.string,
206
- onReady: PropTypes.func,
207
- onPlay: PropTypes.func,
208
- onPause: PropTypes.func,
209
- onEnded: PropTypes.func,
210
- onSeeked: PropTypes.func,
211
- onTimeUpdate: PropTypes.func,
212
- onProgressStep: PropTypes.func,
213
- onDurationChange: PropTypes.func,
214
- onVolumeChange: PropTypes.func,
215
- onPlayError: PropTypes.func
216
- };
217
- var defaultProps$1 = {
218
- media: null,
219
- mediaRef: null,
220
- muted: false,
221
- autoPlay: false,
222
- paused: false,
223
- loop: false,
224
- preload: 'auto',
225
- shouldLoad: true,
226
- waveFake: false,
227
- // waveProps: null,
228
- withWave: false,
229
- autoWaveHeight: false,
230
- // reduceBufferFactor: 100,
231
- updateInterval: 1000,
232
- className: null,
233
- onReady: null,
234
- onPlay: null,
235
- onPause: null,
236
- onEnded: null,
237
- onSeeked: null,
238
- onTimeUpdate: null,
239
- onProgressStep: null,
240
- onDurationChange: null,
241
- onVolumeChange: null,
242
- onPlayError: null
243
- };
244
- var Audio = function Audio(_ref) {
245
- var media = _ref.media,
246
- mediaRef = _ref.mediaRef,
247
- muted = _ref.muted,
248
- autoPlay = _ref.autoPlay,
249
- paused = _ref.paused,
250
- loop = _ref.loop,
251
- preload = _ref.preload,
252
- shouldLoad = _ref.shouldLoad,
253
- waveFake = _ref.waveFake,
254
- withWave = _ref.withWave,
255
- autoWaveHeight = _ref.autoWaveHeight,
256
- updateInterval = _ref.updateInterval,
257
- className = _ref.className,
258
- onReady = _ref.onReady,
259
- onPlay = _ref.onPlay,
260
- onPause = _ref.onPause,
261
- onEnded = _ref.onEnded,
262
- onSeeked = _ref.onSeeked,
263
- onTimeUpdate = _ref.onTimeUpdate,
264
- onProgressStep = _ref.onProgressStep,
265
- customOnDurationChange = _ref.onDurationChange,
266
- customOnVolumeChange = _ref.onVolumeChange,
267
- onPlayError = _ref.onPlayError;
268
- var _ref2 = media || {},
269
- _ref2$url = _ref2.url,
270
- url = _ref2$url === void 0 ? null : _ref2$url;
271
- var _ref4 = React.useRef(null);
272
- var currentTime = hooks.useMediaCurrentTime(_ref4.current, {
273
- id: url,
274
- disabled: paused || !withWave && onProgressStep === null,
275
- updateInterval: updateInterval
276
- });
277
- var ready = hooks.useMediaReady(_ref4.current, {
278
- id: url
279
- });
280
- var duration = hooks.useMediaDuration(_ref4.current, {
281
- id: url
282
- });
283
-
284
- // const audioLevels = useMediaWaveform(media, {
285
- // fake: waveFake,
286
- // reduceBufferFactor,
287
- // });
288
-
289
- hooks.useMediaLoad(_ref4.current, {
290
- preload: preload,
291
- shouldLoad: shouldLoad
292
- });
293
- var waveReady = waveFake || ready;
294
- React.useEffect(function () {
295
- if (duration > 0 && customOnDurationChange !== null) {
296
- customOnDurationChange(duration);
297
- }
298
- }, [duration, customOnDurationChange]);
299
- var onVolumeChange = React.useCallback(function () {
300
- var _ref$current = _ref4.current,
301
- element = _ref$current === void 0 ? null : _ref$current;
302
- if (element === null) {
303
- return;
304
- }
305
- if (customOnVolumeChange !== null) {
306
- customOnVolumeChange(element.volume);
307
- }
308
- }, [_ref4.current, customOnVolumeChange]);
309
- var onWavePlay = React.useCallback(function () {
310
- var _ref$current2 = _ref4.current,
311
- element = _ref$current2 === void 0 ? null : _ref$current2;
312
- if (element === null) {
313
- return;
314
- }
315
- element.play();
316
- }, [_ref4.current]);
317
- var onWaveSeek = React.useCallback(function (newTime) {
318
- var _ref$current3 = _ref4.current,
319
- element = _ref$current3 === void 0 ? null : _ref$current3;
320
- if (element === null) {
321
- return;
322
- }
323
- if (isNumber(newTime)) {
324
- element.currentTime = newTime;
325
- }
326
- }, [_ref4.current]);
327
- React.useEffect(function () {
328
- if (waveReady && onReady !== null) {
329
- onReady();
330
- }
331
- }, [waveReady, onReady]);
332
- React.useEffect(function () {
333
- var _ref$current4 = _ref4.current,
334
- element = _ref$current4 === void 0 ? null : _ref$current4;
335
- if (element === null) {
336
- return;
337
- }
338
- var isPaused = element.paused;
339
- if (paused && !isPaused) {
340
- element.pause();
341
- } else if (!paused && isPaused) {
342
- element.play()["catch"](function (e) {
343
- if (onPlayError !== null) {
344
- onPlayError(e);
345
- }
346
- });
347
- }
348
- }, [paused, media, onPlayError]);
349
- hooks.useProgressSteps({
350
- currentTime: currentTime,
351
- duration: duration,
352
- disabled: paused,
353
- onStep: onProgressStep
354
- });
355
- var progress = currentTime !== null && duration > 0 ? currentTime / duration : 0;
356
- return /*#__PURE__*/React.createElement("div", {
357
- className: classNames([styles$1.container, _defineProperty({}, className, className !== null)])
358
- }, /*#__PURE__*/React.createElement("audio", {
359
- key: url,
360
- ref: function ref(newRef) {
361
- _ref4.current = newRef;
362
- if (mediaRef !== null && isFunction(mediaRef)) {
363
- mediaRef(newRef);
364
- } else if (mediaRef !== null) {
365
- mediaRef.current = newRef;
366
- }
367
- },
368
- src: url,
369
- autoPlay: autoPlay,
370
- muted: muted,
371
- loop: loop,
372
- crossOrigin: "anonymous",
373
- preload: preload,
374
- onPlay: onPlay,
375
- onPause: onPause,
376
- onEnded: onEnded,
377
- onSeeked: onSeeked,
378
- onTimeUpdate: onTimeUpdate,
379
- onVolumeChange: onVolumeChange
380
- }), withWave ? /*#__PURE__*/React.createElement(AudioBars, {
381
- className: classNames([styles$1.wave, _defineProperty({}, styles$1.withAutoHeight, autoWaveHeight)]),
382
- progress: progress
383
- // {...waveProps}
384
- ,
385
- duration: duration,
386
- playing: !paused,
387
- seek: onWaveSeek,
388
- play: onWavePlay
389
- }) : null);
390
- };
391
- Audio.propTypes = propTypes$1;
392
- Audio.defaultProps = defaultProps$1;
393
- var Audio$1 = /*#__PURE__*/React.forwardRef(function (props, ref) {
394
- return /*#__PURE__*/React.createElement(Audio, Object.assign({
395
- mediaRef: ref
396
- }, props));
397
- });
398
-
399
- var styles = {"container":"micromag-element-audio-audio-wave-container","button":"micromag-element-audio-audio-wave-button","canvasBackground":"micromag-element-audio-audio-wave-canvasBackground","canvasProgress":"micromag-element-audio-audio-wave-canvasProgress"};
400
-
401
- var propTypes = {
402
- currentTime: PropTypes.number,
403
- duration: PropTypes.number,
404
- playing: PropTypes.bool,
405
- sampleWidth: PropTypes.number,
406
- sampleMargin: PropTypes.number,
407
- minSampleHeight: PropTypes.number,
408
- backgroundColor: core.PropTypes.color,
409
- progressColor: core.PropTypes.color,
410
- audioLevels: PropTypes.arrayOf(PropTypes.number),
411
- className: PropTypes.string,
412
- onSeek: PropTypes.func,
413
- onResume: PropTypes.func,
414
- onReady: PropTypes.func
415
- };
416
- var defaultProps = {
417
- currentTime: null,
418
- duration: null,
419
- playing: false,
420
- sampleWidth: 3,
421
- sampleMargin: 1,
422
- minSampleHeight: 2,
423
- backgroundColor: 'white',
424
- progressColor: null,
425
- audioLevels: null,
426
- className: null,
427
- onSeek: null,
428
- onResume: null,
429
- onReady: null
430
- };
431
- function AudioWave(_ref) {
432
- var currentTime = _ref.currentTime,
433
- duration = _ref.duration,
434
- playing = _ref.playing,
435
- sampleWidth = _ref.sampleWidth,
436
- sampleMargin = _ref.sampleMargin,
437
- minSampleHeight = _ref.minSampleHeight,
438
- backgroundColor = _ref.backgroundColor,
439
- progressColor = _ref.progressColor,
440
- audioLevels = _ref.audioLevels,
441
- className = _ref.className,
442
- onSeek = _ref.onSeek,
443
- onResume = _ref.onResume,
444
- onReady = _ref.onReady;
445
- var canvasBackgroundRef = React.useRef(null);
446
- var canvasProgressRef = React.useRef(null);
447
- var mainColor = React.useMemo(function () {
448
- var _ref2 = backgroundColor || {},
449
- _ref2$color = _ref2.color,
450
- color = _ref2$color === void 0 ? 'white' : _ref2$color;
451
- return color;
452
- }, [backgroundColor]);
453
- var alternateColor = React.useMemo(function () {
454
- return utils.getContrastingColor(backgroundColor, progressColor);
455
- }, [progressColor, backgroundColor]);
456
- var _useDimensionObserver = hooks.useDimensionObserver(),
457
- elRef = _useDimensionObserver.ref,
458
- _useDimensionObserver2 = _useDimensionObserver.width,
459
- elWidth = _useDimensionObserver2 === void 0 ? null : _useDimensionObserver2,
460
- elHeight = _useDimensionObserver.height;
461
-
462
- // Linear animation for progress bar
463
-
464
- var _useSpring = core$1.useSpring(function () {
465
- return {
466
- x: 0,
467
- config: {
468
- duration: 0
469
- }
470
- };
471
- }),
472
- _useSpring2 = _slicedToArray(_useSpring, 2),
473
- springProps = _useSpring2[0],
474
- setSpringProps = _useSpring2[1];
475
- React.useEffect(function () {
476
- if (currentTime === null || duration === null) {
477
- return;
478
- }
479
- var progress = currentTime / duration;
480
- setSpringProps.start({
481
- reset: true,
482
- immediate: !playing,
483
- from: {
484
- x: progress
485
- },
486
- to: {
487
- x: playing ? 1 : progress
488
- },
489
- config: {
490
- duration: (duration - currentTime) * 1000
491
- }
492
- });
493
- }, [playing, duration, currentTime, setSpringProps]);
494
-
495
- // draw canvas
496
-
497
- React.useEffect(function () {
498
- if (audioLevels === null || audioLevels.length === 0 || elRef.current === null) {
499
- return;
500
- }
501
- var sampleOuterWidth = sampleWidth + sampleMargin * 2;
502
- var samplesCount = Math.floor(elWidth / sampleOuterWidth);
503
-
504
- // const amplitudes = [];
505
-
506
- // get samples
507
-
508
- var levelsBySamples = audioLevels.length / samplesCount;
509
- var amplitudes = _toConsumableArray(Array(samplesCount).keys()).reduce(function (newAmplitudes, index) {
510
- var levelStartIndex = index * levelsBySamples;
511
- var levelEndIndex = levelStartIndex + levelsBySamples;
512
- var newValues = [];
513
- for (var i = Math.floor(levelStartIndex); i < Math.round(levelEndIndex); i += 1) {
514
- newValues.push(audioLevels[i]);
515
- }
516
- return levelsBySamples >= 1 ? [].concat(_toConsumableArray(newAmplitudes), [newValues.reduce(function (total, value) {
517
- return total + value;
518
- }, 0) / newValues.length]) : [].concat(_toConsumableArray(newAmplitudes), newValues);
519
- }, []);
520
-
521
- // for (let sampleI = 0; sampleI < samplesCount; sampleI += levelsBySamples) {
522
- // // if (levelsBySamples >= 1) {
523
- // // const sampleSize = Math.floor(levelsBySamples);
524
- // // const sampleStart = sampleSize * sampleI;
525
- // // let sum = 0;
526
- // // for (let sampleSizeI = 0; sampleSizeI < sampleSize; sampleSizeI += 1) {
527
- // // sum += Math.abs(audioLevels[sampleStart + sampleSizeI]);
528
- // // }
529
- // // amplitudes.push(sum / sampleSize);
530
- // // } else {
531
- // console.log(sampleI);
532
- // amplitudes.push(Math.abs(audioLevels[Math.floor(sampleI)]));
533
- // // for (let sampleSizeI = 0; sampleSizeI < sampleSize; sampleSizeI += 1) {
534
- // // console.log(sampleI, sampleSize);
535
- // // amplitudes.push(Math.abs(audioLevels[sampleI % sampleSize]));
536
- // // }
537
- // // }
538
- // }
539
- var minAmplitude = Math.min.apply(Math, _toConsumableArray(amplitudes));
540
- var maxAmplitude = Math.max.apply(Math, _toConsumableArray(amplitudes));
541
- var delta = maxAmplitude - minAmplitude;
542
- var normalizedAmplitudes = amplitudes.map(function (n) {
543
- return (n - minAmplitude) / delta;
544
- });
545
-
546
- // draw samples
547
- var canvasBg = canvasBackgroundRef.current;
548
- var canvasProgress = canvasProgressRef.current;
549
- var scale = typeof window !== 'undefined' ? window.devicePixelRatio : 1;
550
- canvasBg.width = canvasProgress.width = Math.floor(elWidth * scale);
551
- canvasBg.height = canvasProgress.height = Math.floor(elHeight * scale);
552
- var ctxBG = canvasBg.getContext('2d');
553
- var ctxProgress = canvasProgress.getContext('2d');
554
- ctxBG.scale(scale, scale);
555
- ctxProgress.scale(scale, scale);
556
- ctxBG.clearRect(0, 0, elWidth, elHeight);
557
- ctxProgress.clearRect(0, 0, elWidth, elHeight);
558
- ctxBG.fillStyle = mainColor;
559
- ctxProgress.fillStyle = alternateColor;
560
- var offsetLeft = (elWidth - samplesCount * sampleOuterWidth) / 2;
561
- normalizedAmplitudes.forEach(function (amplitude, amplitudeI) {
562
- var sampleHeight = Math.max(minSampleHeight, amplitude * elHeight);
563
- var sampleX = sampleOuterWidth * amplitudeI + offsetLeft + sampleMargin;
564
- var sampleY = elHeight / 2 - sampleHeight / 2;
565
- ctxBG.fillRect(Math.round(sampleX), Math.round(sampleY), sampleWidth, Math.round(sampleHeight));
566
- ctxProgress.fillRect(Math.round(sampleX), Math.round(sampleY), sampleWidth, Math.round(sampleHeight));
567
- });
568
- if (onReady !== null) {
569
- onReady();
570
- }
571
- }, [audioLevels, sampleWidth, sampleMargin, minSampleHeight, elWidth, elHeight, mainColor, alternateColor, onReady]);
572
-
573
- // User events
574
- var seekFromX = React.useCallback(function (x) {
575
- var _elRef$current$getBou = elRef.current.getBoundingClientRect(),
576
- elX = _elRef$current$getBou.left,
577
- width = _elRef$current$getBou.width;
578
- var progress = Math.max(0, Math.min(1, (x - elX) / width));
579
- if (onSeek !== null && duration !== null) {
580
- onSeek(progress * duration);
581
- }
582
- if (!playing) {
583
- onResume();
584
- }
585
- }, [duration, playing, onSeek, onResume]);
586
- var bind = react.useGesture({
587
- onDrag: function onDrag(_ref3) {
588
- var _ref3$xy = _slicedToArray(_ref3.xy, 1),
589
- x = _ref3$xy[0],
590
- elapsedTime = _ref3.elapsedTime,
591
- active = _ref3.active;
592
- if (!active && elapsedTime > 300) {
593
- return;
594
- }
595
- seekFromX(x);
596
- }
597
- }, {
598
- drag: {
599
- axis: 'x',
600
- filterTaps: true
601
- }
602
- });
603
- return /*#__PURE__*/React.createElement("div", Object.assign({
604
- className: classNames([styles.container, _defineProperty({}, className, className !== null)]),
605
- ref: elRef
606
- }, bind()), /*#__PURE__*/React.createElement("canvas", {
607
- ref: canvasBackgroundRef,
608
- className: styles.canvasBackground
609
- }), /*#__PURE__*/React.createElement(web.animated.canvas, {
610
- ref: canvasProgressRef,
611
- className: styles.canvasProgress,
612
- style: {
613
- clipPath: springProps.x.to(function (x) {
614
- return "polygon(0 0, ".concat(x * 100, "% 0, ").concat(x * 100, "% 100%, 0 100%)");
615
- })
616
- }
617
- }));
618
- }
619
- AudioWave.propTypes = propTypes;
620
- AudioWave.defaultProps = defaultProps;
621
-
622
- exports.AudioWave = AudioWave;
623
- exports.default = Audio$1;