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
};
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
;
20 mpsc
::{channel
, Receiver
, Sender
},
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
);
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
();
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
>,
49 impl Codec2RxAdapter
{
50 pub fn new() -> Self {
52 state
: Arc
::new(Mutex
::new(AdapterState
{
54 out_buf
: VecDeque
::new(),
55 codec2
: Codec2
::new(Codec2Mode
::MODE_3200
),
63 pub fn set_output_card
<S
: Into
<String
>>(&mut self, card_name
: S
) {
64 self.output_card
= Some(card_name
.into
());
68 impl Default
for Codec2RxAdapter
{
69 fn default() -> Self {
76 /// Circular buffer of output samples for playback
77 out_buf
: VecDeque
<i16>,
79 end_tx
: Option
<Sender
<()>>,
80 resampler
: Option
<SincFixedIn
<f32>>,
83 impl StreamAdapter
for Codec2RxAdapter
{
84 fn start(&self, handle
: TxHandle
) -> Result
<(), AdapterError
> {
85 self.state
.lock().unwrap
().tx
= Some(handle
);
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
{
100 oversampling_factor
: 128,
101 interpolation
: rubato
::SincInterpolationType
::Cubic
,
102 window
: rubato
::WindowFunction
::BlackmanHarris2
,
105 self.state
.lock().unwrap
().resampler
=
106 Some(SincFixedIn
::new(sample_rate
as f64 / 8000f64, 1.0, params
, 160, 1).unwrap
());
111 fn close(&self) -> Result
<(), AdapterError
> {
112 let mut state
= self.state
.lock().unwrap
();
118 fn stream_began(&self, _link_setup
: LinkSetup
) {
119 // for now we will assume:
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
);
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
();
138 state
.out_buf
.push_back((s
* 16383.0f32) as i16);
141 // TODO: maybe get rid of VecDeque so we can decode directly into ring buffer?
143 state
.out_buf
.push_back(s
);
147 debug
!("out_buf overflow");
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
));
160 /// Create and manage the stream from a dedicated thread since it's `!Send`
163 setup_tx
: Sender
<Result
<u32, AdapterError
>>,
164 state
: Arc
<Mutex
<AdapterState
>>,
165 output_card
: Option
<String
>,
167 let host
= cpal
::default_host();
168 let device
= if let Some(output_card
) = output_card
{
169 // TODO: more error handling for unwraps
173 .find
(|d
| d
.name().unwrap
() == output_card
)
177 let _
= setup_tx
.send(Err(M17Codec2Error
::CardUnavailable(output_card
).into
()));
182 match host
.default_output_device() {
185 let _
= setup_tx
.send(Err(M17Codec2Error
::DefaultCardUnavailable
.into
()));
190 let card_name
= device
.name().unwrap
();
191 let mut configs
= match device
.supported_output_configs() {
194 let _
= setup_tx
.send(Err(
195 M17Codec2Error
::OutputConfigsUnavailable(card_name
, e
).into
()
200 let config
= match configs
.find
(|c
| {
201 (c
.channels() == 1 || c
.channels() == 2) && c
.sample_format() == SampleFormat
::I16
205 let _
= setup_tx
.send(Err(
206 M17Codec2Error
::SupportedOutputUnavailable(card_name
).into
()
212 let target_sample_rate
=
213 if config
.min_sample_rate().0 <= 8000 && config
.max_sample_rate().0 >= 8000 {
216 config
.min_sample_rate().0
218 let channels
= config
.channels();
220 let config
= config
.with_sample_rate(SampleRate(target_sample_rate
));
221 let stream
= match device
.build_output_stream(
223 move |data
: &mut [i16], _info
: &cpal
::OutputCallbackInfo
| {
224 output_cb(data
, &state
, channels
);
227 // trigger end_tx here? always more edge cases
228 debug
!("error occurred in codec2 playback: {e:?}");
234 let _
= setup_tx
.send(Err(
235 M17Codec2Error
::OutputStreamBuildError(card_name
, e
).into
()
240 match stream
.play() {
243 let _
= setup_tx
.send(Err(
244 M17Codec2Error
::OutputStreamPlayError(card_name
, e
).into
()
249 let _
= setup_tx
.send(Ok(target_sample_rate
));
251 // it seems concrete impls of Stream have a Drop implementation that will handle termination