#![allow(unused_imports)]
mod flac;
pub use crate::flac::{FlacEncoderUnmovable, FlacEncoder};
pub use crate::flac::{FlacDecoderUnmovable, FlacDecoder};
pub mod options {
pub use crate::flac::{FlacAudioForm, SamplesInfo};
pub use crate::flac::{FlacCompression, FlacEncoderParams};
}
pub mod closure_objects {
pub use crate::flac::SamplesInfo;
pub use crate::flac::{FlacReadStatus, FlacInternalDecoderError};
}
pub mod errors {
pub use crate::flac::FlacError;
pub use crate::flac::{FlacEncoderError, FlacDecoderError};
pub use crate::flac::{FlacEncoderErrorCode, FlacDecoderErrorCode};
pub use crate::flac::{FlacEncoderInitError, FlacDecoderInitError};
pub use crate::flac::{FlacEncoderInitErrorCode, FlacDecoderInitErrorCode};
}
#[test]
fn test() {
use std::{io::{self, Read, Write, Seek, SeekFrom, BufReader, BufWriter}, cmp::Ordering, fs::File};
type ReaderType = BufReader<File>;
let mut reader: ReaderType = BufReader::new(File::open("test.flac").unwrap());
let length = {
reader.seek(SeekFrom::End(0)).unwrap();
let ret = reader.stream_position().unwrap();
reader.seek(SeekFrom::Start(0)).unwrap();
ret
};
type WriterType = BufWriter<File>;
let mut writer: WriterType = BufWriter::new(File::create("output.flac").unwrap());
let mut pcm_frames = Vec::<Vec<i16>>::new();
let mut encoder = FlacEncoder::new(
&mut writer,
Box::new(|writer: &mut WriterType, data: &[u8]| -> Result<(), io::Error> {
writer.write_all(data)
}),
Box::new(|writer: &mut WriterType, position: u64| -> Result<(), io::Error> {
writer.seek(SeekFrom::Start(position))?;
Ok(())
}),
Box::new(|writer: &mut WriterType| -> Result<u64, io::Error> {
writer.stream_position()
}),
&FlacEncoderParams {
verify_decoded: false,
compression: FlacCompression::Level8,
channels: 2,
sample_rate: 44100,
bits_per_sample: 16,
total_samples_estimate: 0
}
).unwrap();
encoder.initialize().unwrap();
let mut decoder = FlacDecoder::new(
&mut reader,
Box::new(|reader: &mut ReaderType, data: &mut [u8]| -> (usize, FlacReadStatus) {
let to_read = data.len();
match reader.read(data) {
Ok(size) => {
match size.cmp(&to_read) {
Ordering::Equal => (size, FlacReadStatus::GoOn),
Ordering::Less => (size, FlacReadStatus::Eof),
Ordering::Greater => panic!("`reader.read()` returns a size greater than the desired size."),
}
},
Err(e) => {
eprintln!("on_read(): {:?}", e);
(0, FlacReadStatus::Abort)
}
}
}),
Box::new(|reader: &mut ReaderType, position: u64| -> Result<(), io::Error> {
reader.seek(SeekFrom::Start(position))?;
Ok(())
}),
Box::new(|reader: &mut ReaderType| -> Result<u64, io::Error> {
reader.stream_position()
}),
Box::new(|_reader: &mut ReaderType| -> Result<u64, io::Error>{
Ok(length)
}),
Box::new(|reader: &mut ReaderType| -> bool {
reader.stream_position().unwrap() >= length
}),
Box::new(|samples: &[Vec<i32>], sample_info: &SamplesInfo| -> Result<(), io::Error>{
if sample_info.bits_per_sample != 16 {
panic!("The test function only tests 16-bit per sample FLAC files.")
}
let pcm_converted: Vec<Vec<i16>> = samples.iter().map(|frame: &Vec<i32>|{
frame.into_iter().map(|x32|{*x32 as i16}).collect()
}).collect();
pcm_frames.extend(pcm_converted);
let i32pcm: Vec::<Vec<i32>> = pcm_frames.iter().map(|frame: &Vec<i16>|{
frame.into_iter().map(|x16|{*x16 as i32}).collect()
}).collect();
encoder.write_frames(&i32pcm).unwrap();
pcm_frames.clear();
Ok(())
}),
Box::new(|error: FlacInternalDecoderError| {
panic!("{error}");
}),
true, false, FlacAudioForm::FrameArray
).unwrap();
decoder.decode_all().unwrap();
decoder.finalize();
encoder.finalize();
}