* Start audio-only capture stream. * Independent from video - use startVideoStream separately if you also need video. * Works on both desktop (macOS) and iOS.
()
| 685 | * Works on both desktop (macOS) and iOS. |
| 686 | */ |
| 687 | async startAudioStream(): Promise<AudioStreamResult> { |
| 688 | if (!isTauri()) { |
| 689 | throw new Error('Audio capture only available in Tauri'); |
| 690 | } |
| 691 | |
| 692 | Logger.info("TAURI_STREAM", `Starting audio-only capture stream`); |
| 693 | |
| 694 | let audioContext: AudioContext | null = null; |
| 695 | let workletNode: AudioWorkletNode | null = null; |
| 696 | let audioStream: MediaStream | null = null; |
| 697 | let audioDestination: MediaStreamAudioDestinationNode | null = null; |
| 698 | let audioChunkCount = 0; |
| 699 | let isActive = true; |
| 700 | let configuredSampleRate = 0; |
| 701 | |
| 702 | // AudioWorklet code for mono PCM playback using efficient ring buffer with prebuffering |
| 703 | // NOTE: This worklet runs at 16kHz to match the unified PCM pipeline |
| 704 | const workletCode = ` |
| 705 | class PCMPlayerProcessor extends AudioWorkletProcessor { |
| 706 | constructor() { |
| 707 | super(); |
| 708 | // Ring buffer: ~4 seconds at 16kHz (power of 2 for fast modulo) |
| 709 | this.bufferSize = 65536; |
| 710 | this.bufferMask = this.bufferSize - 1; |
| 711 | this.buffer = new Float32Array(this.bufferSize); |
| 712 | this.writeIndex = 0; |
| 713 | this.readIndex = 0; |
| 714 | |
| 715 | // Prebuffer: wait for ~100ms of audio before starting playback |
| 716 | // This absorbs jitter from network/IPC delays |
| 717 | this.prebufferSamples = 1600; // ~100ms at 16kHz (unified PCM pipeline rate) |
| 718 | this.prebuffering = true; |
| 719 | this.underrunCount = 0; |
| 720 | |
| 721 | this.port.onmessage = (e) => { |
| 722 | const samples = e.data; |
| 723 | const len = samples.length; |
| 724 | for (let i = 0; i < len; i++) { |
| 725 | this.buffer[(this.writeIndex + i) & this.bufferMask] = samples[i]; |
| 726 | } |
| 727 | this.writeIndex = (this.writeIndex + len) & this.bufferMask; |
| 728 | }; |
| 729 | } |
| 730 | |
| 731 | process(inputs, outputs, parameters) { |
| 732 | const output = outputs[0]; |
| 733 | const frameSize = output[0].length; |
| 734 | const available = (this.writeIndex - this.readIndex) & this.bufferMask; |
| 735 | |
| 736 | // Prebuffering phase: wait until we have enough audio buffered |
| 737 | if (this.prebuffering) { |
| 738 | if (available >= this.prebufferSamples) { |
| 739 | this.prebuffering = false; |
| 740 | } else { |
| 741 | // Output silence while prebuffering |
| 742 | for (let ch = 0; ch < output.length; ch++) { |
| 743 | output[ch].fill(0); |
| 744 | } |
no test coverage detected