Skip to main content

A pure-Rust implementation of the Opus audio codec (RFC 6716) - fast, with first-class NumPy interop.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ruopus-0.1.1.tar.gz (318.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ruopus-0.1.1-cp312-cp312-manylinux_2_34_x86_64.whl (984.5 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

File details

Details for the file ruopus-0.1.1.tar.gz.

File metadata

  • Download URL: ruopus-0.1.1.tar.gz
  • Upload date:
  • Size: 318.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.14.1

File hashes

Hashes for ruopus-0.1.1.tar.gz
Algorithm Hash digest
SHA256 64a5ed27a0b0ea2d9ac5501ebc65dbf601649f1d6493492fa37795c416f53d1e
MD5 55b060bad118f4e8a163f19dd50e5ef2
BLAKE2b-256 eee91030a3a98beb9df32a67714f1108632c7c4c7b0a98e25609e4fe96e556ca

See more details on using hashes here.

File details

Details for the file ruopus-0.1.1-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for ruopus-0.1.1-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 bc567a4592d6ef2240125ff1e3e9f24954a20b5518db02e985b2d483f1af8573
MD5 ed23f6358cbe3492caa83aa25c71601c
BLAKE2b-256 2a64122bf372a79bfe0b7e03f4f8c6c336b6c39ca32d29e41830b2536baeab9f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page