]> code.octet-stream.net Git - m17rt/blobdiff - m17codec2/src/lib.rs
Address more clippy lints
[m17rt] / m17codec2 / src / lib.rs
index e33409f81b25f2176fd2bc78d9aa30b4f42f2d4d..1d05f2720f827ac23ed982c6f243b9b06486232e 100755 (executable)
@@ -6,10 +6,9 @@ use cpal::{Sample, SampleFormat, SampleRate};
 use log::debug;
 use m17app::adapter::StreamAdapter;
 use m17app::app::TxHandle;
 use log::debug;
 use m17app::adapter::StreamAdapter;
 use m17app::app::TxHandle;
-use m17core::address::Address;
-use m17core::address::Callsign;
-use m17core::protocol::LsfFrame;
-use m17core::protocol::StreamFrame;
+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::collections::VecDeque;
 use std::fs::File;
 use std::io::Write;
@@ -62,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
 struct AdapterState {
     tx: Option<TxHandle>,
     /// Circular buffer of output samples for playback
@@ -91,7 +96,7 @@ impl StreamAdapter for Codec2Adapter {
 
     fn tnc_closed(&self) {}
 
 
     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
         // for now we will assume:
         // - unencrypted
         // - data type is Voice (Codec2 3200), not Voice+Data
@@ -142,7 +147,7 @@ fn stream_thread(end: Receiver<()>, state: Arc<Mutex<AdapterState>>, output_card
     let stream = device
         .build_output_stream(
             &config.into(),
     let stream = device
         .build_output_stream(
             &config.into(),
-            move |data: &mut [i16], info: &cpal::OutputCallbackInfo| {
+            move |data: &mut [i16], _info: &cpal::OutputCallbackInfo| {
                 output_cb(data, &state);
             },
             |e| {
                 output_cb(data, &state);
             },
             |e| {
@@ -160,7 +165,13 @@ fn stream_thread(end: Receiver<()>, state: Arc<Mutex<AdapterState>>, output_card
 pub struct WavePlayer;
 
 impl WavePlayer {
 pub struct WavePlayer;
 
 impl WavePlayer {
-    pub fn play(path: PathBuf, tx: TxHandle) {
+    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 reader = hound::WavReader::open(path).unwrap();
         let mut samples = reader.samples::<i16>();
 
@@ -172,19 +183,14 @@ impl WavePlayer {
         let mut next_tick = Instant::now() + TICK;
         let mut frame_number = 0;
 
         let mut next_tick = Instant::now() + TICK;
         let mut frame_number = 0;
 
-        // TODO: need a better way to create addresses from std strings
-
-        let lsf = LsfFrame::new_voice(
-            &Address::Callsign(Callsign(b"VK7XT    ".clone())),
-            &Address::Broadcast,
-        );
-
-        tx.transmit_stream_start(lsf.clone());
+        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;
 
         loop {
             let mut last_one = false;
-            for mut out in out_buf.chunks_mut(8) {
-                for i in 0..160 {
+            for out in out_buf.chunks_mut(8) {
+                for i in in_buf.iter_mut() {
                     let sample = match samples.next() {
                         Some(Ok(sample)) => sample,
                         _ => {
                     let sample = match samples.next() {
                         Some(Ok(sample)) => sample,
                         _ => {
@@ -192,18 +198,16 @@ impl WavePlayer {
                             0
                         }
                     };
                             0
                         }
                     };
-                    in_buf[i] = sample;
+                    *i = sample;
                 }
                 }
-                codec.encode(&mut out, &in_buf);
+                codec.encode(out, &in_buf);
             }
             }
-            tx.transmit_stream_next(StreamFrame {
+            tx.transmit_stream_next(&StreamFrame {
                 lich_idx: lsf_chunk as u8,
                 lich_idx: lsf_chunk as u8,
-                lich_part: lsf.0[lsf_chunk * 5..(lsf_chunk + 1) * 5]
-                    .try_into()
-                    .unwrap(),
+                lich_part: setup.lich_part(lsf_chunk as u8),
                 frame_number,
                 end_of_stream: last_one,
                 frame_number,
                 end_of_stream: last_one,
-                stream_data: out_buf.clone(),
+                stream_data: out_buf,
             });
             frame_number += 1;
             lsf_chunk = (lsf_chunk + 1) % 6;
             });
             frame_number += 1;
             lsf_chunk = (lsf_chunk + 1) % 6;