Skip to content

Instantly share code, notes, and snippets.

@TonalidadeHidrica
Last active February 16, 2021 15:52
Show Gist options
  • Save TonalidadeHidrica/3f2ac116b00399c298909635ed3d3655 to your computer and use it in GitHub Desktop.
Save TonalidadeHidrica/3f2ac116b00399c298909635ed3d3655 to your computer and use it in GitHub Desktop.
/target
/song.ogg
/ignore
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "anyhow"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afddf7f520a80dbf76e6f50a35bca42a2331ef227a28b3b6dc5c2e2338d114b1"
[[package]]
name = "byteorder"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "either"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "getrandom"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "itertools"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37d572918e350e82412fe766d24b15e6682fb2ed2bbe018280caa810397cb319"
dependencies = [
"either",
]
[[package]]
name = "lewton"
version = "0.10.2"
dependencies = [
"byteorder",
"ogg",
"tinyvec",
]
[[package]]
name = "libc"
version = "0.2.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c"
[[package]]
name = "ogg"
version = "0.8.0"
dependencies = [
"byteorder",
]
[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "rand"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
dependencies = [
"rand_core",
]
[[package]]
name = "test_ogg"
version = "0.1.0"
dependencies = [
"anyhow",
"itertools",
"lewton",
"ogg",
"rand",
]
[[package]]
name = "tinyvec"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "317cca572a0e89c3ce0ca1f1bdc9369547fe318a683418e42ac8f59d14701023"
dependencies = [
"tinyvec_macros",
]
[[package]]
name = "tinyvec_macros"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[package]
name = "test_ogg"
version = "0.1.0"
authors = ["TonalidadeHidrica <47710717+TonalidadeHidrica@users.noreply.github.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
lewton = { path = "../../TonalidadeHidrica/lewton/" }
ogg = { path = "../../TonalidadeHidrica/ogg/" }
anyhow = "1.0.38"
itertools = "0.10.0"
rand = "0.8.3"
[[bin]]
name = "show_packets"
path = "show_packets.rs"
[[bin]]
name = "test_seek"
path = "test_seek.rs"
use itertools::Itertools;
use lewton::{
audio::{get_decoded_sample_count, read_audio_packet, PreviousWindowRight},
inside_ogg::read_headers,
};
use ogg::reading::PacketReader;
use std::fs::File;
use std::io::BufReader;
fn main() -> anyhow::Result<()> {
let mut packet_reader = PacketReader::new(BufReader::new(File::open("song.ogg")?));
let ((ident_header, _content_header, setup_header), _stream_serial) =
read_headers(&mut packet_reader)?;
let mut pwr = PreviousWindowRight::new();
let mut parsed = 0;
while let Some(packet) = packet_reader.read_packet()? {
let res = read_audio_packet(&ident_header, &setup_header, &packet.data, &mut pwr)?;
parsed += res[0].len() as u64;
println!(
"{} {:08b} {:9} {:9} {:9} {:9} {:9} {}{}{}{} {:?}",
if packet.last_in_page() && parsed != packet.absgp_page() {
'!'
} else {
' '
},
packet.data[0],
packet.stream_serial(),
packet.absgp_page(),
parsed,
res[0].len(),
get_decoded_sample_count(&ident_header, &setup_header, &packet.data)?,
if packet.first_in_stream() { '^' } else { ' ' },
if packet.first_in_page() { '^' } else { ' ' },
if packet.last_in_page() { 'v' } else { ' ' },
if packet.last_in_stream() { 'v' } else { ' ' },
res[0]
.iter()
.zip(res[1].iter())
.flat_map(|(x, y)| vec![x, y].into_iter())
.take(10)
.collect_vec(),
);
}
Ok(())
}
use lewton::inside_ogg::SeekableOggStreamReader;
use lewton::samples::InterleavedSamples;
use rand::distributions::uniform::Uniform;
use rand::prelude::*;
use std::fs::File;
use std::io::{BufReader, Read, Seek};
fn take_n_samples<R, N>(
reader: &mut SeekableOggStreamReader<R>,
n: N,
debug: bool,
) -> anyhow::Result<Vec<i16>>
where
R: Read + Seek,
N: Into<Option<usize>>,
{
let n = n.into().unwrap_or(usize::MAX);
let mut samples = Vec::new();
while let Some(decoded) = reader
.inner_mut()
.read_dec_packet_generic::<InterleavedSamples<i16>>()?
{
if debug {
println!("Decoded: {:?}", &decoded.samples)
}
samples.extend(decoded.samples);
if n <= samples.len() {
samples.truncate(n);
break;
}
}
Ok(samples)
}
fn main() -> anyhow::Result<()> {
let mut reader = SeekableOggStreamReader::new(BufReader::new(File::open("song.ogg")?))?;
let samples = take_n_samples(&mut reader, 1_500_000, false)?;
let absgp_max = samples.len() as u64 / 2;
dbg!(absgp_max);
let mut check_seek = |absgp, debug| -> anyhow::Result<()> {
let take_n = 2048;
reader.seek_absgp(absgp)?;
let obtained = take_n_samples(&mut reader, take_n, debug)?;
let absgp = absgp as usize;
let expected =
&samples[(absgp * 2).min(samples.len())..(absgp * 2 + take_n).min(samples.len())];
if debug {
println!("Expected: {:?}", &expected);
}
if &obtained[..] != expected {
println!("Failed seeking to {}", absgp);
println!("Expected: {:?}", expected);
println!("Obtained: {:?}", obtained);
}
Ok(())
};
check_seek(114514, false)?;
check_seek(0, true)?;
for i in 0..=10 {
// check_seek(i, false)?;
// check_seek(absgp_max - 5 + i, false)?;
}
// check_seek(absgp_max + 4096, false)?;
// let mut rng = StdRng::from_seed([0; 32]);
// for (_, absgp) in Uniform::from(0..=absgp_max)
// .sample_iter(&mut rng)
// .take(1000)
// .enumerate()
// {
// check_seek(absgp, false)?;
// }
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment