diff --git a/decoder.go b/decoder.go index da6ac81..5fb72e3 100644 --- a/decoder.go +++ b/decoder.go @@ -18,6 +18,12 @@ bridge_decoder_get_last_packet_duration(OpusDecoder *st, opus_int32 *samples) { return opus_decoder_ctl(st, OPUS_GET_LAST_PACKET_DURATION(samples)); } + +int +bridge_decoder_set_complexity(OpusDecoder *st, opus_int32 complexity) +{ + return opus_decoder_ctl(st, OPUS_SET_COMPLEXITY(complexity)); +} */ import "C" @@ -260,3 +266,14 @@ func (dec *Decoder) LastPacketDuration() (int, error) { } return int(samples), nil } + +// SetComplexity sets the decoders's computational complexity +// Note that this feature is only available if using libopus >= 1.5 +// This function will return ErrUnimplemented if the feature is not available +func (enc *Decoder) SetComplexity(complexity int) error { + res := C.bridge_decoder_set_complexity(enc.p, C.opus_int32(complexity)) + if res != C.OPUS_OK { + return Error(res) + } + return nil +} diff --git a/decoder_test.go b/decoder_test.go index afb188f..6ef6b59 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -5,6 +5,7 @@ package opus import ( + "fmt" "testing" ) @@ -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) + } + }) + } +}