-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 🚩 add pull/get_page API Change-Id: I2fbd448abd39cc123f483bc19963c1f7c75b8122 * fix: directly return ref vec Co-authored-by: David C. <[email protected]> * Add examples * Use audio sample for examples * Update changelog --------- Co-authored-by: David C. <[email protected]>
- Loading branch information
Showing
7 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use hound::WavReader; | ||
use opusenc::{Comments, Encoder, MappingFamily, RecommendedTag}; | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
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/speech.opus", | ||
Comments::create() | ||
.add(RecommendedTag::Title, "Opus Speech Samples")? | ||
.add(RecommendedTag::Artist, "Various Artists")?, | ||
48_000, | ||
1, | ||
MappingFamily::MonoStereo, | ||
)?; | ||
|
||
encoder.write(&audio_data)?; | ||
encoder.drain()?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::io::Write; | ||
|
||
use hound::WavReader; | ||
use opusenc::{Comments, Encoder, MappingFamily, RecommendedTag}; | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
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, "Opus Speech Samples")? | ||
.add(RecommendedTag::Artist, "Various Artists")?, | ||
48_000, | ||
1, | ||
MappingFamily::MonoStereo, | ||
)?; | ||
|
||
let mut output_file = std::fs::File::create("/tmp/speech.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)?; | ||
} | ||
|
||
Ok(()) | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters