read_block

Function read_block 

Source
pub fn read_block<R, B>(r: R) -> Result<Option<B>, Error>
where R: Read, B: MetadataBlock,
Expand description

Returns first instance of the given block from the given reader

The reader should be positioned at the start of the FLAC file.

Because this may perform many small reads, using a buffered reader may greatly improve performance when reading from a raw File.

ยงExample

use flac_codec::{
    metadata::{read_block, Streaminfo},
    encode::{FlacSampleWriter, Options},
};
use std::io::{Cursor, Seek};

let mut flac: Cursor<Vec<u8>> = Cursor::new(vec![]);  // a FLAC file in memory

let mut writer = FlacSampleWriter::new(
    &mut flac,           // our wrapped writer
    Options::default(),  // default encoding options
    44100,               // sample rate
    16,                  // bits-per-sample
    1,                   // channel count
    Some(1),             // total samples
).unwrap();

// write a simple FLAC file
writer.write(std::slice::from_ref(&0)).unwrap();
writer.finalize().unwrap();

flac.rewind().unwrap();

// STREAMINFO block must always be present
let streaminfo = match read_block::<_, Streaminfo>(flac) {
    Ok(Some(streaminfo)) => streaminfo,
    _ => panic!("STREAMINFO not found"),
};

// verify STREAMINFO fields against encoding parameters
assert_eq!(streaminfo.sample_rate, 44100);
assert_eq!(u32::from(streaminfo.bits_per_sample), 16);
assert_eq!(streaminfo.channels.get(), 1);
assert_eq!(streaminfo.total_samples.map(|s| s.get()), Some(1));