Kirika/vector/audio.go
DataHoarder 514a88aec1
Some checks failed
continuous-integration/drone/push Build is failing
Update to Go 1.20
2023-04-09 13:10:30 +02:00

131 lines
5 KiB
Go

//go:build cgo
package vector
/*
#cgo CFLAGS: -march=native -Ofast -std=c99
#include "audio.h"
*/
import "C"
import (
"runtime"
"unsafe"
)
// MultipleChannelsToMonoFloat32 bring any number of channels to mono, equally weighted
func MultipleChannelsToMonoFloat32(buffer []float32, channels int) (buf []float32) {
buf = make([]float32, len(buffer)/channels)
C.audio_multiple_channels_to_mono((*C.float)(unsafe.SliceData(buffer)), C.size_t(len(buffer)), (*C.float)(unsafe.SliceData(buf)), C.int(channels))
runtime.KeepAlive(buffer)
return
}
// MultipleChannelsToStereoFloat32 bring any number of channels to stereo, using downmix formulas when necessary
func MultipleChannelsToStereoFloat32(buffer []float32, channels int) (buf []float32) {
buf = make([]float32, (len(buffer)/channels)*2)
C.audio_multiple_channels_to_stereo((*C.float)(unsafe.SliceData(buffer)), C.size_t(len(buffer)), (*C.float)(unsafe.SliceData(buf)), C.int(channels))
runtime.KeepAlive(buffer)
return
}
// MultipleChannelsToMonoInt32 bring any number of channels to mono, equally weighted
func MultipleChannelsToMonoInt32(buffer []int32, channels int) (buf []int32) {
buf = make([]int32, len(buffer)/channels)
C.audio_multiple_channels_to_mono_int32((*C.int32_t)(unsafe.SliceData(buffer)), C.size_t(len(buffer)), (*C.int32_t)(unsafe.SliceData(buf)), C.int(channels))
runtime.KeepAlive(buffer)
return
}
// MultipleChannelsToStereoInt32 bring any number of channels to stereo, using downmix formulas when necessary
func MultipleChannelsToStereoInt32(buffer []int32, channels int) (buf []int32) {
buf = make([]int32, (len(buffer)/channels)*2)
C.audio_multiple_channels_to_stereo_int32((*C.int32_t)(unsafe.SliceData(buffer)), C.size_t(len(buffer)), (*C.int32_t)(unsafe.SliceData(buf)), C.int(channels))
runtime.KeepAlive(buffer)
return
}
func Int32ToInt16(data []int32, bitDepth int) (buf []int16) {
buf = make([]int16, len(data))
C.audio_int32_to_int16((*C.int32_t)(unsafe.SliceData(data)), C.size_t(len(data)), (*C.int16_t)(unsafe.Pointer(unsafe.SliceData(buf))), C.int(bitDepth))
runtime.KeepAlive(data)
return
}
func Int16ToInt32(data []int32, bitDepth int) (buf []int16) {
buf = make([]int16, len(data))
C.audio_int32_to_int16((*C.int32_t)(unsafe.SliceData(data)), C.size_t(len(data)), (*C.int16_t)(unsafe.Pointer(unsafe.SliceData(buf))), C.int(bitDepth))
runtime.KeepAlive(data)
return
}
func Int32ToBytes(data []int32, bitDepth int) (buf []byte) {
buf = make([]byte, len(data)*(bitDepth/8))
C.audio_int32_to_bytes((*C.int32_t)(unsafe.SliceData(data)), C.size_t(len(data)), (*C.int8_t)(unsafe.Pointer(unsafe.SliceData(buf))), C.int(bitDepth))
runtime.KeepAlive(data)
return
}
func BytesToInt32(data []byte, bitDepth int) (buf []int32) {
buf = make([]int32, len(data)/(bitDepth/8))
C.audio_bytes_to_int32((*C.int8_t)(unsafe.Pointer(unsafe.SliceData(data))), C.size_t(len(data)), (*C.int32_t)(unsafe.SliceData(buf)), C.int(bitDepth))
runtime.KeepAlive(data)
return
}
func Int32ToFloat32(data []int32, bitDepth int) (buf []float32) {
buf = make([]float32, len(data))
C.audio_int32_to_float32((*C.int32_t)(unsafe.SliceData(data)), C.size_t(len(data)), (*C.float)(unsafe.SliceData(buf)), C.int(bitDepth))
runtime.KeepAlive(data)
return
}
// Int24ToFloat32 special case
func Int24ToFloat32(data []byte, bitDepth int) (buf []float32) {
buf = make([]float32, len(data)/3)
C.audio_int24_to_float32((*C.int8_t)(unsafe.Pointer(unsafe.SliceData(data))), C.size_t(len(data)), (*C.float)(unsafe.SliceData(buf)), C.int(bitDepth))
runtime.KeepAlive(data)
return
}
func Int16ToFloat32(data []int16, bitDepth int) (buf []float32) {
buf = make([]float32, len(data))
C.audio_int16_to_float32((*C.int16_t)(unsafe.SliceData(data)), C.size_t(len(data)), (*C.float)(unsafe.SliceData(buf)), C.int(bitDepth))
runtime.KeepAlive(data)
return
}
func Int8ToFloat32(data []int8, bitDepth int) (buf []float32) {
buf = make([]float32, len(data))
C.audio_int8_to_float32((*C.int8_t)(unsafe.SliceData(data)), C.size_t(len(data)), (*C.float)(unsafe.SliceData(buf)), C.int(bitDepth))
runtime.KeepAlive(data)
return
}
func Float32ToInt32(data []float32, bitDepth int) (buf []int32) {
buf = make([]int32, len(data))
C.audio_float32_to_int32((*C.float)(unsafe.SliceData(data)), C.size_t(len(data)), (*C.int32_t)(unsafe.SliceData(buf)), C.int(bitDepth))
runtime.KeepAlive(data)
return
}
func Float32ToInt24(data []float32) (buf []byte) {
buf = make([]byte, len(data)*3)
C.audio_float32_to_int24((*C.float)(unsafe.SliceData(data)), C.size_t(len(data)), (*C.int8_t)(unsafe.Pointer(unsafe.SliceData(buf))))
runtime.KeepAlive(data)
return
}
func Float32ToInt16(data []float32) (buf []int16) {
buf = make([]int16, len(data))
C.audio_float32_to_int16((*C.float)(unsafe.SliceData(data)), C.size_t(len(data)), (*C.int16_t)(unsafe.SliceData(buf)))
runtime.KeepAlive(data)
return
}
func Float32ToInt8(data []float32) (buf []int8) {
buf = make([]int8, len(data))
C.audio_float32_to_int8((*C.float)(unsafe.SliceData(data)), C.size_t(len(data)), (*C.int8_t)(unsafe.SliceData(buf)))
runtime.KeepAlive(data)
return
}