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 ¶
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.