This Go package provides decoding of streams that have been encoded with the Opus codec. When provided with an opus stream the raw PCM data can be extracted.
The xiph.org opus, opusfile, and ogg C libraries are used to do the decoding.
readSeeker, err := os.Open("an-opus-file.opus")
if err != nil { ... }
decoder, err := opus.NewDecoder(readSeeker)
if err != nil { ... }
var pcm = make([]int16, 2_000)
for {
n, err := decoder.Read(pcm)
fmt.Printf("read %d samples (per channel)\n", n)
samples := pcm[:n]
// Do something with the samples...
if err == io.EOF {
break
}
}
decoder.Destroy()
readSeeker.Close()
Because of the use of cgo in the library, the Go toolchain will need to be able to find a C compiler (typically gcc or clang) and linker on the path.
When building an application that uses the opus package
(with go build
or go run
)
the first build may take several seconds.
This is because of the use of a C compiler to compile the
opus
, opusfile
, and ogg
libraries.
Subsequent builds will be much quicker as the built package
will be cached by the Go toolchain.
There are a couple of examples in the example
directory.
beep
- Uses the beep package to play an Opus file.
go run example\beep\*.go [your-file.opus]
- Without an argument, plays an embedded sample Opus file.
- With an argument that is a path to an Opus file, will play the file.
simple
- Uses various methods of
opus.Decoder
to get and print various information about an Opus stream. go run example\simple\*.go [your-file.opus]
- Without an argument, uses an embedded sample Opus file.
- With an argument that is a path to an Opus file, uses the file.
- Uses various methods of
https://github.com/pion/opus is a pure Go implementation of the Opus codec.
https://github.com/hraban/opus provides C bindings to the xiph.org C libraries.
- It is more comprehensive than this library, and supports encoding as well as decoding of Opus streams.
- It requires libopus and libopusfile development packages to be installed on the system.
- install
goimports
if not already installed - install
golangci-lint
if not already installed - install the
pre-commit
application if not already installed - install pre-commit hook in this repo's workspace
pre-commit install