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