forked from ai/audio-recorder-polyfill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wave-encoder.js
74 lines (66 loc) · 2.06 KB
/
wave-encoder.js
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
// Copied from https://github.com/chris-rudmin/Recorderjs
module.exports = function () {
var BYTES_PER_SAMPLE = 2
var recorded = []
function encode (buffer) {
var length = buffer.length
var data = new Uint8Array(length * BYTES_PER_SAMPLE)
for (var i = 0; i < length; i++) {
var index = i * BYTES_PER_SAMPLE
var sample = buffer[i]
if (sample > 1) {
sample = 1
} else if (sample < -1) {
sample = -1
}
sample = sample * 32768
data[index] = sample
data[index + 1] = sample >> 8
}
recorded.push(data)
}
function dump (sampleRate) {
var bufferLength = recorded.length ? recorded[0].length : 0
var length = recorded.length * bufferLength
var wav = new Uint8Array(44 + length)
var view = new DataView(wav.buffer)
// RIFF identifier 'RIFF'
view.setUint32(0, 1380533830, false)
// file length minus RIFF identifier length and file description length
view.setUint32(4, 36 + length, true)
// RIFF type 'WAVE'
view.setUint32(8, 1463899717, false)
// format chunk identifier 'fmt '
view.setUint32(12, 1718449184, false)
// format chunk length
view.setUint32(16, 16, true)
// sample format (raw)
view.setUint16(20, 1, true)
// channel count
view.setUint16(22, 1, true)
// sample rate
view.setUint32(24, sampleRate, true)
// byte rate (sample rate * block align)
view.setUint32(28, sampleRate * BYTES_PER_SAMPLE, true)
// block align (channel count * bytes per sample)
view.setUint16(32, BYTES_PER_SAMPLE, true)
// bits per sample
view.setUint16(34, 8 * BYTES_PER_SAMPLE, true)
// data chunk identifier 'data'
view.setUint32(36, 1684108385, false)
// data chunk length
view.setUint32(40, length, true)
for (var i = 0; i < recorded.length; i++) {
wav.set(recorded[i], i * bufferLength + 44)
}
recorded = []
postMessage(wav.buffer, [wav.buffer])
}
onmessage = function (e) {
if (e.data[0] === 'encode') {
encode(e.data[1])
} else {
dump(e.data[1])
}
}
}