From 212dfb23548ed79ba759cbe1d9f1cc189dab9f5a Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 8 Jan 2023 16:14:22 +0100 Subject: Rework fl2k buffer size --- src/fl2k.rs | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'src/fl2k.rs') 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, tx : mpsc::SyncSender<(Vec, Vec)>, + + r_buf : Vec, + g_buf : Vec } 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, g_buf: Vec) -> bool { + pub fn send(&mut self, mut r_buf: Vec, mut g_buf: Vec) -> 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 + } } } -- cgit v1.2.3