]> code.octet-stream.net Git - m17rt/commitdiff
Sound card input support for demodulation chain
authorThomas Karpiniec <tom.karpiniec@outlook.com>
Tue, 7 Jan 2025 10:00:57 +0000 (21:00 +1100)
committerThomas Karpiniec <tom.karpiniec@outlook.com>
Tue, 7 Jan 2025 10:00:57 +0000 (21:00 +1100)
Cargo.lock
demod/src/main.rs
m17app/Cargo.toml
m17app/src/soundmodem.rs
m17codec2/src/lib.rs
m17core/src/tnc.rs

index 0f90ba53ac7185e980974a82f759b82f4b64a0ee..c79675b9e8385faf5df60dff423a27f93a0aa1d3 100755 (executable)
@@ -440,6 +440,7 @@ checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
 name = "m17app"
 version = "0.1.0"
 dependencies = [
 name = "m17app"
 version = "0.1.0"
 dependencies = [
+ "cpal",
  "log",
  "m17core",
 ]
  "log",
  "m17core",
 ]
index 0f7f031d236a1adeaae1c4c8f49ef24c58983559..563b7218cc3bece260818bd23504fa5a5af29ce9 100755 (executable)
@@ -1,11 +1,12 @@
 use m17app::app::M17App;
 use m17app::app::M17App;
-use m17app::soundmodem::{InputRrcFile, Soundmodem};
+use m17app::soundmodem::{InputRrcFile, InputSoundcard, Soundmodem};
 use m17codec2::Codec2Adapter;
 use std::path::PathBuf;
 
 pub fn m17app_test() {
 use m17codec2::Codec2Adapter;
 use std::path::PathBuf;
 
 pub fn m17app_test() {
-    let path = PathBuf::from("../../Data/test_vk7xt.rrc");
-    let source = InputRrcFile::new(path);
+    //let path = PathBuf::from("../../Data/test_vk7xt.rrc");
+    //let source = InputRrcFile::new(path);
+    let source = InputSoundcard::new();
     let soundmodem = Soundmodem::new_with_input(source);
     let app = M17App::new(soundmodem);
     app.add_stream_adapter(Codec2Adapter::new());
     let soundmodem = Soundmodem::new_with_input(source);
     let app = M17App::new(soundmodem);
     app.add_stream_adapter(Codec2Adapter::new());
index 11805eba16cc4dd6debd7b3bfc36425c74f9cdf0..daca51f5b666f4b1a0c833399fcbffe532a11d31 100755 (executable)
@@ -10,5 +10,6 @@ repository = "https://code.octet-stream.net/m17rt"
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
+cpal = "0.15.3"
 m17core = { path = "../m17core" }
 log = "0.4.22"
 m17core = { path = "../m17core" }
 log = "0.4.22"
index 8b7f80f74aa4765aba9e74bd3667375c87b91c33..9d9cec9e3e08d250bd392ab2e47841b0bcb694e0 100644 (file)
@@ -1,6 +1,10 @@
 use std::io::{self, ErrorKind, Read, Write};
 
 use crate::tnc::{Tnc, TncError};
 use std::io::{self, ErrorKind, Read, Write};
 
 use crate::tnc::{Tnc, TncError};
+use cpal::traits::DeviceTrait;
+use cpal::traits::HostTrait;
+use cpal::traits::StreamTrait;
+use cpal::{SampleFormat, SampleRate};
 use log::debug;
 use m17core::kiss::MAX_FRAME_LEN;
 use m17core::modem::{Demodulator, SoftDemodulator};
 use log::debug;
 use m17core::kiss::MAX_FRAME_LEN;
 use m17core::modem::{Demodulator, SoftDemodulator};
@@ -154,16 +158,68 @@ pub trait InputSource: Send + Sync + 'static {
 }
 
 pub struct InputSoundcard {
 }
 
 pub struct InputSoundcard {
-    cpal_name: String,
+    cpal_name: Option<String>,
+    end_tx: Mutex<Option<Sender<()>>>,
+}
+
+impl InputSoundcard {
+    pub fn new() -> Self {
+        Self {
+            cpal_name: None,
+            end_tx: Mutex::new(None),
+        }
+    }
+
+    pub fn new_with_card(card_name: String) -> Self {
+        Self {
+            cpal_name: Some(card_name),
+            end_tx: Mutex::new(None),
+        }
+    }
 }
 
 impl InputSource for InputSoundcard {
     fn start(&self, samples: SyncSender<SoundmodemEvent>) {
 }
 
 impl InputSource for InputSoundcard {
     fn start(&self, samples: SyncSender<SoundmodemEvent>) {
-        todo!()
+        let (end_tx, end_rx) = channel();
+        let cpal_name = self.cpal_name.clone();
+        std::thread::spawn(move || {
+            let host = cpal::default_host();
+            let device = if let Some(name) = cpal_name.as_deref() {
+                host.input_devices()
+                    .unwrap()
+                    .find(|d| d.name().unwrap() == name)
+                    .unwrap()
+            } else {
+                host.default_input_device().unwrap()
+            };
+            let mut configs = device.supported_input_configs().unwrap();
+            let config = configs
+                .find(|c| c.channels() == 1 && c.sample_format() == SampleFormat::I16)
+                .unwrap()
+                .with_sample_rate(SampleRate(48000));
+            let stream = device
+                .build_input_stream(
+                    &config.into(),
+                    move |data: &[i16], _info: &cpal::InputCallbackInfo| {
+                        debug!("input has given us {} samples", data.len());
+                        let out: Vec<i16> = data.iter().map(|s| *s).collect();
+                        let _ = samples.try_send(SoundmodemEvent::BasebandInput(out.into()));
+                    },
+                    |e| {
+                        // TODO: abort?
+                        debug!("error occurred in soundcard input: {e:?}");
+                    },
+                    None,
+                )
+                .unwrap();
+            stream.play().unwrap();
+            let _ = end_rx.recv();
+        });
+        *self.end_tx.lock().unwrap() = Some(end_tx);
     }
 
     fn close(&self) {
     }
 
     fn close(&self) {
-        todo!()
+        let _ = self.end_tx.lock().unwrap().take();
     }
 }
 
     }
 }
 
