-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioRecorder.js
More file actions
44 lines (39 loc) · 1.4 KB
/
Copy pathAudioRecorder.js
File metadata and controls
44 lines (39 loc) · 1.4 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
// src/utils/AudioRecorder.js
export default class AudioRecorder {
constructor(onAudioChunk) {
this.onAudioChunk = onAudioChunk; // Float32Array 音声データを受け取るコールバック
this.mediaRecorder = null;
this.audioContext = null;
this.sourceNode = null;
this.processorNode = null;
}
async start() {
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
throw new Error("getUserMedia not supported");
}
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
this.sourceNode = this.audioContext.createMediaStreamSource(stream);
this.processorNode = this.audioContext.createScriptProcessor(4096, 1, 1);
this.sourceNode.connect(this.processorNode);
this.processorNode.connect(this.audioContext.destination);
this.processorNode.onaudioprocess = (event) => {
const inputData = event.inputBuffer.getChannelData(0);
this.onAudioChunk(inputData);
};
}
stop() {
if (this.processorNode) {
this.processorNode.disconnect();
this.processorNode = null;
}
if (this.sourceNode) {
this.sourceNode.disconnect();
this.sourceNode = null;
}
if (this.audioContext) {
this.audioContext.close();
this.audioContext = null;
}
}
}