Skip to content

Commit

Permalink
Use audio sample for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
d-k-bo committed Oct 21, 2024
1 parent 5376f2b commit 0920097
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 28 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ cstr = "0.2.11"
num_enum = "0.7.2"
opusenc-sys = { version = "0.2.1", path = "opusenc-sys" }

[dev-dependencies]
hound = "3.5.1"

[[package.metadata.release.pre-release-replacements]]
file = "CHANGELOG.md"
search = "## \\[Unreleased\\]"
Expand Down
26 changes: 13 additions & 13 deletions examples/file_api.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use hound::WavReader;
use opusenc::{Comments, Encoder, MappingFamily, RecommendedTag};
use std::io::Read;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let audio_data: Vec<i16> = {
let mut file = std::fs::File::open("/dev/urandom")?;
let mut buf = vec![0; 60 * 48_000 * 2 * 2];
file.read_exact(&mut buf)?;
buf.chunks_exact(2)
.map(|a| i16::from_ne_bytes([a[0], a[1]]))
.collect()
};
let mut wav = WavReader::open("examples/speech_orig.wav")?;

let spec = wav.spec();
assert_eq!(spec.channels, 1);
assert_eq!(spec.sample_format, hound::SampleFormat::Int);
assert_eq!(spec.bits_per_sample, 16);

let audio_data = wav.samples::<i16>().collect::<hound::Result<Vec<i16>>>()?;

let mut encoder = Encoder::create_file(
"/tmp/noise.opus",
"/tmp/speech.opus",
Comments::create()
.add(RecommendedTag::Title, "Random Noise")?
.add(RecommendedTag::Artist, "/dev/urandom")?,
.add(RecommendedTag::Title, "Opus Speech Samples")?
.add(RecommendedTag::Artist, "Various Artists")?,
48_000,
2,
1,
MappingFamily::MonoStereo,
)?;

Expand Down
41 changes: 26 additions & 15 deletions examples/pull_api.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
use std::io::Write;

use hound::WavReader;
use opusenc::{Comments, Encoder, MappingFamily, RecommendedTag};
use std::io::{Read, Write};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let audio_data: Vec<i16> = {
let mut file = std::fs::File::open("/dev/urandom")?;
let mut buf = vec![0; 60 * 48_000 * 2 * 2];
file.read_exact(&mut buf)?;
buf.chunks_exact(2)
.map(|a| i16::from_ne_bytes([a[0], a[1]]))
.collect()
};
let mut wav = WavReader::open("examples/speech_orig.wav")?;

let spec = wav.spec();
assert_eq!(spec.channels, 1);
assert_eq!(spec.sample_format, hound::SampleFormat::Int);
assert_eq!(spec.bits_per_sample, 16);

let audio_data = wav.samples::<i16>().collect::<hound::Result<Vec<i16>>>()?;

let mut encoder = Encoder::create_pull(
Comments::create()
.add(RecommendedTag::Title, "Random Noise")?
.add(RecommendedTag::Artist, "/dev/urandom")?,
.add(RecommendedTag::Title, "Opus Speech Samples")?
.add(RecommendedTag::Artist, "Various Artists")?,
48_000,
2,
1,
MappingFamily::MonoStereo,
)?;

encoder.write(&audio_data)?;
encoder.drain()?;
let mut output_file = std::fs::File::create("/tmp/speech.opus")?;

let mut output_file = std::fs::File::create("/tmp/noise.opus")?;
// let's simulate that we are reading the audio data from multiple buffers
// as if we were reading it from a network stream
for chunk in audio_data.chunks(audio_data.len() / 4) {
encoder.write(chunk)?;

while let Some(page) = encoder.get_page(true) {
output_file.write_all(page)?;
}
}

encoder.drain()?;

while let Some(page) = encoder.get_page(true) {
output_file.write_all(page)?;
Expand Down
Binary file added examples/speech_orig.wav
Binary file not shown.
2 changes: 2 additions & 0 deletions examples/speech_orig.wav.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# SPDX-FileCopyrightText: Copyright © 2011-2024 Xiph.Org Foundation
# SPDX-License-Identifier: CC-BY-3.0

0 comments on commit 0920097

Please sign in to comment.