opencode-voice 0.1.4

A cli utility to control opencode using voice via the HTTP API
Documentation
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! cpal-based microphone capture: 16kHz mono i16 PCM audio recording.
//!
//! Tries the ideal config (16kHz mono) first.  When the device doesn't support
//! it — common on macOS — falls back to the device's native sample-rate and
//! channel count and resamples to 16kHz mono in the audio callback.

use anyhow::{Context, Result};
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{SampleFormat, StreamConfig};
use std::sync::{Arc, Mutex};
use std::time::Instant;

/// Lists all available audio input device names.
pub fn list_devices() -> Result<Vec<String>> {
    let host = cpal::default_host();
    let devices = host
        .input_devices()
        .context("Failed to enumerate input devices")?;
    Ok(devices.filter_map(|d| d.name().ok()).collect())
}

/// Resampling state carried across audio callbacks.
struct ResampleState {
    /// native_sample_rate / 16_000
    ratio: f64,
    /// Fractional input-sample position carried between callbacks.
    phase: f64,
}

/// Records microphone audio via cpal, always producing 16kHz mono i16 output.
pub struct CpalRecorder {
    device_name: Option<String>,
    samples: Arc<Mutex<Vec<i16>>>,
    stream: Option<cpal::Stream>,
    start_time: Option<Instant>,
    energy_tx: Option<tokio::sync::mpsc::UnboundedSender<f32>>,
}

impl CpalRecorder {
    /// Creates a new recorder for the given device (or default if None).
    pub fn new(device: Option<&str>) -> Result<Self> {
        Ok(CpalRecorder {
            device_name: device.map(|s| s.to_string()),
            samples: Arc::new(Mutex::new(Vec::new())),
            stream: None,
            start_time: None,
            energy_tx: None,
        })
    }

    /// Starts recording. Returns a receiver for RMS energy updates (0.0–1.0).
    pub fn start(&mut self) -> Result<tokio::sync::mpsc::UnboundedReceiver<f32>> {
        let host = cpal::default_host();

        // Find device
        let device = if let Some(ref name) = self.device_name {
            host.input_devices()
                .context("Failed to enumerate devices")?
                .find(|d| d.name().map(|n| n == *name).unwrap_or(false))
                .with_context(|| format!("Audio device '{}' not found", name))?
        } else {
            host.default_input_device().context(
                "No default audio input device found. Please check microphone connection.",
            )?
        };

        // Store the resolved device name for debugging.
        self.device_name = device.name().ok();

        let (energy_tx, energy_rx) = tokio::sync::mpsc::unbounded_channel::<f32>();

        let stream = self.build_stream(&device, energy_tx.clone())?;

        stream.play().context("Failed to start audio stream")?;

        self.stream = Some(stream);
        self.start_time = Some(Instant::now());
        self.energy_tx = Some(energy_tx);

        Ok(energy_rx)
    }

    /// Builds the cpal input stream.
    ///
    /// 1. Try 16kHz mono i16 (zero conversion — ideal).
    /// 2. Try 16kHz mono f32 (format conversion only).
    /// 3. Fall back to the device's native config and resample in the callback.
    fn build_stream(
        &self,
        device: &cpal::Device,
        energy_tx: tokio::sync::mpsc::UnboundedSender<f32>,
    ) -> Result<cpal::Stream> {
        let ideal_config = StreamConfig {
            channels: 1,
            sample_rate: cpal::SampleRate(16_000),
            buffer_size: cpal::BufferSize::Default,
        };

        let debug = std::env::var("RUST_LOG").is_ok();

        // --- Strategy 1: 16kHz mono i16 (ideal) ---
        if let Ok(stream) = self.build_direct_i16_stream(device, &ideal_config, energy_tx.clone()) {
            if debug {
                eprintln!("[audio] Using 16kHz mono i16 (ideal)");
            }
            return Ok(stream);
        }

        // --- Strategy 2: 16kHz mono f32 ---
        if let Ok(stream) = self.build_direct_f32_stream(device, &ideal_config, energy_tx.clone()) {
            if debug {
                eprintln!("[audio] Using 16kHz mono f32");
            }
            return Ok(stream);
        }

        // --- Strategy 3: native config + resample ---
        let default_config = device
            .default_input_config()
            .context("Failed to get any supported input config from audio device")?;

        let native_rate = default_config.sample_rate().0;
        let native_channels = default_config.channels();
        let native_format = default_config.sample_format();

        if debug {
            eprintln!(
                "[audio] Capturing at native {}Hz {}ch {:?}, resampling to 16kHz",
                native_rate, native_channels, native_format
            );
        }

        let stream_config = StreamConfig {
            channels: native_channels,
            sample_rate: cpal::SampleRate(native_rate),
            buffer_size: cpal::BufferSize::Default,
        };

        match native_format {
            SampleFormat::I16 => self.build_resampling_i16_stream(
                device,
                &stream_config,
                native_rate,
                native_channels,
                energy_tx,
            ),
            _ => self.build_resampling_f32_stream(
                device,
                &stream_config,
                native_rate,
                native_channels,
                energy_tx,
            ),
        }
        .context("Failed to build audio input stream with any supported configuration. Check microphone permissions.")
    }

