From 2ba887beb5493f580c491934d8690677f13a71fb Mon Sep 17 00:00:00 2001 From: David Barksdale Date: Sat, 28 Sep 2013 17:16:27 -0500 Subject: Do the mp3 encoding. --- mp3lame.rs | 22 ++++++++++++++++++++++ per.rs | 6 +++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/mp3lame.rs b/mp3lame.rs index 627a50f..d26b667 100644 --- a/mp3lame.rs +++ b/mp3lame.rs @@ -1,4 +1,5 @@ use std::libc::{c_float, c_int, c_short, c_uchar, c_ulong, c_void}; +use std::vec; type GlobalFlags_ = *c_void; @@ -92,6 +93,27 @@ impl LameContext { pub fn init_params(&self) { unsafe { lame_init_params(self.gfp) }; } + #[fixed_stack_segment] + pub fn encode_buffer_interleaved(&self, pcm: &[u8]) -> ~[u8] { + if pcm.len() % 4 != 0 { + return ~[]; + } + let num_samples = pcm.len() / 4; + let mp3buf_size = (1.25 * num_samples as float + 7200.0) as uint; + unsafe { + let mut mp3buf: ~[u8] = vec::with_capacity(mp3buf_size); + let length = lame_encode_buffer_interleaved(self.gfp, + vec::raw::to_ptr(pcm) as *c_short, + num_samples as c_int, + vec::raw::to_mut_ptr(mp3buf) as *c_uchar, + mp3buf_size as c_int); + if length < 0 { + return ~[]; + } + vec::raw::set_len(&mut mp3buf, length as uint); + mp3buf + } + } } impl Drop for LameContext { diff --git a/per.rs b/per.rs index 1caf622..1cb38e9 100644 --- a/per.rs +++ b/per.rs @@ -43,12 +43,16 @@ fn main() { println(fmt!("Sample rate: %d Hz", speed)); lame.set_quality(2); lame.set_bitrate(128); + lame.set_disable_reservoir(true); + lame.init_params(); let (port, chan) = stream::<~[u8]>(); do spawn_sched(SingleThreaded) { dsp.read_all(&chan); } loop { let buffer = port.recv(); - println(fmt!("Read buffer of length %u", buffer.len())); + debug!("Read buffer of length %u", buffer.len()); + let mp3buf = lame.encode_buffer_interleaved(buffer); + debug!("Encoded buffer of length %u", mp3buf.len()); } } -- cgit v1.2.3-18-g5258