From d4becdcdaaed3590b59d55efcb1ba2dfb751e8b8 Mon Sep 17 00:00:00 2001 From: wys Date: Fri, 13 Jun 2025 22:20:04 +0800 Subject: [PATCH] =?UTF-8?q?feat(encoder):=20=E6=B7=BB=E5=8A=A0=E6=81=92?= =?UTF-8?q?=E5=AE=9A=E6=AF=94=E7=89=B9=E7=8E=87(CBR)=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- encoder.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/encoder.go b/encoder.go index b88dc98..ba5e54e 100644 --- a/encoder.go +++ b/encoder.go @@ -104,6 +104,19 @@ bridge_encoder_reset_state(OpusEncoder *st) return opus_encoder_ctl(st, OPUS_RESET_STATE); } +int +bridge_encoder_set_vbr(OpusEncoder *st, opus_int32 vbr) +{ + return opus_encoder_ctl(st, OPUS_SET_VBR(vbr)); +} + +int +bridge_encoder_get_vbr(OpusEncoder *st, opus_int32 *vbr) +{ + return opus_encoder_ctl(st, OPUS_GET_VBR(vbr)); +} + + */ import "C" @@ -400,3 +413,26 @@ func (enc *Encoder) Reset() error { } return nil } + +// SetCBR sets the encoder to use constant bitrate (CBR) or variable bitrate (VBR). +func (enc *Encoder) SetCBR(cbr bool) error { + vbr := 1 + if cbr { + vbr = 0 + } + res := C.bridge_encoder_set_vbr(enc.p, C.opus_int32(vbr)) + if res != C.OPUS_OK { + return Error(res) + } + return nil +} + +// IsCBR checks if encoder is using CBR (false = VBR). +func (enc *Encoder) IsCBR() (bool, error) { + var vbr C.opus_int32 + res := C.bridge_encoder_get_vbr(enc.p, &vbr) + if res != C.OPUS_OK { + return false, Error(res) + } + return vbr == 0, nil +}