Updated everything for dependencies. All sub packages are now part of the project. This was a massive update, hopefully won't have to be reverted.
This commit is contained in:
12
gumble/go-opus/AUTHORS
Normal file
12
gumble/go-opus/AUTHORS
Normal file
@ -0,0 +1,12 @@
|
||||
All code and content in this project is Copyright © 2015-2022 Go Opus Authors
|
||||
|
||||
Go Opus Authors and copyright holders of this package are listed below, in no
|
||||
particular order. By adding yourself to this list you agree to license your
|
||||
contributions under the relevant license (see the LICENSE file).
|
||||
|
||||
Hraban Luyat <hraban@0brg.net>
|
||||
Dejian Xu <xudejian2008@gmail.com>
|
||||
Tobias Wellnitz <tobias.wellnitz@gmail.com>
|
||||
Elinor Natanzon <stop.start.dev@gmail.com>
|
||||
Victor Gaydov <victor@enise.org>
|
||||
Randy Reddig <ydnar@shaderlab.com>
|
19
gumble/go-opus/LICENSE
Normal file
19
gumble/go-opus/LICENSE
Normal file
@ -0,0 +1,19 @@
|
||||
Copyright © 2015-2022 Go Opus Authors (see AUTHORS file)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
302
gumble/go-opus/README.md
Normal file
302
gumble/go-opus/README.md
Normal file
@ -0,0 +1,302 @@
|
||||
[](https://github.com/hraban/opus/actions?query=workflow%3ATest)
|
||||
|
||||
## Go wrapper for Opus
|
||||
|
||||
This package provides Go bindings for the xiph.org C libraries libopus and
|
||||
libopusfile.
|
||||
|
||||
The C libraries and docs are hosted at https://opus-codec.org/. This package
|
||||
just handles the wrapping in Go, and is unaffiliated with xiph.org.
|
||||
|
||||
Features:
|
||||
|
||||
- ✅ encode and decode raw PCM data to raw Opus data
|
||||
- ✅ useful when you control the recording device, _and_ the playback
|
||||
- ✅ decode .opus and .ogg files into raw audio data ("PCM")
|
||||
- ✅ reuse the system libraries for opus decoding (libopus)
|
||||
- ✅ works easily on Linux, Mac and Docker; needs libs on Windows
|
||||
- ❌ does not _create_ .opus or .ogg files (but feel free to send a PR)
|
||||
- ❌ does not work with .wav files (you need a separate .wav library for that)
|
||||
- ❌ no self-contained binary (you need the xiph.org libopus lib, e.g. through a package manager)
|
||||
- ❌ no cross compiling (because it uses CGo)
|
||||
|
||||
Good use cases:
|
||||
|
||||
- 👍 you are writing a music player app in Go, and you want to play back .opus files
|
||||
- 👍 you record raw wav in a web app or mobile app, you encode it as Opus on the client, you send the opus to a remote webserver written in Go, and you want to decode it back to raw audio data on that server
|
||||
|
||||
## Details
|
||||
|
||||
This wrapper provides a Go translation layer for three elements from the
|
||||
xiph.org opus libs:
|
||||
|
||||
* encoders
|
||||
* decoders
|
||||
* files & streams
|
||||
|
||||
### Import
|
||||
|
||||
```go
|
||||
import "gopkg.in/hraban/opus.v2"
|
||||
```
|
||||
|
||||
### Encoding
|
||||
|
||||
To encode raw audio to the Opus format, create an encoder first:
|
||||
|
||||
```go
|
||||
const sampleRate = 48000
|
||||
const channels = 1 // mono; 2 for stereo
|
||||
|
||||
enc, err := opus.NewEncoder(sampleRate, channels, opus.AppVoIP)
|
||||
if err != nil {
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Then pass it some raw PCM data to encode.
|
||||
|
||||
Make sure that the raw PCM data you want to encode has a legal Opus frame size.
|
||||
This means it must be exactly 2.5, 5, 10, 20, 40 or 60 ms long. The number of
|
||||
bytes this corresponds to depends on the sample rate (see the [libopus
|
||||
documentation](https://www.opus-codec.org/docs/opus_api-1.1.3/group__opus__encoder.html)).
|
||||
|
||||
```go
|
||||
var pcm []int16 = ... // obtain your raw PCM data somewhere
|
||||
const bufferSize = 1000 // choose any buffer size you like. 1k is plenty.
|
||||
|
||||
// Check the frame size. You don't need to do this if you trust your input.
|
||||
frameSize := len(pcm) // must be interleaved if stereo
|
||||
frameSizeMs := float32(frameSize) / channels * 1000 / sampleRate
|
||||
switch frameSizeMs {
|
||||
case 2.5, 5, 10, 20, 40, 60:
|
||||
// Good.
|
||||
default:
|
||||
return fmt.Errorf("Illegal frame size: %d bytes (%f ms)", frameSize, frameSizeMs)
|
||||
}
|
||||
|
||||
data := make([]byte, bufferSize)
|
||||
n, err := enc.Encode(pcm, data)
|
||||
if err != nil {
|
||||
...
|
||||
}
|
||||
data = data[:n] // only the first N bytes are opus data. Just like io.Reader.
|
||||
```
|
||||
|
||||
Note that you must choose a target buffer size, and this buffer size will affect
|
||||
the encoding process:
|
||||
|
||||
> Size of the allocated memory for the output payload. This may be used to
|
||||
> impose an upper limit on the instant bitrate, but should not be used as the
|
||||
> only bitrate control. Use `OPUS_SET_BITRATE` to control the bitrate.
|
||||
|
||||
-- https://opus-codec.org/docs/opus_api-1.1.3/group__opus__encoder.html
|
||||
|
||||
### Decoding
|
||||
|
||||
To decode opus data to raw PCM format, first create a decoder:
|
||||
|
||||
```go
|
||||
dec, err := opus.NewDecoder(sampleRate, channels)
|
||||
if err != nil {
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Now pass it the opus bytes, and a buffer to store the PCM sound in:
|
||||
|
||||
```go
|
||||
var frameSizeMs float32 = ... // if you don't know, go with 60 ms.
|
||||
frameSize := channels * frameSizeMs * sampleRate / 1000
|
||||
pcm := make([]int16, int(frameSize))
|
||||
n, err := dec.Decode(data, pcm)
|
||||
if err != nil {
|
||||
...
|
||||
}
|
||||
|
||||
// To get all samples (interleaved if multiple channels):
|
||||
pcm = pcm[:n*channels] // only necessary if you didn't know the right frame size
|
||||
|
||||
// or access sample per sample, directly:
|
||||
for i := 0; i < n; i++ {
|
||||
ch1 := pcm[i*channels+0]
|
||||
// For stereo output: copy ch1 into ch2 in mono mode, or deinterleave stereo
|
||||
ch2 := pcm[(i*channels)+(channels-1)]
|
||||
}
|
||||
```
|
||||
|
||||
To handle packet loss from an unreliable network, see the
|
||||
[DecodePLC](https://godoc.org/gopkg.in/hraban/opus.v2#Decoder.DecodePLC) and
|
||||
[DecodeFEC](https://godoc.org/gopkg.in/hraban/opus.v2#Decoder.DecodeFEC)
|
||||
options.
|
||||
|
||||
### Streams (and Files)
|
||||
|
||||
To decode a .opus file (or .ogg with Opus data), or to decode a "Opus stream"
|
||||
(which is a Ogg stream with Opus data), use the `Stream` interface. It wraps an
|
||||
io.Reader providing the raw stream bytes and returns the decoded Opus data.
|
||||
|
||||
A crude example for reading from a .opus file:
|
||||
|
||||
```go
|
||||
f, err := os.Open(fname)
|
||||
if err != nil {
|
||||
...
|
||||
}
|
||||
s, err := opus.NewStream(f)
|
||||
if err != nil {
|
||||
...
|
||||
}
|
||||
defer s.Close()
|
||||
pcmbuf := make([]int16, 16384)
|
||||
for {
|
||||
n, err = s.Read(pcmbuf)
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
...
|
||||
}
|
||||
pcm := pcmbuf[:n*channels]
|
||||
|
||||
// send pcm to audio device here, or write to a .wav file
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
See https://godoc.org/gopkg.in/hraban/opus.v2#Stream for further info.
|
||||
|
||||
### "My .ogg/.opus file doesn't play!" or "How do I play Opus in VLC / mplayer / ...?"
|
||||
|
||||
Note: this package only does _encoding_ of your audio, to _raw opus data_. You can't just dump those all in one big file and play it back. You need extra info. First of all, you need to know how big each individual block is. Remember: opus data is a stream of encoded separate blocks, not one big stream of bytes. Second, you need meta-data: how many channels? What's the sampling rate? Frame size? Etc.
|
||||
|
||||
Look closely at the decoding sample code (not stream), above: we're passing all that meta-data in, hard-coded. If you just put all your encoded bytes in one big file and gave that to a media player, it wouldn't know what to do with it. It wouldn't even know that it's Opus data. It would just look like `/dev/random`.
|
||||
|
||||
What you need is a [container format](https://en.wikipedia.org/wiki/Container_format_(computing)).
|
||||
|
||||
Compare it to video:
|
||||
|
||||
* Encodings: MPEG[1234], VP9, H26[45], AV1
|
||||
* Container formats: .mkv, .avi, .mov, .ogv
|
||||
|
||||
For Opus audio, the most common container format is OGG, aka .ogg or .opus. You'll know OGG from OGG/Vorbis: that's [Vorbis](https://xiph.org/vorbis/) encoded audio in an OGG container. So for Opus, you'd call it OGG/Opus. But technically you could stick opus data in any container format that supports it, including e.g. Matroska (.mka for audio, you probably know it from .mkv for video).
|
||||
|
||||
Note: libopus, the C library that this wraps, technically comes with libopusfile, which can help with the creation of OGG/Opus streams from raw audio data. I just never needed it myself, so I haven't added the necessary code for it. If you find yourself adding it: send me a PR and we'll get it merged.
|
||||
|
||||
This libopus wrapper _does_ come with code for _decoding_ an OGG/Opus stream. Just not for writing one.
|
||||
|
||||
### API Docs
|
||||
|
||||
Go wrapper API reference:
|
||||
https://godoc.org/gopkg.in/hraban/opus.v2
|
||||
|
||||
Full libopus C API reference:
|
||||
https://www.opus-codec.org/docs/opus_api-1.1.3/
|
||||
|
||||
For more examples, see the `_test.go` files.
|
||||
|
||||
## Build & Installation
|
||||
|
||||
This package requires libopus and libopusfile development packages to be
|
||||
installed on your system. These are available on Debian based systems from
|
||||
aptitude as `libopus-dev` and `libopusfile-dev`, and on Mac OS X from homebrew.
|
||||
|
||||
They are linked into the app using pkg-config.
|
||||
|
||||
Debian, Ubuntu, ...:
|
||||
```sh
|
||||
sudo apt-get install pkg-config libopus-dev libopusfile-dev
|
||||
```
|
||||
|
||||
Mac:
|
||||
```sh
|
||||
brew install pkg-config opus opusfile
|
||||
```
|
||||
|
||||
### Building Without `libopusfile`
|
||||
|
||||
This package can be built without `libopusfile` by using the build tag `nolibopusfile`.
|
||||
This enables the compilation of statically-linked binaries with no external
|
||||
dependencies on operating systems without a static `libopusfile`, such as
|
||||
[Alpine Linux](https://pkgs.alpinelinux.org/contents?branch=edge&name=opusfile-dev&arch=x86_64&repo=main).
|
||||
|
||||
**Note:** this will disable all file and `Stream` APIs.
|
||||
|
||||
To enable this feature, add `-tags nolibopusfile` to your `go build` or `go test` commands:
|
||||
|
||||
```sh
|
||||
# Build
|
||||
go build -tags nolibopusfile ...
|
||||
|
||||
# Test
|
||||
go test -tags nolibopusfile ./...
|
||||
```
|
||||
|
||||
### Using in Docker
|
||||
|
||||
If your Dockerized app has this library as a dependency (directly or
|
||||
indirectly), it will need to install the aforementioned packages, too.
|
||||
|
||||
This means you can't use the standard `golang:*-onbuild` images, because those
|
||||
will try to build the app from source before allowing you to install extra
|
||||
dependencies. Instead, try this as a Dockerfile:
|
||||
|
||||
```Dockerfile
|
||||
# Choose any golang image, just make sure it doesn't have -onbuild
|
||||
FROM golang:1
|
||||
|
||||
RUN apt-get update && apt-get -y install libopus-dev libopusfile-dev
|
||||
|
||||
# Everything below is copied manually from the official -onbuild image,
|
||||
# with the ONBUILD keywords removed.
|
||||
|
||||
RUN mkdir -p /go/src/app
|
||||
WORKDIR /go/src/app
|
||||
|
||||
CMD ["go-wrapper", "run"]
|
||||
COPY . /go/src/app
|
||||
RUN go-wrapper download
|
||||
RUN go-wrapper install
|
||||
```
|
||||
|
||||
For more information, see <https://hub.docker.com/_/golang/>.
|
||||
|
||||
### Linking libopus and libopusfile
|
||||
|
||||
The opus and opusfile libraries will be linked into your application
|
||||
dynamically. This means everyone who uses the resulting binary will need those
|
||||
libraries available on their system. E.g. if you use this wrapper to write a
|
||||
music app in Go, everyone using that music app will need libopus and libopusfile
|
||||
on their system. On Debian systems the packages are called `libopus0` and
|
||||
`libopusfile0`.
|
||||
|
||||
The "cleanest" way to do this is to publish your software through a package
|
||||
manager and specify libopus and libopusfile as dependencies of your program. If
|
||||
that is not an option, you can compile the dynamic libraries yourself and ship
|
||||
them with your software as seperate (.dll or .so) files.
|
||||
|
||||
On Linux, for example, you would need the libopus.so.0 and libopusfile.so.0
|
||||
files in the same directory as the binary. Set your ELF binary's rpath to
|
||||
`$ORIGIN` (this is not a shell variable but elf magic):
|
||||
|
||||
```sh
|
||||
patchelf --set-origin '$ORIGIN' your-app-binary
|
||||
```
|
||||
|
||||
Now you can run the binary and it will automatically pick up shared library
|
||||
files from its own directory.
|
||||
|
||||
Wrap it all in a .zip, and ship.
|
||||
|
||||
I know there is a similar trick for Mac (involving prefixing the shared library
|
||||
names with `./`, which is, arguably, better). And Windows... probably just picks
|
||||
up .dll files from the same dir by default? I don't know. But there are ways.
|
||||
|
||||
## License
|
||||
|
||||
The licensing terms for the Go bindings are found in the LICENSE file. The
|
||||
authors and copyright holders are listed in the AUTHORS file.
|
||||
|
||||
The copyright notice uses range notation to indicate all years in between are
|
||||
subject to copyright, as well. This statement is necessary, apparently. For all
|
||||
those nefarious actors ready to abuse a copyright notice with incorrect
|
||||
notation, but thwarted by a mention in the README. Pfew!
|
29
gumble/go-opus/callbacks.c
Normal file
29
gumble/go-opus/callbacks.c
Normal file
@ -0,0 +1,29 @@
|
||||
// +build !nolibopusfile
|
||||
|
||||
// Copyright © Go Opus Authors (see AUTHORS file)
|
||||
//
|
||||
// License for use of this code is detailed in the LICENSE file
|
||||
|
||||
// Allocate callback struct in C to ensure it's not managed by the Go GC. This
|
||||
// plays nice with the CGo rules and avoids any confusion.
|
||||
|
||||
#include <opusfile.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Defined in Go. Uses the same signature as Go, no need for proxy function.
|
||||
int go_readcallback(void *p, unsigned char *buf, int nbytes);
|
||||
|
||||
static struct OpusFileCallbacks callbacks = {
|
||||
.read = go_readcallback,
|
||||
};
|
||||
|
||||
// Proxy function for op_open_callbacks, because it takes a void * context but
|
||||
// we want to pass it non-pointer data, namely an arbitrary uintptr_t
|
||||
// value. This is legal C, but go test -race (-d=checkptr) complains anyway. So
|
||||
// we have this wrapper function to shush it.
|
||||
// https://groups.google.com/g/golang-nuts/c/995uZyRPKlU
|
||||
OggOpusFile *
|
||||
my_open_callbacks(uintptr_t p, int *error)
|
||||
{
|
||||
return op_open_callbacks((void *)p, &callbacks, NULL, 0, error);
|
||||
}
|
276
gumble/go-opus/decoder.go
Normal file
276
gumble/go-opus/decoder.go
Normal file
@ -0,0 +1,276 @@
|
||||
// Copyright ÂGo Opus Authors (see AUTHORS file)
|
||||
//
|
||||
// License for use of this code is detailed in the LICENSE file
|
||||
|
||||
package opus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
/*
|
||||
#cgo pkg-config: opus
|
||||
#include <opus.h>
|
||||
|
||||
int
|
||||
bridge_decoder_get_last_packet_duration(OpusDecoder *st, opus_int32 *samples)
|
||||
{
|
||||
return opus_decoder_ctl(st, OPUS_GET_LAST_PACKET_DURATION(samples));
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
var errDecUninitialized = fmt.Errorf("opus decoder uninitialized")
|
||||
|
||||
type Decoder struct {
|
||||
p *C.struct_OpusDecoder
|
||||
// Same purpose as encoder struct
|
||||
mem []byte
|
||||
sample_rate int
|
||||
channels int
|
||||
}
|
||||
|
||||
// NewDecoder allocates a new Opus decoder and initializes it with the
|
||||
// appropriate parameters. All related memory is managed by the Go GC.
|
||||
func NewDecoder(sample_rate int, channels int) (*Decoder, error) {
|
||||
var dec Decoder
|
||||
err := dec.Init(sample_rate, channels)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dec, nil
|
||||
}
|
||||
|
||||
func (dec *Decoder) Init(sample_rate int, channels int) error {
|
||||
if dec.p != nil {
|
||||
return fmt.Errorf("opus decoder already initialized")
|
||||
}
|
||||
if channels != 1 && channels != 2 {
|
||||
return fmt.Errorf("Number of channels must be 1 or 2: %d", channels)
|
||||
}
|
||||
size := C.opus_decoder_get_size(C.int(channels))
|
||||
dec.sample_rate = sample_rate
|
||||
dec.channels = channels
|
||||
dec.mem = make([]byte, size)
|
||||
dec.p = (*C.OpusDecoder)(unsafe.Pointer(&dec.mem[0]))
|
||||
errno := C.opus_decoder_init(
|
||||
dec.p,
|
||||
C.opus_int32(sample_rate),
|
||||
C.int(channels))
|
||||
if errno != 0 {
|
||||
return Error(errno)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Decode encoded Opus data into the supplied buffer. On success, returns the
|
||||
// number of samples correctly written to the target buffer.
|
||||
func (dec *Decoder) Decode(data []byte, pcm []int16) (int, error) {
|
||||
if dec.p == nil {
|
||||
return 0, errDecUninitialized
|
||||
}
|
||||
if len(data) == 0 {
|
||||
return 0, fmt.Errorf("opus: no data supplied")
|
||||
}
|
||||
if len(pcm) == 0 {
|
||||
return 0, fmt.Errorf("opus: target buffer empty")
|
||||
}
|
||||
if cap(pcm)%dec.channels != 0 {
|
||||
return 0, fmt.Errorf("opus: target buffer capacity must be multiple of channels")
|
||||
}
|
||||
n := int(C.opus_decode(
|
||||
dec.p,
|
||||
(*C.uchar)(&data[0]),
|
||||
C.opus_int32(len(data)),
|
||||
(*C.opus_int16)(&pcm[0]),
|
||||
C.int(cap(pcm)/dec.channels),
|
||||
0))
|
||||
if n < 0 {
|
||||
return 0, Error(n)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Decode encoded Opus data into the supplied buffer. On success, returns the
|
||||
// number of samples correctly written to the target buffer.
|
||||
func (dec *Decoder) DecodeFloat32(data []byte, pcm []float32) (int, error) {
|
||||
if dec.p == nil {
|
||||
return 0, errDecUninitialized
|
||||
}
|
||||
if len(data) == 0 {
|
||||
return 0, fmt.Errorf("opus: no data supplied")
|
||||
}
|
||||
if len(pcm) == 0 {
|
||||
return 0, fmt.Errorf("opus: target buffer empty")
|
||||
}
|
||||
if cap(pcm)%dec.channels != 0 {
|
||||
return 0, fmt.Errorf("opus: target buffer capacity must be multiple of channels")
|
||||
}
|
||||
n := int(C.opus_decode_float(
|
||||
dec.p,
|
||||
(*C.uchar)(&data[0]),
|
||||
C.opus_int32(len(data)),
|
||||
(*C.float)(&pcm[0]),
|
||||
C.int(cap(pcm)/dec.channels),
|
||||
0))
|
||||
if n < 0 {
|
||||
return 0, Error(n)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// DecodeFEC encoded Opus data into the supplied buffer with forward error
|
||||
// correction.
|
||||
//
|
||||
// It is to be used on the packet directly following the lost one. The supplied
|
||||
// buffer needs to be exactly the duration of audio that is missing
|
||||
//
|
||||
// When a packet is considered "lost", DecodeFEC can be called on the next
|
||||
// packet in order to try and recover some of the lost data. The PCM needs to be
|
||||
// exactly the duration of audio that is missing. `LastPacketDuration()` can be
|
||||
// used on the decoder to get the length of the last packet. Note also that in
|
||||
// order to use this feature the encoder needs to be configured with
|
||||
// SetInBandFEC(true) and SetPacketLossPerc(x) options.
|
||||
//
|
||||
// Note that DecodeFEC automatically falls back to PLC when no FEC data is
|
||||
// available in the provided packet.
|
||||
func (dec *Decoder) DecodeFEC(data []byte, pcm []int16) error {
|
||||
if dec.p == nil {
|
||||
return errDecUninitialized
|
||||
}
|
||||
if len(data) == 0 {
|
||||
return fmt.Errorf("opus: no data supplied")
|
||||
}
|
||||
if len(pcm) == 0 {
|
||||
return fmt.Errorf("opus: target buffer empty")
|
||||
}
|
||||
if cap(pcm)%dec.channels != 0 {
|
||||
return fmt.Errorf("opus: target buffer capacity must be multiple of channels")
|
||||
}
|
||||
n := int(C.opus_decode(
|
||||
dec.p,
|
||||
(*C.uchar)(&data[0]),
|
||||
C.opus_int32(len(data)),
|
||||
(*C.opus_int16)(&pcm[0]),
|
||||
C.int(cap(pcm)/dec.channels),
|
||||
1))
|
||||
if n < 0 {
|
||||
return Error(n)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecodeFECFloat32 encoded Opus data into the supplied buffer with forward error
|
||||
// correction. It is to be used on the packet directly following the lost one.
|
||||
// The supplied buffer needs to be exactly the duration of audio that is missing
|
||||
func (dec *Decoder) DecodeFECFloat32(data []byte, pcm []float32) error {
|
||||
if dec.p == nil {
|
||||
return errDecUninitialized
|
||||
}
|
||||
if len(data) == 0 {
|
||||
return fmt.Errorf("opus: no data supplied")
|
||||
}
|
||||
if len(pcm) == 0 {
|
||||
return fmt.Errorf("opus: target buffer empty")
|
||||
}
|
||||
if cap(pcm)%dec.channels != 0 {
|
||||
return fmt.Errorf("opus: target buffer capacity must be multiple of channels")
|
||||
}
|
||||
n := int(C.opus_decode_float(
|
||||
dec.p,
|
||||
(*C.uchar)(&data[0]),
|
||||
C.opus_int32(len(data)),
|
||||
(*C.float)(&pcm[0]),
|
||||
C.int(cap(pcm)/dec.channels),
|
||||
1))
|
||||
if n < 0 {
|
||||
return Error(n)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecodePLC recovers a lost packet using Opus Packet Loss Concealment feature.
|
||||
//
|
||||
// The supplied buffer needs to be exactly the duration of audio that is missing.
|
||||
// When a packet is considered "lost", `DecodePLC` and `DecodePLCFloat32` methods
|
||||
// can be called in order to obtain something better sounding than just silence.
|
||||
// The PCM needs to be exactly the duration of audio that is missing.
|
||||
// `LastPacketDuration()` can be used on the decoder to get the length of the
|
||||
// last packet.
|
||||
//
|
||||
// This option does not require any additional encoder options. Unlike FEC,
|
||||
// PLC does not introduce additional latency. It is calculated from the previous
|
||||
// packet, not from the next one.
|
||||
func (dec *Decoder) DecodePLC(pcm []int16) error {
|
||||
if dec.p == nil {
|
||||
return errDecUninitialized
|
||||
}
|
||||
if len(pcm) == 0 {
|
||||
return fmt.Errorf("opus: target buffer empty")
|
||||
}
|
||||
if cap(pcm)%dec.channels != 0 {
|
||||
return fmt.Errorf("opus: output buffer capacity must be multiple of channels")
|
||||
}
|
||||
n := int(C.opus_decode(
|
||||
dec.p,
|
||||
nil,
|
||||
0,
|
||||
(*C.opus_int16)(&pcm[0]),
|
||||
C.int(cap(pcm)/dec.channels),
|
||||
0))
|
||||
if n < 0 {
|
||||
return Error(n)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecodePLCFloat32 recovers a lost packet using Opus Packet Loss Concealment feature.
|
||||
// The supplied buffer needs to be exactly the duration of audio that is missing.
|
||||
func (dec *Decoder) DecodePLCFloat32(pcm []float32) error {
|
||||
if dec.p == nil {
|
||||
return errDecUninitialized
|
||||
}
|
||||
if len(pcm) == 0 {
|
||||
return fmt.Errorf("opus: target buffer empty")
|
||||
}
|
||||
if cap(pcm)%dec.channels != 0 {
|
||||
return fmt.Errorf("opus: output buffer capacity must be multiple of channels")
|
||||
}
|
||||
n := int(C.opus_decode_float(
|
||||
dec.p,
|
||||
nil,
|
||||
0,
|
||||
(*C.float)(&pcm[0]),
|
||||
C.int(cap(pcm)/dec.channels),
|
||||
0))
|
||||
if n < 0 {
|
||||
return Error(n)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// LastPacketDuration gets the duration (in samples)
|
||||
// of the last packet successfully decoded or concealed.
|
||||
func (dec *Decoder) LastPacketDuration() (int, error) {
|
||||
var samples C.opus_int32
|
||||
res := C.bridge_decoder_get_last_packet_duration(dec.p, &samples)
|
||||
if res != C.OPUS_OK {
|
||||
return 0, Error(res)
|
||||
}
|
||||
return int(samples), nil
|
||||
}
|
||||
|
||||
// Reset reinitializes the decoder with the existing parameters
|
||||
func (dec *Decoder) Reset() error {
|
||||
// Store current parameters
|
||||
sample_rate := dec.sample_rate
|
||||
channels := dec.channels
|
||||
|
||||
// Clear the decoder
|
||||
dec.p = nil
|
||||
dec.mem = nil
|
||||
|
||||
// Reinitialize with same parameters
|
||||
return dec.Init(sample_rate, channels)
|
||||
}
|
68
gumble/go-opus/decoder_test.go
Normal file
68
gumble/go-opus/decoder_test.go
Normal file
@ -0,0 +1,68 @@
|
||||
// Copyright © Go Opus Authors (see AUTHORS file)
|
||||
//
|
||||
// License for use of this code is detailed in the LICENSE file
|
||||
|
||||
package opus
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDecoderNew(t *testing.T) {
|
||||
dec, err := NewDecoder(48000, 1)
|
||||
if err != nil || dec == nil {
|
||||
t.Errorf("Error creating new decoder: %v", err)
|
||||
}
|
||||
dec, err = NewDecoder(12345, 1)
|
||||
if err == nil || dec != nil {
|
||||
t.Errorf("Expected error for illegal samplerate 12345")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecoderUnitialized(t *testing.T) {
|
||||
var dec Decoder
|
||||
_, err := dec.Decode(nil, nil)
|
||||
if err != errDecUninitialized {
|
||||
t.Errorf("Expected \"unitialized decoder\" error: %v", err)
|
||||
}
|
||||
_, err = dec.DecodeFloat32(nil, nil)
|
||||
if err != errDecUninitialized {
|
||||
t.Errorf("Expected \"unitialized decoder\" error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecoder_GetLastPacketDuration(t *testing.T) {
|
||||
const G4 = 391.995
|
||||
const SAMPLE_RATE = 48000
|
||||
const FRAME_SIZE_MS = 60
|
||||
const FRAME_SIZE = SAMPLE_RATE * FRAME_SIZE_MS / 1000
|
||||
pcm := make([]int16, FRAME_SIZE)
|
||||
enc, err := NewEncoder(SAMPLE_RATE, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Fatalf("Error creating new encoder: %v", err)
|
||||
}
|
||||
addSine(pcm, SAMPLE_RATE, G4)
|
||||
|
||||
data := make([]byte, 1000)
|
||||
n, err := enc.Encode(pcm, data)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't encode data: %v", err)
|
||||
}
|
||||
data = data[:n]
|
||||
|
||||
dec, err := NewDecoder(SAMPLE_RATE, 1)
|
||||
if err != nil || dec == nil {
|
||||
t.Fatalf("Error creating new decoder: %v", err)
|
||||
}
|
||||
n, err = dec.Decode(data, pcm)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't decode data: %v", err)
|
||||
}
|
||||
samples, err := dec.LastPacketDuration()
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't get last packet duration: %v", err)
|
||||
}
|
||||
if samples != n {
|
||||
t.Fatalf("Wrong duration length. Expected %d. Got %d", n, samples)
|
||||
}
|
||||
}
|
402
gumble/go-opus/encoder.go
Normal file
402
gumble/go-opus/encoder.go
Normal file
@ -0,0 +1,402 @@
|
||||
// Copyright © Go Opus Authors (see AUTHORS file)
|
||||
//
|
||||
// License for use of this code is detailed in the LICENSE file
|
||||
|
||||
package opus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
/*
|
||||
#cgo pkg-config: opus
|
||||
#include <opus.h>
|
||||
|
||||
int
|
||||
bridge_encoder_set_dtx(OpusEncoder *st, opus_int32 use_dtx)
|
||||
{
|
||||
return opus_encoder_ctl(st, OPUS_SET_DTX(use_dtx));
|
||||
}
|
||||
|
||||
int
|
||||
bridge_encoder_get_dtx(OpusEncoder *st, opus_int32 *dtx)
|
||||
{
|
||||
return opus_encoder_ctl(st, OPUS_GET_DTX(dtx));
|
||||
}
|
||||
|
||||
int
|
||||
bridge_encoder_get_in_dtx(OpusEncoder *st, opus_int32 *in_dtx)
|
||||
{
|
||||
return opus_encoder_ctl(st, OPUS_GET_IN_DTX(in_dtx));
|
||||
}
|
||||
|
||||
int
|
||||
bridge_encoder_get_sample_rate(OpusEncoder *st, opus_int32 *sample_rate)
|
||||
{
|
||||
return opus_encoder_ctl(st, OPUS_GET_SAMPLE_RATE(sample_rate));
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
bridge_encoder_set_bitrate(OpusEncoder *st, opus_int32 bitrate)
|
||||
{
|
||||
return opus_encoder_ctl(st, OPUS_SET_BITRATE(bitrate));
|
||||
}
|
||||
|
||||
int
|
||||
bridge_encoder_get_bitrate(OpusEncoder *st, opus_int32 *bitrate)
|
||||
{
|
||||
return opus_encoder_ctl(st, OPUS_GET_BITRATE(bitrate));
|
||||
}
|
||||
|
||||
int
|
||||
bridge_encoder_set_complexity(OpusEncoder *st, opus_int32 complexity)
|
||||
{
|
||||
return opus_encoder_ctl(st, OPUS_SET_COMPLEXITY(complexity));
|
||||
}
|
||||
|
||||
int
|
||||
bridge_encoder_get_complexity(OpusEncoder *st, opus_int32 *complexity)
|
||||
{
|
||||
return opus_encoder_ctl(st, OPUS_GET_COMPLEXITY(complexity));
|
||||
}
|
||||
|
||||
int
|
||||
bridge_encoder_set_max_bandwidth(OpusEncoder *st, opus_int32 max_bw)
|
||||
{
|
||||
return opus_encoder_ctl(st, OPUS_SET_MAX_BANDWIDTH(max_bw));
|
||||
}
|
||||
|
||||
int
|
||||
bridge_encoder_get_max_bandwidth(OpusEncoder *st, opus_int32 *max_bw)
|
||||
{
|
||||
return opus_encoder_ctl(st, OPUS_GET_MAX_BANDWIDTH(max_bw));
|
||||
}
|
||||
|
||||
int
|
||||
bridge_encoder_set_inband_fec(OpusEncoder *st, opus_int32 fec)
|
||||
{
|
||||
return opus_encoder_ctl(st, OPUS_SET_INBAND_FEC(fec));
|
||||
}
|
||||
|
||||
int
|
||||
bridge_encoder_get_inband_fec(OpusEncoder *st, opus_int32 *fec)
|
||||
{
|
||||
return opus_encoder_ctl(st, OPUS_GET_INBAND_FEC(fec));
|
||||
}
|
||||
|
||||
int
|
||||
bridge_encoder_set_packet_loss_perc(OpusEncoder *st, opus_int32 loss_perc)
|
||||
{
|
||||
return opus_encoder_ctl(st, OPUS_SET_PACKET_LOSS_PERC(loss_perc));
|
||||
}
|
||||
|
||||
int
|
||||
bridge_encoder_get_packet_loss_perc(OpusEncoder *st, opus_int32 *loss_perc)
|
||||
{
|
||||
return opus_encoder_ctl(st, OPUS_GET_PACKET_LOSS_PERC(loss_perc));
|
||||
}
|
||||
|
||||
int
|
||||
bridge_encoder_reset_state(OpusEncoder *st)
|
||||
{
|
||||
return opus_encoder_ctl(st, OPUS_RESET_STATE);
|
||||
}
|
||||
|
||||
*/
|
||||
import "C"
|
||||
|
||||
type Bandwidth int
|
||||
|
||||
const (
|
||||
// 4 kHz passband
|
||||
Narrowband = Bandwidth(C.OPUS_BANDWIDTH_NARROWBAND)
|
||||
// 6 kHz passband
|
||||
Mediumband = Bandwidth(C.OPUS_BANDWIDTH_MEDIUMBAND)
|
||||
// 8 kHz passband
|
||||
Wideband = Bandwidth(C.OPUS_BANDWIDTH_WIDEBAND)
|
||||
// 12 kHz passband
|
||||
SuperWideband = Bandwidth(C.OPUS_BANDWIDTH_SUPERWIDEBAND)
|
||||
// 20 kHz passband
|
||||
Fullband = Bandwidth(C.OPUS_BANDWIDTH_FULLBAND)
|
||||
)
|
||||
|
||||
var errEncUninitialized = fmt.Errorf("opus encoder uninitialized")
|
||||
|
||||
// Encoder contains the state of an Opus encoder for libopus.
|
||||
type Encoder struct {
|
||||
p *C.struct_OpusEncoder
|
||||
channels int
|
||||
// Memory for the encoder struct allocated on the Go heap to allow Go GC to
|
||||
// manage it (and obviate need to free())
|
||||
mem []byte
|
||||
}
|
||||
|
||||
// NewEncoder allocates a new Opus encoder and initializes it with the
|
||||
// appropriate parameters. All related memory is managed by the Go GC.
|
||||
func NewEncoder(sample_rate int, channels int, application Application) (*Encoder, error) {
|
||||
var enc Encoder
|
||||
err := enc.Init(sample_rate, channels, application)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &enc, nil
|
||||
}
|
||||
|
||||
// Init initializes a pre-allocated opus encoder. Unless the encoder has been
|
||||
// created using NewEncoder, this method must be called exactly once in the
|
||||
// life-time of this object, before calling any other methods.
|
||||
func (enc *Encoder) Init(sample_rate int, channels int, application Application) error {
|
||||
if enc.p != nil {
|
||||
return fmt.Errorf("opus encoder already initialized")
|
||||
}
|
||||
if channels != 1 && channels != 2 {
|
||||
return fmt.Errorf("Number of channels must be 1 or 2: %d", channels)
|
||||
}
|
||||
size := C.opus_encoder_get_size(C.int(channels))
|
||||
enc.channels = channels
|
||||
enc.mem = make([]byte, size)
|
||||
enc.p = (*C.OpusEncoder)(unsafe.Pointer(&enc.mem[0]))
|
||||
errno := int(C.opus_encoder_init(
|
||||
enc.p,
|
||||
C.opus_int32(sample_rate),
|
||||
C.int(channels),
|
||||
C.int(application)))
|
||||
if errno != 0 {
|
||||
return Error(int(errno))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Encode raw PCM data and store the result in the supplied buffer. On success,
|
||||
// returns the number of bytes used up by the encoded data.
|
||||
func (enc *Encoder) Encode(pcm []int16, data []byte) (int, error) {
|
||||
if enc.p == nil {
|
||||
return 0, errEncUninitialized
|
||||
}
|
||||
if len(pcm) == 0 {
|
||||
return 0, fmt.Errorf("opus: no data supplied")
|
||||
}
|
||||
if len(data) == 0 {
|
||||
return 0, fmt.Errorf("opus: no target buffer")
|
||||
}
|
||||
// libopus talks about samples as 1 sample containing multiple channels. So
|
||||
// e.g. 20 samples of 2-channel data is actually 40 raw data points.
|
||||
if len(pcm)%enc.channels != 0 {
|
||||
return 0, fmt.Errorf("opus: input buffer length must be multiple of channels")
|
||||
}
|
||||
samples := len(pcm) / enc.channels
|
||||
n := int(C.opus_encode(
|
||||
enc.p,
|
||||
(*C.opus_int16)(&pcm[0]),
|
||||
C.int(samples),
|
||||
(*C.uchar)(&data[0]),
|
||||
C.opus_int32(cap(data))))
|
||||
if n < 0 {
|
||||
return 0, Error(n)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Encode raw PCM data and store the result in the supplied buffer. On success,
|
||||
// returns the number of bytes used up by the encoded data.
|
||||
func (enc *Encoder) EncodeFloat32(pcm []float32, data []byte) (int, error) {
|
||||
if enc.p == nil {
|
||||
return 0, errEncUninitialized
|
||||
}
|
||||
if len(pcm) == 0 {
|
||||
return 0, fmt.Errorf("opus: no data supplied")
|
||||
}
|
||||
if len(data) == 0 {
|
||||
return 0, fmt.Errorf("opus: no target buffer")
|
||||
}
|
||||
if len(pcm)%enc.channels != 0 {
|
||||
return 0, fmt.Errorf("opus: input buffer length must be multiple of channels")
|
||||
}
|
||||
samples := len(pcm) / enc.channels
|
||||
n := int(C.opus_encode_float(
|
||||
enc.p,
|
||||
(*C.float)(&pcm[0]),
|
||||
C.int(samples),
|
||||
(*C.uchar)(&data[0]),
|
||||
C.opus_int32(cap(data))))
|
||||
if n < 0 {
|
||||
return 0, Error(n)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// SetDTX configures the encoder's use of discontinuous transmission (DTX).
|
||||
func (enc *Encoder) SetDTX(dtx bool) error {
|
||||
i := 0
|
||||
if dtx {
|
||||
i = 1
|
||||
}
|
||||
res := C.bridge_encoder_set_dtx(enc.p, C.opus_int32(i))
|
||||
if res != C.OPUS_OK {
|
||||
return Error(res)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DTX reports whether this encoder is configured to use discontinuous
|
||||
// transmission (DTX).
|
||||
func (enc *Encoder) DTX() (bool, error) {
|
||||
var dtx C.opus_int32
|
||||
res := C.bridge_encoder_get_dtx(enc.p, &dtx)
|
||||
if res != C.OPUS_OK {
|
||||
return false, Error(res)
|
||||
}
|
||||
return dtx != 0, nil
|
||||
}
|
||||
|
||||
// InDTX returns whether the last encoded frame was either a comfort noise update
|
||||
// during DTX or not encoded because of DTX.
|
||||
func (enc *Encoder) InDTX() (bool, error) {
|
||||
var inDTX C.opus_int32
|
||||
res := C.bridge_encoder_get_in_dtx(enc.p, &inDTX)
|
||||
if res != C.OPUS_OK {
|
||||
return false, Error(res)
|
||||
}
|
||||
return inDTX != 0, nil
|
||||
}
|
||||
|
||||
// SampleRate returns the encoder sample rate in Hz.
|
||||
func (enc *Encoder) SampleRate() (int, error) {
|
||||
var sr C.opus_int32
|
||||
res := C.bridge_encoder_get_sample_rate(enc.p, &sr)
|
||||
if res != C.OPUS_OK {
|
||||
return 0, Error(res)
|
||||
}
|
||||
return int(sr), nil
|
||||
}
|
||||
|
||||
// SetBitrate sets the bitrate of the Encoder
|
||||
func (enc *Encoder) SetBitrate(bitrate int) error {
|
||||
res := C.bridge_encoder_set_bitrate(enc.p, C.opus_int32(bitrate))
|
||||
if res != C.OPUS_OK {
|
||||
return Error(res)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetBitrateToAuto will allow the encoder to automatically set the bitrate
|
||||
func (enc *Encoder) SetBitrateToAuto() error {
|
||||
res := C.bridge_encoder_set_bitrate(enc.p, C.opus_int32(C.OPUS_AUTO))
|
||||
if res != C.OPUS_OK {
|
||||
return Error(res)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetBitrateToMax causes the encoder to use as much rate as it can. This can be
|
||||
// useful for controlling the rate by adjusting the output buffer size.
|
||||
func (enc *Encoder) SetBitrateToMax() error {
|
||||
res := C.bridge_encoder_set_bitrate(enc.p, C.opus_int32(C.OPUS_BITRATE_MAX))
|
||||
if res != C.OPUS_OK {
|
||||
return Error(res)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Bitrate returns the bitrate of the Encoder
|
||||
func (enc *Encoder) Bitrate() (int, error) {
|
||||
var bitrate C.opus_int32
|
||||
res := C.bridge_encoder_get_bitrate(enc.p, &bitrate)
|
||||
if res != C.OPUS_OK {
|
||||
return 0, Error(res)
|
||||
}
|
||||
return int(bitrate), nil
|
||||
}
|
||||
|
||||
// SetComplexity sets the encoder's computational complexity
|
||||
func (enc *Encoder) SetComplexity(complexity int) error {
|
||||
res := C.bridge_encoder_set_complexity(enc.p, C.opus_int32(complexity))
|
||||
if res != C.OPUS_OK {
|
||||
return Error(res)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Complexity returns the computational complexity used by the encoder
|
||||
func (enc *Encoder) Complexity() (int, error) {
|
||||
var complexity C.opus_int32
|
||||
res := C.bridge_encoder_get_complexity(enc.p, &complexity)
|
||||
if res != C.OPUS_OK {
|
||||
return 0, Error(res)
|
||||
}
|
||||
return int(complexity), nil
|
||||
}
|
||||
|
||||
// SetMaxBandwidth configures the maximum bandpass that the encoder will select
|
||||
// automatically
|
||||
func (enc *Encoder) SetMaxBandwidth(maxBw Bandwidth) error {
|
||||
res := C.bridge_encoder_set_max_bandwidth(enc.p, C.opus_int32(maxBw))
|
||||
if res != C.OPUS_OK {
|
||||
return Error(res)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MaxBandwidth gets the encoder's configured maximum allowed bandpass.
|
||||
func (enc *Encoder) MaxBandwidth() (Bandwidth, error) {
|
||||
var maxBw C.opus_int32
|
||||
res := C.bridge_encoder_get_max_bandwidth(enc.p, &maxBw)
|
||||
if res != C.OPUS_OK {
|
||||
return 0, Error(res)
|
||||
}
|
||||
return Bandwidth(maxBw), nil
|
||||
}
|
||||
|
||||
// SetInBandFEC configures the encoder's use of inband forward error
|
||||
// correction (FEC)
|
||||
func (enc *Encoder) SetInBandFEC(fec bool) error {
|
||||
i := 0
|
||||
if fec {
|
||||
i = 1
|
||||
}
|
||||
res := C.bridge_encoder_set_inband_fec(enc.p, C.opus_int32(i))
|
||||
if res != C.OPUS_OK {
|
||||
return Error(res)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InBandFEC gets the encoder's configured inband forward error correction (FEC)
|
||||
func (enc *Encoder) InBandFEC() (bool, error) {
|
||||
var fec C.opus_int32
|
||||
res := C.bridge_encoder_get_inband_fec(enc.p, &fec)
|
||||
if res != C.OPUS_OK {
|
||||
return false, Error(res)
|
||||
}
|
||||
return fec != 0, nil
|
||||
}
|
||||
|
||||
// SetPacketLossPerc configures the encoder's expected packet loss percentage.
|
||||
func (enc *Encoder) SetPacketLossPerc(lossPerc int) error {
|
||||
res := C.bridge_encoder_set_packet_loss_perc(enc.p, C.opus_int32(lossPerc))
|
||||
if res != C.OPUS_OK {
|
||||
return Error(res)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PacketLossPerc gets the encoder's configured packet loss percentage.
|
||||
func (enc *Encoder) PacketLossPerc() (int, error) {
|
||||
var lossPerc C.opus_int32
|
||||
res := C.bridge_encoder_get_packet_loss_perc(enc.p, &lossPerc)
|
||||
if res != C.OPUS_OK {
|
||||
return 0, Error(res)
|
||||
}
|
||||
return int(lossPerc), nil
|
||||
}
|
||||
|
||||
// Reset resets the codec state to be equivalent to a freshly initialized state.
|
||||
func (enc *Encoder) Reset() error {
|
||||
res := C.bridge_encoder_reset_state(enc.p)
|
||||
if res != C.OPUS_OK {
|
||||
return Error(res)
|
||||
}
|
||||
return nil
|
||||
}
|
393
gumble/go-opus/encoder_test.go
Normal file
393
gumble/go-opus/encoder_test.go
Normal file
@ -0,0 +1,393 @@
|
||||
// Copyright © Go Opus Authors (see AUTHORS file)
|
||||
//
|
||||
// License for use of this code is detailed in the LICENSE file
|
||||
|
||||
package opus
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestEncoderNew(t *testing.T) {
|
||||
enc, err := NewEncoder(48000, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Errorf("Error creating new encoder: %v", err)
|
||||
}
|
||||
enc, err = NewEncoder(12345, 1, AppVoIP)
|
||||
if err == nil || enc != nil {
|
||||
t.Errorf("Expected error for illegal samplerate 12345")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoderUnitialized(t *testing.T) {
|
||||
var enc Encoder
|
||||
_, err := enc.Encode(nil, nil)
|
||||
if err != errEncUninitialized {
|
||||
t.Errorf("Expected \"unitialized encoder\" error: %v", err)
|
||||
}
|
||||
_, err = enc.EncodeFloat32(nil, nil)
|
||||
if err != errEncUninitialized {
|
||||
t.Errorf("Expected \"unitialized encoder\" error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoderDTX(t *testing.T) {
|
||||
enc, err := NewEncoder(8000, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Errorf("Error creating new encoder: %v", err)
|
||||
}
|
||||
vals := []bool{true, false}
|
||||
for _, dtx := range vals {
|
||||
err := enc.SetDTX(dtx)
|
||||
if err != nil {
|
||||
t.Fatalf("Error setting DTX to %t: %v", dtx, err)
|
||||
}
|
||||
gotv, err := enc.DTX()
|
||||
if err != nil {
|
||||
t.Fatalf("Error getting DTX (%t): %v", dtx, err)
|
||||
}
|
||||
if gotv != dtx {
|
||||
t.Errorf("Error set dtx: expect dtx=%v, got dtx=%v", dtx, gotv)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoderInDTX(t *testing.T) {
|
||||
// Create bogus input sound
|
||||
const G4 = 391.995
|
||||
const SAMPLE_RATE = 48000
|
||||
const FRAME_SIZE_MS = 60
|
||||
const FRAME_SIZE = SAMPLE_RATE * FRAME_SIZE_MS / 1000
|
||||
pcm := make([]int16, FRAME_SIZE)
|
||||
silentPCM := make([]int16, FRAME_SIZE)
|
||||
out := make([]byte, FRAME_SIZE*4)
|
||||
addSine(pcm, SAMPLE_RATE, G4)
|
||||
|
||||
vals := []bool{true, false}
|
||||
for _, dtx := range vals {
|
||||
enc, err := NewEncoder(SAMPLE_RATE, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Fatalf("Error creating new encoder: %v", err)
|
||||
}
|
||||
enc.SetDTX(dtx)
|
||||
if _, err = enc.Encode(pcm, out); err != nil {
|
||||
t.Fatalf("Error encoding non-silent frame: %v", err)
|
||||
}
|
||||
gotDTX, err := enc.InDTX()
|
||||
if err != nil {
|
||||
t.Fatalf("Error getting in DTX (%t): %v", dtx, err)
|
||||
}
|
||||
if gotDTX {
|
||||
t.Fatalf("Error get in dtx: expect in dtx=false, got=true")
|
||||
}
|
||||
// Encode a few frames to let DTX kick in
|
||||
for i := 0; i < 5; i++ {
|
||||
if _, err = enc.Encode(silentPCM, out); err != nil {
|
||||
t.Fatalf("Error encoding silent frame: %v", err)
|
||||
}
|
||||
}
|
||||
gotDTX, err = enc.InDTX()
|
||||
if err != nil {
|
||||
t.Fatalf("Error getting in DTX (%t): %v", dtx, err)
|
||||
}
|
||||
if gotDTX != dtx {
|
||||
t.Errorf("Error set dtx: expect in dtx=%v, got in dtx=%v", dtx, gotDTX)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoderSampleRate(t *testing.T) {
|
||||
sample_rates := []int{8000, 12000, 16000, 24000, 48000}
|
||||
for _, f := range sample_rates {
|
||||
enc, err := NewEncoder(f, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Fatalf("Error creating new encoder with sample_rate %d Hz: %v", f, err)
|
||||
}
|
||||
f2, err := enc.SampleRate()
|
||||
if err != nil {
|
||||
t.Fatalf("Error getting sample rate (%d Hz): %v", f, err)
|
||||
}
|
||||
if f != f2 {
|
||||
t.Errorf("Unexpected sample rate reported by %d Hz encoder: %d", f, f2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoder_SetGetBitrate(t *testing.T) {
|
||||
enc, err := NewEncoder(8000, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Errorf("Error creating new encoder: %v", err)
|
||||
}
|
||||
vals := []int{500, 100000, 300000}
|
||||
for _, bitrate := range vals {
|
||||
err := enc.SetBitrate(bitrate)
|
||||
if err != nil {
|
||||
t.Error("Error set bitrate:", err)
|
||||
}
|
||||
br, err := enc.Bitrate()
|
||||
if err != nil {
|
||||
t.Error("Error getting bitrate", err)
|
||||
}
|
||||
if br != bitrate {
|
||||
t.Errorf("Unexpected bitrate. Got %d, but expected %d", br, bitrate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoder_SetBitrateToAuto(t *testing.T) {
|
||||
enc, err := NewEncoder(8000, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Errorf("Error creating new encoder: %v", err)
|
||||
}
|
||||
|
||||
bitrate := 5000
|
||||
if err := enc.SetBitrate(bitrate); err != nil {
|
||||
t.Error("Error setting bitrate:", err)
|
||||
}
|
||||
|
||||
br, err := enc.Bitrate()
|
||||
if err != nil {
|
||||
t.Error("Error getting bitrate", err)
|
||||
}
|
||||
|
||||
if br != bitrate {
|
||||
t.Errorf("Unexpected bitrate. Got %d, but expected %d", br, bitrate)
|
||||
}
|
||||
|
||||
err = enc.SetBitrateToAuto()
|
||||
if err != nil {
|
||||
t.Error("Error setting Auto bitrate:", err)
|
||||
}
|
||||
|
||||
br, err = enc.Bitrate()
|
||||
if err != nil {
|
||||
t.Error("Error getting bitrate", err)
|
||||
}
|
||||
|
||||
brDefault := 32000 //default start value
|
||||
if br != brDefault {
|
||||
t.Errorf("Unexpected bitrate. Got %d, but expected %d", br, brDefault)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoder_SetBitrateToMax(t *testing.T) {
|
||||
enc, err := NewEncoder(8000, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Errorf("Error creating new encoder: %v", err)
|
||||
}
|
||||
|
||||
bitrate := 5000
|
||||
if err := enc.SetBitrate(bitrate); err != nil {
|
||||
t.Error("Error setting bitrate:", err)
|
||||
}
|
||||
|
||||
br, err := enc.Bitrate()
|
||||
if err != nil {
|
||||
t.Error("Error getting bitrate", err)
|
||||
}
|
||||
|
||||
if br != bitrate {
|
||||
t.Errorf("Unexpected bitrate. Got %d, but expected %d", br, bitrate)
|
||||
}
|
||||
|
||||
err = enc.SetBitrateToMax()
|
||||
if err != nil {
|
||||
t.Error("Error setting Max bitrate:", err)
|
||||
}
|
||||
|
||||
br, err = enc.Bitrate()
|
||||
if err != nil {
|
||||
t.Error("Error getting bitrate", err)
|
||||
}
|
||||
|
||||
brMax := 4083200
|
||||
if br != brMax { //default start value
|
||||
t.Errorf("Unexpected bitrate. Got %d, but expected %d", br, brMax)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoder_SetGetInvalidBitrate(t *testing.T) {
|
||||
enc, err := NewEncoder(8000, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Errorf("Error creating new encoder: %v", err)
|
||||
}
|
||||
invalidVals := []int{-20, 0}
|
||||
for _, bitrate := range invalidVals {
|
||||
err := enc.SetBitrate(bitrate)
|
||||
if err == nil {
|
||||
t.Errorf("Expected Error invalid bitrate: %d", bitrate)
|
||||
}
|
||||
br, err := enc.Bitrate()
|
||||
if err != nil {
|
||||
t.Error("Error getting bitrate", err)
|
||||
}
|
||||
// default bitrate is 32 kbit/s
|
||||
if br != 32000 {
|
||||
t.Errorf("Unexpected bitrate. Got %d, but expected %d", br, bitrate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoder_SetGetComplexity(t *testing.T) {
|
||||
enc, err := NewEncoder(8000, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Errorf("Error creating new encoder: %v", err)
|
||||
}
|
||||
vals := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
for _, complexity := range vals {
|
||||
err := enc.SetComplexity(complexity)
|
||||
if err != nil {
|
||||
t.Error("Error setting complexity value:", err)
|
||||
}
|
||||
cpx, err := enc.Complexity()
|
||||
if err != nil {
|
||||
t.Error("Error getting complexity value", err)
|
||||
}
|
||||
if cpx != complexity {
|
||||
t.Errorf("Unexpected encoder complexity value. Got %d, but expected %d",
|
||||
cpx, complexity)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoder_SetGetInvalidComplexity(t *testing.T) {
|
||||
enc, err := NewEncoder(8000, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Errorf("Error creating new encoder: %v", err)
|
||||
}
|
||||
invalidVals := []int{-20, 11, 1000}
|
||||
for _, complexity := range invalidVals {
|
||||
err := enc.SetComplexity(complexity)
|
||||
if err == nil {
|
||||
t.Errorf("Expected Error invalid complexity value: %d", complexity)
|
||||
}
|
||||
if err.Error() != "opus: invalid argument" {
|
||||
t.Error("Unexpected Error message")
|
||||
}
|
||||
|
||||
cpx, err := enc.Complexity()
|
||||
if err != nil {
|
||||
t.Error("Error getting complexity value", err)
|
||||
}
|
||||
|
||||
// default complexity value is 9
|
||||
if cpx != 9 {
|
||||
t.Errorf("Unexpected complexity value. Got %d, but expected %d",
|
||||
cpx, complexity)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoder_SetGetMaxBandwidth(t *testing.T) {
|
||||
enc, err := NewEncoder(8000, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Errorf("Error creating new encoder: %v", err)
|
||||
}
|
||||
vals := []Bandwidth{
|
||||
Narrowband,
|
||||
Mediumband,
|
||||
Wideband,
|
||||
SuperWideband,
|
||||
Fullband,
|
||||
}
|
||||
for _, maxBw := range vals {
|
||||
err := enc.SetMaxBandwidth(maxBw)
|
||||
if err != nil {
|
||||
t.Error("Error setting max Bandwidth:", err)
|
||||
}
|
||||
maxBwRead, err := enc.MaxBandwidth()
|
||||
if err != nil {
|
||||
t.Error("Error getting max Bandwidth", err)
|
||||
}
|
||||
if maxBwRead != maxBw {
|
||||
t.Errorf("Unexpected max Bandwidth value. Got %d, but expected %d",
|
||||
maxBwRead, maxBw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoder_SetGetInBandFEC(t *testing.T) {
|
||||
enc, err := NewEncoder(8000, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Errorf("Error creating new encoder: %v", err)
|
||||
}
|
||||
|
||||
if err := enc.SetInBandFEC(true); err != nil {
|
||||
t.Error("Error setting fec:", err)
|
||||
}
|
||||
|
||||
fec, err := enc.InBandFEC()
|
||||
if err != nil {
|
||||
t.Error("Error getting fec", err)
|
||||
}
|
||||
if !fec {
|
||||
t.Errorf("Wrong fec value. Expected %t", true)
|
||||
}
|
||||
|
||||
if err := enc.SetInBandFEC(false); err != nil {
|
||||
t.Error("Error setting fec:", err)
|
||||
}
|
||||
|
||||
fec, err = enc.InBandFEC()
|
||||
if err != nil {
|
||||
t.Error("Error getting fec", err)
|
||||
}
|
||||
if fec {
|
||||
t.Errorf("Wrong fec value. Expected %t", false)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoder_SetGetPacketLossPerc(t *testing.T) {
|
||||
enc, err := NewEncoder(8000, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Errorf("Error creating new encoder: %v", err)
|
||||
}
|
||||
vals := []int{0, 5, 10, 20}
|
||||
for _, lossPerc := range vals {
|
||||
err := enc.SetPacketLossPerc(lossPerc)
|
||||
if err != nil {
|
||||
t.Error("Error setting loss percentage value:", err)
|
||||
}
|
||||
lp, err := enc.PacketLossPerc()
|
||||
if err != nil {
|
||||
t.Error("Error getting loss percentage value", err)
|
||||
}
|
||||
if lp != lossPerc {
|
||||
t.Errorf("Unexpected encoder loss percentage value. Got %d, but expected %d",
|
||||
lp, lossPerc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoder_SetGetInvalidPacketLossPerc(t *testing.T) {
|
||||
enc, err := NewEncoder(8000, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Errorf("Error creating new encoder: %v", err)
|
||||
}
|
||||
vals := []int{-1, 101}
|
||||
for _, lossPerc := range vals {
|
||||
err := enc.SetPacketLossPerc(lossPerc)
|
||||
if err == nil {
|
||||
t.Errorf("Expected Error invalid loss percentage: %d", lossPerc)
|
||||
}
|
||||
lp, err := enc.PacketLossPerc()
|
||||
if err != nil {
|
||||
t.Error("Error getting loss percentage value", err)
|
||||
}
|
||||
// default packet loss percentage is 0
|
||||
if lp != 0 {
|
||||
t.Errorf("Unexpected encoder loss percentage value. Got %d, but expected %d",
|
||||
lp, lossPerc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoder_Reset(t *testing.T) {
|
||||
enc, err := NewEncoder(48000, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Errorf("Error creating new encoder: %v", err)
|
||||
}
|
||||
RunTestCodec(t, enc)
|
||||
if err := enc.Reset(); err != nil {
|
||||
t.Errorf("Error reset encoder: %v", err)
|
||||
}
|
||||
RunTestCodec(t, enc)
|
||||
}
|
36
gumble/go-opus/errors.go
Normal file
36
gumble/go-opus/errors.go
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright © Go Opus Authors (see AUTHORS file)
|
||||
//
|
||||
// License for use of this code is detailed in the LICENSE file
|
||||
|
||||
package opus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
/*
|
||||
#cgo pkg-config: opus
|
||||
#include <opus.h>
|
||||
*/
|
||||
import "C"
|
||||
|
||||
type Error int
|
||||
|
||||
var _ error = Error(0)
|
||||
|
||||
// Libopus errors
|
||||
const (
|
||||
ErrOK = Error(C.OPUS_OK)
|
||||
ErrBadArg = Error(C.OPUS_BAD_ARG)
|
||||
ErrBufferTooSmall = Error(C.OPUS_BUFFER_TOO_SMALL)
|
||||
ErrInternalError = Error(C.OPUS_INTERNAL_ERROR)
|
||||
ErrInvalidPacket = Error(C.OPUS_INVALID_PACKET)
|
||||
ErrUnimplemented = Error(C.OPUS_UNIMPLEMENTED)
|
||||
ErrInvalidState = Error(C.OPUS_INVALID_STATE)
|
||||
ErrAllocFail = Error(C.OPUS_ALLOC_FAIL)
|
||||
)
|
||||
|
||||
// Error string (in human readable format) for libopus errors.
|
||||
func (e Error) Error() string {
|
||||
return fmt.Sprintf("opus: %s", C.GoString(C.opus_strerror(C.int(e))))
|
||||
}
|
36
gumble/go-opus/opus.go
Normal file
36
gumble/go-opus/opus.go
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright © Go Opus Authors (see AUTHORS file)
|
||||
//
|
||||
// License for use of this code is detailed in the LICENSE file
|
||||
|
||||
package opus
|
||||
|
||||
/*
|
||||
// Link opus using pkg-config.
|
||||
#cgo pkg-config: opus
|
||||
#include <opus.h>
|
||||
*/
|
||||
import "C"
|
||||
|
||||
type Application int
|
||||
|
||||
const (
|
||||
// Optimize encoding for VoIP
|
||||
AppVoIP = Application(C.OPUS_APPLICATION_VOIP)
|
||||
// Optimize encoding for non-voice signals like music
|
||||
AppAudio = Application(C.OPUS_APPLICATION_AUDIO)
|
||||
// Optimize encoding for low latency applications
|
||||
AppRestrictedLowdelay = Application(C.OPUS_APPLICATION_RESTRICTED_LOWDELAY)
|
||||
)
|
||||
|
||||
const (
|
||||
xMAX_BITRATE = 48000
|
||||
xMAX_FRAME_SIZE_MS = 60
|
||||
xMAX_FRAME_SIZE = xMAX_BITRATE * xMAX_FRAME_SIZE_MS / 1000
|
||||
// Maximum size of an encoded frame. I actually have no idea, but this
|
||||
// looks like it's big enough.
|
||||
maxEncodedFrameSize = 10000
|
||||
)
|
||||
|
||||
func Version() string {
|
||||
return C.GoString(C.opus_get_version_string())
|
||||
}
|
643
gumble/go-opus/opus_test.go
Normal file
643
gumble/go-opus/opus_test.go
Normal file
@ -0,0 +1,643 @@
|
||||
// Copyright © Go Opus Authors (see AUTHORS file)
|
||||
//
|
||||
// License for use of this code is detailed in the LICENSE file
|
||||
|
||||
package opus
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestVersion(t *testing.T) {
|
||||
if ver := Version(); !strings.HasPrefix(ver, "libopus") {
|
||||
t.Errorf("Unexpected linked libopus version: " + ver)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpusErrstr(t *testing.T) {
|
||||
// I scooped this -1 up from opus_defines.h, it's OPUS_BAD_ARG. Not pretty,
|
||||
// but it's better than not testing at all. Again, accessing #defines from
|
||||
// CGO is not possible.
|
||||
if ErrBadArg.Error() != "opus: invalid argument" {
|
||||
t.Errorf("Expected \"invalid argument\" error message for error code %d: %v",
|
||||
ErrBadArg, ErrBadArg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodec(t *testing.T) {
|
||||
const SAMPLE_RATE = 48000
|
||||
enc, err := NewEncoder(SAMPLE_RATE, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Fatalf("Error creating new encoder: %v", err)
|
||||
}
|
||||
RunTestCodec(t, enc)
|
||||
}
|
||||
|
||||
func RunTestCodec(t *testing.T, enc *Encoder) {
|
||||
// Create bogus input sound
|
||||
const G4 = 391.995
|
||||
const SAMPLE_RATE = 48000
|
||||
const FRAME_SIZE_MS = 60
|
||||
const FRAME_SIZE = SAMPLE_RATE * FRAME_SIZE_MS / 1000
|
||||
pcm := make([]int16, FRAME_SIZE)
|
||||
addSine(pcm, SAMPLE_RATE, G4)
|
||||
data := make([]byte, 1000)
|
||||
n, err := enc.Encode(pcm, data)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't encode data: %v", err)
|
||||
}
|
||||
data = data[:n]
|
||||
dec, err := NewDecoder(SAMPLE_RATE, 1)
|
||||
if err != nil || dec == nil {
|
||||
t.Fatalf("Error creating new decoder: %v", err)
|
||||
}
|
||||
n, err = dec.Decode(data, pcm)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't decode data: %v", err)
|
||||
}
|
||||
if len(pcm) != n {
|
||||
t.Fatalf("Length mismatch: %d samples in, %d out", len(pcm), n)
|
||||
}
|
||||
// Checking the output programmatically is seriously not easy. I checked it
|
||||
// by hand and by ear, it looks fine. I'll just assume the API faithfully
|
||||
// passes error codes through, and that's that.
|
||||
}
|
||||
|
||||
func TestCodecFloat32(t *testing.T) {
|
||||
// Create bogus input sound
|
||||
const G4 = 391.995
|
||||
const SAMPLE_RATE = 48000
|
||||
const FRAME_SIZE_MS = 60
|
||||
const FRAME_SIZE = SAMPLE_RATE * FRAME_SIZE_MS / 1000
|
||||
pcm := make([]float32, FRAME_SIZE)
|
||||
enc, err := NewEncoder(SAMPLE_RATE, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Fatalf("Error creating new encoder: %v", err)
|
||||
}
|
||||
addSineFloat32(pcm, SAMPLE_RATE, G4)
|
||||
data := make([]byte, 1000)
|
||||
n, err := enc.EncodeFloat32(pcm, data)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't encode data: %v", err)
|
||||
}
|
||||
dec, err := NewDecoder(SAMPLE_RATE, 1)
|
||||
if err != nil || dec == nil {
|
||||
t.Fatalf("Error creating new decoder: %v", err)
|
||||
}
|
||||
// TODO: Uh-oh.. it looks like I forgot to put a data = data[:n] here, yet
|
||||
// the test is not failing. Why?
|
||||
n, err = dec.DecodeFloat32(data, pcm)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't decode data: %v", err)
|
||||
}
|
||||
if len(pcm) != n {
|
||||
t.Fatalf("Length mismatch: %d samples in, %d out", len(pcm), n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodecFEC(t *testing.T) {
|
||||
// Create bogus input sound
|
||||
const G4 = 391.995
|
||||
const SAMPLE_RATE = 48000
|
||||
const FRAME_SIZE_MS = 10
|
||||
const FRAME_SIZE = SAMPLE_RATE * FRAME_SIZE_MS / 1000
|
||||
const NUMBER_OF_FRAMES = 6
|
||||
pcm := make([]int16, 0)
|
||||
|
||||
enc, err := NewEncoder(SAMPLE_RATE, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Fatalf("Error creating new encoder: %v", err)
|
||||
}
|
||||
enc.SetPacketLossPerc(30)
|
||||
enc.SetInBandFEC(true)
|
||||
|
||||
dec, err := NewDecoder(SAMPLE_RATE, 1)
|
||||
if err != nil || dec == nil {
|
||||
t.Fatalf("Error creating new decoder: %v", err)
|
||||
}
|
||||
|
||||
mono := make([]int16, FRAME_SIZE*NUMBER_OF_FRAMES)
|
||||
addSine(mono, SAMPLE_RATE, G4)
|
||||
|
||||
encodedData := make([][]byte, NUMBER_OF_FRAMES)
|
||||
for i, idx := 0, 0; i < len(mono); i += FRAME_SIZE {
|
||||
data := make([]byte, 1000)
|
||||
n, err := enc.Encode(mono[i:i+FRAME_SIZE], data)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't encode data: %v", err)
|
||||
}
|
||||
data = data[:n]
|
||||
encodedData[idx] = data
|
||||
idx++
|
||||
}
|
||||
|
||||
lost := false
|
||||
for i := 0; i < len(encodedData); i++ {
|
||||
// "Dropping" packets 2 and 4
|
||||
if i == 2 || i == 4 {
|
||||
lost = true
|
||||
continue
|
||||
}
|
||||
if lost {
|
||||
samples, err := dec.LastPacketDuration()
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't get last packet duration: %v", err)
|
||||
}
|
||||
|
||||
pcmBuffer := make([]int16, samples)
|
||||
if err = dec.DecodeFEC(encodedData[i], pcmBuffer); err != nil {
|
||||
t.Fatalf("Couldn't decode data: %v", err)
|
||||
}
|
||||
pcm = append(pcm, pcmBuffer...)
|
||||
lost = false
|
||||
}
|
||||
|
||||
pcmBuffer := make([]int16, NUMBER_OF_FRAMES*FRAME_SIZE)
|
||||
n, err := dec.Decode(encodedData[i], pcmBuffer)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't decode data: %v", err)
|
||||
}
|
||||
pcmBuffer = pcmBuffer[:n]
|
||||
if n != FRAME_SIZE {
|
||||
t.Fatalf("Length mismatch: %d samples expected, %d out", FRAME_SIZE, n)
|
||||
}
|
||||
pcm = append(pcm, pcmBuffer...)
|
||||
}
|
||||
|
||||
if len(mono) != len(pcm) {
|
||||
t.Fatalf("Input/Output length mismatch: %d samples in, %d out", len(mono), len(pcm))
|
||||
}
|
||||
|
||||
// Commented out for the same reason as in TestStereo
|
||||
/*
|
||||
fmt.Printf("in,out\n")
|
||||
for i := range mono {
|
||||
fmt.Printf("%d,%d\n", mono[i], pcm[i])
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
func TestCodecFECFloat32(t *testing.T) {
|
||||
// Create bogus input sound
|
||||
const G4 = 391.995
|
||||
const SAMPLE_RATE = 48000
|
||||
const FRAME_SIZE_MS = 10
|
||||
const FRAME_SIZE = SAMPLE_RATE * FRAME_SIZE_MS / 1000
|
||||
const NUMBER_OF_FRAMES = 6
|
||||
pcm := make([]float32, 0)
|
||||
|
||||
enc, err := NewEncoder(SAMPLE_RATE, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Fatalf("Error creating new encoder: %v", err)
|
||||
}
|
||||
enc.SetPacketLossPerc(30)
|
||||
enc.SetInBandFEC(true)
|
||||
|
||||
dec, err := NewDecoder(SAMPLE_RATE, 1)
|
||||
if err != nil || dec == nil {
|
||||
t.Fatalf("Error creating new decoder: %v", err)
|
||||
}
|
||||
|
||||
mono := make([]float32, FRAME_SIZE*NUMBER_OF_FRAMES)
|
||||
addSineFloat32(mono, SAMPLE_RATE, G4)
|
||||
|
||||
encodedData := make([][]byte, NUMBER_OF_FRAMES)
|
||||
for i, idx := 0, 0; i < len(mono); i += FRAME_SIZE {
|
||||
data := make([]byte, 1000)
|
||||
n, err := enc.EncodeFloat32(mono[i:i+FRAME_SIZE], data)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't encode data: %v", err)
|
||||
}
|
||||
data = data[:n]
|
||||
encodedData[idx] = data
|
||||
idx++
|
||||
}
|
||||
|
||||
lost := false
|
||||
for i := 0; i < len(encodedData); i++ {
|
||||
// "Dropping" packets 2 and 4
|
||||
if i == 2 || i == 4 {
|
||||
lost = true
|
||||
continue
|
||||
}
|
||||
if lost {
|
||||
samples, err := dec.LastPacketDuration()
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't get last packet duration: %v", err)
|
||||
}
|
||||
|
||||
pcmBuffer := make([]float32, samples)
|
||||
if err = dec.DecodeFECFloat32(encodedData[i], pcmBuffer); err != nil {
|
||||
t.Fatalf("Couldn't decode data: %v", err)
|
||||
}
|
||||
pcm = append(pcm, pcmBuffer...)
|
||||
lost = false
|
||||
}
|
||||
|
||||
pcmBuffer := make([]float32, NUMBER_OF_FRAMES*FRAME_SIZE)
|
||||
n, err := dec.DecodeFloat32(encodedData[i], pcmBuffer)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't decode data: %v", err)
|
||||
}
|
||||
pcmBuffer = pcmBuffer[:n]
|
||||
if n != FRAME_SIZE {
|
||||
t.Fatalf("Length mismatch: %d samples expected, %d out", FRAME_SIZE, n)
|
||||
}
|
||||
pcm = append(pcm, pcmBuffer...)
|
||||
}
|
||||
|
||||
if len(mono) != len(pcm) {
|
||||
t.Fatalf("Input/Output length mismatch: %d samples in, %d out", len(mono), len(pcm))
|
||||
}
|
||||
|
||||
// Commented out for the same reason as in TestStereo
|
||||
/*
|
||||
fmt.Printf("in,out\n")
|
||||
for i := 0; i < len(mono); i++ {
|
||||
fmt.Printf("%f,%f\n", mono[i], pcm[i])
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
func TestCodecPLC(t *testing.T) {
|
||||
// Create bogus input sound
|
||||
const G4 = 391.995
|
||||
const SAMPLE_RATE = 48000
|
||||
const FRAME_SIZE_MS = 10
|
||||
const FRAME_SIZE = SAMPLE_RATE * FRAME_SIZE_MS / 1000
|
||||
const NUMBER_OF_FRAMES = 6
|
||||
pcm := make([]int16, 0)
|
||||
|
||||
enc, err := NewEncoder(SAMPLE_RATE, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Fatalf("Error creating new encoder: %v", err)
|
||||
}
|
||||
|
||||
dec, err := NewDecoder(SAMPLE_RATE, 1)
|
||||
if err != nil || dec == nil {
|
||||
t.Fatalf("Error creating new decoder: %v", err)
|
||||
}
|
||||
|
||||
mono := make([]int16, FRAME_SIZE*NUMBER_OF_FRAMES)
|
||||
addSine(mono, SAMPLE_RATE, G4)
|
||||
|
||||
encodedData := make([][]byte, NUMBER_OF_FRAMES)
|
||||
for i, idx := 0, 0; i < len(mono); i += FRAME_SIZE {
|
||||
data := make([]byte, 1000)
|
||||
n, err := enc.Encode(mono[i:i+FRAME_SIZE], data)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't encode data: %v", err)
|
||||
}
|
||||
data = data[:n]
|
||||
encodedData[idx] = data
|
||||
idx++
|
||||
}
|
||||
|
||||
lost := false
|
||||
for i := 0; i < len(encodedData); i++ {
|
||||
// "Dropping" packets 2 and 4
|
||||
if i == 2 || i == 4 {
|
||||
lost = true
|
||||
continue
|
||||
}
|
||||
if lost {
|
||||
samples, err := dec.LastPacketDuration()
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't get last packet duration: %v", err)
|
||||
}
|
||||
|
||||
pcmBuffer := make([]int16, samples)
|
||||
if err = dec.DecodePLC(pcmBuffer); err != nil {
|
||||
t.Fatalf("Couldn't decode data: %v", err)
|
||||
}
|
||||
nonZero := 0
|
||||
for n := range pcmBuffer {
|
||||
if pcmBuffer[n] != 0 {
|
||||
nonZero++
|
||||
}
|
||||
}
|
||||
if nonZero == 0 {
|
||||
t.Fatalf("DecodePLC produced only zero samples")
|
||||
}
|
||||
pcm = append(pcm, pcmBuffer...)
|
||||
lost = false
|
||||
}
|
||||
|
||||
pcmBuffer := make([]int16, NUMBER_OF_FRAMES*FRAME_SIZE)
|
||||
n, err := dec.Decode(encodedData[i], pcmBuffer)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't decode data: %v", err)
|
||||
}
|
||||
pcmBuffer = pcmBuffer[:n]
|
||||
if n != FRAME_SIZE {
|
||||
t.Fatalf("Length mismatch: %d samples expected, %d out", FRAME_SIZE, n)
|
||||
}
|
||||
pcm = append(pcm, pcmBuffer...)
|
||||
}
|
||||
|
||||
if len(mono) != len(pcm) {
|
||||
t.Fatalf("Input/Output length mismatch: %d samples in, %d out", len(mono), len(pcm))
|
||||
}
|
||||
|
||||
// Commented out for the same reason as in TestStereo
|
||||
/*
|
||||
fmt.Printf("in,out\n")
|
||||
for i := range mono {
|
||||
fmt.Printf("%d,%d\n", mono[i], pcm[i])
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
func TestCodecPLCFloat32(t *testing.T) {
|
||||
// Create bogus input sound
|
||||
const G4 = 391.995
|
||||
const SAMPLE_RATE = 48000
|
||||
const FRAME_SIZE_MS = 10
|
||||
const FRAME_SIZE = SAMPLE_RATE * FRAME_SIZE_MS / 1000
|
||||
const NUMBER_OF_FRAMES = 6
|
||||
pcm := make([]float32, 0)
|
||||
|
||||
enc, err := NewEncoder(SAMPLE_RATE, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Fatalf("Error creating new encoder: %v", err)
|
||||
}
|
||||
|
||||
dec, err := NewDecoder(SAMPLE_RATE, 1)
|
||||
if err != nil || dec == nil {
|
||||
t.Fatalf("Error creating new decoder: %v", err)
|
||||
}
|
||||
|
||||
mono := make([]float32, FRAME_SIZE*NUMBER_OF_FRAMES)
|
||||
addSineFloat32(mono, SAMPLE_RATE, G4)
|
||||
|
||||
encodedData := make([][]byte, NUMBER_OF_FRAMES)
|
||||
for i, idx := 0, 0; i < len(mono); i += FRAME_SIZE {
|
||||
data := make([]byte, 1000)
|
||||
n, err := enc.EncodeFloat32(mono[i:i+FRAME_SIZE], data)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't encode data: %v", err)
|
||||
}
|
||||
data = data[:n]
|
||||
encodedData[idx] = data
|
||||
idx++
|
||||
}
|
||||
|
||||
lost := false
|
||||
for i := 0; i < len(encodedData); i++ {
|
||||
// "Dropping" packets 2 and 4
|
||||
if i == 2 || i == 4 {
|
||||
lost = true
|
||||
continue
|
||||
}
|
||||
if lost {
|
||||
samples, err := dec.LastPacketDuration()
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't get last packet duration: %v", err)
|
||||
}
|
||||
|
||||
pcmBuffer := make([]float32, samples)
|
||||
if err = dec.DecodePLCFloat32(pcmBuffer); err != nil {
|
||||
t.Fatalf("Couldn't decode data: %v", err)
|
||||
}
|
||||
nonZero := 0
|
||||
for n := range pcmBuffer {
|
||||
if pcmBuffer[n] != 0.0 {
|
||||
nonZero++
|
||||
}
|
||||
}
|
||||
if nonZero == 0 {
|
||||
t.Fatalf("DecodePLC produced only zero samples")
|
||||
}
|
||||
pcm = append(pcm, pcmBuffer...)
|
||||
lost = false
|
||||
}
|
||||
|
||||
pcmBuffer := make([]float32, NUMBER_OF_FRAMES*FRAME_SIZE)
|
||||
n, err := dec.DecodeFloat32(encodedData[i], pcmBuffer)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't decode data: %v", err)
|
||||
}
|
||||
pcmBuffer = pcmBuffer[:n]
|
||||
if n != FRAME_SIZE {
|
||||
t.Fatalf("Length mismatch: %d samples expected, %d out", FRAME_SIZE, n)
|
||||
}
|
||||
pcm = append(pcm, pcmBuffer...)
|
||||
}
|
||||
|
||||
if len(mono) != len(pcm) {
|
||||
t.Fatalf("Input/Output length mismatch: %d samples in, %d out", len(mono), len(pcm))
|
||||
}
|
||||
|
||||
// Commented out for the same reason as in TestStereo
|
||||
/*
|
||||
fmt.Printf("in,out\n")
|
||||
for i := 0; i < len(mono); i++ {
|
||||
fmt.Printf("%f,%f\n", mono[i], pcm[i])
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
func TestStereo(t *testing.T) {
|
||||
// Create bogus input sound
|
||||
const G4 = 391.995
|
||||
const E3 = 164.814
|
||||
const SAMPLE_RATE = 48000
|
||||
const FRAME_SIZE_MS = 60
|
||||
const CHANNELS = 2
|
||||
const FRAME_SIZE_MONO = SAMPLE_RATE * FRAME_SIZE_MS / 1000
|
||||
|
||||
enc, err := NewEncoder(SAMPLE_RATE, CHANNELS, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Fatalf("Error creating new encoder: %v", err)
|
||||
}
|
||||
dec, err := NewDecoder(SAMPLE_RATE, CHANNELS)
|
||||
if err != nil || dec == nil {
|
||||
t.Fatalf("Error creating new decoder: %v", err)
|
||||
}
|
||||
|
||||
// Source signal (left G4, right E3)
|
||||
left := make([]int16, FRAME_SIZE_MONO)
|
||||
right := make([]int16, FRAME_SIZE_MONO)
|
||||
addSine(left, SAMPLE_RATE, G4)
|
||||
addSine(right, SAMPLE_RATE, E3)
|
||||
pcm := interleave(left, right)
|
||||
|
||||
data := make([]byte, 1000)
|
||||
n, err := enc.Encode(pcm, data)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't encode data: %v", err)
|
||||
}
|
||||
data = data[:n]
|
||||
n, err = dec.Decode(data, pcm)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't decode data: %v", err)
|
||||
}
|
||||
if n*CHANNELS != len(pcm) {
|
||||
t.Fatalf("Length mismatch: %d samples in, %d out", len(pcm), n*CHANNELS)
|
||||
}
|
||||
|
||||
// This is hard to check programatically, but I plotted the graphs in a
|
||||
// spreadsheet and it looked great. The encoded waves both start out with a
|
||||
// string of zero samples before they pick up, and the G4 is phase shifted
|
||||
// by half a period, but that's all fine for lossy audio encoding.
|
||||
/*
|
||||
leftdec, rightdec := split(pcm)
|
||||
fmt.Printf("left_in,left_out,right_in,right_out\n")
|
||||
for i := range left {
|
||||
fmt.Printf("%d,%d,%d,%d\n", left[i], leftdec[i], right[i], rightdec[i])
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
func TestBufferSize(t *testing.T) {
|
||||
const G4 = 391.995
|
||||
const SAMPLE_RATE = 48000
|
||||
const FRAME_SIZE_MS = 60
|
||||
const FRAME_SIZE = SAMPLE_RATE * FRAME_SIZE_MS / 1000
|
||||
const GUARD_SIZE = 100
|
||||
|
||||
checkGuardInt16 := func(t *testing.T, s []int16) {
|
||||
for n := range s {
|
||||
if s[n] != 0 {
|
||||
t.Fatal("Memory corruption detected")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
checkGuardFloat32 := func(t *testing.T, s []float32) {
|
||||
for n := range s {
|
||||
if s[n] != 0 {
|
||||
t.Fatal("Memory corruption detected")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
checkResult := func(t *testing.T, n int, err error, expectOK bool) {
|
||||
if expectOK {
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't decode data: %v", err)
|
||||
}
|
||||
if n != FRAME_SIZE {
|
||||
t.Fatalf("Length mismatch: %d samples in, %d out", FRAME_SIZE, n)
|
||||
}
|
||||
} else {
|
||||
if err == nil {
|
||||
t.Fatalf("Expected decode failure, but it succeeded")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
encodeFrame := func(t *testing.T) []byte {
|
||||
pcm := make([]int16, FRAME_SIZE)
|
||||
enc, err := NewEncoder(SAMPLE_RATE, 1, AppVoIP)
|
||||
if err != nil || enc == nil {
|
||||
t.Fatalf("Error creating new encoder: %v", err)
|
||||
}
|
||||
addSine(pcm, SAMPLE_RATE, G4)
|
||||
data := make([]byte, 1000)
|
||||
n, err := enc.Encode(pcm, data)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't encode data: %v", err)
|
||||
}
|
||||
return data[:n]
|
||||
}
|
||||
|
||||
createDecoder := func(t *testing.T) *Decoder {
|
||||
dec, err := NewDecoder(SAMPLE_RATE, 1)
|
||||
if err != nil || dec == nil {
|
||||
t.Fatalf("Error creating new decoder: %v", err)
|
||||
}
|
||||
return dec
|
||||
}
|
||||
|
||||
decodeInt16 := func(t *testing.T, data []byte, decodeSize int, expectOK bool) {
|
||||
dec := createDecoder(t)
|
||||
decodedMem := make([]int16, decodeSize+GUARD_SIZE*2)
|
||||
decodedRef := decodedMem[GUARD_SIZE : GUARD_SIZE+decodeSize : GUARD_SIZE+decodeSize]
|
||||
n, err := dec.Decode(data, decodedRef)
|
||||
checkGuardInt16(t, decodedMem[:GUARD_SIZE])
|
||||
checkGuardInt16(t, decodedMem[decodeSize+GUARD_SIZE:])
|
||||
checkResult(t, n, err, expectOK)
|
||||
}
|
||||
|
||||
decodeFloat32 := func(t *testing.T, data []byte, decodeSize int, expectOK bool) {
|
||||
dec := createDecoder(t)
|
||||
decodedMem := make([]float32, decodeSize+GUARD_SIZE*2)
|
||||
decodedRef := decodedMem[GUARD_SIZE : GUARD_SIZE+decodeSize : GUARD_SIZE+decodeSize]
|
||||
n, err := dec.DecodeFloat32(data, decodedRef)
|
||||
checkGuardFloat32(t, decodedMem[:GUARD_SIZE])
|
||||
checkGuardFloat32(t, decodedMem[decodeSize+GUARD_SIZE:])
|
||||
checkResult(t, n, err, expectOK)
|
||||
}
|
||||
|
||||
decodeFecInt16 := func(t *testing.T, data []byte, decodeSize int, expectOK bool) {
|
||||
dec := createDecoder(t)
|
||||
decodedMem := make([]int16, decodeSize+GUARD_SIZE*2)
|
||||
decodedRef := decodedMem[GUARD_SIZE : GUARD_SIZE+decodeSize : GUARD_SIZE+decodeSize]
|
||||
err := dec.DecodeFEC(data, decodedRef)
|
||||
checkGuardInt16(t, decodedMem[:GUARD_SIZE])
|
||||
checkGuardInt16(t, decodedMem[decodeSize+GUARD_SIZE:])
|
||||
checkResult(t, FRAME_SIZE, err, expectOK)
|
||||
}
|
||||
|
||||
decodeFecFloat32 := func(t *testing.T, data []byte, decodeSize int, expectOK bool) {
|
||||
dec := createDecoder(t)
|
||||
decodedMem := make([]float32, decodeSize+GUARD_SIZE*2)
|
||||
decodedRef := decodedMem[GUARD_SIZE : GUARD_SIZE+decodeSize : GUARD_SIZE+decodeSize]
|
||||
err := dec.DecodeFECFloat32(data, decodedRef)
|
||||
checkGuardFloat32(t, decodedMem[:GUARD_SIZE])
|
||||
checkGuardFloat32(t, decodedMem[decodeSize+GUARD_SIZE:])
|
||||
checkResult(t, FRAME_SIZE, err, expectOK)
|
||||
}
|
||||
|
||||
t.Run("smaller-buffer-int16", func(t *testing.T) {
|
||||
decodeInt16(t, encodeFrame(t), FRAME_SIZE-1, false)
|
||||
})
|
||||
|
||||
t.Run("smaller-buffer-float32", func(t *testing.T) {
|
||||
decodeFloat32(t, encodeFrame(t), FRAME_SIZE-1, false)
|
||||
})
|
||||
|
||||
t.Run("smaller-buffer-int16-fec", func(t *testing.T) {
|
||||
decodeFecFloat32(t, encodeFrame(t), FRAME_SIZE-1, false)
|
||||
})
|
||||
|
||||
t.Run("smaller-buffer-float32-fec", func(t *testing.T) {
|
||||
decodeFecFloat32(t, encodeFrame(t), FRAME_SIZE-1, false)
|
||||
})
|
||||
|
||||
t.Run("exact-buffer-int16", func(t *testing.T) {
|
||||
decodeInt16(t, encodeFrame(t), FRAME_SIZE, true)
|
||||
})
|
||||
|
||||
t.Run("exact-buffer-float32", func(t *testing.T) {
|
||||
decodeFloat32(t, encodeFrame(t), FRAME_SIZE, true)
|
||||
})
|
||||
|
||||
t.Run("exact-buffer-int16-fec", func(t *testing.T) {
|
||||
decodeFecInt16(t, encodeFrame(t), FRAME_SIZE, true)
|
||||
})
|
||||
|
||||
t.Run("exact-buffer-float32-fec", func(t *testing.T) {
|
||||
decodeFecFloat32(t, encodeFrame(t), FRAME_SIZE, true)
|
||||
})
|
||||
|
||||
t.Run("larger-buffer-int16", func(t *testing.T) {
|
||||
decodeInt16(t, encodeFrame(t), FRAME_SIZE+1, true)
|
||||
})
|
||||
|
||||
t.Run("larger-buffer-float32", func(t *testing.T) {
|
||||
decodeFloat32(t, encodeFrame(t), FRAME_SIZE+1, true)
|
||||
})
|
||||
|
||||
t.Run("larger-buffer-int16-fec", func(t *testing.T) {
|
||||
decodeFecInt16(t, encodeFrame(t), FRAME_SIZE+1, false)
|
||||
})
|
||||
|
||||
t.Run("larger-buffer-float32-fec", func(t *testing.T) {
|
||||
decodeFecFloat32(t, encodeFrame(t), FRAME_SIZE+1, false)
|
||||
})
|
||||
}
|
183
gumble/go-opus/stream.go
Normal file
183
gumble/go-opus/stream.go
Normal file
@ -0,0 +1,183 @@
|
||||
// Copyright © Go Opus Authors (see AUTHORS file)
|
||||
//
|
||||
// License for use of this code is detailed in the LICENSE file
|
||||
|
||||
// +build !nolibopusfile
|
||||
|
||||
package opus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
/*
|
||||
#cgo pkg-config: opusfile
|
||||
#include <opusfile.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
OggOpusFile *my_open_callbacks(uintptr_t p, int *error);
|
||||
|
||||
*/
|
||||
import "C"
|
||||
|
||||
// Stream wraps a io.Reader in a decoding layer. It provides an API similar to
|
||||
// io.Reader, but it provides raw PCM data instead of the encoded Opus data.
|
||||
//
|
||||
// This is not the same as directly decoding the bytes on the io.Reader; opus
|
||||
// streams are Ogg Opus audio streams, which package raw Opus data.
|
||||
//
|
||||
// This wraps libopusfile. For more information, see the api docs on xiph.org:
|
||||
//
|
||||
// https://www.opus-codec.org/docs/opusfile_api-0.7/index.html
|
||||
type Stream struct {
|
||||
id uintptr
|
||||
oggfile *C.OggOpusFile
|
||||
read io.Reader
|
||||
// Preallocated buffer to pass to the reader
|
||||
buf []byte
|
||||
}
|
||||
|
||||
var streams = newStreamsMap()
|
||||
|
||||
//export go_readcallback
|
||||
func go_readcallback(p unsafe.Pointer, cbuf *C.uchar, cmaxbytes C.int) C.int {
|
||||
streamId := uintptr(p)
|
||||
stream := streams.Get(streamId)
|
||||
if stream == nil {
|
||||
// This is bad
|
||||
return -1
|
||||
}
|
||||
|
||||
maxbytes := int(cmaxbytes)
|
||||
if maxbytes > cap(stream.buf) {
|
||||
maxbytes = cap(stream.buf)
|
||||
}
|
||||
// Don't bother cleaning up old data because that's not required by the
|
||||
// io.Reader API.
|
||||
n, err := stream.read.Read(stream.buf[:maxbytes])
|
||||
// Go allows returning non-nil error (like EOF) and n>0, libopusfile doesn't
|
||||
// expect that. So return n first to indicate the valid bytes, let the
|
||||
// subsequent call (which will be n=0, same-error) handle the actual error.
|
||||
if n == 0 && err != nil {
|
||||
if err == io.EOF {
|
||||
return 0
|
||||
} else {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
C.memcpy(unsafe.Pointer(cbuf), unsafe.Pointer(&stream.buf[0]), C.size_t(n))
|
||||
return C.int(n)
|
||||
}
|
||||
|
||||
// NewStream creates and initializes a new stream. Don't call .Init() on this.
|
||||
func NewStream(read io.Reader) (*Stream, error) {
|
||||
var s Stream
|
||||
err := s.Init(read)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
// Init initializes a stream with an io.Reader to fetch opus encoded data from
|
||||
// on demand. Errors from the reader are all transformed to an EOF, any actual
|
||||
// error information is lost. The same happens when a read returns succesfully,
|
||||
// but with zero bytes.
|
||||
func (s *Stream) Init(read io.Reader) error {
|
||||
if s.oggfile != nil {
|
||||
return fmt.Errorf("opus stream is already initialized")
|
||||
}
|
||||
if read == nil {
|
||||
return fmt.Errorf("Reader must be non-nil")
|
||||
}
|
||||
|
||||
s.read = read
|
||||
s.buf = make([]byte, maxEncodedFrameSize)
|
||||
s.id = streams.NextId()
|
||||
var errno C.int
|
||||
|
||||
// Immediately delete the stream after .Init to avoid leaking if the
|
||||
// caller forgets to (/ doesn't want to) call .Close(). No need for that,
|
||||
// since the callback is only ever called during a .Read operation; just
|
||||
// Save and Delete from the map around that every time a reader function is
|
||||
// called.
|
||||
streams.Save(s)
|
||||
defer streams.Del(s)
|
||||
oggfile := C.my_open_callbacks(C.uintptr_t(s.id), &errno)
|
||||
if errno != 0 {
|
||||
return StreamError(errno)
|
||||
}
|
||||
s.oggfile = oggfile
|
||||
return nil
|
||||
}
|
||||
|
||||
// Read a chunk of raw opus data from the stream and decode it. Returns the
|
||||
// number of decoded samples per channel. This means that a dual channel
|
||||
// (stereo) feed will have twice as many samples as the value returned.
|
||||
//
|
||||
// Read may successfully read less bytes than requested, but it will never read
|
||||
// exactly zero bytes succesfully if a non-zero buffer is supplied.
|
||||
//
|
||||
// The number of channels in the output data must be known in advance. It is
|
||||
// possible to extract this information from the stream itself, but I'm not
|
||||
// motivated to do that. Feel free to send a pull request.
|
||||
func (s *Stream) Read(pcm []int16) (int, error) {
|
||||
if s.oggfile == nil {
|
||||
return 0, fmt.Errorf("opus stream is uninitialized or already closed")
|
||||
}
|
||||
if len(pcm) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
streams.Save(s)
|
||||
defer streams.Del(s)
|
||||
n := C.op_read(
|
||||
s.oggfile,
|
||||
(*C.opus_int16)(&pcm[0]),
|
||||
C.int(len(pcm)),
|
||||
nil)
|
||||
if n < 0 {
|
||||
return 0, StreamError(n)
|
||||
}
|
||||
if n == 0 {
|
||||
return 0, io.EOF
|
||||
}
|
||||
return int(n), nil
|
||||
}
|
||||
|
||||
// ReadFloat32 is the same as Read, but decodes to float32 instead of int16.
|
||||
func (s *Stream) ReadFloat32(pcm []float32) (int, error) {
|
||||
if s.oggfile == nil {
|
||||
return 0, fmt.Errorf("opus stream is uninitialized or already closed")
|
||||
}
|
||||
if len(pcm) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
streams.Save(s)
|
||||
defer streams.Del(s)
|
||||
n := C.op_read_float(
|
||||
s.oggfile,
|
||||
(*C.float)(&pcm[0]),
|
||||
C.int(len(pcm)),
|
||||
nil)
|
||||
if n < 0 {
|
||||
return 0, StreamError(n)
|
||||
}
|
||||
if n == 0 {
|
||||
return 0, io.EOF
|
||||
}
|
||||
return int(n), nil
|
||||
}
|
||||
|
||||
func (s *Stream) Close() error {
|
||||
if s.oggfile == nil {
|
||||
return fmt.Errorf("opus stream is uninitialized or already closed")
|
||||
}
|
||||
C.op_free(s.oggfile)
|
||||
if closer, ok := s.read.(io.Closer); ok {
|
||||
return closer.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
75
gumble/go-opus/stream_errors.go
Normal file
75
gumble/go-opus/stream_errors.go
Normal file
@ -0,0 +1,75 @@
|
||||
// Copyright © 2015-2017 Go Opus Authors (see AUTHORS file)
|
||||
//
|
||||
// License for use of this code is detailed in the LICENSE file
|
||||
|
||||
// +build !nolibopusfile
|
||||
|
||||
package opus
|
||||
|
||||
/*
|
||||
#cgo pkg-config: opusfile
|
||||
#include <opusfile.h>
|
||||
*/
|
||||
import "C"
|
||||
|
||||
// StreamError represents an error from libopusfile.
|
||||
type StreamError int
|
||||
|
||||
var _ error = StreamError(0)
|
||||
|
||||
// Libopusfile errors. The names are copied verbatim from the libopusfile
|
||||
// library.
|
||||
const (
|
||||
ErrStreamFalse = StreamError(C.OP_FALSE)
|
||||
ErrStreamEOF = StreamError(C.OP_EOF)
|
||||
ErrStreamHole = StreamError(C.OP_HOLE)
|
||||
ErrStreamRead = StreamError(C.OP_EREAD)
|
||||
ErrStreamFault = StreamError(C.OP_EFAULT)
|
||||
ErrStreamImpl = StreamError(C.OP_EIMPL)
|
||||
ErrStreamInval = StreamError(C.OP_EINVAL)
|
||||
ErrStreamNotFormat = StreamError(C.OP_ENOTFORMAT)
|
||||
ErrStreamBadHeader = StreamError(C.OP_EBADHEADER)
|
||||
ErrStreamVersion = StreamError(C.OP_EVERSION)
|
||||
ErrStreamNotAudio = StreamError(C.OP_ENOTAUDIO)
|
||||
ErrStreamBadPacked = StreamError(C.OP_EBADPACKET)
|
||||
ErrStreamBadLink = StreamError(C.OP_EBADLINK)
|
||||
ErrStreamNoSeek = StreamError(C.OP_ENOSEEK)
|
||||
ErrStreamBadTimestamp = StreamError(C.OP_EBADTIMESTAMP)
|
||||
)
|
||||
|
||||
func (i StreamError) Error() string {
|
||||
switch i {
|
||||
case ErrStreamFalse:
|
||||
return "OP_FALSE"
|
||||
case ErrStreamEOF:
|
||||
return "OP_EOF"
|
||||
case ErrStreamHole:
|
||||
return "OP_HOLE"
|
||||
case ErrStreamRead:
|
||||
return "OP_EREAD"
|
||||
case ErrStreamFault:
|
||||
return "OP_EFAULT"
|
||||
case ErrStreamImpl:
|
||||
return "OP_EIMPL"
|
||||
case ErrStreamInval:
|
||||
return "OP_EINVAL"
|
||||
case ErrStreamNotFormat:
|
||||
return "OP_ENOTFORMAT"
|
||||
case ErrStreamBadHeader:
|
||||
return "OP_EBADHEADER"
|
||||
case ErrStreamVersion:
|
||||
return "OP_EVERSION"
|
||||
case ErrStreamNotAudio:
|
||||
return "OP_ENOTAUDIO"
|
||||
case ErrStreamBadPacked:
|
||||
return "OP_EBADPACKET"
|
||||
case ErrStreamBadLink:
|
||||
return "OP_EBADLINK"
|
||||
case ErrStreamNoSeek:
|
||||
return "OP_ENOSEEK"
|
||||
case ErrStreamBadTimestamp:
|
||||
return "OP_EBADTIMESTAMP"
|
||||
default:
|
||||
return "libopusfile error: %d (unknown code)"
|
||||
}
|
||||
}
|
134
gumble/go-opus/stream_test.go
Normal file
134
gumble/go-opus/stream_test.go
Normal file
@ -0,0 +1,134 @@
|
||||
// Copyright © Go Opus Authors (see AUTHORS file)
|
||||
//
|
||||
// License for use of this code is detailed in the LICENSE file
|
||||
|
||||
// +build !nolibopusfile
|
||||
|
||||
package opus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStreamIllegal(t *testing.T) {
|
||||
// Simple testing, no actual decoding
|
||||
reader := strings.NewReader("hello test test this is not a legal Opus stream")
|
||||
_, err := NewStream(reader)
|
||||
if err == nil {
|
||||
t.Fatalf("Expected error while initializing illegal opus stream")
|
||||
}
|
||||
}
|
||||
|
||||
func readStreamPcm(t *testing.T, stream *Stream, buffersize int) []int16 {
|
||||
var pcm []int16
|
||||
pcmbuf := make([]int16, buffersize)
|
||||
for {
|
||||
n, err := stream.Read(pcmbuf)
|
||||
switch err {
|
||||
case io.EOF:
|
||||
return pcm
|
||||
case nil:
|
||||
break
|
||||
default:
|
||||
t.Fatalf("Error while decoding opus file: %v", err)
|
||||
}
|
||||
if n == 0 {
|
||||
t.Fatal("Nil-error Read() must not return 0")
|
||||
}
|
||||
pcm = append(pcm, pcmbuf[:n]...)
|
||||
}
|
||||
}
|
||||
|
||||
func mustOpenFile(t *testing.T, fname string) *os.File {
|
||||
f, err := os.Open(fname)
|
||||
if err != nil {
|
||||
t.Fatalf("Error while opening %s: %v", fname, err)
|
||||
return nil
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func mustOpenStream(t *testing.T, r io.Reader) *Stream {
|
||||
stream, err := NewStream(r)
|
||||
if err != nil {
|
||||
t.Fatalf("Error while creating opus stream: %v", err)
|
||||
return nil
|
||||
}
|
||||
return stream
|
||||
}
|
||||
|
||||
func opus2pcm(t *testing.T, fname string, buffersize int) []int16 {
|
||||
reader := mustOpenFile(t, fname)
|
||||
stream := mustOpenStream(t, reader)
|
||||
return readStreamPcm(t, stream, buffersize)
|
||||
}
|
||||
|
||||
// Extract raw pcm data from .wav file
|
||||
func extractWavPcm(t *testing.T, fname string) []int16 {
|
||||
bytes, err := ioutil.ReadFile(fname)
|
||||
if err != nil {
|
||||
t.Fatalf("Error reading file data from %s: %v", fname, err)
|
||||
}
|
||||
const wavHeaderSize = 44
|
||||
if (len(bytes)-wavHeaderSize)%2 == 1 {
|
||||
t.Fatalf("Illegal wav data: payload must be encoded in byte pairs")
|
||||
}
|
||||
numSamples := (len(bytes) - wavHeaderSize) / 2
|
||||
samples := make([]int16, numSamples)
|
||||
for i := 0; i < numSamples; i++ {
|
||||
samples[i] += int16(bytes[wavHeaderSize+i*2])
|
||||
samples[i] += int16(bytes[wavHeaderSize+i*2+1]) << 8
|
||||
}
|
||||
return samples
|
||||
}
|
||||
|
||||
func TestStream(t *testing.T) {
|
||||
opuspcm := opus2pcm(t, "testdata/speech_8.opus", 10000)
|
||||
wavpcm := extractWavPcm(t, "testdata/speech_8.wav")
|
||||
if len(opuspcm) != len(wavpcm) {
|
||||
t.Fatalf("Unexpected length of decoded opus file: %d (.wav: %d)", len(opuspcm), len(wavpcm))
|
||||
}
|
||||
d := maxDiff(opuspcm, wavpcm)
|
||||
// No science behind this number
|
||||
const epsilon = 18
|
||||
if d > epsilon {
|
||||
t.Errorf("Maximum difference between decoded streams too high: %d", d)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStreamSmallBuffer(t *testing.T) {
|
||||
smallbuf := opus2pcm(t, "testdata/speech_8.opus", 1)
|
||||
bigbuf := opus2pcm(t, "testdata/speech_8.opus", 10000)
|
||||
if !reflect.DeepEqual(smallbuf, bigbuf) {
|
||||
t.Errorf("Reading with 1-sample buffer size yields different audio data")
|
||||
}
|
||||
}
|
||||
|
||||
type mockCloser struct {
|
||||
io.Reader
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (m *mockCloser) Close() error {
|
||||
if m.closed {
|
||||
return fmt.Errorf("Already closed")
|
||||
}
|
||||
m.closed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestCloser(t *testing.T) {
|
||||
f := mustOpenFile(t, "testdata/speech_8.opus")
|
||||
mc := &mockCloser{Reader: f}
|
||||
stream := mustOpenStream(t, mc)
|
||||
stream.Close()
|
||||
if !mc.closed {
|
||||
t.Error("Expected opus stream to call .Close on the reader")
|
||||
}
|
||||
}
|
64
gumble/go-opus/streams_map.go
Normal file
64
gumble/go-opus/streams_map.go
Normal file
@ -0,0 +1,64 @@
|
||||
// Copyright © Go Opus Authors (see AUTHORS file)
|
||||
//
|
||||
// License for use of this code is detailed in the LICENSE file
|
||||
|
||||
// +build !nolibopusfile
|
||||
|
||||
package opus
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// A map of simple integers to the actual pointers to stream structs. Avoids
|
||||
// passing pointers into the Go heap to C.
|
||||
//
|
||||
// As per the CGo pointers design doc for go 1.6:
|
||||
//
|
||||
// A particular unsafe area is C code that wants to hold on to Go func and
|
||||
// pointer values for future callbacks from C to Go. This works today but is not
|
||||
// permitted by the invariant. It is hard to detect. One safe approach is: Go
|
||||
// code that wants to preserve funcs/pointers stores them into a map indexed by
|
||||
// an int. Go code calls the C code, passing the int, which the C code may store
|
||||
// freely. When the C code wants to call into Go, it passes the int to a Go
|
||||
// function that looks in the map and makes the call. An explicit call is
|
||||
// required to release the value from the map if it is no longer needed, but
|
||||
// that was already true before.
|
||||
//
|
||||
// - https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md
|
||||
type streamsMap struct {
|
||||
sync.RWMutex
|
||||
m map[uintptr]*Stream
|
||||
counter uintptr
|
||||
}
|
||||
|
||||
func (sm *streamsMap) Get(id uintptr) *Stream {
|
||||
sm.RLock()
|
||||
defer sm.RUnlock()
|
||||
return sm.m[id]
|
||||
}
|
||||
|
||||
func (sm *streamsMap) Del(s *Stream) {
|
||||
sm.Lock()
|
||||
defer sm.Unlock()
|
||||
delete(sm.m, s.id)
|
||||
}
|
||||
|
||||
// NextId returns a unique ID for each call.
|
||||
func (sm *streamsMap) NextId() uintptr {
|
||||
return atomic.AddUintptr(&sm.counter, 1)
|
||||
}
|
||||
|
||||
func (sm *streamsMap) Save(s *Stream) {
|
||||
sm.Lock()
|
||||
defer sm.Unlock()
|
||||
sm.m[s.id] = s
|
||||
}
|
||||
|
||||
func newStreamsMap() *streamsMap {
|
||||
return &streamsMap{
|
||||
counter: 0,
|
||||
m: map[uintptr]*Stream{},
|
||||
}
|
||||
}
|
BIN
gumble/go-opus/testdata/speech_8.opus
vendored
Normal file
BIN
gumble/go-opus/testdata/speech_8.opus
vendored
Normal file
Binary file not shown.
BIN
gumble/go-opus/testdata/speech_8.wav
vendored
Normal file
BIN
gumble/go-opus/testdata/speech_8.wav
vendored
Normal file
Binary file not shown.
67
gumble/go-opus/utils_test.go
Normal file
67
gumble/go-opus/utils_test.go
Normal file
@ -0,0 +1,67 @@
|
||||
// Copyright © Go Opus Authors (see AUTHORS file)
|
||||
//
|
||||
// License for use of this code is detailed in the LICENSE file
|
||||
|
||||
package opus
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// utility functions for unit tests
|
||||
|
||||
func addSineFloat32(buf []float32, sampleRate int, freq float64) {
|
||||
factor := 2 * math.Pi * freq / float64(sampleRate)
|
||||
for i := range buf {
|
||||
buf[i] += float32(math.Sin(float64(i) * factor))
|
||||
}
|
||||
}
|
||||
|
||||
func addSine(buf []int16, sampleRate int, freq float64) {
|
||||
factor := 2 * math.Pi * freq / float64(sampleRate)
|
||||
for i := range buf {
|
||||
buf[i] += int16(math.Sin(float64(i)*factor) * (math.MaxInt16 - 1))
|
||||
}
|
||||
}
|
||||
|
||||
func maxDiff(a []int16, b []int16) int32 {
|
||||
if len(a) != len(b) {
|
||||
return math.MaxInt16
|
||||
}
|
||||
var max int32 = 0
|
||||
for i := range a {
|
||||
d := int32(a[i]) - int32(b[i])
|
||||
if d < 0 {
|
||||
d = -d
|
||||
}
|
||||
if d > max {
|
||||
max = d
|
||||
}
|
||||
}
|
||||
return max
|
||||
}
|
||||
|
||||
func interleave(a []int16, b []int16) []int16 {
|
||||
if len(a) != len(b) {
|
||||
panic("interleave: buffers must have equal length")
|
||||
}
|
||||
result := make([]int16, 2*len(a))
|
||||
for i := range a {
|
||||
result[2*i] = a[i]
|
||||
result[2*i+1] = b[i]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func split(interleaved []int16) ([]int16, []int16) {
|
||||
if len(interleaved)%2 != 0 {
|
||||
panic("split: interleaved buffer must have even number of samples")
|
||||
}
|
||||
left := make([]int16, len(interleaved)/2)
|
||||
right := make([]int16, len(interleaved)/2)
|
||||
for i := range left {
|
||||
left[i] = interleaved[2*i]
|
||||
right[i] = interleaved[2*i+1]
|
||||
}
|
||||
return left, right
|
||||
}
|
Reference in New Issue
Block a user