Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ bridge_decoder_get_last_packet_duration(OpusDecoder *st, opus_int32 *samples)
{
return opus_decoder_ctl(st, OPUS_GET_LAST_PACKET_DURATION(samples));
}

#define BRIDGE_ERR_NOT_SUPPORTED -12345

int
bridge_decoder_set_complexity(OpusDecoder *st, opus_int32 complexity)
{
#if defined(OPUS_SET_COMPLEXITY)
return opus_decoder_ctl(st, OPUS_SET_COMPLEXITY(complexity));
#endif
return BRIDGE_ERR_NOT_SUPPORTED;
}
*/
import "C"

Expand Down Expand Up @@ -260,3 +271,18 @@ func (dec *Decoder) LastPacketDuration() (int, error) {
}
return int(samples), nil
}

const bridgeErrUnimplemented = C.BRIDGE_ERR_NOT_SUPPORTED

// SetComplexity sets the decoders's computational complexity
// Note that this feature is only available if using libopus >= 1.5
func (dec *Decoder) SetComplexity(complexity int) error {
res := int(C.bridge_decoder_set_complexity(dec.p, C.int(complexity)))
if res == bridgeErrUnimplemented {
return fmt.Errorf("SetComplexity for decoders requires libopus 1.5 or higher")
}
if res != int(C.OPUS_OK) {
return Error(res)
}
return nil
}
28 changes: 28 additions & 0 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package opus

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -66,3 +67,30 @@ func TestDecoder_GetLastPacketDuration(t *testing.T) {
t.Fatalf("Wrong duration length. Expected %d. Got %d", n, samples)
}
}

func TestDecoder_SetComplexity(t *testing.T) {
const SAMPLE_RATE = 48000

dec, err := NewDecoder(SAMPLE_RATE, 1)
if err != nil || dec == nil {
t.Fatalf("Error creating new decoder: %v", err)
}

t.Run("Complexity 0 to 10", func(t *testing.T) {
for i := 0; i <= 10; i++ {
err = dec.SetComplexity(i)
if err != nil {
t.Fatalf("Expected nil got %v", err)
}
}
})

for _, tt := range []int{-1, 11, 99} {
t.Run(fmt.Sprintf("Complexity %d", tt), func(t *testing.T) {
err = dec.SetComplexity(tt)
if err != ErrBadArg {
t.Fatalf("Expected %v got %v", ErrBadArg, err)
}
})
}
}