mqa_identify/
lib.rs

1pub fn identify_mqa(path: impl AsRef<std::path::Path>) -> Result<bool, claxon::Error> {
2    let mut decoder = claxon::FlacReader::open(path)?;
3    let position = decoder.streaminfo().bits_per_sample - 16;
4
5    let mut frame_reader = decoder.blocks();
6    let block = vec![];
7    let mut current_block = frame_reader.read_next_or_eof(block);
8
9    let mut buffer0 = 0u64;
10    let mut buffer1 = 0u64;
11    let mut buffer2 = 0u64;
12
13    while let Ok(Some(samples)) = current_block {
14        for (x, y) in samples.stereo_samples() {
15            buffer0 |= ((x as u64 ^ y as u64) >> position) & 1;
16            buffer1 |= ((x as u64 ^ y as u64) >> (position + 1)) & 1;
17            buffer2 |= ((x as u64 ^ y as u64) >> (position + 2)) & 1;
18
19            if buffer0 == 0xbe0498c88 || buffer1 == 0xbe0498c88 || buffer2 == 0xbe0498c88 {
20                return Ok(true);
21            }
22
23            buffer0 = (buffer0 << 1) & 0xFFFFFFFFF;
24            buffer1 = (buffer1 << 1) & 0xFFFFFFFFF;
25            buffer2 = (buffer2 << 1) & 0xFFFFFFFFF;
26        }
27
28        current_block = frame_reader.read_next_or_eof(samples.into_buffer());
29    }
30
31    Ok(false)
32}