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