React Native C++ Turbo Module for Opus audio encoding and decoding.
Forked from Aduomas/react-native-opus to add encoder support.
- ✅ Opus decoding (from original)
- ✅ Opus encoding (added)
- ✅ Real-time streaming support
- ✅ Configurable bitrate, complexity, VBR
- ✅ iOS and Android support
- ✅ React Native New Architecture (Turbo Modules)
npm install github:MillsAirCode/react-native-opus-encoderOr add to package.json:
"react-native-opus": "github:MillsAirCode/react-native-opus-encoder"import {
createOpusEncoder,
encodeOpusPacket,
destroyOpusEncoder,
setEncoderBitrate,
OPUS_APPLICATION_VOIP,
} from 'react-native-opus';
// Create encoder (24kHz, mono, VOIP optimized)
const { success, encoderId } = await createOpusEncoder(24000, 1, OPUS_APPLICATION_VOIP);
// Encode PCM data (base64 Int16 samples)
const { encodedDataBase64, encodedBytes } = await encodeOpusPacket(
pcmBase64,
encoderId,
480 // frame size (20ms at 24kHz)
);
// Optional: adjust bitrate
await setEncoderBitrate(encoderId, 32000); // 32kbps
// Cleanup
await destroyOpusEncoder(encoderId);import {
createOpusDecoder,
decodeOpusPacket,
destroyOpusDecoder,
} from 'react-native-opus';
// Create decoder (24kHz, mono)
const { success, decoderId } = await createOpusDecoder(24000, 1);
// Decode Opus packet
const { decodedDataBase64, samplesDecoded } = await decodeOpusPacket(
opusBase64,
decoderId
);
// Cleanup
await destroyOpusDecoder(decoderId);| Method | Description |
|---|---|
createOpusEncoder(sampleRate, channels, application) |
Create encoder. Application: OPUS_APPLICATION_VOIP (2048), OPUS_APPLICATION_AUDIO (2049), OPUS_APPLICATION_RESTRICTED_LOWDELAY (2051) |
encodeOpusPacket(pcmBase64, encoderId, frameSize) |
Encode PCM (Int16) to Opus |
setEncoderBitrate(encoderId, bitrate) |
Set bitrate in bps |
destroyOpusEncoder(encoderId) |
Cleanup encoder |
| Method | Description |
|---|---|
createOpusDecoder(sampleRate, channels) |
Create decoder |
decodeOpusPacket(opusBase64, decoderId) |
Decode Opus to PCM (Int16) |
decodeOpusData(dataBase64, decoderId, chunkSize) |
Decode multiple packets |
destroyOpusDecoder(decoderId) |
Cleanup decoder |
Opus supports these frame sizes (in samples at your sample rate):
| Duration | 24kHz | 48kHz |
|---|---|---|
| 2.5ms | 60 | 120 |
| 5ms | 120 | 240 |
| 10ms | 240 | 480 |
| 20ms | 480 | 960 |
| 40ms | 960 | 1920 |
| 60ms | 1440 | 2880 |
20ms (480 samples at 24kHz) is recommended for real-time voice.
MIT - Based on libopus (BSD-3-Clause)
Brad Mills (@MillsAirCode)