opus_codec/
lib.rs

1//! Safe, ergonomic wrappers around libopus for encoding/decoding Opus audio.
2#![warn(missing_docs)]
3#![warn(clippy::all)]
4#![warn(clippy::pedantic)]
5#![warn(clippy::cargo)]
6#![allow(clippy::cast_possible_wrap)]
7#![allow(clippy::cast_possible_truncation)]
8
9// Include the generated bindings
10#[allow(warnings)]
11#[allow(clippy::all)]
12mod bindings {
13    include!("bindings.rs");
14}
15
16pub mod constants;
17pub mod decoder;
18#[cfg(feature = "dred")]
19/// Deep Redundancy (DRED) decoder support.
20pub mod dred;
21pub mod encoder;
22pub mod error;
23pub mod multistream;
24pub mod packet;
25pub mod projection;
26pub mod repacketizer;
27pub mod types;
28
29pub use constants::{MAX_FRAME_SAMPLES_48KHZ, MAX_PACKET_DURATION_MS, max_frame_samples_for};
30pub use decoder::Decoder;
31#[cfg(feature = "dred")]
32pub use dred::{DredDecoder, DredState};
33pub use encoder::Encoder;
34pub use error::{Error, Result};
35pub use multistream::{MSDecoder, MSEncoder, Mapping};
36pub use packet::{
37    packet_bandwidth, packet_channels, packet_has_lbrr, packet_nb_frames, packet_nb_samples,
38    packet_parse, packet_samples_per_frame, soft_clip,
39};
40pub use projection::{ProjectionDecoder, ProjectionEncoder};
41pub use repacketizer::Repacketizer;
42pub use types::{
43    Application, Bandwidth, Bitrate, Channels, Complexity, ExpertFrameDuration, FrameSize,
44    SampleRate, Signal,
45};
46
47#[doc(hidden)]
48pub use bindings::*;
49
50/// Returns the bundled libopus version string of this crate.
51#[must_use]
52pub fn version() -> &'static str {
53    "1.5.2"
54}
55
56/// Returns the runtime libopus version string from the linked C library.
57#[must_use]
58pub fn runtime_version() -> &'static str {
59    unsafe {
60        let ptr = crate::bindings::opus_get_version_string();
61        if ptr.is_null() {
62            return "";
63        }
64        std::ffi::CStr::from_ptr(ptr).to_str().unwrap_or("")
65    }
66}
67
68/// Returns a human-readable string for a libopus error code (via runtime library).
69#[must_use]
70pub fn strerror(code: i32) -> &'static str {
71    unsafe {
72        let ptr = crate::bindings::opus_strerror(code);
73        if ptr.is_null() {
74            return "";
75        }
76        std::ffi::CStr::from_ptr(ptr).to_str().unwrap_or("")
77    }
78}