crunker 0.4.1 → 1.3.0
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/.babelrc +5 -1
- package/.prettierrc +6 -0
- package/README.md +7 -4
- package/dist/crunker.d.ts +161 -0
- package/dist/crunker.js +2 -1
- package/dist/crunker.js.map +1 -0
- package/examples/client/index.html +58 -59
- package/examples/server/index.html +32 -34
- package/package.json +21 -16
- package/src/crunker.ts +373 -0
- package/tsconfig.json +63 -0
- package/webpack.config.js +21 -13
- package/webpack.test.config.js +22 -16
- package/src/crunker.js +0 -251
package/src/crunker.js
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
export default class Crunker {
|
|
4
|
-
constructor({ sampleRate = 44100 } = {}) {
|
|
5
|
-
this._sampleRate = sampleRate;
|
|
6
|
-
this._context = this._createContext();
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
_createContext() {
|
|
10
|
-
window.AudioContext =
|
|
11
|
-
window.AudioContext ||
|
|
12
|
-
window.webkitAudioContext ||
|
|
13
|
-
window.mozAudioContext;
|
|
14
|
-
return new AudioContext();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
async fetchAudio(...filepaths) {
|
|
18
|
-
const files = filepaths.map(async (filepath) => {
|
|
19
|
-
let buffer;
|
|
20
|
-
|
|
21
|
-
if (filepath instanceof File) {
|
|
22
|
-
buffer = await filepath.arrayBuffer();
|
|
23
|
-
} else {
|
|
24
|
-
buffer = await fetch(filepath).then((response) => response.arrayBuffer());
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return await this._context.decodeAudioData(buffer);
|
|
28
|
-
});
|
|
29
|
-
return await Promise.all(files);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
mergeAudio(buffers) {
|
|
33
|
-
const output = this._context.createBuffer(
|
|
34
|
-
this._maxNumberOfChannels(buffers),
|
|
35
|
-
this._sampleRate * this._maxDuration(buffers),
|
|
36
|
-
this._sampleRate
|
|
37
|
-
);
|
|
38
|
-
|
|
39
|
-
buffers.forEach((buffer) => {
|
|
40
|
-
for (
|
|
41
|
-
let channelNumber = 0;
|
|
42
|
-
channelNumber < buffer.numberOfChannels;
|
|
43
|
-
channelNumber += 1
|
|
44
|
-
) {
|
|
45
|
-
const outputData = output.getChannelData(channelNumber);
|
|
46
|
-
const bufferData = buffer.getChannelData(channelNumber);
|
|
47
|
-
|
|
48
|
-
for (
|
|
49
|
-
let i = buffer.getChannelData(channelNumber).length - 1;
|
|
50
|
-
i >= 0;
|
|
51
|
-
i -= 1
|
|
52
|
-
) {
|
|
53
|
-
outputData[i] += bufferData[i];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
output.getChannelData(channelNumber).set(outputData);
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
return output;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
concatAudio(buffers) {
|
|
63
|
-
const output = this._context.createBuffer(
|
|
64
|
-
this._maxNumberOfChannels(buffers),
|
|
65
|
-
this._totalLength(buffers),
|
|
66
|
-
this._sampleRate
|
|
67
|
-
);
|
|
68
|
-
let offset = 0;
|
|
69
|
-
|
|
70
|
-
buffers.forEach((buffer) => {
|
|
71
|
-
for (
|
|
72
|
-
let channelNumber = 0;
|
|
73
|
-
channelNumber < buffer.numberOfChannels;
|
|
74
|
-
channelNumber += 1
|
|
75
|
-
) {
|
|
76
|
-
output
|
|
77
|
-
.getChannelData(channelNumber)
|
|
78
|
-
.set(buffer.getChannelData(channelNumber), offset);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
offset += buffer.length;
|
|
82
|
-
});
|
|
83
|
-
return output;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
padAudio(buffer, padStart = 0, seconds = 0) {
|
|
87
|
-
if (seconds === 0) return buffer;
|
|
88
|
-
if (padStart < 0)
|
|
89
|
-
throw new Error(
|
|
90
|
-
'Crunker: Parameter "padStart" in padAudio must be positive'
|
|
91
|
-
);
|
|
92
|
-
if (seconds < 0)
|
|
93
|
-
throw new Error(
|
|
94
|
-
'Crunker: Parameter "seconds" in padAudio must be positive'
|
|
95
|
-
);
|
|
96
|
-
|
|
97
|
-
const updatedBuffer = this._context.createBuffer(
|
|
98
|
-
buffer.numberOfChannels,
|
|
99
|
-
buffer.length + seconds * buffer.sampleRate,
|
|
100
|
-
buffer.sampleRate
|
|
101
|
-
);
|
|
102
|
-
|
|
103
|
-
for (
|
|
104
|
-
let channelNumber = 0;
|
|
105
|
-
channelNumber < buffer.numberOfChannels;
|
|
106
|
-
channelNumber += 1
|
|
107
|
-
) {
|
|
108
|
-
const channelData = buffer.getChannelData(channelNumber);
|
|
109
|
-
updatedBuffer
|
|
110
|
-
.getChannelData(channelNumber)
|
|
111
|
-
.set(channelData.subarray(0, padStart * buffer.sampleRate + 1), 0);
|
|
112
|
-
|
|
113
|
-
updatedBuffer
|
|
114
|
-
.getChannelData(channelNumber)
|
|
115
|
-
.set(
|
|
116
|
-
channelData.subarray(
|
|
117
|
-
padStart * buffer.sampleRate + 2,
|
|
118
|
-
updatedBuffer.length + 1
|
|
119
|
-
),
|
|
120
|
-
(padStart + seconds) * buffer.sampleRate
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return updatedBuffer;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
play(buffer) {
|
|
128
|
-
const source = this._context.createBufferSource();
|
|
129
|
-
source.buffer = buffer;
|
|
130
|
-
source.connect(this._context.destination);
|
|
131
|
-
source.start();
|
|
132
|
-
return source;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export(buffer, audioType) {
|
|
136
|
-
const type = audioType || "audio/mp3";
|
|
137
|
-
const recorded = this._interleave(buffer);
|
|
138
|
-
const dataview = this._writeHeaders(recorded);
|
|
139
|
-
const audioBlob = new Blob([dataview], { type: type });
|
|
140
|
-
|
|
141
|
-
return {
|
|
142
|
-
blob: audioBlob,
|
|
143
|
-
url: this._renderURL(audioBlob),
|
|
144
|
-
element: this._renderAudioElement(audioBlob, type),
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
download(blob, filename) {
|
|
149
|
-
const name = filename || "crunker";
|
|
150
|
-
const a = document.createElement("a");
|
|
151
|
-
a.style = "display: none";
|
|
152
|
-
a.href = this._renderURL(blob);
|
|
153
|
-
a.download = `${name}.${blob.type.split("/")[1]}`;
|
|
154
|
-
a.click();
|
|
155
|
-
return a;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
notSupported(callback) {
|
|
159
|
-
return !this._isSupported() && callback();
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
close() {
|
|
163
|
-
this._context.close();
|
|
164
|
-
return this;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
_maxDuration(buffers) {
|
|
168
|
-
return Math.max.apply(
|
|
169
|
-
Math,
|
|
170
|
-
buffers.map((buffer) => buffer.duration)
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
_maxNumberOfChannels(buffers) {
|
|
175
|
-
return Math.max.apply(
|
|
176
|
-
Math,
|
|
177
|
-
buffers.map((buffer) => buffer.numberOfChannels)
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
_totalLength(buffers) {
|
|
182
|
-
return buffers.map((buffer) => buffer.length).reduce((a, b) => a + b, 0);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
_isSupported() {
|
|
186
|
-
return "AudioContext" in window;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
_writeHeaders(buffer) {
|
|
190
|
-
let arrayBuffer = new ArrayBuffer(44 + buffer.length * 2),
|
|
191
|
-
view = new DataView(arrayBuffer);
|
|
192
|
-
|
|
193
|
-
this._writeString(view, 0, "RIFF");
|
|
194
|
-
view.setUint32(4, 32 + buffer.length * 2, true);
|
|
195
|
-
this._writeString(view, 8, "WAVE");
|
|
196
|
-
this._writeString(view, 12, "fmt ");
|
|
197
|
-
view.setUint32(16, 16, true);
|
|
198
|
-
view.setUint16(20, 1, true);
|
|
199
|
-
view.setUint16(22, 2, true);
|
|
200
|
-
view.setUint32(24, this._sampleRate, true);
|
|
201
|
-
view.setUint32(28, this._sampleRate * 4, true);
|
|
202
|
-
view.setUint16(32, 4, true);
|
|
203
|
-
view.setUint16(34, 16, true);
|
|
204
|
-
this._writeString(view, 36, "data");
|
|
205
|
-
view.setUint32(40, buffer.length * 2, true);
|
|
206
|
-
|
|
207
|
-
return this._floatTo16BitPCM(view, buffer, 44);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
_floatTo16BitPCM(dataview, buffer, offset) {
|
|
211
|
-
for (var i = 0; i < buffer.length; i++, offset += 2) {
|
|
212
|
-
let tmp = Math.max(-1, Math.min(1, buffer[i]));
|
|
213
|
-
dataview.setInt16(offset, tmp < 0 ? tmp * 0x8000 : tmp * 0x7fff, true);
|
|
214
|
-
}
|
|
215
|
-
return dataview;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
_writeString(dataview, offset, header) {
|
|
219
|
-
let output;
|
|
220
|
-
for (var i = 0; i < header.length; i++) {
|
|
221
|
-
dataview.setUint8(offset + i, header.charCodeAt(i));
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
_interleave(input) {
|
|
226
|
-
let buffer = input.getChannelData(0),
|
|
227
|
-
length = buffer.length * 2,
|
|
228
|
-
result = new Float32Array(length),
|
|
229
|
-
index = 0,
|
|
230
|
-
inputIndex = 0;
|
|
231
|
-
|
|
232
|
-
while (index < length) {
|
|
233
|
-
result[index++] = buffer[inputIndex];
|
|
234
|
-
result[index++] = buffer[inputIndex];
|
|
235
|
-
inputIndex++;
|
|
236
|
-
}
|
|
237
|
-
return result;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
_renderAudioElement(blob, type) {
|
|
241
|
-
const audio = document.createElement("audio");
|
|
242
|
-
audio.controls = "controls";
|
|
243
|
-
audio.type = type;
|
|
244
|
-
audio.src = this._renderURL(blob);
|
|
245
|
-
return audio;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
_renderURL(blob) {
|
|
249
|
-
return (window.URL || window.webkitURL).createObjectURL(blob);
|
|
250
|
-
}
|
|
251
|
-
}
|