diff options
author | David Barksdale <amatus@amatus.name> | 2013-09-28 17:16:27 -0500 |
---|---|---|
committer | David Barksdale <amatus@amatus.name> | 2013-09-28 17:16:27 -0500 |
commit | 2ba887beb5493f580c491934d8690677f13a71fb (patch) | |
tree | e9d65fa9bf6261fbb08aec905b2f0296787822a0 | |
parent | 60fbeeae8f13256504dc0a0bde4237e81af20cbd (diff) |
Do the mp3 encoding.
-rw-r--r-- | mp3lame.rs | 22 | ||||
-rw-r--r-- | per.rs | 6 |
2 files changed, 27 insertions, 1 deletions
@@ -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 { @@ -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()); } } |