adhoc_audio/lib.rs
1#![allow(clippy::needless_doctest_main)]
2//! # Adhoc Audio
3//! A crate for compressing audio written in pure-rust
4//!
5//! # Example - compressing a wav file
6//! ```
7//! use adhoc_audio::{codec::Streamable, AdhocCodec, WavCodec};
8//! use std::fs::File;
9//!
10//! fn main() {
11//! println!("compressing file example..");
12//!
13//! //set up a buffer for reading/writing samples
14//! let mut samples = [0.0; 1024];
15//!
16//! //open wav file
17//! let mut wav_reader = WavCodec::load(
18//! File::open("./resources/taunt.wav").unwrap()
19//! ).unwrap();
20//!
21//! let mut adhoc = AdhocCodec::new()
22//! // compression-level 0 means 'lossless'
23//! // while levels 1-10 does quantization + dithering (quality suffers)
24//! .with_compression_level(7)
25//! // AdhocCodec::with_info(.. ) MUST BE CALLED before calling
26//! // encode/decode when you are creating a new instance of AdhocCodec
27//! .with_info(wav_reader.info());
28//!
29//! //'decode' wav stream bit-by-bit
30//! //Note:in this case we are just reading PCM info
31//! while let Some(samples_read) = wav_reader.decode(&mut samples) {
32//! //encode wav data bit-by-bit
33//! //memory is allocated as needed
34//! adhoc.encode(&samples[0..samples_read]);
35//! }
36//!
37//! //write compressed audio back to disk
38//! adhoc
39//! .save_to(File::create("./resources/taunt.adhoc").unwrap())
40//! .unwrap();
41//!
42//! println!("taunt.adhoc written to: ./resources");
43//! }
44//! ```
45//!
46//!
47//! # Example - decoding compressed audio
48//!
49//! ## Decode Example
50//! ```rust
51//!use adhoc_audio::{codec::Streamable, AdhocCodec, WavCodec};
52//!use std::fs::File;
53//!
54//!fn main() {
55//! println!("decompressing file from 'compress' example...");
56//!
57//! //set up a buffer for reading/writing samples
58//! let mut samples = [0.0; 1024];
59//!
60//! //open wav file
61//! let mut adhoc = AdhocCodec::load(
62//! File::open("./resources/taunt.adhoc").expect("run example 'compress' before this one"),
63//! )
64//! .unwrap();
65//!
66//! let mut wav_writer = WavCodec::new(adhoc.info());
67//!
68//! //decode adhoc stream a chunk of samples at a time
69//! while let Some(samples_read) = adhoc.decode(&mut samples) {
70//! //encode wav data bit-by-bit
71//! //memory is allocated as needed
72//! wav_writer.encode(&samples[0..samples_read]);
73//! }
74//!
75//! //write compressed audio back to disk
76//! wav_writer
77//! .save_to(File::create("./resources/taunt_decompressed.wav").unwrap())
78//! .unwrap();
79//!
80//! println!("taunt.adhoc written to: ./resources");
81//!}
82//!```
83//!
84//!
85//!
86
87/// all the audio stuff is in here
88pub mod codec;
89mod collections;
90mod math;
91
92pub use codec::{adhoc::AdhocCodec, wav::WavCodec, StreamInfo, Streamable};
93pub use std::io::SeekFrom;