diff --git a/encoder.go b/encoder.go index e873887..b88dc98 100644 --- a/encoder.go +++ b/encoder.go @@ -98,6 +98,12 @@ 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" @@ -385,3 +391,12 @@ func (enc *Encoder) PacketLossPerc() (int, error) { } 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 +} diff --git a/encoder_test.go b/encoder_test.go index 0795cae..56d1282 100644 --- a/encoder_test.go +++ b/encoder_test.go @@ -379,3 +379,15 @@ func TestEncoder_SetGetInvalidPacketLossPerc(t *testing.T) { } } } + +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) +} diff --git a/opus_test.go b/opus_test.go index 82e30f0..2f3f3bf 100644 --- a/opus_test.go +++ b/opus_test.go @@ -26,16 +26,21 @@ func TestOpusErrstr(t *testing.T) { } 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) - 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)