1 extern crate nihav_core;
3 use nihav_core::io::byteio::*;
4 use nihav_core::frame::*;
8 pub struct WavWriter<'a> {
9 io: Box<ByteWriter<'a>>,
15 fn write_byte(wr: &mut ByteWriter, sample: u8) -> ByteIOResult<()> {
19 fn write_s16(wr: &mut ByteWriter, sample: i16) -> ByteIOResult<()> {
20 wr.write_u16le(sample as u16)
23 fn write_s32(wr: &mut ByteWriter, sample: i32) -> ByteIOResult<()> {
24 wr.write_u16le((sample >> 16) as u16)
27 fn write_f32(wr: &mut ByteWriter, sample: f32) -> ByteIOResult<()> {
28 let mut out = (sample * 32768.0) as i32;
29 if out < -32768 { out = -32768; }
30 if out > 32767 { out = 32767; }
31 if out < 0 { out += 65536; }
32 wr.write_u16le(out as u16)
35 macro_rules! write_data {
36 ($wr:expr, $buf:expr, $write:ident) => ({
37 let len = $buf.get_length();
38 let ainfo = $buf.get_info();
39 let nch = ainfo.get_channels() as usize;
40 let mut offs: Vec<usize> = Vec::with_capacity(nch);
41 for ch in 0..nch { offs.push($buf.get_offset(ch)); }
42 let is_planar = $buf.get_step() == 1;
43 let data = $buf.get_data();
48 let sample = data[offs[ch] + i];
61 impl<'a> WavWriter<'a> {
62 pub fn new(name: &str) -> Self {
63 let file = File::create(name).unwrap();
64 let fw = Box::new(FileWriter::new_write(file));
65 let io = ByteWriter::new(Box::leak(fw));
66 WavWriter { io: Box::new(io), data_pos: 0, be: false, bits: 0 }
68 pub fn write_header(&mut self, ainfo: NAAudioInfo) -> ByteIOResult<()> {
69 let bits = ainfo.get_format().get_bits() as usize;
71 self.io.write_buf(b"RIFF")?;
72 self.io.write_u32le(0)?;
73 self.io.write_buf(b"WAVE")?;
75 self.io.write_buf(b"fmt ")?;
76 self.io.write_u32le(16)?;
77 self.io.write_u16le(0x0001)?; // PCM
78 self.io.write_u16le(ainfo.get_channels() as u16)?;
79 self.io.write_u32le(ainfo.get_sample_rate() as u32)?;
82 self.io.write_u32le((ainfo.get_channels() as u32) * (ainfo.get_sample_rate() as u32))?;
83 self.io.write_u16le(ainfo.get_channels() as u16)?; // block align
84 self.io.write_u16le(8)?;
86 self.io.write_u32le(2 * (ainfo.get_channels() as u32) * (ainfo.get_sample_rate() as u32))?;
87 self.io.write_u16le((2 * ainfo.get_channels()) as u16)?; // block align
88 self.io.write_u16le(16)?;
91 self.io.write_buf(b"data")?;
92 self.io.write_u32le(0)?;
94 self.bits = bits as u8;
95 self.be = ainfo.get_format().is_be();
96 self.data_pos = self.io.tell();
99 pub fn write_frame(&mut self, abuf: NABufferType) -> ByteIOResult<()> {
101 NABufferType::AudioU8(ref buf) => {
102 write_data!(&mut self.io, buf, write_byte);
104 NABufferType::AudioI16(ref buf) => {
105 write_data!(&mut self.io, buf, write_s16);
107 NABufferType::AudioI32(ref buf) => {
108 write_data!(&mut self.io, buf, write_s32);
110 NABufferType::AudioF32(ref buf) => {
111 write_data!(&mut self.io, buf, write_f32);
113 NABufferType::AudioPacked(ref buf) => {
114 if !self.be || self.bits == 8 {
115 self.io.write_buf(buf.get_data().as_slice())?;
117 let data = buf.get_data();
120 for samp in data.chunks(2) {
121 self.io.write_byte(samp[1])?;
122 self.io.write_byte(samp[0])?;
126 for samp in data.chunks(3) {
127 self.io.write_byte(samp[2])?;
128 self.io.write_byte(samp[1])?;
129 self.io.write_byte(samp[0])?;
133 for samp in data.chunks(4) {
134 self.io.write_byte(samp[3])?;
135 self.io.write_byte(samp[2])?;
136 self.io.write_byte(samp[1])?;
137 self.io.write_byte(samp[0])?;
140 _ => unimplemented!(),
150 impl<'a> Drop for WavWriter<'a> {
151 #[allow(unused_variables)]
153 let size = self.io.tell();
154 if (self.data_pos > 0) && (size >= self.data_pos) {
155 let res = self.io.seek(SeekFrom::Start(4));
156 let res = self.io.write_u32le((size - 8) as u32);
157 let res = self.io.seek(SeekFrom::Start(self.data_pos - 4));
158 let res = self.io.write_u32le(((size as u64) - self.data_pos) as u32);