Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add reset method for encoder #51

Merged
merged 1 commit into from
Sep 25, 2023
Merged
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
15 changes: 15 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
13 changes: 9 additions & 4 deletions opus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down