]> code.octet-stream.net Git - m17rt/blobdiff - m17codec2/src/lib.rs
Address more clippy lints
[m17rt] / m17codec2 / src / lib.rs
index bfd30950bd1b762633ce993b97cfabc31232dc75..1d05f2720f827ac23ed982c6f243b9b06486232e 100755 (executable)
@@ -6,15 +6,20 @@ use cpal::{Sample, SampleFormat, SampleRate};
 use log::debug;
 use m17app::adapter::StreamAdapter;
 use m17app::app::TxHandle;
-use m17core::protocol::LsfFrame;
+use m17app::link_setup::LinkSetup;
+use m17app::link_setup::M17Address;
+use m17app::StreamFrame;
 use std::collections::VecDeque;
 use std::fs::File;
 use std::io::Write;
 use std::path::Path;
+use std::path::PathBuf;
 use std::sync::{
     mpsc::{channel, Receiver, Sender},
     Arc, Mutex,
 };
+use std::time::Duration;
+use std::time::Instant;
 
 pub fn decode_codec2<P: AsRef<Path>>(data: &[u8], out_path: P) -> Vec<i16> {
     let codec2 = Codec2::new(Codec2Mode::MODE_3200);
@@ -56,6 +61,12 @@ impl Codec2Adapter {
     }
 }
 
+impl Default for Codec2Adapter {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
 struct AdapterState {
     tx: Option<TxHandle>,
     /// Circular buffer of output samples for playback
@@ -85,7 +96,7 @@ impl StreamAdapter for Codec2Adapter {
 
     fn tnc_closed(&self) {}
 
-    fn stream_began(&self, lsf: LsfFrame) {
+    fn stream_began(&self, _link_setup: LinkSetup) {
         // for now we will assume:
         // - unencrypted
         // - data type is Voice (Codec2 3200), not Voice+Data
@@ -94,7 +105,7 @@ impl StreamAdapter for Codec2Adapter {
         self.state.lock().unwrap().codec2 = Codec2::new(Codec2Mode::MODE_3200);
     }
 
-    fn stream_data(&self, frame_number: u16, is_final: bool, data: Arc<[u8; 16]>) {
+    fn stream_data(&self, _frame_number: u16, _is_final: bool, data: Arc<[u8; 16]>) {
         let mut state = self.state.lock().unwrap();
         for encoded in data.chunks(8) {
             if state.out_buf.len() < 1024 {
@@ -113,11 +124,6 @@ impl StreamAdapter for Codec2Adapter {
 
 fn output_cb(data: &mut [i16], state: &Mutex<AdapterState>) {
     let mut state = state.lock().unwrap();
-    debug!(
-        "sound card wants {} samples, we have {} in the buffer",
-        data.len(),
-        state.out_buf.len()
-    );
     for d in data {
         *d = state.out_buf.pop_front().unwrap_or(i16::EQUILIBRIUM);
     }
@@ -141,12 +147,7 @@ fn stream_thread(end: Receiver<()>, state: Arc<Mutex<AdapterState>>, output_card
     let stream = device
         .build_output_stream(
             &config.into(),
-            move |data: &mut [i16], info: &cpal::OutputCallbackInfo| {
-                debug!(
-                    "callback {:?} playback {:?}",
-                    info.timestamp().callback,
-                    info.timestamp().playback
-                );
+            move |data: &mut [i16], _info: &cpal::OutputCallbackInfo| {
                 output_cb(data, &state);
             },
             |e| {
@@ -160,3 +161,63 @@ fn stream_thread(end: Receiver<()>, state: Arc<Mutex<AdapterState>>, output_card
     let _ = end.recv();
     // it seems concrete impls of Stream have a Drop implementation that will handle termination
 }
+
+pub struct WavePlayer;
+
+impl WavePlayer {
+    pub fn play(
+        path: PathBuf,
+        tx: TxHandle,
+        source: &M17Address,
+        destination: &M17Address,
+        channel_access_number: u8,
+    ) {
+        let mut reader = hound::WavReader::open(path).unwrap();
+        let mut samples = reader.samples::<i16>();
+
+        let mut codec = Codec2::new(Codec2Mode::MODE_3200);
+        let mut in_buf = [0i16; 160];
+        let mut out_buf = [0u8; 16];
+        let mut lsf_chunk: usize = 0;
+        const TICK: Duration = Duration::from_millis(40);
+        let mut next_tick = Instant::now() + TICK;
+        let mut frame_number = 0;
+
+        let mut setup = LinkSetup::new_voice(source, destination);
+        setup.set_channel_access_number(channel_access_number);
+        tx.transmit_stream_start(&setup);
+
+        loop {
+            let mut last_one = false;
+            for out in out_buf.chunks_mut(8) {
+                for i in in_buf.iter_mut() {
+                    let sample = match samples.next() {
+                        Some(Ok(sample)) => sample,
+                        _ => {
+                            last_one = true;
+                            0
+                        }
+                    };
+                    *i = sample;
+                }
+                codec.encode(out, &in_buf);
+            }
+            tx.transmit_stream_next(&StreamFrame {
+                lich_idx: lsf_chunk as u8,
+                lich_part: setup.lich_part(lsf_chunk as u8),
+                frame_number,
+                end_of_stream: last_one,
+                stream_data: out_buf,
+            });
+            frame_number += 1;
+            lsf_chunk = (lsf_chunk + 1) % 6;
+
+            if last_one {
+                break;
+            }
+
+            std::thread::sleep(next_tick.duration_since(Instant::now()));
+            next_tick += TICK;
+        }
+    }
+}