Skip to content

Commit 61e5214

Browse files
committed
Added SetComplexity to decoder
1 parent 0188a62 commit 61e5214

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

decoder.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ bridge_decoder_get_last_packet_duration(OpusDecoder *st, opus_int32 *samples)
1818
{
1919
return opus_decoder_ctl(st, OPUS_GET_LAST_PACKET_DURATION(samples));
2020
}
21+
22+
int
23+
bridge_decoder_set_complexity(OpusDecoder *st, opus_int32 complexity)
24+
{
25+
return opus_decoder_ctl(st, OPUS_SET_COMPLEXITY(complexity));
26+
}
2127
*/
2228
import "C"
2329

@@ -260,3 +266,14 @@ func (dec *Decoder) LastPacketDuration() (int, error) {
260266
}
261267
return int(samples), nil
262268
}
269+
270+
// SetComplexity sets the decoders's computational complexity
271+
// Note that this feature is only available if using libopus >= 1.5
272+
// This function will return ErrUnimplemented if the feature is not available
273+
func (enc *Decoder) SetComplexity(complexity int) error {
274+
res := C.bridge_decoder_set_complexity(enc.p, C.opus_int32(complexity))
275+
if res != C.OPUS_OK {
276+
return Error(res)
277+
}
278+
return nil
279+
}

decoder_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package opus
66

77
import (
8+
"fmt"
89
"testing"
910
)
1011

@@ -66,3 +67,30 @@ func TestDecoder_GetLastPacketDuration(t *testing.T) {
6667
t.Fatalf("Wrong duration length. Expected %d. Got %d", n, samples)
6768
}
6869
}
70+
71+
func TestDecoder_SetComplexity(t *testing.T) {
72+
const SAMPLE_RATE = 48000
73+
74+
dec, err := NewDecoder(SAMPLE_RATE, 1)
75+
if err != nil || dec == nil {
76+
t.Fatalf("Error creating new decoder: %v", err)
77+
}
78+
79+
t.Run("Complexity 0 to 10", func(t *testing.T) {
80+
for i := 0; i <= 10; i++ {
81+
err = dec.SetComplexity(i)
82+
if err != nil {
83+
t.Fatalf("Expected nil got %v", err)
84+
}
85+
}
86+
})
87+
88+
for _, tt := range []int{-1, 11, 99} {
89+
t.Run(fmt.Sprintf("Complexity %d", tt), func(t *testing.T) {
90+
err = dec.SetComplexity(tt)
91+
if err != ErrBadArg {
92+
t.Fatalf("Expected %v got %v", ErrBadArg, err)
93+
}
94+
})
95+
}
96+
}

0 commit comments

Comments
 (0)