Use new c-gaborator API without callbacks

This commit is contained in:
DataHoarder 2022-07-13 18:26:18 +02:00
parent a6269aaf83
commit d5aae73cd6
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
3 changed files with 18 additions and 15 deletions

View file

@ -5,7 +5,7 @@ Simple Gaborator cgo implementation.
Requires [c-gaborator](https://git.gammaspectra.live/S.O.N.G/c-gaborator) installed.
```shell
git clone --recursive --depth 1 https://git.gammaspectra.live/S.O.N.G/c-gaborator
cd c-gaborator && make build && cd build
cd c-gaborator && mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CXX_FLAGS_RELEASE="-march=native" -DCMAKE_C_FLAGS_RELEASE="-march=native" \
-DCMAKE_INSTALL_PREFIX="/usr"

2
go.mod
View file

@ -1,3 +1,3 @@
module git.gammaspectra.live/S.O.N.G/goborator
go 1.17
go 1.18

View file

@ -3,10 +3,6 @@ package goborator
/*
#cgo pkg-config: cgaborator
#include <cgaborator.h>
void cgoCallback(uintptr_t callback_data, float* data, size_t size, size_t slice_size);
typedef void (*gaborator_transform_callback)(uintptr_t callback_data, float* data, size_t size, size_t slice_size);
*/
import "C"
import (
@ -154,19 +150,26 @@ func (g *Gaborator) analyze(block []float32) {
handle := cgo.NewHandle(g.coefficientOutputChannel)
defer handle.Delete()
var returnSize C.size_t
var sliceSize C.size_t
var returnData *C.float
if len(block) == 0 {
C.gaborator_transform(g.pointer, (*C.float)(nil), C.int(0), C.gaborator_transform_callback(C.cgoCallback), C.uintptr_t(handle))
returnData = C.gaborator_transform(g.pointer, (*C.float)(nil), C.int64_t(0), &returnSize, &sliceSize)
} else {
C.gaborator_transform(g.pointer, (*C.float)(&block[0]), C.int(len(block)), C.gaborator_transform_callback(C.cgoCallback), C.uintptr_t(handle))
returnData = C.gaborator_transform(g.pointer, (*C.float)(&block[0]), C.int64_t(len(block)), &returnSize, &sliceSize)
}
if returnData != nil && returnSize > 0 {
g.outputResult(unsafe.Slice((*float32)(returnData), uint64(returnSize)), int(sliceSize))
}
}
//export cgoCallback
func cgoCallback(ptr C.uintptr_t, data *C.float, size, sliceSize C.size_t) {
buf := make([]float32, size)
channel := cgo.Handle(ptr).Value().(chan []float32)
copy(buf, unsafe.Slice((*float32)(data), int(size)))
for i := 0; i < int(size); i += int(sliceSize) {
channel <- buf[i : i+int(sliceSize)]
func (g *Gaborator) outputResult(block []float32, sliceSize int) {
buf := make([]float32, len(block))
copy(buf, block)
for i := 0; i < len(block); i += sliceSize {
g.coefficientOutputChannel <- buf[i : i+sliceSize]
}
}