File size: 10,005 Bytes
d7d3273
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
export type EngineState =
  | 'uninitialized'
  | 'initializing'
  | 'running'
  | 'suspended'
  | 'failed'
  | 'closed'
  | 'fallback'

// PCM Int16 -> Float32 conversion
function pcmToFloat32(int16: Int16Array): Float32Array {
  const float32 = new Float32Array(int16.length)
  for (let i = 0; i < int16.length; i++) {
    const sample = int16[i]!
    float32[i] = sample < 0 ? sample / 0x8000 : sample / 0x7fff
  }
  return float32
}

// PCM Int16 -> WAV Blob (for fallback mode)
function pcmToWav(pcmBuffer: ArrayBuffer): Blob {
  const int16 = new Int16Array(pcmBuffer)
  const numSamples = int16.length
  if (numSamples === 0) {
    const empty = new ArrayBuffer(44)
    const v = new DataView(empty)
    const w = (o: number, s: string) => { for (let i = 0; i < s.length; i++) v.setUint8(o + i, s.charCodeAt(i)) }
    w(0, 'RIFF'); v.setUint32(4, 36, true); w(8, 'WAVE')
    w(12, 'fmt '); v.setUint32(16, 16, true); v.setUint16(20, 1, true)
    v.setUint16(22, 1, true); v.setUint32(24, 24000, true); v.setUint32(28, 48000, true)
    v.setUint16(32, 2, true); v.setUint16(34, 16, true)
    w(36, 'data'); v.setUint32(40, 0, true)
    return new Blob([empty], { type: 'audio/wav' })
  }

  const sampleRate = 24000
  const numChannels = 1
  const bitsPerSample = 16
  const blockAlign = (numChannels * bitsPerSample) / 8
  const byteRate = sampleRate * blockAlign
  const dataSize = numSamples * blockAlign
  const totalSize = 44 + dataSize

  const buf = new ArrayBuffer(totalSize)
  const v = new DataView(buf)

  const w = (o: number, s: string) => { for (let i = 0; i < s.length; i++) v.setUint8(o + i, s.charCodeAt(i)) }

  w(0, 'RIFF')
  v.setUint32(4, totalSize - 8, true)
  w(8, 'WAVE')
  w(12, 'fmt ')
  v.setUint32(16, 16, true) // PCM
  v.setUint16(20, 1, true)
  v.setUint16(22, numChannels, true)
  v.setUint32(24, sampleRate, true)
  v.setUint32(28, byteRate, true)
  v.setUint16(32, blockAlign, true)
  v.setUint16(34, bitsPerSample, true)
  w(36, 'data')
  v.setUint32(40, dataSize, true)

  new Int16Array(buf, 44, numSamples).set(int16)
  return new Blob([buf], { type: 'audio/wav' })
}

// Safety limit: 60 seconds = 1,440,000 samples @ 24kHz
const MAX_FALLBACK_SAMPLES = 24000 * 60

export class AudioEngine {
  private ctx: AudioContext | null = null
  private state: EngineState = 'uninitialized'
  private nextTime: number = 0
  private keepaliveOsc: OscillatorNode | null = null
  private keepaliveGain: GainNode | null = null

  // MediaStream keepalive (secondary — most reliable for WebKit)
  private silentAudioEl: HTMLAudioElement | null = null
  private mediaStreamDest: MediaStreamAudioDestinationNode | null = null

  // Fallback accumulators
  private chunks: Int16Array[] = []
  private totalSamples: number = 0

  // State change callback
  private onStateChange: ((s: EngineState) => void) | null = null

  constructor(onStateChange?: (s: EngineState) => void) {
    this.onStateChange = onStateChange || null
  }

  getState(): EngineState {
    return this.state
  }

  private setState(s: EngineState) {
    this.state = s
    this.onStateChange?.(s)
  }

  async initialize(): Promise<boolean> {
    if (this.state === 'running') return true
    if (this.state === 'fallback') return false

    this.setState('initializing')

    try {
      // Create AudioContext (MUST be called during user gesture)
      const ctx = new (window.AudioContext || (window as any).webkitAudioContext)()
      this.ctx = ctx

      // Primary keepalive: low-freq oscillator (inaudible, keeps context running)
      this.keepaliveGain = ctx.createGain()
      this.keepaliveGain.gain.value = 0.001
      this.keepaliveOsc = ctx.createOscillator()
      this.keepaliveOsc.frequency.value = 1
      this.keepaliveOsc.connect(this.keepaliveGain)
      this.keepaliveGain.connect(ctx.destination)
      this.keepaliveOsc.start()

      // Secondary keepalive: MediaStream destination trick
      // WebKit treats a live MediaStream as non-idle and won't suspend the context
      try {
        this.mediaStreamDest = ctx.createMediaStreamDestination()
        const keepaliveGain2 = ctx.createGain()
        keepaliveGain2.gain.value = 0
        keepaliveGain2.connect(this.mediaStreamDest)
        this.silentAudioEl = new Audio()
        this.silentAudioEl.srcObject = this.mediaStreamDest.stream
        this.silentAudioEl.play().catch(() => {})
      } catch {
        // MediaStream destination not supported; oscillator keepalive is sufficient
      }

      this.nextTime = ctx.currentTime

      // Handle state changes
      ctx.onstatechange = () => this.handleStateChange()

      this.setState('running')

      console.log('[AudioEngine] AudioContext created, keepalive running')
      return true
    } catch (e) {
      console.warn('[AudioEngine] Failed to create AudioContext, switching to fallback mode', e)
      this.enterFallbackMode()
      return false
    }
  }

