1 use crate::io::byteio::*;
5 pub struct WavWriter<'a> {
6 io: &'a mut ByteWriter<'a>,
10 fn write_byte(wr: &mut ByteWriter, sample: u8) -> ByteIOResult<()> {
14 fn write_s16(wr: &mut ByteWriter, sample: i16) -> ByteIOResult<()> {
15 wr.write_u16le(sample as u16)
18 fn write_s32(wr: &mut ByteWriter, sample: i32) -> ByteIOResult<()> {
19 wr.write_u16le((sample >> 16) as u16)
22 fn write_f32(wr: &mut ByteWriter, sample: f32) -> ByteIOResult<()> {
23 let mut out = (sample * 32768.0) as i32;
24 if out < -32768 { out = -32768; }
25 if out > 32767 { out = 32767; }
26 if out < 0 { out += 65536; }
27 wr.write_u16le(out as u16)
30 macro_rules! write_data {
31 ($wr:expr, $buf:expr, $write:ident) => ({
32 let len = $buf.get_length();
33 let ainfo = $buf.get_info();
34 let nch = ainfo.get_channels() as usize;
35 let mut offs: Vec<usize> = Vec::with_capacity(nch);
36 for ch in 0..nch { offs.push($buf.get_offset(ch)); }
37 let data = $buf.get_data();
41 let sample = data[offs[ch] + i];
48 impl<'a> WavWriter<'a> {
49 pub fn new(io: &'a mut ByteWriter<'a>) -> Self {
50 WavWriter { io, data_pos: 0 }
52 pub fn write_header(&mut self, ainfo: NAAudioInfo) -> ByteIOResult<()> {
53 let bits = ainfo.get_format().get_bits() as usize;
55 self.io.write_buf(b"RIFF")?;
56 self.io.write_u32le(0)?;
57 self.io.write_buf(b"WAVE")?;
59 self.io.write_buf(b"fmt ")?;
60 self.io.write_u32le(16)?;
61 self.io.write_u16le(0x0001)?; // PCM
62 self.io.write_u16le(u16::from(ainfo.get_channels()))?;
63 self.io.write_u32le(ainfo.get_sample_rate())?;
66 self.io.write_u32le(u32::from(ainfo.get_channels()) * ainfo.get_sample_rate())?;
67 self.io.write_u16le(u16::from(ainfo.get_channels()))?; // block align
68 self.io.write_u16le(8)?;
70 self.io.write_u32le(2 * u32::from(ainfo.get_channels()) * ainfo.get_sample_rate())?;
71 self.io.write_u16le(u16::from(2 * ainfo.get_channels()))?; // block align
72 self.io.write_u16le(16)?;
75 self.io.write_buf(b"data")?;
76 self.io.write_u32le(0)?;
78 self.data_pos = self.io.tell();
81 pub fn write_frame(&mut self, abuf: NABufferType) -> ByteIOResult<()> {
83 NABufferType::AudioU8(ref buf) => {
84 write_data!(self.io, buf, write_byte);
86 NABufferType::AudioI16(ref buf) => {
87 write_data!(self.io, buf, write_s16);
89 NABufferType::AudioI32(ref buf) => {
90 write_data!(self.io, buf, write_s32);
92 NABufferType::AudioF32(ref buf) => {
93 write_data!(self.io, buf, write_f32);
95 NABufferType::AudioPacked(ref buf) => {
96 self.io.write_buf(buf.get_data().as_slice())?;
104 impl<'a> Drop for WavWriter<'a> {
105 #[allow(unused_variables)]
107 let size = self.io.tell();
108 if (self.data_pos > 0) && (size >= self.data_pos) {
109 let res = self.io.seek(SeekFrom::Start(4));
110 let res = self.io.write_u32le((size - 8) as u32);
111 let res = self.io.seek(SeekFrom::Start(self.data_pos - 4));
112 let res = self.io.write_u32le(((size as u64) - self.data_pos) as u32);