]> code.octet-stream.net Git - m17rt/blob - m17codec2/src/rx.rs
Split m17codec2 into modules
[m17rt] / m17codec2 / src / rx.rs
1 use crate::M17Codec2Error;
2 use codec2::{Codec2, Codec2Mode};
3 use cpal::traits::DeviceTrait;
4 use cpal::traits::HostTrait;
5 use cpal::traits::StreamTrait;
6 use cpal::{Sample, SampleFormat, SampleRate};
7 use log::debug;
8 use m17app::adapter::StreamAdapter;
9 use m17app::app::TxHandle;
10 use m17app::error::AdapterError;
11 use m17app::link_setup::LinkSetup;
12 use rubato::Resampler;
13 use rubato::SincFixedIn;
14 use rubato::SincInterpolationParameters;
15 use std::collections::VecDeque;
16 use std::fs::File;
17 use std::io::Write;
18 use std::path::Path;
19 use std::sync::{
20 mpsc::{channel, Receiver, Sender},
21 Arc, Mutex,
22 };
23
24 /// Write one or more 8-byte chunks of 3200-bit Codec2 to a raw S16LE file
25 /// and return the samples.
26 pub fn decode_codec2<P: AsRef<Path>>(data: &[u8], out_path: P) -> Vec<i16> {
27 let codec2 = Codec2::new(Codec2Mode::MODE_3200);
28 let var_name = codec2;
29 let mut codec = var_name;
30 let mut all_samples: Vec<i16> = vec![];
31 for i in 0..(data.len() / 8) {
32 let mut samples = vec![0; codec.samples_per_frame()];
33 codec.decode(&mut samples, &data[i * 8..((i + 1) * 8)]);
34 all_samples.append(&mut samples);
35 }
36 let mut speech_out = File::create(out_path).unwrap();
37 for b in &all_samples {
38 speech_out.write_all(&b.to_le_bytes()).unwrap();
39 }
40 all_samples
41 }
42
43 /// Subscribes to M17 streams and attempts to play the decoded Codec2
44 pub struct Codec2RxAdapter {
45 state: Arc<Mutex<AdapterState>>,
46 output_card: Option<String>,
47 }
48
49 impl Codec2RxAdapter {
50 pub fn new() -> Self {
51 Self {
52 state: Arc::new(Mutex::new(AdapterState {
53 tx: None,
54 out_buf: VecDeque::new(),
55 codec2: Codec2::new(Codec2Mode::MODE_3200),
56 end_tx: None,
57 resampler: None,
58 })),
59 output_card: None,
60 }
61 }
62
63 pub fn set_output_card<S: Into<String>>(&mut self, card_name: S) {
64 self.output_card = Some(card_name.into());
65 }
66 }
67
68 impl Default for Codec2RxAdapter {
69 fn default() -> Self {
70 Self::new()
71 }
72 }
73
74 struct AdapterState {
75 tx: Option<TxHandle>,
76 /// Circular buffer of output samples for playback
77 out_buf: VecDeque<i16>,
78 codec2: Codec2,
79 end_tx: Option<Sender<()>>,
80 resampler: Option<SincFixedIn<f32>>,
81 }
82
83 impl StreamAdapter for Codec2RxAdapter {
84 fn start(&self, handle: TxHandle) -> Result<(), AdapterError> {
85 self.state.lock().unwrap().tx = Some(handle);
86
87 let (end_tx, end_rx) = channel();
88 let (setup_tx, setup_rx) = channel();
89 let state = self.state.clone();
90 let output_card = self.output_card.clone();
91 std::thread::spawn(move || stream_thread(end_rx, setup_tx, state, output_card));
92 self.state.lock().unwrap().end_tx = Some(end_tx);
93 // Propagate any errors arising in the thread
94 let sample_rate = setup_rx.recv()??;
95 debug!("selected codec2 output sample rate {sample_rate}");
96 if sample_rate != 8000 {
97 let params = SincInterpolationParameters {
98 sinc_len: 256,
99 f_cutoff: 0.95,
100 oversampling_factor: 128,
101 interpolation: rubato::SincInterpolationType::Cubic,
102 window: rubato::WindowFunction::BlackmanHarris2,
103 };
104 // TODO: fix unwrap
105 self.state.lock().unwrap().resampler =
106 Some(SincFixedIn::new(sample_rate as f64 / 8000f64, 1.0, params, 160, 1).unwrap());
107 }
108 Ok(())
109 }
110
111 fn close(&self) -> Result<(), AdapterError> {
112 let mut state = self.state.lock().unwrap();
113 state.tx = None;
114 state.end_tx = None;
115 Ok(())
116 }
117
118 fn stream_began(&self, _link_setup: LinkSetup) {
119 // for now we will assume:
120 // - unencrypted
121 // - data type is Voice (Codec2 3200), not Voice+Data
122 // TODO: is encryption handled here or in M17App, such that we get a decrypted stream?
123 // TODO: handle the Voice+Data combination with Codec2 1600
124 self.state.lock().unwrap().codec2 = Codec2::new(Codec2Mode::MODE_3200);
125 }
126
127 fn stream_data(&self, _frame_number: u16, _is_final: bool, data: Arc<[u8; 16]>) {
128 let mut state = self.state.lock().unwrap();
129 for encoded in data.chunks(8) {
130 if state.out_buf.len() < 8192 {
131 let mut samples = [i16::EQUILIBRIUM; 160]; // while assuming 3200
132 state.codec2.decode(&mut samples, encoded);
133 if let Some(resampler) = state.resampler.as_mut() {
134 let samples_f: Vec<f32> =
135 samples.iter().map(|s| *s as f32 / 16384.0f32).collect();
136 let res = resampler.process(&[samples_f], None).unwrap();
137 for s in &res[0] {
138 state.out_buf.push_back((s * 16383.0f32) as i16);
139 }
140 } else {
141 // TODO: maybe get rid of VecDeque so we can decode directly into ring buffer?
142 for s in samples {
143 state.out_buf.push_back(s);
144 }
145 }
146 } else {
147 debug!("out_buf overflow");
148 }
149 }
150 }
151 }
152
153 fn output_cb(data: &mut [i16], state: &Mutex<AdapterState>, channels: u16) {
154 let mut state = state.lock().unwrap();
155 for d in data.chunks_mut(channels as usize) {
156 d.fill(state.out_buf.pop_front().unwrap_or(i16::EQUILIBRIUM));
157 }
158 }
159
160 /// Create and manage the stream from a dedicated thread since it's `!Send`
161 fn stream_thread(
162 end: Receiver<()>,
163 setup_tx: Sender<Result<u32, AdapterError>>,
164 state: Arc<Mutex<AdapterState>>,
165 output_card: Option<String>,
166 ) {
167 let host = cpal::default_host();
168 let device = if let Some(output_card) = output_card {
169 // TODO: more error handling for unwraps
170 match host
171 .output_devices()
172 .unwrap()
173 .find(|d| d.name().unwrap() == output_card)
174 {
175 Some(d) => d,
176 None => {
177 let _ = setup_tx.send(Err(M17Codec2Error::CardUnavailable(output_card).into()));
178 return;
179 }
180 }
181 } else {
182 match host.default_output_device() {
183 Some(d) => d,
184 None => {
185 let _ = setup_tx.send(Err(M17Codec2Error::DefaultCardUnavailable.into()));
186 return;
187 }
188 }
189 };
190 let card_name = device.name().unwrap();
191 let mut configs = match device.supported_output_configs() {
192 Ok(c) => c,
193 Err(e) => {
194 let _ = setup_tx.send(Err(
195 M17Codec2Error::OutputConfigsUnavailable(card_name, e).into()
196 ));
197 return;
198 }
199 };
200 let config = match configs.find(|c| {
201 (c.channels() == 1 || c.channels() == 2) && c.sample_format() == SampleFormat::I16
202 }) {
203 Some(c) => c,
204 None => {
205 let _ = setup_tx.send(Err(
206 M17Codec2Error::SupportedOutputUnavailable(card_name).into()
207 ));
208 return;
209 }
210 };
211
212 let target_sample_rate =
213 if config.min_sample_rate().0 <= 8000 && config.max_sample_rate().0 >= 8000 {
214 8000
215 } else {
216 config.min_sample_rate().0
217 };
218 let channels = config.channels();
219
220 let config = config.with_sample_rate(SampleRate(target_sample_rate));
221 let stream = match device.build_output_stream(
222 &config.into(),
223 move |data: &mut [i16], _info: &cpal::OutputCallbackInfo| {
224 output_cb(data, &state, channels);
225 },
226 |e| {
227 // trigger end_tx here? always more edge cases
228 debug!("error occurred in codec2 playback: {e:?}");
229 },
230 None,
231 ) {
232 Ok(s) => s,
233 Err(e) => {
234 let _ = setup_tx.send(Err(
235 M17Codec2Error::OutputStreamBuildError(card_name, e).into()
236 ));
237 return;
238 }
239 };
240 match stream.play() {
241 Ok(()) => (),
242 Err(e) => {
243 let _ = setup_tx.send(Err(
244 M17Codec2Error::OutputStreamPlayError(card_name, e).into()
245 ));
246 return;
247 }
248 }
249 let _ = setup_tx.send(Ok(target_sample_rate));
250 let _ = end.recv();
251 // it seems concrete impls of Stream have a Drop implementation that will handle termination
252 }