👕 refactor: removed the memcpy by moving it to a place where I had to memcpy anyway

main
_ 2021-11-13 17:51:06 +00:00
parent f6b85a24da
commit 3b45f4309f
2 changed files with 7 additions and 22 deletions

View File

@ -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 <FfAudioFrame> for AudioFrame {
#[derive (Default)]
pub struct PcmBuffers {
buffers: Vec <Vec <f32>>,
buffers: Vec <Vec <u8>>,
// 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::<usize> () - self.consumer_cursor
(self.buffers.iter ().map (|b| b.len ()).sum::<usize> () - 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 <f32>) {
pub fn produce_bytes (&mut self, new_buffer: Vec <u8>) {
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::<LittleEndian> (&mut b).unwrap ();
self.produce (b);
}
}
pub struct Decoder {

View File

@ -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;