Skip to content

Commit

Permalink
Wrap C type to allow for external WebRtcVadInst type reference
Browse files Browse the repository at this point in the history
To use VAD instance in some contexts, for example: a struct field, `sync.Pool`, etc. it's necessary to reference the type explicitly.
golang/go#13467

Signed-off-by: Arkadi Shishlov <[email protected]>
  • Loading branch information
arkadijs committed Feb 7, 2024
1 parent 6a18123 commit 7262642
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions vad.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,41 @@ 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")
}
return
}

// 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")
}
return
}

// 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 {
Expand Down

0 comments on commit 7262642

Please sign in to comment.