Skip to content

Commit

Permalink
More examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentMazare committed Aug 29, 2024
1 parent 09912c9 commit 565ec5e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ tokenizer.json
vocab.json
merges.txt
*.ogg
*.ogg_table
*.wav
51 changes: 34 additions & 17 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,49 @@ use ogg_table::OggVorbisReader;

fn main() -> Result<()> {
let ogg_filename = "foo.ogg";
let start_time_sec = 314.15;
let duration_sec = 60.;
let (all_data, sample_rate) = {
let file = std::fs::File::open(ogg_filename)?;
let rdr = std::io::BufReader::new(file);
let mut ovr = OggVorbisReader::new(rdr)?;
let data = ovr.decode(0, 1000000000000)?;
(data, ovr.sample_rate())
};
let (selected_data, _sr) =
ogg_table::read_ogg_vorbis_sample(ogg_filename, start_time_sec, duration_sec)?;
let mut out_file = std::fs::File::create("foo1.wav")?;
ogg_table::wav::write_wav(&mut out_file, &selected_data[0], sample_rate)?;

let start_in_samples = (start_time_sec * sample_rate as f64) as usize;
let len_in_samples = (duration_sec * sample_rate as f64) as usize;
let data = &all_data[0][start_in_samples..start_in_samples + len_in_samples];
let mut out_file = std::fs::File::create("foo2.wav")?;
ogg_table::wav::write_wav(&mut out_file, &data, sample_rate)?;

for i in 0..1000 {
if &selected_data[0][i..i + 100] == &data[..100] {
println!("offset: {i}")
for with_ogg_table in [false, true] {
let ogg_table = std::path::Path::new(ogg_filename).with_extension("ogg_table");
if with_ogg_table {
let file = std::fs::File::open(ogg_filename)?;
let mut rdr = std::io::BufReader::new(file);
let table = ogg_table::TableOfContent::from_ogg_reader(&mut rdr)?;
let mut ogg_table = std::fs::File::create(ogg_table)?;
table.write(&mut ogg_table)?
} else {
let _maybe_err = std::fs::remove_file(ogg_table);
}
if &selected_data[0][..100] == &data[i..i + 100] {
println!("offset -{i}")
for start_time_sec in [314.15, 13.37, 13.36, 999.9, 3999.9] {
let duration_sec = 60.;
let start_instant = std::time::Instant::now();
let (selected_data, _sr) =
ogg_table::read_ogg_vorbis_sample(ogg_filename, start_time_sec, duration_sec)?;
let dt = start_instant.elapsed();
println!("start-time: {start_time_sec} {dt:?}");
let mut out_file = std::fs::File::create("foo1.wav")?;
ogg_table::wav::write_wav(&mut out_file, &selected_data[0], sample_rate)?;

let start_in_samples = (start_time_sec * sample_rate as f64) as usize;
let len_in_samples = (duration_sec * sample_rate as f64) as usize;
let data = &all_data[0][start_in_samples..start_in_samples + len_in_samples];
let mut out_file = std::fs::File::create("foo2.wav")?;
ogg_table::wav::write_wav(&mut out_file, data, sample_rate)?;

for i in 0..1000 {
if selected_data[0][i..i + 100] == data[..100] {
println!("offset: {i}")
}
if selected_data[0][..100] == data[i..i + 100] {
println!("offset -{i}")
}
}
}
}
Ok(())
Expand Down

0 comments on commit 565ec5e

Please sign in to comment.