From 7262642b4b1440a6885cdf10b01077c81faedf47 Mon Sep 17 00:00:00 2001 From: Arkadi Shishlov Date: Wed, 7 Feb 2024 18:01:12 +0200 Subject: [PATCH] Wrap C type to allow for external WebRtcVadInst type reference To use VAD instance in some contexts, for example: a struct field, `sync.Pool`, etc. it's necessary to reference the type explicitly. https://github.com/golang/go/issues/13467 Signed-off-by: Arkadi Shishlov --- vad.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/vad.go b/vad.go index f060bcf..f0c594d 100644 --- a/vad.go +++ b/vad.go @@ -12,19 +12,23 @@ import ( "unsafe" ) +type VadInst struct { + inst *C.struct_WebRtcVadInst +} + // Create Creates an instance to the VAD structure. -func Create() *C.struct_WebRtcVadInst { - return C.WebRtcVad_Create() +func Create() VadInst { + return VadInst{C.WebRtcVad_Create()} } // Free Frees the dynamic memory of a specified VAD instance. -func Free(vadInst *C.struct_WebRtcVadInst) { - C.WebRtcVad_Free(vadInst) +func Free(vadInst VadInst) { + C.WebRtcVad_Free(vadInst.inst) } // Init Initializes a VAD instance. -func Init(vadInst *C.struct_WebRtcVadInst) (err error) { - result := C.WebRtcVad_Init(vadInst) +func Init(vadInst VadInst) (err error) { + result := C.WebRtcVad_Init(vadInst.inst) if result == -1 { err = errors.New("null pointer or Default mode could not be set") } @@ -32,8 +36,8 @@ func Init(vadInst *C.struct_WebRtcVadInst) (err error) { } // SetMode Sets the VAD operating mode. -func SetMode(vadInst *C.struct_WebRtcVadInst, mode int) (err error) { - result := C.WebRtcVad_set_mode(vadInst, C.int(mode)) +func SetMode(vadInst VadInst, mode int) (err error) { + result := C.WebRtcVad_set_mode(vadInst.inst, C.int(mode)) if result == -1 { err = errors.New("mode could not be set or the VAD instance has not been initialized") } @@ -41,8 +45,8 @@ func SetMode(vadInst *C.struct_WebRtcVadInst, mode int) (err error) { } // Process Sets the VAD operating mode. -func Process(vadInst *C.struct_WebRtcVadInst, fs int, audioFrame []byte, frameLength int) (active bool, err error) { - result := C.WebRtcVad_Process(vadInst, C.int(fs), (*C.short)(unsafe.Pointer(&audioFrame[0])), C.size_t(frameLength)) +func Process(vadInst VadInst, fs int, audioFrame []byte, frameLength int) (active bool, err error) { + result := C.WebRtcVad_Process(vadInst.inst, C.int(fs), (*C.short)(unsafe.Pointer(&audioFrame[0])), C.size_t(frameLength)) if result == 1 { active = true } else if result == 0 {