ruopus 0.1.1

A pure-Rust implementation of the Opus audio codec (RFC 6716). No FFI; unsafe confined to documented SIMD kernels.
Documentation

ruopus

Crates.io docs.rs PyPI MSRV License: MIT

A pure-Rust implementation of the Opus audio codec (RFC 6716): decoder and encoder, with no C and no FFI.

Pure Rust. unsafe only in a few SIMD kernels, every one checked under Miri. Runs on stable Rust

The decoder passes the official Opus conformance vectors, and the encoder produces standard Opus that libopus and ffmpeg decode.

Python bindings are available on PyPI.

Overview

ruopus is a from-scratch Rust implementation of Opus. It links no libopus, needs no C toolchain, and exposes plain &[u8]/&[i16]/&[f32] interfaces, so it embeds under any audio stack.

  • Pure Rust, no FFI. Builds on wasm32. The decoder is no_std + alloc (build with default-features = false, features = ["libm"]); the encoder currently needs std.
  • unsafe is denied by default. The only exceptions are a few std::arch SIMD hot loops, each carrying a // SAFETY: note (docs/unsafe.md) and checked for undefined behaviour by Miri on both the SSE2 and AVX2 paths (tools/miri.sh). No portable_simd, no inline asm.
  • Zero required dependencies. The default build adds one optional FFT crate for faster decoding; default-features = false is fully dependency-free.

Use

[dependencies]
ruopus = "0.1"
use ruopus::{OpusDecoder, OpusEncoder};

// Decode Opus packets to interleaved f32 PCM.
let mut dec = OpusDecoder::new(2); // channels
let pcm = dec.decode_packet(&packet)?;

// Encode 48 kHz PCM (one 20 ms frame is 960 samples per channel, interleaved).
let mut enc = OpusEncoder::new(1);
enc.set_bitrate(Some(24_000));
let packet = enc.encode_auto(&pcm_960, 4000)?;
// Whole Ogg Opus files.
let (pcm, head) = ruopus::decode_ogg_opus(&bytes)?;
let ogg = ruopus::encode_ogg_opus(&pcm, 2, 96_000);

Python

Install from PyPI:

pip install ruopus

The bindings wrap OpusDecoder and OpusEncoder with NumPy interop — decoded PCM is returned as a (frames, channels) float32 array; the encoder accepts the same shape or a flat interleaved array.

import numpy as np
import ruopus

# Decode
dec = ruopus.OpusDecoder(channels=2, sample_rate=48_000)
pcm = dec.decode_packet(packet)          # ndarray (frames, 2), float32

# Encode
enc = ruopus.OpusEncoder(channels=2, bitrate=64_000)
frame = np.zeros((960, 2), dtype=np.float32)   # 20 ms at 48 kHz
packet = enc.encode(frame)               # bytes

Performance

Measured against libopus 1.6.1 (SIMD-enabled C) on identical data, one core, pinned to a single performance core: cargo bench --bench vs_libopus --features std. Figures are x realtime; "ratio" is ruopus divided by libopus.

Decode

Mode ruopus libopus ratio
SILK wideband 16 kb/s 2095x 1171x 1.79x
hybrid fullband 32 kb/s 1199x 850x 1.41x
CELT fullband 64 kb/s 1389x 1566x 0.89x

Speech decode (SILK, hybrid) is faster than SIMD libopus. CELT trails on the MDCT, where libopus's SIMD wins.

Encode (matched complexity)

Mode ruopus libopus ratio
SILK wideband 16 kb/s 734x 740x 0.99x
hybrid fullband 32 kb/s 560x 562x 1.00x
CELT fullband 64 kb/s 1088x 1092x 1.00x

At matched complexity, encode is at parity with libopus across all modes. Against libopus at its default complexity it runs 1.6 to 3.2x faster (it does not yet spend cycles on delayed-decision NSQ or warped noise shaping).

Conformance

Passes the official Opus conformance criterion: all twelve RFC 8251 test vectors score 99.2 to 100% on opus_compare, with per-packet final ranges bit-exact. Fetch the vectors with tools/fetch-testvectors.sh (about 121 MB, not committed); the conformance tests skip cleanly without them.

License

MIT, see LICENSE. The Opus format is royalty-free; see the Opus IPR statements.