Use new c-gaborator API without callbacks
This commit is contained in:
parent
a6269aaf83
commit
d5aae73cd6
|
@ -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"
|
||||
|
|
29
goborator.go
29
goborator.go
|
@ -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]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue