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