From 3b45f4309fc224ba14cfe4952f7a6bb5cd4cb040 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sat, 13 Nov 2021 17:51:06 +0000 Subject: [PATCH] :shirt: refactor: removed the memcpy by moving it to a place where I had to memcpy anyway --- src/decoder.rs | 27 ++++++--------------------- src/main.rs | 2 +- 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/src/decoder.rs b/src/decoder.rs index c5c9a01..2c633c7 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -1,15 +1,11 @@ -use std::{ - io::Cursor, -}; - use anyhow::{ anyhow, Result, }; use byteorder::{ + ByteOrder, LittleEndian, - ReadBytesExt, }; // This crate flitters between being very convenient and being a type labyrinth. @@ -59,7 +55,7 @@ impl From for AudioFrame { #[derive (Default)] pub struct PcmBuffers { - buffers: Vec >, + buffers: Vec >, // Always points into the first buffer, if any consumer_cursor: usize, @@ -67,7 +63,7 @@ pub struct PcmBuffers { impl PcmBuffers { pub fn samples_available (&self) -> usize { - self.buffers.iter ().map (|b| b.len ()).sum:: () - self.consumer_cursor + (self.buffers.iter ().map (|b| b.len ()).sum:: () - self.consumer_cursor) / 8 } #[warn(unused_must_use)] @@ -82,27 +78,16 @@ impl PcmBuffers { self.consumer_cursor = 0; } - *x = self.buffers [0][self.consumer_cursor]; - self.consumer_cursor += 1; + *x = LittleEndian::read_f32 (&self.buffers [0][self.consumer_cursor..]); + self.consumer_cursor += 4; } true } - pub fn produce (&mut self, new_buffer: Vec ) { + pub fn produce_bytes (&mut self, new_buffer: Vec ) { self.buffers.push (new_buffer); } - - pub fn produce_bytes (&mut self, new_buffer: &[u8]) { - let mut b = vec! [0.0f32; new_buffer.len () / 4]; - let mut rdr = Cursor::new (new_buffer); - - // Pointless memcpy. I can get rid of it later by changing PcmBuffers' - // internal format - rdr.read_f32_into:: (&mut b).unwrap (); - - self.produce (b); - } } pub struct Decoder { diff --git a/src/main.rs b/src/main.rs index 34873e5..1490748 100644 --- a/src/main.rs +++ b/src/main.rs @@ -121,7 +121,7 @@ fn cmd_play (args: &[String]) -> Result <()> { while pcm_buffers.samples_available () < 12_000 { // tracing::trace! ("Decoder is trying to work..."); match decoder.next ()? { - Some (frame) => pcm_buffers.produce_bytes (frame.data ()), + Some (frame) => pcm_buffers.produce_bytes (frame.data ().into ()), None => { tracing::info! ("Finished decoding file"); break 'one_file;