diff --git a/README.md b/README.md index 7d7558e..5282010 100644 --- a/README.md +++ b/README.md @@ -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" diff --git a/go.mod b/go.mod index be4dd3a..9f40681 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module git.gammaspectra.live/S.O.N.G/goborator -go 1.17 +go 1.18 diff --git a/goborator.go b/goborator.go index 7e2e7db..0d4bcf7 100644 --- a/goborator.go +++ b/goborator.go @@ -3,10 +3,6 @@ package goborator /* #cgo pkg-config: cgaborator #include - -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] } }