index f09d2b8821f67caaa9d6d043d98c91ec641b8c53..bfd30950bd1b762633ce993b97cfabc31232dc75 100755 (executable)
@@ -50,6 +50,7 @@ impl Codec2Adapter {
                 codec2: Codec2::new(Codec2Mode::MODE_3200),
                 end_tx: None,
             })),
                 codec2: Codec2::new(Codec2Mode::MODE_3200),
                 end_tx: None,
             })),
+            // TODO: this doesn't work on rpi. Use default_output_device() by default
             output_card: "default".to_owned(),
         }
     }
             output_card: "default".to_owned(),
         }
     }
@@ -131,6 +132,8 @@ fn stream_thread(end: Receiver<()>, state: Arc<Mutex<AdapterState>>, output_card
         .find(|d| d.name().unwrap() == output_card)
         .unwrap();
     let mut configs = device.supported_output_configs().unwrap();
         .find(|d| d.name().unwrap() == output_card)
         .unwrap();
     let mut configs = device.supported_output_configs().unwrap();
+    // TODO: channels == 1 doesn't work on a Raspberry Pi
+    // make this configurable and support interleaving LRLR stereo samples if using 2 channels
     let config = configs
         .find(|c| c.channels() == 1 && c.sample_format() == SampleFormat::I16)
         .unwrap()
     let config = configs
         .find(|c| c.channels() == 1 && c.sample_format() == SampleFormat::I16)
         .unwrap()
index 8b8e5a1c5bf90be43079927b9196a3377c4c9a88..ae95628a3134dc4bb74d7b1d3d729343d9330777 100644 (file)
@@ -65,6 +65,7 @@ impl SoftTnc {
                                 let end = start + payload_len;
                                 rx.packet[start..(start + payload_len)]
                                     .copy_from_slice(&packet.payload);
                                 let end = start + payload_len;
                                 rx.packet[start..(start + payload_len)]
                                     .copy_from_slice(&packet.payload);
+                                // TODO: compatible packets should be sent on port 0 too
                                 let kiss =
                                     KissFrame::new_full_packet(&rx.lsf.0, &rx.packet[0..end])
                                         .unwrap();
                                 let kiss =
                                     KissFrame::new_full_packet(&rx.lsf.0, &rx.packet[0..end])
                                         .unwrap();