]> code.octet-stream.net Git - m17rt/blobdiff - demod/src/main.rs
Begin adding support for sound card output via codec2 adapter
[m17rt] / demod / src / main.rs
index f67f87dccbbe18de1bfc41326815d1e57bc509c6..c1a7eb9e744a136e6b1856b9ee299c48de448c02 100755 (executable)
@@ -1,9 +1,13 @@
+use cpal::traits::DeviceTrait;
+use cpal::traits::HostTrait;
+use cpal::traits::StreamTrait;
+use cpal::{SampleFormat, SampleRate};
 use log::debug;
 use m17core::{
     modem::{Demodulator, SoftDemodulator},
     protocol::{Frame, LichCollection},
 };
 use log::debug;
 use m17core::{
     modem::{Demodulator, SoftDemodulator},
     protocol::{Frame, LichCollection},
 };
-pub(crate) use std::{fs::File, io::Read};
+use std::{fs::File, io::Read};
 
 pub fn run_my_decode() {
     let file = File::open("../../Data/test_vk7xt.rrc").unwrap();
 
 pub fn run_my_decode() {
     let file = File::open("../../Data/test_vk7xt.rrc").unwrap();
@@ -39,14 +43,67 @@ pub fn run_my_decode() {
                     debug!("len of codec2 data: {}", codec2_data.len());
                     assert_eq!(codec2_data.len(), 1504);
 
                     debug!("len of codec2 data: {}", codec2_data.len());
                     assert_eq!(codec2_data.len(), 1504);
 
-                    m17codec2::decode_codec2(&codec2_data, "../../Data/speech_out.raw");
+                    let samples =
+                        m17codec2::decode_codec2(&codec2_data, "../../Data/speech_out.raw");
+                    let host = cpal::default_host();
+                    let def = host.default_output_device().unwrap();
+                    let mut configs = def.supported_output_configs().unwrap();
+                    let config = configs
+                        .find(|c| c.channels() == 1 && c.sample_format() == SampleFormat::I16)
+                        .unwrap()
+                        .with_sample_rate(SampleRate(8000));
+                    let mut counter = 0;
+                    let mut index = 0;
+                    let stream = def
+                        .build_output_stream(
+                            &config.into(),
+                            move |data: &mut [i16], _: &cpal::OutputCallbackInfo| {
+                                println!(
+                                    "iteration {counter} asked for {} samples at time {}",
+                                    data.len(),
+                                    std::time::SystemTime::now()
+                                        .duration_since(std::time::UNIX_EPOCH)
+                                        .unwrap()
+                                        .as_millis()
+                                );
+                                counter += 1;
+                                let qty = data.len().min(samples.len() - index);
+                                println!("providing {qty} samples");
+                                data[0..qty].copy_from_slice(&samples[index..(index + qty)]);
+                                index += qty;
+                            },
+                            move |e| {
+                                println!("error occurred");
+                            },
+                            None,
+                        )
+                        .unwrap();
+                    stream.play().unwrap();
+
+                    std::thread::sleep(std::time::Duration::from_secs(10));
                 }
             }
         }
     }
 }
 
                 }
             }
         }
     }
 }
 
+pub fn cpal_test() {
+    let host = cpal::default_host();
+    for d in host.devices().unwrap() {
+        println!("Found card: {:?}", d.name().unwrap());
+    }
+    let def = host.default_output_device().unwrap();
+    println!("the default output device is: {}", def.name().unwrap());
+
+    for c in def.supported_output_configs().unwrap() {
+        println!("config supported: {:?}", c);
+    }
+
+    println!("all supported output configs shown");
+}
+
 fn main() {
     env_logger::init();
     run_my_decode();
 fn main() {
     env_logger::init();
     run_my_decode();
+    //cpal_test();
 }
 }