Skip to main content

Crate opus1

Crate opus1 

Source
Expand description

opus1: the Opus audio codec (RFC 6716) in Rust, ported from libopus 1.6.1, the reference implementation in C.

  • OpusEncoder and OpusDecoder code one stream of mono or stereo audio, at 8, 12, 16, 24 or 48 kHz (SampleRate, Channels).
  • The module multistream codes several streams in one packet, for example 5.1 surround sound (feature alloc, which the default feature std turns on).
  • The module packet reads, splits and joins Opus packets without decoding them, and extensions reads and writes the extensions in their padding.
  • With the feature dred, the encoder adds deep redundancy (DRED) to its packets, and OpusDredDecoder reads it back to rebuild lost audio.

Every call that can fail returns Error, whose ErrorKind is the error code libopus returns in the same case.

use opus1::{Application, Channels, OpusDecoder, OpusEncoder, SampleRate};

let mut encoder = OpusEncoder::new(SampleRate::Hz48000, Channels::Mono, Application::Audio)?;
let mut decoder = OpusDecoder::new(SampleRate::Hz48000, Channels::Mono)?;
let pcm: Vec<f32> = (0..960).map(|i| (i as f32 * 0.05).sin() * 0.5).collect();
let mut packet = [0u8; 1275];
let len = encoder.encode(&pcm, 960, &mut packet)?;
let mut out = [0.0f32; 960];
assert_eq!(decoder.decode(&packet[..len], 960, &mut out)?, 960);

opus1 follows libopus 1.6.1. The tests of the repository compare its packets and its decoded audio with that release. The docs folder of the repository explains the codec, the library and where opus1 differs from libopus.

Modules§

extensions
Packet extensions: extra data stored in the padding of an Opus packet.
multistreamalloc
Multichannel encoding and decoding (several Opus streams in one packet). It allocates in its constructors, so it needs the alloc feature, which std and heap (both on by default) turn on. It works without std when the application has a global allocator. The constructors build each stream codec in place in that memory, so they need little stack, also without heap. The single-stream codec stays no_std without alloc.
packet
Opus packet framing (RFC 6716 section 3 and appendix B): reading the frames out of a packet, writing frames into a packet, and padding.

Structs§

DnnBlobdeep-plc and dnn
A validated weight blob in the libopus format (OPUS_SET_DNN_BLOB). Cloning is cheap: the bytes are not copied.
DnnModelsdeep-plc and dnn
A set of model groups, as returned by every set_dnn_blob (OpusDecoder, OpusEncoder, OpusDredDecoder and multistream::MultistreamDecoder): the groups that the call loaded.
DredParsednn and dred
What OpusDredDecoder::parse found in a packet.
Error
An error of opus1: its kind and a message that says what was wrong.
OpusDecoder
A decoder of one Opus stream to mono or stereo audio: libopus OpusDecoder.
OpusDreddnn and dred
The DRED data of one packet (libopus OpusDRED, 11,236 bytes).
OpusDredDecoderdnn and dred
The DRED decoder (libopus OpusDREDDecoder): the RDOVAE decoder’s weights and the kernel variant. It keeps no state between packets, so one decoder can serve any number of streams.
OpusEncoder
An encoder of one Opus stream of mono or stereo audio: libopus OpusEncoder.

Enums§

Application
What an encoder is tuned for: libopus OPUS_APPLICATION_*, the application argument of OpusEncoder::new.
Bandwidth
An audio bandwidth: libopus OPUS_BANDWIDTH_*. Each value says up to which frequency a packet carries audio.
Bitrate
The bitrate setting of an encoder: libopus OPUS_SET_BITRATE. See OpusEncoder::set_bitrate; MultistreamEncoder::set_bitrate takes it too.
Channels
The number of channels of the audio that an encoder takes or a decoder writes. A single Opus stream has one or two channels. For more, use the multistream module, which counts channels with a usize.
DredProcessingdnn and dred
When OpusDredDecoder::parse runs the RDOVAE decoder: libopus’s defer_processing flag of opus_dred_parse.
ErrorKind
What kind of error happened: one per libopus error code.
ForcedMode
A coding mode the caller asks the encoder to use: libopus MODE_SILK_ONLY, MODE_HYBRID and MODE_CELT_ONLY for OPUS_SET_FORCE_MODE. See OpusEncoder::set_force_mode.
FrameDuration
The frame duration setting of an encoder: libopus OPUS_SET_EXPERT_FRAME_DURATION. See OpusEncoder::set_expert_frame_duration; MultistreamEncoder::set_expert_frame_duration takes it too.
InbandFec
In-band forward error correction (FEC): libopus OPUS_SET_INBAND_FEC. See OpusEncoder::set_inband_fec.
SampleRate
A sampling rate of the audio that an encoder takes or a decoder writes. Opus supports these five rates and no others.
Signal
What the input of an encoder is: libopus OPUS_SET_SIGNAL. See OpusEncoder::set_signal.

Constants§

VERSION
The version of this crate, for example "0.3.0": the version of its Cargo.toml.

Functions§

pcm_soft_clip
libopus opus_pcm_soft_clip (opus.c): bring a float signal into [-1, 1] without hard clipping.