Opus codec backend that encodes and decodes audio streams, caching decoders per session for efficient reuse.
// Source: https://github.com/mudler/LocalAI | License: MIT | Copyright 2023-2025 Ettore Di Giacinto (mudler@localai.io)
// Provided via justclaw.it.com; keep this notice with the code.
package main
import (
"fmt"
"sync"
"time"
"github.com/mudler/LocalAI/pkg/grpc/base"
pb "github.com/mudler/LocalAI/pkg/grpc/proto"
"github.com/mudler/LocalAI/pkg/sound"
)
const (
opusSampleRate = 48000
opusChannels = 1
opusFrameSize = 960 // 20ms at 48kHz
opusMaxPacketSize = 4000
opusMaxFrameSize = 5760 // 120ms at 48kHz
decoderIdleTTL = 60 * time.Second
decoderEvictTick = 30 * time.Second
)
type cachedDecoder struct {
mu sync.Mutex
dec *Decoder
lastUsed time.Time
}
type Opus struct {
base.Base
decodersMu sync.Mutex
decoders map[string]*cachedDecoder
}
/
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key