]> code.octet-stream.net Git - m17rt/blobdiff - m17app/src/soundmodem.rs
Correctly call all adapter lifecycle methods, docs and test
[m17rt] / m17app / src / soundmodem.rs
index 974fd45085600b8fa60d1f7cac377e765415426d..16bb97642900e86139c5d22aab31a681b0b53c2d 100644 (file)
@@ -1,3 +1,4 @@
+use crate::error::M17Error;
 use crate::tnc::{Tnc, TncError};
 use log::debug;
 use m17core::kiss::MAX_FRAME_LEN;
 use crate::tnc::{Tnc, TncError};
 use log::debug;
 use m17core::kiss::MAX_FRAME_LEN;
@@ -138,8 +139,12 @@ fn spawn_soundmodem_worker(
         let mut ptt = false;
         while let Ok(ev) = event_rx.recv() {
             // Update clock on TNC before we do anything
         let mut ptt = false;
         while let Ok(ev) = event_rx.recv() {
             // Update clock on TNC before we do anything
-            let sample_time = (start.elapsed().as_nanos() / 48000) as u64;
-            tnc.set_now(sample_time);
+            let sample_time = start.elapsed();
+            let secs = sample_time.as_secs();
+            let nanos = sample_time.subsec_nanos();
+            // Accurate to within approx 1 sample
+            let now_samples = 48000 * secs + (nanos as u64 / 20833);
+            tnc.set_now(now_samples);
 
             // Handle event
             match ev {
 
             // Handle event
             match ev {
@@ -236,29 +241,28 @@ pub trait InputSource: Send + Sync + 'static {
 }
 
 pub struct InputRrcFile {
 }
 
 pub struct InputRrcFile {
-    path: PathBuf,
+    baseband: Arc<[u8]>,
     end_tx: Mutex<Option<Sender<()>>>,
 }
 
 impl InputRrcFile {
     end_tx: Mutex<Option<Sender<()>>>,
 }
 
 impl InputRrcFile {
-    pub fn new(path: PathBuf) -> Self {
-        Self {
-            path,
+    pub fn new(path: PathBuf) -> Result<Self, M17Error> {
+        let mut file = File::open(&path).map_err(|_| M17Error::InvalidRrcPath(path.clone()))?;
+        let mut baseband = vec![];
+        file.read_to_end(&mut baseband)
+            .map_err(|_| M17Error::RrcReadFailed(path))?;
+        Ok(Self {
+            baseband: baseband.into(),
             end_tx: Mutex::new(None),
             end_tx: Mutex::new(None),
-        }
+        })
     }
 }
 
 impl InputSource for InputRrcFile {
     fn start(&self, samples: SyncSender<SoundmodemEvent>) {
         let (end_tx, end_rx) = channel();
     }
 }
 
 impl InputSource for InputRrcFile {
     fn start(&self, samples: SyncSender<SoundmodemEvent>) {
         let (end_tx, end_rx) = channel();
-        let path = self.path.clone();
+        let baseband = self.baseband.clone();
         std::thread::spawn(move || {
         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;
             // assuming 48 kHz for now
             const TICK: Duration = Duration::from_millis(25);
             const SAMPLES_PER_TICK: usize = 1200;