Permalink
Browse files

cleaner error handling with try and unwrap()

  • Loading branch information...
jfo committed Oct 12, 2016
1 parent 44981b9 commit 8994c8e0163a7a0d67bcac9f043745dd3327421f
Showing with 23 additions and 70 deletions.
  1. +23 −70 src/main.rs
@@ -14,90 +14,43 @@ const AUDIO_FORMAT: u32 = 1;
const BIT_DEPTH: u32 = 8;
const BYTE_SIZE: u32 = 8;

#[allow(unused_must_use)]
fn write_header<T: Write>(seconds: u32, handle: &mut T) -> Result<(), std::io::Error>{
fn write_header<T: Write>(seconds: u32, handle: &mut T) -> Result<(), Error> {

let numsamples = SAMPLE_RATE * seconds;

match handle.write(b"RIFF") {
Ok(val) => val,
Err(err) => return Err(err)
};
match handle.write_u32::<LittleEndian>(HEADER_SIZE + numsamples) {
Ok(val) => val,
Err(err) => return Err(err)
};

match handle.write(b"WAVEfmt ") {
Ok(val) => val,
Err(err) => return Err(err)
};
match handle.write_u32::<LittleEndian>(SUBCHUNK1_SIZE) {
Ok(val) => val,
Err(err) => return Err(err)
};
match handle.write_u16::<LittleEndian>(AUDIO_FORMAT as u16) {
Ok(val) => val,
Err(err) => return Err(err)
};
match handle.write_u16::<LittleEndian>(CHANNELS as u16) {
Ok(val) => val,
Err(err) => return Err(err)
};

match handle.write_u32::<LittleEndian>(SAMPLE_RATE) {
Ok(val) => val,
Err(err) => return Err(err)
};
match handle.write_u32::<LittleEndian>(SAMPLE_RATE * CHANNELS * (BIT_DEPTH / BYTE_SIZE)) {
Ok(val) => val,
Err(err) => return Err(err)
};
match handle.write_u16::<LittleEndian>((CHANNELS * (BIT_DEPTH / BYTE_SIZE)) as u16) {
Ok(val) => val,
Err(err) => return Err(err)
};
match handle.write_u16::<LittleEndian>(BIT_DEPTH as u16) {
Ok(val) => val,
Err(err) => return Err(err)
};

match handle.write(b"data") {
Ok(val) => val,
Err(err) => return Err(err)
};
match handle.write_u32::<LittleEndian>(numsamples * CHANNELS * (BIT_DEPTH / BYTE_SIZE)) {
Ok(val) => val,
Err(err) => return Err(err)
};
try!(handle.write(b"RIFF"));
try!(handle.write_u32::<LittleEndian>(HEADER_SIZE + numsamples));

try!(handle.write(b"WAVEfmt "));
try!(handle.write_u32::<LittleEndian>(SUBCHUNK1_SIZE));
try!(handle.write_u16::<LittleEndian>(AUDIO_FORMAT as u16));
try!(handle.write_u16::<LittleEndian>(CHANNELS as u16));

try!(handle.write_u32::<LittleEndian>(SAMPLE_RATE));
try!(handle.write_u32::<LittleEndian>(SAMPLE_RATE * CHANNELS * (BIT_DEPTH / BYTE_SIZE)));
try!(handle.write_u16::<LittleEndian>((CHANNELS * (BIT_DEPTH / BYTE_SIZE)) as u16));
try!(handle.write_u16::<LittleEndian>(BIT_DEPTH as u16));

try!(handle.write(b"data"));
try!(handle.write_u32::<LittleEndian>(numsamples * CHANNELS * (BIT_DEPTH / BYTE_SIZE)));

Ok(())
}

fn make_some_noise<T: Write>(seconds: u32, handle: &mut T) -> Result<(), Error > {

for _ in 0..seconds * SAMPLE_RATE {
match handle.write(&[ rand::random::<u8>() ]) {
Ok(val) => val,
Err(err) => return Err(err)
};
try!(handle.write(&[ rand::random::<u8>() ]));
}

Ok(())
}

fn main() {
let duration = 1;

let mut fp = match File::create("out.wav") {
Ok(val) => val,
Err(_) => panic!("File creation failed.")
};

match write_header(duration, &mut fp) {
Ok(val) => val,
Err(_) => panic!("Header writing failed")
};
match make_some_noise(duration, &mut fp) {
Ok(val) => val,
Err(_) => panic!("Noisemaking failed.")
};
let mut fp = File::create("out.wav").unwrap();

write_header(duration, &mut fp).unwrap();
make_some_noise(duration, &mut fp).unwrap();
}

0 comments on commit 8994c8e

Please sign in to comment.