  private handleStateChange() {
    if (!this.ctx) return
    const ctx = this.ctx

    if (ctx.state === 'suspended') {
      console.log('[AudioEngine] context suspended, attempting resume')
      this.setState('suspended')
      ctx.resume()
        .then(() => {
          if (this.ctx?.state === 'running') {
            this.setState('running')
            // Reset nextTime after suspension
            this.nextTime = Math.max(this.nextTime, this.ctx.currentTime)
          }
        })
        .catch(() => {
          console.warn('[AudioEngine] resume() failed, switching to fallback')
          this.enterFallbackMode()
        })
    } else if (ctx.state === 'closed') {
      console.warn('[AudioEngine] context closed, switching to fallback')
      this.enterFallbackMode()
    } else if (ctx.state === 'running') {
      this.setState('running')
    }
  }

  private enterFallbackMode() {
    if (this.state === 'fallback') return
    console.log('[AudioEngine] entering fallback mode')
    this.setState('fallback')
    if (this.ctx) {
      this.ctx.close().catch(() => {})
      this.ctx = null
    }
    if (this.keepaliveOsc) {
      try { this.keepaliveOsc.stop() } catch {}
      this.keepaliveOsc = null
    }
    if (this.keepaliveGain) {
      this.keepaliveGain = null
    }
    this.cleanupMediaStream()
  }

  private cleanupMediaStream() {
    if (this.silentAudioEl) {
      this.silentAudioEl.pause()
      this.silentAudioEl.srcObject = null
      this.silentAudioEl = null
    }
    if (this.mediaStreamDest) {
      this.mediaStreamDest.disconnect()
      this.mediaStreamDest = null
    }
  }

  enqueuePCM(pcmData: ArrayBuffer): void {
    if (this.state === 'fallback') {
      this.enqueueFallback(pcmData)
      return
    }
    if (this.state !== 'running' || !this.ctx) {
      // Drop audio until initialized; user needs to click Connect
      return
    }

    const ctx = this.ctx
    const int16 = new Int16Array(pcmData)
    const float32 = pcmToFloat32(int16)

    // Apply micro-fades at chunk boundaries to prevent clicking artifacts.
    // 128 samples @ 24kHz = ~5.3ms fade — inaudible as a fade but eliminates
    // the DC discontinuity between consecutive scheduled chunks.
    const FADE_LEN = Math.min(128, float32.length >> 1)
    if (FADE_LEN > 0) {
      for (let i = 0; i < FADE_LEN; i++) {
        const t = i / FADE_LEN
        float32[i] *= t                         // fade in
        float32[float32.length - 1 - i] *= t    // fade out
      }
    }

    // Create audio buffer
    const audioBuffer = ctx.createBuffer(1, float32.length, 24000)
    audioBuffer.getChannelData(0).set(float32)

    // Create source node
    const source = ctx.createBufferSource()
    source.buffer = audioBuffer

    // Connect through a gain node for consistency
    // We'll use the keepalive gain's destination, but create separate gain per buffer
    // Actually just connect to destination directly; we have keepalive running already
    source.connect(ctx.destination)

    // Schedule at the correct time for gapless playback
    const startTime = Math.max(ctx.currentTime, this.nextTime)
    source.start(startTime)

    // Advance the scheduled time
    this.nextTime = startTime + audioBuffer.duration

    // Cleanup when playback ends
    source.onended = () => {
      try { source.disconnect() } catch {}
    }
  }

  private enqueueFallback(pcmData: ArrayBuffer) {
    const chunk = new Int16Array(pcmData)
    this.chunks.push(chunk)
    this.totalSamples += chunk.length

    // Safety: if we exceed 60s, force-flush so user hears SOMETHING
    if (this.totalSamples >= MAX_FALLBACK_SAMPLES) {
      console.warn('[AudioEngine] Fallback buffer exceeded 60s, force-flushing')
      // We don't flush here; flush() is called externally on `done`
      // The caller needs to decide how to play the WAV
    }
  }

  /**
   * In fallback mode: concatenate all accumulated PCM and return a WAV Blob.
   * In Web Audio mode: returns null (audio already scheduled).
   */
  flush(): Blob | null {
    if (this.state === 'fallback' && this.totalSamples > 0) {
      const combined = new Int16Array(this.totalSamples)
      let offset = 0
      for (const chunk of this.chunks) {
        combined.set(chunk, offset)
        offset += chunk.length
      }
      this.chunks = []
      this.totalSamples = 0
      return pcmToWav(combined.buffer)
    }
    // In Web Audio mode, nothing to do
    return null
  }

  stop(): void {
    // Stop keepalive
    if (this.keepaliveOsc) {
      try { this.keepaliveOsc.stop() } catch {}
      try { this.keepaliveOsc.disconnect() } catch {}
      this.keepaliveOsc = null
    }
    if (this.keepaliveGain) {
      try { this.keepaliveGain.disconnect() } catch {}
      this.keepaliveGain = null
    }
    this.cleanupMediaStream()

    // Close AudioContext
    if (this.ctx) {
      this.ctx.close().catch(() => {})
      this.ctx = null
    }

    // Clear fallback accumulators
    this.chunks = []
    this.totalSamples = 0

    this.nextTime = 0
    this.setState('uninitialized')
  }
}