Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pull/get_page API #3

Merged
merged 5 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions examples/file_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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 encoder = Encoder::create_file(
"/tmp/noise.opus",
Comments::create()
.add(RecommendedTag::Title, "Random Noise")?
.add(RecommendedTag::Artist, "/dev/urandom")?,
48_000,
2,
MappingFamily::MonoStereo,
)?;

encoder.write(&audio_data)?;
encoder.drain()?;

Ok(())
}
33 changes: 33 additions & 0 deletions examples/pull_api.rs
d-k-bo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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 encoder = Encoder::create_pull(
Comments::create()
.add(RecommendedTag::Title, "Random Noise")?
.add(RecommendedTag::Artist, "/dev/urandom")?,
48_000,
2,
MappingFamily::MonoStereo,
)?;

encoder.write(&audio_data)?;
encoder.drain()?;

let mut output_file = std::fs::File::create("/tmp/noise.opus")?;

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

Ok(())
}
46 changes: 46 additions & 0 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,31 @@ impl Encoder {

Ok(Self { ptr, channels })
}

/// Create a new OggOpus encoder in pull style.
///
/// Use together with [`Encoder::get_page`] to get the encoded data.
pub fn create_pull(
comments: impl Borrow<Comments>,
rate: i32,
channels: usize,
family: MappingFamily,
) -> Result<Self> {
let mut error = 0;
let ptr = unsafe {
crate::ffi::ope_encoder_create_pull(
comments.borrow().ptr(),
rate,
channels.try_into().unwrap(),
family as i32,
&mut error,
)
};
error.check_result()?;
assert!(!ptr.is_null());

Ok(Self { ptr, channels })
}
}

impl Encoder {
Expand Down Expand Up @@ -94,6 +119,27 @@ impl Encoder {
}
.check_result()
}

/// Get the next page from the stream
///
/// Only use if creating encoder with [`Encoder::create_pull`].
pub fn get_page(&mut self, flush: bool) -> Option<&[u8]> {
let mut page_ptr: *mut std::ffi::c_uchar = std::ptr::null_mut();
let mut page_size = 0;
unsafe {
let available = crate::ffi::ope_encoder_get_page(
self.ptr,
&mut page_ptr,
&mut page_size,
flush as i32,
);
if available == 1 {
Some(std::slice::from_raw_parts(page_ptr, page_size as usize))
} else {
None
}
}
}
/// Finalizes the stream.
pub fn drain(&mut self) -> Result<()> {
unsafe { crate::ffi::ope_encoder_drain(self.ptr) }.check_result()
Expand Down
Loading