A FLAC decoding library in Rust.
Many media players crash on corrupted input (not FLAC in particular). This is bad, the decoder should signal an error on invalid input, it should not crash. I suspect that this is partly due to the fact that most decoders are written in C. I thought I'd try and write a decoder in a safe language: Rust. Video codecs can be quite complex, and nowadays CPU decoding is not all that common any more. Therefore, I decided to first try and write a decoder for an audio codec that I love and use on a daily basis: FLAC.
The following example computes the root mean square (RMS) of a FLAC file:
rust
let mut reader = claxon::FlacReader::open("testsamples/pop.flac").unwrap();
let mut sqr_sum = 0.0;
let mut count = 0;
for sample in reader.samples() {
let s = sample.unwrap() as f64;
sqr_sum += s * s;
count += 1;
}
println!("RMS is {}", (sqr_sum / count as f64).sqrt());
More examples can be found in the examples directory. For a simple example of decoding a FLAC file to wav with Claxon and Hound, see decode_simple.rs. A more efficient way of decoding requires dealing with a few details of the FLAC format. See decode.rs for an example.
These are the times to decode 5 real-world FLAC files to wav, average and standard deviation of 11 runs, normalized to version 1.3.2 of the reference implementation. Measurements were done on a Skylake i7.
| Decoder | Time / reference | | ------- | ---------------- | | Claxon | 1.13 ± 0.03 | | libflac | 1.00 ± 0.03 |
Claxon is licensed under the Apache 2.0 license. It may be used in free software as well as closed-source applications, both for commercial and non-commercial use under the conditions given in the license. If you want to use Claxon in your GPLv2-licensed software, you can add an exception to your copyright notice. Please do not open an issue if you disagree with the choice of license.