| 1038 | // Initialize AudioContext with 16kHz to match unified PCM pipeline |
| 1039 | // (Rust resamples all audio to 16kHz for transcription) |
| 1040 | const initializeAudio = async () => { |
| 1041 | try { |
| 1042 | const targetSampleRate = PCM_SAMPLE_RATE; // 16kHz - matches unified pipeline |
| 1043 | const blob = new Blob([workletCode], { type: 'application/javascript' }); |
| 1044 | const workletUrl = URL.createObjectURL(blob); |
| 1045 | |
| 1046 | audioContext = new AudioContext({ sampleRate: targetSampleRate }); |
| 1047 | await audioContext.audioWorklet.addModule(workletUrl); |
| 1048 | URL.revokeObjectURL(workletUrl); |
| 1049 | |
| 1050 | workletNode = new AudioWorkletNode(audioContext, 'pcm-player-processor', { |
| 1051 | outputChannelCount: [2], |
| 1052 | }); |
| 1053 | |
| 1054 | audioDestination = audioContext.createMediaStreamDestination(); |
| 1055 | workletNode.connect(audioDestination); |
| 1056 | screenAudioStream = audioDestination.stream; |
| 1057 | |
| 1058 | audioInitialized = true; |
| 1059 | configuredSampleRate = targetSampleRate; |
| 1060 | Logger.info("TAURI_STREAM", `Audio MediaStream initialized (${targetSampleRate}Hz, mono) - matches unified PCM pipeline`); |
| 1061 | } catch (error) { |
| 1062 | Logger.error("TAURI_STREAM", `Failed to initialize audio stream: ${error}`); |
| 1063 | } |
| 1064 | }; |
| 1065 | |
| 1066 | // Initialize audio upfront |
| 1067 | await initializeAudio(); |