File size: 6,086 Bytes
b830719
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5151e54
b830719
 
5151e54
b830719
 
 
5151e54
 
 
 
 
 
 
 
 
 
 
 
 
 
b830719
5151e54
b830719
 
5151e54
 
 
 
 
 
 
 
b830719
 
 
 
 
5151e54
b830719
 
 
 
 
 
 
 
5151e54
 
 
 
b830719
 
5151e54
b830719
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5151e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b830719
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
 * Audio capture and processing utilities
 *
 * Uses Web Audio API with ScriptProcessorNode for real-time PCM audio capture
 */

const WHISPER_SAMPLING_RATE = 16000;

export class AudioRecorder {
  constructor(onDataAvailable) {
    this.onDataAvailable = onDataAvailable;
    this.audioContext = null;
    this.stream = null;
    this.source = null;
    this.processor = null;
    this.isRecording = false;
    this.audioChunks = [];
  }

  async start(deviceId = null) {
    /**
     * Start recording audio from microphone using Web Audio API
     * @param {string} deviceId - Optional specific device ID to use
     */
    try {
      // Request microphone access
      // Note: Disable echo cancellation and noise suppression in Chrome
      // as they can conflict with cross-origin isolation headers
      const audioConstraints = {
        channelCount: 1,
        echoCancellation: false,
        noiseSuppression: false,
        autoGainControl: false,
      };

      // If specific device requested, add deviceId constraint
      if (deviceId) {
        audioConstraints.deviceId = { exact: deviceId };
      }

      this.stream = await navigator.mediaDevices.getUserMedia({
        audio: audioConstraints
      });

      // Create AudioContext at native sample rate (browser will choose optimal rate)
      this.audioContext = new AudioContext();
      const nativeSampleRate = this.audioContext.sampleRate;

      // Resume AudioContext if suspended (required by some browsers)
      if (this.audioContext.state === 'suspended') {
        await this.audioContext.resume();
      }

      // Create source from stream
      this.source = this.audioContext.createMediaStreamSource(this.stream);

      // Create ScriptProcessorNode (deprecated but works everywhere)
      // Use larger buffer at native rate
      const bufferSize = 4096;
      this.processor = this.audioContext.createScriptProcessor(bufferSize, 1, 1);

      this.processor.onaudioprocess = (event) => {
        if (!this.isRecording) return;

        const inputData = event.inputBuffer.getChannelData(0);

        // Resample from native rate to 16kHz
        const resampled = this.resample(inputData, nativeSampleRate, WHISPER_SAMPLING_RATE);

        this.audioChunks.push(resampled);

        if (this.onDataAvailable) {
          this.onDataAvailable(resampled);
        }
      };

      // Connect: source -> processor -> destination
      this.source.connect(this.processor);
      this.processor.connect(this.audioContext.destination);

      this.isRecording = true;

      return true;
    } catch (error) {
      console.error('Failed to start recording:', error);
      throw error;
    }
  }

  resample(audioData, sourceSampleRate, targetSampleRate) {
    /**
     * Simple linear interpolation resampler
     * Converts audio from sourceSampleRate to targetSampleRate
     */
    if (sourceSampleRate === targetSampleRate) {
      return new Float32Array(audioData);
    }

    const ratio = sourceSampleRate / targetSampleRate;
    const newLength = Math.round(audioData.length / ratio);
    const result = new Float32Array(newLength);

    for (let i = 0; i < newLength; i++) {
      const srcIndex = i * ratio;
      const srcIndexFloor = Math.floor(srcIndex);
      const srcIndexCeil = Math.min(srcIndexFloor + 1, audioData.length - 1);
      const t = srcIndex - srcIndexFloor;

      // Linear interpolation
      result[i] = audioData[srcIndexFloor] * (1 - t) + audioData[srcIndexCeil] * t;
    }

    return result;
  }

  requestData() {
    /**
     * No-op for ScriptProcessor (data comes automatically)
     */
    // Data is emitted automatically via onaudioprocess
  }

  async stop() {
    /**
     * Stop recording and return complete audio as Float32Array
     */
    return new Promise((resolve) => {
      this.isRecording = false;

      // Disconnect nodes
      if (this.processor) {
        this.processor.disconnect();
        this.processor = null;
      }

      if (this.source) {
        this.source.disconnect();
        this.source = null;
      }

      // Concatenate all chunks
      let totalLength = 0;
      for (const chunk of this.audioChunks) {
        totalLength += chunk.length;
      }

      const completeAudio = new Float32Array(totalLength);
      let offset = 0;
      for (const chunk of this.audioChunks) {
        completeAudio.set(chunk, offset);
        offset += chunk.length;
      }

      // Clean up
      this.cleanup();

      resolve(completeAudio);
    });
  }

  cleanup() {
    /**
     * Clean up resources
     */
    if (this.stream) {
      this.stream.getTracks().forEach(track => track.stop());
      this.stream = null;
    }

    if (this.audioContext && this.audioContext.state !== 'closed') {
      this.audioContext.close();
      this.audioContext = null;
    }

    this.audioChunks = [];
    this.isRecording = false;
  }
}

export class AudioProcessor {
  /**
   * Process audio chunks for real-time transcription
   */
  constructor(sampleRate = WHISPER_SAMPLING_RATE) {
    this.sampleRate = sampleRate;
    this.audioBuffer = new Float32Array(0);
  }

  appendChunk(chunk) {
    /**
     * Append new audio chunk to buffer
     */
    const newBuffer = new Float32Array(this.audioBuffer.length + chunk.length);
    newBuffer.set(this.audioBuffer);
    newBuffer.set(chunk, this.audioBuffer.length);
    this.audioBuffer = newBuffer;
  }

  getBuffer() {
    /**
     * Get current audio buffer
     */
    return this.audioBuffer;
  }

  getDuration() {
    /**
     * Get current buffer duration in seconds
     */
    return this.audioBuffer.length / this.sampleRate;
  }

  reset() {
    /**
     * Clear audio buffer
     */
    this.audioBuffer = new Float32Array(0);
  }

  trimToSize(maxDuration) {
    /**
     * Trim buffer to maximum duration (in seconds)
     */
    const maxSamples = Math.floor(maxDuration * this.sampleRate);
    if (this.audioBuffer.length > maxSamples) {
      this.audioBuffer = this.audioBuffer.slice(-maxSamples);
    }
  }
}

export { WHISPER_SAMPLING_RATE };