    // ---------------------------------------------------------------
    //  Direct streams (16kHz mono, no resampling)
    // ---------------------------------------------------------------

    /// 16kHz mono i16 — no conversion needed.
    fn build_direct_i16_stream(
        &self,
        device: &cpal::Device,
        config: &StreamConfig,
        energy_tx: tokio::sync::mpsc::UnboundedSender<f32>,
    ) -> Result<cpal::Stream> {
        let samples_arc = Arc::clone(&self.samples);

        let stream = device
            .build_input_stream(
                config,
                move |data: &[i16], _: &cpal::InputCallbackInfo| {
                    if !data.is_empty() {
                        let sum_sq: f64 = data
                            .iter()
                            .map(|&s| {
                                let f = s as f64 / 32768.0;
                                f * f
                            })
                            .sum();
                        let rms = (sum_sq / data.len() as f64).sqrt() as f32;
                        let _ = energy_tx.send(rms.min(1.0));
                    }
                    if let Ok(mut guard) = samples_arc.try_lock() {
                        guard.extend_from_slice(data);
                    }
                },
                |err| eprintln!("Audio stream error: {}", err),
                None,
            )
            .map_err(|e| anyhow::anyhow!("i16 stream: {}", e))?;

        Ok(stream)
    }

    /// 16kHz mono f32 — format conversion only (f32 → i16).
    fn build_direct_f32_stream(
        &self,
        device: &cpal::Device,
        config: &StreamConfig,
        energy_tx: tokio::sync::mpsc::UnboundedSender<f32>,
    ) -> Result<cpal::Stream> {
        let samples_arc = Arc::clone(&self.samples);

        let stream = device
            .build_input_stream(
                config,
                move |data: &[f32], _: &cpal::InputCallbackInfo| {
                    if !data.is_empty() {
                        let sum_sq: f64 = data.iter().map(|&s| (s as f64) * (s as f64)).sum();
                        let rms = (sum_sq / data.len() as f64).sqrt() as f32;
                        let _ = energy_tx.send(rms.min(1.0));
                    }
                    if let Ok(mut guard) = samples_arc.try_lock() {
                        for &s in data {
                            let clamped = s.clamp(-1.0, 1.0);
                            guard.push((clamped * 32767.0) as i16);
                        }
                    }
                },
                |err| eprintln!("Audio stream error: {}", err),
                None,
            )
            .map_err(|e| anyhow::anyhow!("f32 stream: {}", e))?;

        Ok(stream)
    }

    // ---------------------------------------------------------------
    //  Resampling streams (native rate/channels → 16kHz mono)
    // ---------------------------------------------------------------

    /// Native-rate f32 stream with downmix + resample to 16kHz mono i16.
    fn build_resampling_f32_stream(
        &self,
        device: &cpal::Device,
        config: &StreamConfig,
        native_rate: u32,
        native_channels: u16,
        energy_tx: tokio::sync::mpsc::UnboundedSender<f32>,
    ) -> Result<cpal::Stream> {
        let samples_arc = Arc::clone(&self.samples);
        let state = Arc::new(Mutex::new(ResampleState {
            ratio: native_rate as f64 / 16_000.0,
            phase: 0.0,
        }));

        let stream = device
            .build_input_stream(
                config,
                move |data: &[f32], _: &cpal::InputCallbackInfo| {
                    let ch = native_channels as usize;

                    // --- Downmix to mono ---
                    let mono: Vec<f32> = if ch > 1 {
                        data.chunks(ch)
                            .map(|frame| frame.iter().sum::<f32>() / ch as f32)
                            .collect()
                    } else {
                        data.to_vec()
                    };

                    // --- RMS energy ---
                    if !mono.is_empty() {
                        let sum_sq: f64 = mono.iter().map(|&s| (s as f64) * (s as f64)).sum();
                        let rms = (sum_sq / mono.len() as f64).sqrt() as f32;
                        let _ = energy_tx.send(rms.min(1.0));
                    }

                    // --- Resample (linear interpolation) ---
                    if let Ok(mut st) = state.lock() {
                        let ratio = st.ratio;
                        let mut phase = st.phase;
                        let len = mono.len() as f64;
                        let mut resampled = Vec::new();

                        while phase < len {
                            let idx = phase as usize;
                            let frac = (phase - idx as f64) as f32;
                            let a = mono[idx];
                            let b = if idx + 1 < mono.len() {
                                mono[idx + 1]
                            } else {
                                a
                            };
                            let sample = a + (b - a) * frac;
                            let clamped = sample.clamp(-1.0, 1.0);
                            resampled.push((clamped * 32767.0) as i16);
                            phase += ratio;
                        }

                        st.phase = phase - len;

                        if let Ok(mut guard) = samples_arc.try_lock() {
                            guard.extend_from_slice(&resampled);
                        }
                    }
                },
                |err| eprintln!("Audio stream error: {}", err),
                None,
            )
            .map_err(|e| anyhow::anyhow!("Resampling f32 stream: {}", e))?;

        Ok(stream)
    }

