opus

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2023 License: MIT Imports: 3 Imported by: 1

README

PkgGoDev

opus-go

Go bindings for opus. Modern audio compression for the internet.

Why opus-go

The purpose of opus-go is easing the adoption of opus codec library. Using Go, with just a few lines of code you can implement an application that encode/decode data easy.

Is this a new implementation of opus?

No! We are just exposing the great work done by the research organization of Xiph as a golang library. All the functionality and implementation still resides in the official opus project.

Features supported

  • Decode Opus to PCM
  • Encode PCM to Opus

Usage

Decode AAC frame to PCM

package main

import (
	"fmt"

	opus "github.com/qrtc/opus-go"
)

func main() {
	decoder, err := opus.CreateOpusDecoder(&opus.OpusDecoderConfig{
		SampleRate:  48000,
		MaxChannels: 2,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	defer func() {
		decoder.Close()
	}()

	inBuf := []byte{
		// Opus frame
	}
	outBuf := make([]byte, 4096)

	n, err := decoder.Decode(inBuf, outBuf)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(outBuf[0:n])
}

Enode PCM to AAC

package main

import (
	"fmt"

	opus "github.com/qrtc/opus-go"
)

func main() {
	encoder, err := opus.CreateOpusEncoder(&opus.OpusEncoderConfig{
		SampleRate:  48000,
		MaxChannels: 2,
		Application: opus.AppVoIP,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	defer func() {
		encoder.Close()
	}()

	inBuf := []byte{
		// PCM bytes
	}
	outBuf := make([]byte, 4096)

	n, err := encoder.Encode(inBuf, outBuf)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(outBuf[0:n])
}

Dependencies

  • opus

Documentation

Index

Constants

View Source
const (
	// Auto/default setting
	OpusAuto = int(C.OPUS_AUTO)
	// Maximum bitrate
	BitrateMax = int(C.OPUS_BITRATE_MAX)
)
View Source
const (
	// Optimize encoding for VoIP
	AppVoIP = Application(C.OPUS_APPLICATION_VOIP)
	// Optimize encoding for non-voice signals like music
	AppAudio = Application(C.OPUS_APPLICATION_AUDIO)
	// Optimize encoding for low latency applications
	AppRestrictedLowdelay = Application(C.OPUS_APPLICATION_RESTRICTED_LOWDELAY)
)
View Source
const (
	// Signal being encoded is voice
	SignalVoice = SignalType(C.OPUS_SIGNAL_VOICE)
	// Signal being encoded is music
	SignalMusic = SignalType(C.OPUS_SIGNAL_MUSIC)
)
View Source
const (
	// 4 kHz bandpass
	BandwidthNarrowband = BandwidthType(C.OPUS_BANDWIDTH_NARROWBAND)
	// 6 kHz bandpass
	BandwidthMediumband = BandwidthType(C.OPUS_BANDWIDTH_MEDIUMBAND)
	// 8 kHz bandpass
	BandwidthWideband = BandwidthType(C.OPUS_BANDWIDTH_WIDEBAND)
	// 12 kHz bandpass
	BandwidthSuperwideband = BandwidthType(C.OPUS_BANDWIDTH_SUPERWIDEBAND)
	// 20 kHz bandpass
	BandwidthFullband = BandwidthType(C.OPUS_BANDWIDTH_FULLBAND)
)
View Source
const (
	// Select frame size from the argument (default)
	FramesizeArg = FrameSizeType(C.OPUS_FRAMESIZE_ARG)
	// Use 2.5 ms frames
	Framesize2Dot5Ms = FrameSizeType(C.OPUS_FRAMESIZE_2_5_MS)
	// Use 5 ms frames
	Framesize5Ms = FrameSizeType(C.OPUS_FRAMESIZE_5_MS)
	// Use 10 ms frames
	Framesize10Ms = FrameSizeType(C.OPUS_FRAMESIZE_10_MS)
	// Use 20 ms frames
	Framesize20Ms = FrameSizeType(C.OPUS_FRAMESIZE_20_MS)
	// Use 40 ms frames
	Framesize40Ms = FrameSizeType(C.OPUS_FRAMESIZE_40_MS)
	// Use 60 ms frames
	Framesize60Ms = FrameSizeType(C.OPUS_FRAMESIZE_60_MS)
	// Use 80 ms frames
	Framesize80Ms = FrameSizeType(C.OPUS_FRAMESIZE_80_MS)
	// Use 100 ms frames
	Framesize100Ms = FrameSizeType(C.OPUS_FRAMESIZE_100_MS)
	// Use 120 ms frames
	Framesize120Ms = FrameSizeType(C.OPUS_FRAMESIZE_120_MS)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Application

type Application int

Application

type BandwidthType

type BandwidthType int

Bandwidth type

type FrameSizeType

type FrameSizeType int

Frame size type

type OpusDecoder

type OpusDecoder struct {

	// config
	OpusDecoderConfig
	// contains filtered or unexported fields
}

Opus Decoder

func CreateOpusDecoder

func CreateOpusDecoder(config *OpusDecoderConfig) (dec *OpusDecoder, err error)

Create Opus Decoder

func (*OpusDecoder) Close

func (dec *OpusDecoder) Close() error

Close

func (*OpusDecoder) Decode

func (dec *OpusDecoder) Decode(in, out []byte) (int, error)

Decode

type OpusDecoderConfig

type OpusDecoderConfig struct {
	// Sampling rate of input signal (Hz).
	// This must be one of 8000, 12000, 16000, 24000, or 48000.
	SampleRate int
	// Number of channels in input signal.
	MaxChannels int
	// Decoder gain adjustment.
	Gain int
	// Enable inband forward error correction (FEC).
	EnableInbandFEC bool
}

Opus Decoder Config

type OpusEncoder

type OpusEncoder struct {

	// config
	OpusEncoderConfig
	// contains filtered or unexported fields
}

Opus Encoder

func CreateOpusEncoder

func CreateOpusEncoder(config *OpusEncoderConfig) (enc *OpusEncoder, err error)

Create Opus Encoder

func (*OpusEncoder) Close

func (enc *OpusEncoder) Close() error

Close

func (*OpusEncoder) Encode

func (enc *OpusEncoder) Encode(in, out []byte) (int, error)

Encode

func (*OpusEncoder) InDTX

func (enc *OpusEncoder) InDTX() bool

InDTX

func (*OpusEncoder) Lookahead

func (enc *OpusEncoder) Lookahead() int

Lookahead

type OpusEncoderConfig

type OpusEncoderConfig struct {
	// Sampling rate of input signal (Hz).
	// This must be one of 8000, 12000, 16000, 24000, or 48000.
	SampleRate int
	// Number of channels in input signal.
	MaxChannels int
	// Encode mode.
	Application Application
	// Enable discontinuous transmission (DTX).
	EnableDTX bool
	// Enable inband forward error correction (FEC).
	EnableInbandFEC bool
	// Disable almost all use of prediction.
	DisablePrediction bool
	// Disable variable bitrate (VBR).
	DisableVBR bool
	// Disable constrained VBR.
	DisableConstrainedVBR bool
	// Disable the use of phase inversion for intensity stereo.
	DisablePhaseInversion bool
	// Rates from 500 to 512000 bits per second are meaningful,
	// as well as the special values BitrateAuto and BitrateMax.
	Bitrate int
	// Complexity configuration, a value in the range 0-10.
	Complexity int
	// The maximum bandpass that the encoder will select automatically.
	MaxBandwidth BandwidthType
	// Encoder's bandpass to a specific value.
	Bandwidth BandwidthType
	// Expected packet loss percentage.
	PacketLossPercent int
	// The encoder's use of variable duration frames.
	FrameDuration FrameSizeType
	// The type of signal being encoded.
	SignalType SignalType
	// Force mono or stereo.
	ForceChannels int
	// The depth of signal being encoded.
	LSBDepth int
}

Opus Encoder Config

type SignalType

type SignalType int

Signal type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL