-
-
Notifications
You must be signed in to change notification settings - Fork 284
/
AudioRecording.js
155 lines (127 loc) Β· 4.67 KB
/
AudioRecording.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// @ts-ignore
import vmsg from "https://cdn.jsdelivr.net/npm/vmsg@0.4.0/vmsg.js"
export const create_recorder = async () => {
const recorder = new vmsg.Recorder({
wasmURL: new URL("https://unpkg.com/vmsg@0.4.0/vmsg.wasm", import.meta.url),
})
return {
start: async () => {
await recorder.initAudio()
await recorder.initWorker()
recorder.startRecording()
},
stop: async () => {
const blob = await recorder.stopRecording()
return window.URL.createObjectURL(blob)
},
}
}
// really nice but it can only record to audio/ogg or sometihng, nothing that works across all browsers
export const create_recorder_native = async () => {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true })
let chunks = []
const mediaRecorder = new MediaRecorder(stream, {})
mediaRecorder.ondataavailable = function (e) {
chunks.push(e.data)
}
let start_return_promise = new Promise((r) => {
mediaRecorder.onstart = r
})
let stop_return_promise = new Promise((r) => {
mediaRecorder.onstop = function (e) {
const blob = new Blob(chunks, { type: "audio/ogg; codecs=opus" })
chunks = []
const audioURL = window.URL.createObjectURL(blob)
r(audioURL)
}
})
return {
start: () => {
mediaRecorder.start()
start_return_promise
},
stop: () => {
mediaRecorder.stop()
return stop_return_promise
},
}
}
// taken from https://github.com/edoudou/create-silent-audio
// original license from https://github.com/edoudou/create-silent-audio/blob/master/LICENSE
/*
MIT License
Copyright (c) 2019 Edouard Short
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
export function createSilentAudio(time, freq = 44100) {
const length = time * freq
// @ts-ignore
const AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext
if (!AudioContext) {
console.log("No Audio Context")
}
const context = new AudioContext()
const audioFile = context.createBuffer(1, length, freq)
return URL.createObjectURL(bufferToWave(audioFile, length))
}
function bufferToWave(abuffer, len) {
let numOfChan = abuffer.numberOfChannels,
length = len * numOfChan * 2 + 44,
buffer = new ArrayBuffer(length),
view = new DataView(buffer),
channels = [],
i,
sample,
offset = 0,
pos = 0
// write WAVE header
setUint32(0x46464952)
setUint32(length - 8)
setUint32(0x45564157)
setUint32(0x20746d66)
setUint32(16)
setUint16(1)
setUint16(numOfChan)
setUint32(abuffer.sampleRate)
setUint32(abuffer.sampleRate * 2 * numOfChan)
setUint16(numOfChan * 2)
setUint16(16)
setUint32(0x61746164)
setUint32(length - pos - 4)
// write interleaved data
for (i = 0; i < abuffer.numberOfChannels; i++) channels.push(abuffer.getChannelData(i))
while (pos < length) {
for (i = 0; i < numOfChan; i++) {
// interleave channels
sample = Math.max(-1, Math.min(1, channels[i][offset])) // clamp
sample = (0.5 + sample < 0 ? sample * 32768 : sample * 32767) | 0 // scale to 16-bit signed int
view.setInt16(pos, sample, true) // write 16-bit sample
pos += 2
}
offset++ // next source sample
}
// create Blob
return new Blob([buffer], { type: "audio/wav" })
function setUint16(data) {
view.setUint16(pos, data, true)
pos += 2
}
function setUint32(data) {
view.setUint32(pos, data, true)
pos += 4
}
}