diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2023-01-08 16:14:22 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2023-01-08 16:15:13 +0100 |
commit | 212dfb23548ed79ba759cbe1d9f1cc189dab9f5a (patch) | |
tree | 6cae01dbab115b02023f9767964e6f4602c09803 /src/fl2k.rs | |
parent | c00dbb4579b98e248e99d546268782fa0f72bff0 (diff) | |
download | fl2k_ampliphase-212dfb23548ed79ba759cbe1d9f1cc189dab9f5a.tar.gz fl2k_ampliphase-212dfb23548ed79ba759cbe1d9f1cc189dab9f5a.tar.bz2 fl2k_ampliphase-212dfb23548ed79ba759cbe1d9f1cc189dab9f5a.zip |
Rework fl2k buffer size
Diffstat (limited to 'src/fl2k.rs')
-rw-r--r-- | src/fl2k.rs | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/src/fl2k.rs b/src/fl2k.rs index a17a04d..695d32a 100644 --- a/src/fl2k.rs +++ b/src/fl2k.rs @@ -1,5 +1,7 @@ -use std::{ffi::{c_int, c_void}, sync::mpsc}; -use fl2k_ampliphase::{fl2k_dev_t, fl2k_get_device_count, fl2k_open, fl2k_close, fl2k_stop_tx, fl2k_set_sample_rate, fl2k_get_sample_rate, fl2k_start_tx}; +use std::{ffi::{c_int, c_void}, sync::mpsc, mem::swap}; +use fl2k_ampliphase::{fl2k_dev_t, fl2k_get_device_count, fl2k_open, fl2k_close, fl2k_stop_tx, fl2k_set_sample_rate, fl2k_get_sample_rate, fl2k_start_tx, FL2K_BUF_LEN}; + +const BUF_LEN : usize = FL2K_BUF_LEN as usize; #[derive(Debug)] pub enum FL2KError { @@ -32,6 +34,9 @@ pub struct FL2K { device: *mut fl2k_dev_t, callback_ctx: Box<CallbackCtx>, tx : mpsc::SyncSender<(Vec<i8>, Vec<i8>)>, + + r_buf : Vec<i8>, + g_buf : Vec<i8> } pub struct CallbackCtx { @@ -88,7 +93,11 @@ impl FL2K { abort: false, }; unsafe { - let mut fl2k = FL2K { device: std::mem::zeroed(), callback_ctx: Box::new(ctx), tx }; + + let r_buf = Vec::with_capacity(2 * BUF_LEN); + let g_buf = Vec::with_capacity(2 * BUF_LEN); + + let mut fl2k = FL2K { device: std::mem::zeroed(), callback_ctx: Box::new(ctx), tx, r_buf, g_buf }; handle_return_value(fl2k_open(&mut fl2k.device, device_index))?; Ok(fl2k) } @@ -118,11 +127,25 @@ impl FL2K { handle_return_value( unsafe { fl2k_stop_tx(self.device) } ) } - pub fn send(&self, r_buf: Vec<i8>, g_buf: Vec<i8>) -> bool { + pub fn send(&mut self, mut r_buf: Vec<i8>, mut g_buf: Vec<i8>) -> bool { if r_buf.len() != g_buf.len() { panic!("r_buf and g_buf must have same length"); } - self.tx.send((r_buf, g_buf)).is_ok() + + self.r_buf.append(&mut r_buf); + self.g_buf.append(&mut g_buf); + + if self.r_buf.len() >= BUF_LEN { + let mut r = self.r_buf.split_off(BUF_LEN); + let mut g = self.g_buf.split_off(BUF_LEN); + /* self.r_buf contains head, r contains tail. Swap them, as we want to give the head and keep the tail. */ + swap(&mut r, &mut self.r_buf); + swap(&mut g, &mut self.g_buf); + self.tx.send((r, g)).is_ok() + } + else { + true + } } } |