1use crate::*;
2use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
3use ringbuf::{traits::*, HeapProd, HeapRb};
4use std::{
5 sync::{
6 atomic::{AtomicBool, Ordering},
7 Arc,
8 },
9 time::{Duration, Instant},
10};
11
12#[derive(Clone, Default)]
13struct CaptureFaults {
14 overrun: Arc<AtomicBool>,
15 invalid_sample: Arc<AtomicBool>,
16 device_error: Arc<AtomicBool>,
17}
18
19pub struct CaptureSession {
21 session: Session,
22 stream: Option<cpal::Stream>,
23 stop: Arc<AtomicBool>,
24 finished: std::sync::mpsc::Receiver<()>,
25}
26impl CaptureSession {
27 pub fn start(engine: &Engine, mut options: SessionOptions) -> Result<Self, AsrError> {
28 let device = cpal::default_host()
29 .default_input_device()
30 .ok_or_else(|| capture_error("no default input device"))?;
31 let supported = device
32 .default_input_config()
33 .map_err(|_| capture_error("cannot read input configuration"))?;
34 options.input = AudioSpec::mono(supported.sample_rate())?;
35 let rate = options.input.sample_rate;
36 let channels = supported.channels() as usize;
37 if channels == 0 {
38 return Err(capture_error("input device has zero channels"));
39 }
40 let geometry = crate::coordinator::session_geometry(&options)?;
41 let capacity = geometry.capacity;
42 let max_chunk_frames = geometry.max_chunk_frames;
43 let batch = max_chunk_frames.min((rate as usize / 50).max(1)).max(1);
44 let session = engine.start(options)?;
45 let input = session.input();
46 let worker_input = input.clone();
47 let (producer, mut consumer) = HeapRb::<f32>::new(capacity.max(batch)).split();
48 let faults = CaptureFaults::default();
49 let stop = Arc::new(AtomicBool::new(false));
50 let worker_stop = stop.clone();
51 let worker_overrun = faults.overrun.clone();
52 let worker_invalid_sample = faults.invalid_sample.clone();
53 let worker_error = faults.device_error.clone();
54 let config: cpal::StreamConfig = supported.into();
55 let stream = match supported.sample_format() {
56 cpal::SampleFormat::F32 => {
57 build(&device, config, producer, channels, faults, |v: f32| v)
58 }
59 cpal::SampleFormat::I16 => {
60 build(&device, config, producer, channels, faults, normalize_i16)
61 }
62 cpal::SampleFormat::U16 => {
63 build(&device, config, producer, channels, faults, normalize_u16)
64 }
65 _ => {
66 return Err(AsrError::new(
67 ErrorKind::UnsupportedCapability,
68 "capture",
69 "input sample format is unsupported",
70 ))
71 }
72 }?;
73 let (tx, finished) = std::sync::mpsc::channel();
74 std::thread::Builder::new()
75 .name("asr-core-capture-drain".into())
76 .spawn(move || {
77 'drain: loop {
78 if worker_invalid_sample.load(Ordering::Acquire) {
79 worker_input.fail(AsrError::new(
80 ErrorKind::InvalidInput,
81 "capture",
82 "input device produced a non-finite sample",
83 ));
84 break;
85 }
86 if worker_overrun.load(Ordering::Acquire) {
87 worker_input.fail(AsrError::new(
88 ErrorKind::AudioOverrun,
89 "capture",
90 "capture ring overflowed",
91 ));
92 break;
93 }
94 if worker_error.load(Ordering::Acquire) {
95 worker_input.fail(capture_error("input device stream failed"));
96 break;
97 }
98 let mut samples = Vec::with_capacity(batch);
99 while samples.len() < batch {
100 match consumer.try_pop() {
101 Some(v) => samples.push(v),
102 None => break,
103 }
104 }
105 if !samples.is_empty() {
106 let mut chunk = AudioChunk {
107 samples,
108 spec: AudioSpec { sample_rate: rate },
109 };
110 loop {
111 match worker_input
112 .push_wait(chunk, Instant::now() + Duration::from_millis(20))
113 {
114 Ok(()) => break,
115 Err(error) if error.error.kind == ErrorKind::Timeout => {
116 chunk = error.chunk;
117 if worker_overrun.load(Ordering::Acquire) {
118 worker_input.fail(AsrError::new(
119 ErrorKind::AudioOverrun,
120 "capture",
121 "capture ring overflowed",
122 ));
123 break 'drain;
124 }
125 if worker_error.load(Ordering::Acquire) {
126 worker_input
127 .fail(capture_error("input device stream failed"));
128 break 'drain;
129 }
130 }
131 Err(error) => {
132 worker_input.fail(error.error);
133 break 'drain;
134 }
135 }
136 }
137 } else if worker_stop.load(Ordering::Acquire) {
138 break;
139 } else {
140 std::thread::sleep(Duration::from_millis(2));
141 }
142 }
143 let _ = tx.send(());
144 })
145 .map_err(|_| capture_error("cannot start capture drain worker"))?;
146 let result = Self {
147 session,
148 stream: Some(stream),
149 stop,
150 finished,
151 };
152 result
153 .stream
154 .as_ref()
155 .unwrap()
156 .play()
157 .map_err(|_| capture_error("cannot start microphone"))?;
158 Ok(result)
159 }
160 pub fn subscribe(&self) -> Option<Subscription> {
164 self.session.subscribe()
165 }
166 pub fn cancel(&mut self) {
167 self.stream.take();
168 self.stop.store(true, Ordering::Release);
169 self.session.cancel();
170 }
171 pub fn finish(&mut self, deadline: Instant) -> SessionResult {
172 self.stream.take();
173 self.stop.store(true, Ordering::Release);
174 if self
175 .finished
176 .recv_timeout(deadline.saturating_duration_since(Instant::now()))
177 .is_err()
178 {
179 self.session.control().fail(AsrError::new(
180 ErrorKind::Timeout,
181 "capture drain",
182 "capture drain deadline exceeded",
183 ));
184 }
185 self.session.finish(deadline)
186 }
187}
188impl Drop for CaptureSession {
189 fn drop(&mut self) {
190 self.cancel();
191 }
192}
193fn capture_error(message: &str) -> AsrError {
194 AsrError::new(ErrorKind::Io, "capture", message)
195}
196fn build<T: cpal::SizedSample>(
197 device: &cpal::Device,
198 config: cpal::StreamConfig,
199 mut producer: HeapProd<f32>,
200 channels: usize,
201 faults: CaptureFaults,
202 convert: impl Fn(T) -> f32 + Send + 'static,
203) -> Result<cpal::Stream, AsrError> {
204 let callback_faults = faults.clone();
205 device
206 .build_input_stream(
207 config,
208 move |data: &[T], _| {
209 for frame in data.chunks_exact(channels) {
210 let sample = frame.iter().map(|v| convert(*v)).sum::<f32>() / channels as f32;
211 if !sample.is_finite() {
212 callback_faults
213 .invalid_sample
214 .store(true, Ordering::Release);
215 break;
216 }
217 if producer.try_push(sample.clamp(-1.0, 1.0)).is_err() {
218 callback_faults.overrun.store(true, Ordering::Release);
219 break;
220 }
221 }
222 },
223 move |_| {
224 faults.device_error.store(true, Ordering::Release);
225 },
226 None,
227 )
228 .map_err(|_| capture_error("cannot open input stream"))
229}
230
231fn normalize_i16(value: i16) -> f32 {
232 value as f32 / 32768.0
233}
234fn normalize_u16(value: u16) -> f32 {
235 (value as f32 - 32768.0) / 32768.0
236}
237
238#[cfg(test)]
239mod tests;