    /// Native-rate i16 stream with downmix + resample to 16kHz mono i16.
    fn build_resampling_i16_stream(
        &self,
        device: &cpal::Device,
        config: &StreamConfig,
        native_rate: u32,
        native_channels: u16,
        energy_tx: tokio::sync::mpsc::UnboundedSender<f32>,
    ) -> Result<cpal::Stream> {
        let samples_arc = Arc::clone(&self.samples);
        let state = Arc::new(Mutex::new(ResampleState {
            ratio: native_rate as f64 / 16_000.0,
            phase: 0.0,
        }));

        let stream = device
            .build_input_stream(
                config,
                move |data: &[i16], _: &cpal::InputCallbackInfo| {
                    let ch = native_channels as usize;

                    // --- Convert to f32 and downmix to mono ---
                    let mono: Vec<f32> = if ch > 1 {
                        data.chunks(ch)
                            .map(|frame| {
                                let sum: f32 = frame.iter().map(|&s| s as f32 / 32768.0).sum();
                                sum / ch as f32
                            })
                            .collect()
                    } else {
                        data.iter().map(|&s| s as f32 / 32768.0).collect()
                    };

                    // --- RMS energy ---
                    if !mono.is_empty() {
                        let sum_sq: f64 = mono.iter().map(|&s| (s as f64) * (s as f64)).sum();
                        let rms = (sum_sq / mono.len() as f64).sqrt() as f32;
                        let _ = energy_tx.send(rms.min(1.0));
                    }

                    // --- Resample (linear interpolation) ---
                    if let Ok(mut st) = state.lock() {
                        let ratio = st.ratio;
                        let mut phase = st.phase;
                        let len = mono.len() as f64;
                        let mut resampled = Vec::new();

                        while phase < len {
                            let idx = phase as usize;
                            let frac = (phase - idx as f64) as f32;
                            let a = mono[idx];
                            let b = if idx + 1 < mono.len() {
                                mono[idx + 1]
                            } else {
                                a
                            };
                            let sample = a + (b - a) * frac;
                            let clamped = sample.clamp(-1.0, 1.0);
                            resampled.push((clamped * 32767.0) as i16);
                            phase += ratio;
                        }

                        st.phase = phase - len;

                        if let Ok(mut guard) = samples_arc.try_lock() {
                            guard.extend_from_slice(&resampled);
                        }
                    }
                },
                |err| eprintln!("Audio stream error: {}", err),
                None,
            )
            .map_err(|e| anyhow::anyhow!("Resampling i16 stream: {}", e))?;

        Ok(stream)
    }

    /// Stops recording and returns all captured samples (16kHz mono i16).
    pub fn stop(&mut self) -> Result<Vec<i16>> {
        // Drop the stream to stop recording
        self.stream = None;
        self.energy_tx = None;

        let samples = {
            let guard = self
                .samples
                .lock()
                .map_err(|_| anyhow::anyhow!("Failed to lock samples buffer"))?;
            guard.clone()
        };

        // Clear for next use
        if let Ok(mut guard) = self.samples.lock() {
            guard.clear();
        }

        Ok(samples)
    }

    /// Returns the resolved audio device name (available after `start()`).
    pub fn device_name(&self) -> Option<&str> {
        self.device_name.as_deref()
    }

    /// Returns the elapsed recording duration in seconds.
    pub fn duration(&self) -> f64 {
        self.start_time
            .map(|t| t.elapsed().as_secs_f64())
            .unwrap_or(0.0)
    }
}

// Safety: CpalRecorder is Send because Arc<Mutex<>> handles shared state.
// cpal::Stream is not Send on all platforms (e.g. macOS CoreAudio), but we
// manage it carefully: the stream is only dropped (in stop()), never accessed
// from another thread after creation.
unsafe impl Send for CpalRecorder {}