]> code.octet-stream.net Git - m17rt/commitdiff
Defining input sources for Soundmodem
authorThomas Karpiniec <tom.karpiniec@outlook.com>
Sun, 5 Jan 2025 03:27:40 +0000 (14:27 +1100)
committerThomas Karpiniec <tom.karpiniec@outlook.com>
Sun, 5 Jan 2025 03:27:40 +0000 (14:27 +1100)
demod/src/main.rs
m17app/src/soundmodem.rs
m17codec2/src/lib.rs

index c1a7eb9e744a136e6b1856b9ee299c48de448c02..78ff2b0732086a4ea0911b57ecd70274b095d19e 100755 (executable)
@@ -57,7 +57,12 @@ pub fn run_my_decode() {
                     let stream = def
                         .build_output_stream(
                             &config.into(),
-                            move |data: &mut [i16], _: &cpal::OutputCallbackInfo| {
+                            move |data: &mut [i16], info: &cpal::OutputCallbackInfo| {
+                                debug!(
+                                    "callback {:?} playback {:?}",
+                                    info.timestamp().callback,
+                                    info.timestamp().playback
+                                );
                                 println!(
                                     "iteration {counter} asked for {} samples at time {}",
                                     data.len(),
@@ -72,7 +77,7 @@ pub fn run_my_decode() {
                                 data[0..qty].copy_from_slice(&samples[index..(index + qty)]);
                                 index += qty;
                             },
-                            move |e| {
+                            move |_e| {
                                 println!("error occurred");
                             },
                             None,
index 83731daa2cc3da73d4f664f4a0064c995a45ebff..b6fbf4692917d18851abb3ba0d419e5f0a3c5698 100644 (file)
@@ -1,7 +1,13 @@
 use std::io::{self, ErrorKind, Read, Write};
 
 use crate::tnc::{Tnc, TncError};
+use log::debug;
 use m17core::tnc::SoftTnc;
+use std::fs::File;
+use std::path::PathBuf;
+use std::sync::mpsc::{channel, sync_channel, Receiver, Sender, SyncSender, TryRecvError};
+use std::sync::{Arc, Mutex};
+use std::time::{Duration, Instant};
 
 pub struct Soundmodem {
     tnc: SoftTnc,
@@ -10,6 +16,7 @@ pub struct Soundmodem {
 
 pub struct SoundmodemConfig {
     // sound cards, PTT, etc.
+    input: Box<dyn InputSource>,
 }
 
 impl Read for Soundmodem {
@@ -45,3 +52,77 @@ impl Tnc for Soundmodem {
         unimplemented!();
     }
 }
+
+pub enum SoundmodemEvent {
+    Kiss(Arc<[u8]>),
+    BasebandInput(Arc<[i16]>),
+}
+
+pub trait InputSource: Send + Sync + 'static {
+    fn start(&self, samples: SyncSender<SoundmodemEvent>);
+    fn close(&self);
+}
+
+pub struct InputSoundcard {
+    cpal_name: String,
+}
+
+impl InputSource for InputSoundcard {
+    fn start(&self, samples: SyncSender<SoundmodemEvent>) {
+        todo!()
+    }
+
+    fn close(&self) {
+        todo!()
+    }
+}
+
+pub struct InputRrcFile {
+    path: PathBuf,
+    end_tx: Mutex<Option<Sender<()>>>,
+}
+
+impl InputSource for InputRrcFile {
+    fn start(&self, samples: SyncSender<SoundmodemEvent>) {
+        let (end_tx, end_rx) = channel();
+        let path = self.path.clone();
+        std::thread::spawn(move || {
+            // TODO: error handling
+            let mut file = File::open(path).unwrap();
+            let mut baseband = vec![];
+            file.read_to_end(&mut baseband).unwrap();
+
+            // assuming 48 kHz for now
+            const TICK: Duration = Duration::from_millis(25);
+            const SAMPLES_PER_TICK: usize = 1200;
+
+            let mut next_tick = Instant::now() + TICK;
+            let mut buf = [0i16; SAMPLES_PER_TICK];
+            let mut idx = 0;
+
+            for sample in baseband
+                .chunks(2)
+                .map(|pair| i16::from_le_bytes([pair[0], pair[1]]))
+            {
+                buf[idx] = sample;
+                idx += 1;
+                if idx == SAMPLES_PER_TICK {
+                    if let Err(e) = samples.try_send(SoundmodemEvent::BasebandInput(buf.into())) {
+                        debug!("overflow feeding soundmodem: {e:?}");
+                    }
+                    next_tick = next_tick + TICK;
+                    idx = 0;
+                    std::thread::sleep(next_tick.duration_since(Instant::now()));
+                }
+                if end_rx.try_recv() != Err(TryRecvError::Empty) {
+                    break;
+                }
+            }
+        });
+        *self.end_tx.lock().unwrap() = Some(end_tx);
+    }
+
+    fn close(&self) {
+        let _ = self.end_tx.lock().unwrap().take();
+    }
+}
index eb45f770caa8c31b5ea3cadd75b734e01817c1e6..f09d2b8821f67caaa9d6d043d98c91ec641b8c53 100755 (executable)
@@ -1,9 +1,12 @@
 use codec2::{Codec2, Codec2Mode};
-
+use cpal::traits::DeviceTrait;
+use cpal::traits::HostTrait;
+use cpal::traits::StreamTrait;
+use cpal::{Sample, SampleFormat, SampleRate};
+use log::debug;
 use m17app::adapter::StreamAdapter;
 use m17app::app::TxHandle;
 use m17core::protocol::LsfFrame;
-
 use std::collections::VecDeque;
 use std::fs::File;
 use std::io::Write;
@@ -13,13 +16,6 @@ use std::sync::{
     Arc, Mutex,
 };
 
-use cpal::traits::DeviceTrait;
-use cpal::traits::HostTrait;
-use cpal::traits::StreamTrait;
-use cpal::{Sample, SampleFormat, SampleRate};
-
-use log::debug;
-
 pub fn decode_codec2<P: AsRef<Path>>(data: &[u8], out_path: P) -> Vec<i16> {
     let codec2 = Codec2::new(Codec2Mode::MODE_3200);
     let var_name = codec2;
@@ -142,7 +138,12 @@ fn stream_thread(end: Receiver<()>, state: Arc<Mutex<AdapterState>>, output_card
     let stream = device
         .build_output_stream(
             &config.into(),
-            move |data: &mut [i16], _: &cpal::OutputCallbackInfo| {
+            move |data: &mut [i16], info: &cpal::OutputCallbackInfo| {
+                debug!(
+                    "callback {:?} playback {:?}",
+                    info.timestamp().callback,
+                    info.timestamp().playback
+                );
                 output_cb(data, &state);
             },
             |e| {