Supported Audio Sample Rates in Libopus
This article provides a clear overview of the audio sample rates
natively supported for input and output operations in the
libopus library. It outlines the specific sampling
frequencies accepted by the Opus encoder and decoder APIs, explains how
these rates map to internal bandwidths, and describes how non-native
rates are handled.
Natively Supported Sample Rates
The libopus API natively supports exactly five sampling
rates for both input (encoding) and output (decoding) operations:
- 8,000 Hz (8 kHz)
- 12,000 Hz (12 kHz)
- 16,000 Hz (16 kHz)
- 24,000 Hz (24 kHz)
- 48,000 Hz (48 kHz)
When initializing an encoder or decoder using functions like
opus_encoder_init or opus_decoder_init, you
must specify one of these five values. Passing any other sample rate
will result in an OPUS_BAD_ARG error.
Mapping to Internal Bandwidths
These native sample rates correspond directly to the five transmission bandwidths defined in the Opus standard:
- Narrowband (NB): 8 kHz sample rate (4 kHz audio bandwidth)
- Mediumband (MB): 12 kHz sample rate (6 kHz audio bandwidth)
- Wideband (WB): 16 kHz sample rate (8 kHz audio bandwidth)
- Super-wideband (SWB): 24 kHz sample rate (12 kHz audio bandwidth)
- Fullband (FB): 48 kHz sample rate (20 kHz audio bandwidth)
Regardless of the input sample rate chosen, the Opus encoder can dynamically scale its internal coding bandwidth down to save bitrate, or up to maximize quality, based on your configuration and network conditions.
Handling Non-Native Rates (e.g., 44.1 kHz)
Common consumer audio sample rates like 44.1 kHz (CD quality) are not
natively supported by the libopus API.
To process 44.1 kHz audio or any other non-supported frequency: 1.
Resampling: You must use an external resampling library
(such as speexdsp or soxr) to convert the
audio to a supported native rate—ideally 48 kHz to preserve maximum
fidelity—before passing it to the Opus encoder. 2.
Decoding: Similarly, the decoder will output audio at
one of the five native rates (usually 48 kHz). If your playback hardware
requires 44.1 kHz, you must resample the decoded output from 48 kHz to
44.1 kHz in your application layer.