X-Git-Url: https://code.octet-stream.net/m17rt/blobdiff_plain/7b7b572052951018da4d6d0f5753d627836236ad..b3cb86656d6c820a70ceb5ea1a6d602999eb3c85:/demod/src/main.rs diff --git a/demod/src/main.rs b/demod/src/main.rs index f67f87d..c1a7eb9 100755 --- a/demod/src/main.rs +++ b/demod/src/main.rs @@ -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}, }; -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(); @@ -39,14 +43,67 @@ pub fn run_my_decode() { 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(); + //cpal_test(); }