]> code.octet-stream.net Git - m17rt/blob - demod/src/main.rs
c1a7eb9e744a136e6b1856b9ee299c48de448c02
[m17rt] / demod / src / main.rs
1 use cpal::traits::DeviceTrait;
2 use cpal::traits::HostTrait;
3 use cpal::traits::StreamTrait;
4 use cpal::{SampleFormat, SampleRate};
5 use log::debug;
6 use m17core::{
7 modem::{Demodulator, SoftDemodulator},
8 protocol::{Frame, LichCollection},
9 };
10 use std::{fs::File, io::Read};
11
12 pub fn run_my_decode() {
13 let file = File::open("../../Data/test_vk7xt.rrc").unwrap();
14 let mut input = file;
15 let mut baseband = vec![];
16 input.read_to_end(&mut baseband).unwrap();
17
18 let mut lich = LichCollection::new();
19 let mut codec2_data = vec![];
20 let mut modem = SoftDemodulator::new();
21
22 for pair in baseband.chunks(2) {
23 let sample: i16 = i16::from_le_bytes([pair[0], pair[1]]);
24 if let Some(frame) = modem.demod(sample) {
25 debug!("Modem demodulated frame: {:?}", frame);
26 if let Frame::Stream(s) = frame {
27 for b in s.stream_data {
28 codec2_data.push(b);
29
30 let valid_before = lich.valid_segments();
31 lich.set_segment(s.lich_idx, s.lich_part);
32 let valid_after = lich.valid_segments();
33 if valid_before != valid_after {
34 debug!("Valid lich segments: {}", lich.valid_segments());
35 }
36 if valid_before == 5 && valid_after == 6 {
37 if let Some(l) = lich.try_assemble() {
38 debug!("Assembled complete lich: {l:?}");
39 }
40 }
41 }
42 if s.end_of_stream {
43 debug!("len of codec2 data: {}", codec2_data.len());
44 assert_eq!(codec2_data.len(), 1504);
45
46 let samples =
47 m17codec2::decode_codec2(&codec2_data, "../../Data/speech_out.raw");
48 let host = cpal::default_host();
49 let def = host.default_output_device().unwrap();
50 let mut configs = def.supported_output_configs().unwrap();
51 let config = configs
52 .find(|c| c.channels() == 1 && c.sample_format() == SampleFormat::I16)
53 .unwrap()
54 .with_sample_rate(SampleRate(8000));
55 let mut counter = 0;
56 let mut index = 0;
57 let stream = def
58 .build_output_stream(
59 &config.into(),
60 move |data: &mut [i16], _: &cpal::OutputCallbackInfo| {
61 println!(
62 "iteration {counter} asked for {} samples at time {}",
63 data.len(),
64 std::time::SystemTime::now()
65 .duration_since(std::time::UNIX_EPOCH)
66 .unwrap()
67 .as_millis()
68 );
69 counter += 1;
70 let qty = data.len().min(samples.len() - index);
71 println!("providing {qty} samples");
72 data[0..qty].copy_from_slice(&samples[index..(index + qty)]);
73 index += qty;
74 },
75 move |e| {
76 println!("error occurred");
77 },
78 None,
79 )
80 .unwrap();
81 stream.play().unwrap();
82
83 std::thread::sleep(std::time::Duration::from_secs(10));
84 }
85 }
86 }
87 }
88 }
89
90 pub fn cpal_test() {
91 let host = cpal::default_host();
92 for d in host.devices().unwrap() {
93 println!("Found card: {:?}", d.name().unwrap());
94 }
95 let def = host.default_output_device().unwrap();
96 println!("the default output device is: {}", def.name().unwrap());
97
98 for c in def.supported_output_configs().unwrap() {
99 println!("config supported: {:?}", c);
100 }
101
102 println!("all supported output configs shown");
103 }
104
105 fn main() {
106 env_logger::init();
107 run_my_decode();
108 //cpal_test();
109 }