-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathdecode_simple.rs
56 lines (47 loc) · 1.84 KB
/
decode_simple.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Claxon -- A FLAC decoding library in Rust
// Copyright 2017 Ruud van Asseldonk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// A copy of the License has been included in the root of the repository.
// This file contains a minimal example of using Claxon and Hound to decode a
// flac file. This can be done more efficiently, but it is also more verbose.
// See the `decode` example for that.
extern crate claxon;
extern crate hound;
use std::env;
use std::path::Path;
fn decode_file(fname: &Path) -> claxon::Result<()> {
let mut reader = try!(claxon::FlacReader::open(fname));
let spec = hound::WavSpec {
channels: reader.streaminfo().channels as u16,
sample_rate: reader.streaminfo().sample_rate,
bits_per_sample: reader.streaminfo().bits_per_sample as u16,
sample_format: hound::SampleFormat::Int,
};
let fname_wav = fname.with_extension("wav");
let opt_wav_writer = hound::WavWriter::create(fname_wav, spec);
let mut wav_writer = opt_wav_writer.expect("failed to create wav file");
for opt_sample in reader.samples() {
let sample = try!(opt_sample);
wav_writer.write_sample(sample).expect("failed to write wav file");
}
Ok(())
}
fn main() {
for fname in env::args().skip(1) {
print!("{}", fname);
match decode_file(&Path::new(&fname)) {
Ok(()) => println!(": done"),
Err(claxon::Error::Unsupported(msg)) => {
println!(": error, unsupported: {}", msg);
}
Err(claxon::Error::FormatError(msg)) => {
println!(": error, invalid input: {}", msg);
}
Err(claxon::Error::IoError(io_err)) => {
println!(": IO error: {}", io_err);
}
}
}
}