atlury commited on
Commit
0410099
·
verified ·
1 Parent(s): 4d25d8f

Delete MicrophoneAudio.js

Browse files
Files changed (1) hide show
  1. MicrophoneAudio.js +0 -119
MicrophoneAudio.js DELETED
@@ -1,119 +0,0 @@
1
- class MicrophoneAudio {
2
- constructor(options) {
3
- console.log('Initializing MicrophoneAudio');
4
- this.options = {
5
- sampleRate: 16000,
6
- channels: 1,
7
- ...options,
8
- };
9
- this.stream = null;
10
- this.audioContext = null;
11
- this.sourceNode = null;
12
- this.workletNode = null;
13
- this.buffer = new Float32Array();
14
- console.log(`MicrophoneAudio options: ${JSON.stringify(this.options)}`);
15
- }
16
-
17
- getDeviceId() {
18
- console.log('Getting device ID');
19
- return navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
20
- const deviceId = stream.getTracks()[0].getSettings().deviceId;
21
- console.log("The device Id is", deviceId);
22
- return deviceId;
23
- });
24
- }
25
-
26
- async start() {
27
- console.log('Starting MicrophoneAudio');
28
- try {
29
- this.stream = await navigator.mediaDevices.getUserMedia({
30
- audio: {
31
- sampleRate: this.options.sampleRate,
32
- channelCount: this.options.channels,
33
- },
34
- });
35
- console.log('MediaStream acquired');
36
-
37
- this.getDeviceId().then((deviceId) => {
38
- console.log("The device Id is", deviceId);
39
- });
40
- this.audioContext = new AudioContext({
41
- sampleRate: this.options.sampleRate,
42
- });
43
-
44
- await this.audioContext.audioWorklet.addModule(
45
- URL.createObjectURL(new Blob([`
46
- class AudioProcessor extends AudioWorkletProcessor {
47
- constructor() {
48
- super();
49
- this.buffer = new Float32Array();
50
- }
51
-
52
- process(inputs, outputs, parameters) {
53
- const input = inputs[0];
54
- const channelData = input[0];
55
-
56
- this.buffer = Float32Array.from([...this.buffer, ...channelData]);
57
-
58
- while (this.buffer.length >= ${this.options.windowSizeSamples}) {
59
- const chunk = this.buffer.slice(0, ${this.options.windowSizeSamples});
60
- this.port.postMessage(chunk);
61
- this.buffer = this.buffer.slice(${this.options.windowSizeSamples});
62
- }
63
-
64
- return true;
65
- }
66
- }
67
-
68
- registerProcessor('audio-processor', AudioProcessor);
69
- `], { type: 'application/javascript' }))
70
- );
71
-
72
- this.sourceNode = this.audioContext.createMediaStreamSource(this.stream);
73
- this.workletNode = new AudioWorkletNode(this.audioContext, 'audio-processor');
74
-
75
- this.workletNode.port.onmessage = (event) => {
76
- this.options.onAudioData(event.data);
77
- };
78
-
79
- this.sourceNode.connect(this.workletNode);
80
- this.workletNode.connect(this.audioContext.destination);
81
- console.log('AudioWorklet added and connected');
82
- } catch (error) {
83
- console.error('Error starting microphone:', error);
84
- throw error;
85
- }
86
- }
87
-
88
- stop() {
89
- console.log('Stopping MicrophoneAudio');
90
- if (this.workletNode) {
91
- this.workletNode.port.postMessage('flush');
92
- this.workletNode.disconnect();
93
- this.workletNode = null;
94
- }
95
-
96
- if (this.sourceNode) {
97
- this.sourceNode.disconnect();
98
- this.sourceNode = null;
99
- }
100
-
101
- if (this.audioContext) {
102
- this.audioContext.close();
103
- this.audioContext = null;
104
- }
105
-
106
- if (this.stream) {
107
- this.stream.getTracks().forEach((track) => track.stop());
108
- this.stream = null;
109
- }
110
-
111
- if (this.buffer.length > 0) {
112
- this.options.onAudioData(this.buffer);
113
- this.buffer = new Float32Array();
114
- }
115
- console.log('MicrophoneAudio stopped');
116
- }
117
- }
118
-
119
- export default MicrophoneAudio;