-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
126 lines (112 loc) · 2.97 KB
/
utils.js
File metadata and controls
126 lines (112 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* eslint-disable func-names */
/* eslint-disable no-param-reassign */
/**
*
* @param {TypedArrays} data
* @param {Number} offset
* @param {String} str
*/
function writeString(data, offset, str) {
for (let i = 0; i < str.length; i += 1) {
data.setUint8(offset + i, str.charCodeAt(i));
}
}
/**
* @param {Array} arr
* @param {Number} size
* @returns {Array[Array[]]}
*/
const chunk = (arr, size) =>
Array.from({ length: Math.ceil(arr.length / size) }, (v, i) =>
arr.slice(i * size, i * size + size)
);
class Utils {
/**
*
* ,
* @function Utils.compress
* @param {float32array} data [-1, 1]
* @param {number} inputSampleRate
* @param {number} outputSampleRate
* @returns {float32array}
*/
static compress(data, inputSampleRate, outputSampleRate) {
const rate = inputSampleRate / outputSampleRate;
const compression = Math.max(rate, 1);
const lData = data.left;
const rData = data.right;
const length = Math.floor((lData.length + rData.length) / rate);
const result = new Float32Array(length);
let index = 0;
let j = 0;
while (index < length) {
const temp = Math.floor(j);
result[index] = lData[temp];
index += 1;
if (rData.length) {
result[index] = rData[temp];
index += 1;
}
j += compression;
}
return result;
}
// ENDPOINTING
static getWAVBufferWithoutWavHeader(data, startTime) {
const audioData = data.slice(startTime, data.length);
data = null;
const buffer = new ArrayBuffer(audioData.length * 2);
const view = new DataView(buffer);
const { length } = audioData;
let index = 0;
const volume = 1;
for (let i = 0; i < length; i += 1) {
view.setInt16(index, audioData[i] * (0x7fff * volume), true);
index += 2;
}
return buffer;
}
/**
* getUserMedia
* @function Utils.initUserMedia
*/
static initUserMedia() {
if (navigator.mediaDevices === undefined) {
navigator.mediaDevices = {};
}
if (navigator.mediaDevices.getUserMedia === undefined) {
navigator.mediaDevices.getUserMedia = function (constraints) {
const getUserMedia =
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
if (!getUserMedia) {
return Promise.reject(new Error(" getUserMedia !"));
}
return new Promise(function (resolve, reject) {
getUserMedia.call(navigator, constraints, resolve, reject);
});
};
}
}
/**
* ArrayBuffer to Base64
* @param {ArrayBuffer} buffer
* @returns {Base64String}
*/
static arrayBufferToBase64({ buffer }) {
return window.btoa(
["", ...chunk(new Uint8Array(buffer.slice(0)), 8 * 1024)].reduce(
(t, c) => t + String.fromCharCode(...c)
)
);
}
}
Utils.Status = {
initing: "0",
pauseing: "1",
recording: "2",
completed: "3",
};
export default Utils;