X-Git-Url: https://code.octet-stream.net/m17rt/blobdiff_plain/6440cd74346c4b2d63d4774476e8c6113c032534..3903e719137aba15d30dd58b8d917965ec602400:/m17codec2/src/lib.rs?ds=inline diff --git a/m17codec2/src/lib.rs b/m17codec2/src/lib.rs index 016acc7..e4bfbac 100755 --- a/m17codec2/src/lib.rs +++ b/m17codec2/src/lib.rs @@ -1,3 +1,5 @@ +#![doc = include_str!("../README.md")] + use codec2::{Codec2, Codec2Mode}; use cpal::traits::DeviceTrait; use cpal::traits::HostTrait; @@ -40,6 +42,7 @@ pub fn decode_codec2>(data: &[u8], out_path: P) -> Vec { all_samples } +/// Subscribes to M17 streams and attempts to play the decoded Codec2 pub struct Codec2Adapter { state: Arc>, // TODO: make this configurable @@ -61,6 +64,12 @@ impl Codec2Adapter { } } +impl Default for Codec2Adapter { + fn default() -> Self { + Self::new() + } +} + struct AdapterState { tx: Option, /// Circular buffer of output samples for playback @@ -156,9 +165,17 @@ fn stream_thread(end: Receiver<()>, state: Arc>, output_card // it seems concrete impls of Stream have a Drop implementation that will handle termination } +/// Transmits a wave file as an M17 stream pub struct WavePlayer; impl WavePlayer { + /// Plays a wave file (blocking). + /// + /// * `path`: wave file to transmit, must be 8 kHz mono and 16-bit LE + /// * `tx`: a `TxHandle` obtained from an `M17App` + /// * `source`: address of transmission source + /// * `destination`: address of transmission destination + /// * `channel_access_number`: from 0 to 15, usually 0 pub fn play( path: PathBuf, tx: TxHandle, @@ -183,8 +200,8 @@ impl WavePlayer { 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, _ => { @@ -192,16 +209,16 @@ impl WavePlayer { 0 } }; - in_buf[i] = sample; + *i = sample; } - codec.encode(&mut out, &in_buf); + 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.clone(), + stream_data: out_buf, }); frame_number += 1; lsf_chunk = (lsf_chunk + 1) % 6;