Loading repository data…
Loading repository data…
kazzmir / repository
Opus encoder/decoder in pure go
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
This is a pure go implementation of an Ogg/Opus parser, decoder, and encoder. It was produced by transpiling the libopus C sources to Go using ccgo, as well as using GPT 5.2 help. No cgo is used.
Directory structure:
cmd/oggopusdump: command-line tool to dump Ogg/Opus headers and packet sizescmd/oggopusextract: command-line tool to extract Opus audio packetscmd/oggopus2wav: command-line tool to decode Ogg/Opuscmd/wav2oggopus: command-line tool to encode WAV to Ogg/Opusogg - Parser for the Ogg format and Opus packet readeropus - Encoder and decoder opus APIopuscc - Transpiled libopus C source of the decoder logicopusccenc - Transpiled libopus C source of the encoder logiclibcshim - Small libc shim for the transpiled C code, replaces some modernc.org/libc functionalityexamples - Example programs using the libraryDecoding an opus file to get PCM samples. Note the sample rate of the PCM data is always 48000 Hz.
player, _ := opusgo.NewPlayerFromFile("file.opus", true) // true means stream from disk
data := make([]byte, 48000*2*2) // 1 second buffer for stereo s16le
io.ReadFull(player, data)
// data now contains PCM samples
Decoding an opus file to get PCM samples. Note the sample rate of the PCM data is always 48000 Hz.
// error handling omitted for brevity
input, _ := os.Open("file.opus")
reader, _ := ogg.NewOpusReader(input) // input can be any io.Reader
decoder, _ := opus.NewDecoderFromHead(reader.Head)
for {
packet, err := reader.ReadAudioPacket()
if err != nil {
// no more audio packets
break
}
decoded, n, _ := decoder.DecodePacket(packet, nil) // nil can instead be a pre-allocated []int16, otherwise new memory is allocated
// decoded is an []int16 PCM audio buffer with n samples per channel, so total samples = n * reader.Head.Channels
// use decoded PCM samples...
}
From this folder:
go run ./cmd/oggopusdump --no-crc path/to/file.ogg
go run ./cmd/oggopusextract --out packets.bin path/to/file.ogg
packets.bin format: repeating (u32le length, length bytes packet).
.opus to .wav (48kHz s16le):go run ./cmd/oggopus2wav --out out.wav path/to/file.opus
go run ./cmd/oggopus2wav --cpuprofile cpu.pprof --out out.wav path/to/file.opus
go tool pprof -top cpu.pprof
.wav to .opus:go run ./cmd/wav2oggopus --bitrate 64000 --out out.opus path/to/file.wav