audiotranscoder

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2025 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package audiotranscoder provides a real‑time Linear16 → Opus transcoder that keeps wall‑clock time. Even if the Linear16 source delivers no bytes during pauses, the transcoder still outputs Opus packets that represent silence, so that the downstream consumer continues to receive a constant stream.

Build‑time requirement: system libopus (cgo) and the Go package

go get github.com/hraban/opus

Example usage:

src := getYourLinear16Reader()
ctx := context.Background()
s, _ := audiotranscoder.NewOpusStreamer(src, 48_000, 1, nil) // 48 kHz, mono
go s.Run(ctx)
for pkt := range s.Packets() {
    sendOverWebRTC(pkt)
}
if err := <-s.Err(); err != nil && !errors.Is(err, context.Canceled) {
    log.Fatal(err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	FrameDuration time.Duration
	BufferMillis  int
	Application   opus.Application
}

Options tweaks behaviour. Zero values are sensible.

  • FrameDuration — Opus frame size (2.5‑60 ms). 20 ms is good for VoIP.
  • BufferMillis — depth of the internal jitter buffer in milliseconds.
  • Application — opus.AppVoIP (default), opus.AppAudio, opus.AppRestrictedLowDelay.

type OpusStreamer

type OpusStreamer struct {
	// contains filtered or unexported fields
}

OpusStreamer converts Linear16 (little‑endian 16‑bit PCM) to Opus in real time. Pointers returned by Packets() remain valid only until the next read — copy if you need to keep them.

func NewOpusStreamer

func NewOpusStreamer(src io.Reader, sampleRate, channels int, opts *Options) (*OpusStreamer, error)

NewOpusStreamer builds a ready‑to‑run transcoder.

func (*OpusStreamer) Clear

func (s *OpusStreamer) Clear()

func (*OpusStreamer) Err

func (s *OpusStreamer) Err() <-chan error

Err returns a channel with the first terminal error.

func (*OpusStreamer) Packets

func (s *OpusStreamer) Packets() <-chan []byte

Packets yields encoded Opus packets. Close when Run() returns.

func (*OpusStreamer) Run

func (s *OpusStreamer) Run(ctx context.Context)

Run starts transcoding and blocks until ctx.Done() or a fatal error.

Jump to

Keyboard shortcuts

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