octu0 / go-encodec-cpp

Go binding for encodec.cpp

Home Page:https://pkg.go.dev/github.com/octu0/go-encodec-cpp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

go-encodec-cpp

MIT License GoDoc Go Report Card Releases

Go binding for encodec.cpp

encodec.cpp provides a C++ interface, so I create a bridge called cencodec (c-encodec-cpp) that can be handled by cgo to call encodec.cpp from Go.

Installation

install libcencodec.(so|dylib)

$ git clone https://github.com/octu0/go-encodec-cpp.git
$ cd go-encodec-cpp/c-encodec-cpp
$ make install

Example

Compress

package main

imort (
	"os"
	"runtime"

	"github.com/go-audio/wav"
	"github.com/octu0/go-encodec-cpp"
)

func main() {
	gpuLayers := 0
	targetBandwidth := 6

	in, _ := os.Open("/path/to/input.wav")
	out, _ := os.Create("/path/to/output.compress")

	wd := wav.NewDecoder(in)
	buf, _ := wd.FullPCMBuffer()

	ectx, _ := encodec.LoadModel("/path/to/ggml-model.bin", gpuLayers)
	defer ectx.Release()

	ectx.SetTargetBandwidth(targetBandwidth)

	compressedData, free, _ := ectx.CompressAudio(buf.AsFloat32Buffer().Data, runtime.NumCPU())
	defer free()

	gob.NewEncoder(out).Encode(ECDC{
		BitDepth:    32,
		NumChannels: 1,
		SampleRate:  24000,
		AudioFormat: 1,
		Bandwidth:   targetBandwidth,
		Data:        compressedData,
	})
}

Decompress

package main

imort (
	"os"
	"runtime"

	"github.com/go-audio/wav"
	"github.com/octu0/go-encodec-cpp"
)

func main() {
	gpuLayers := 0

	in, _ := os.Open("/path/to/input.compress")
	out, _ := os.Create("/path/to/output.wav")

	ecdc := ECDC{}
	gob.NewDecoder(in).Decode(&ecdc)

	ectx, _ := encodec.LoadModel("/path/to/ggml-model.bin", gpuLayers)
	defer ectx.Release()

	ectx.SetTargetBandwidth(ecdc.Bandwidth)

	decompressData, free, _ := ectx.DecompressAudio(ecdc.Data, runtime.NumCPU())
	defer free()

	we := wav.NewEncoder(out, ecdc.SampleRate, 16, ecdc.NumChannels, ecdc.AudioFormat)
	for _, d := range decompressData {
		d *= 0x7fff
		switch {
		case 0x7fff < d:
			d = 0x7fff
		case d < -0x8000:
			d = -0x8000
		case 0 < d:
			d += 0.5
		case d < 0:
			d -= -0.5
		}
		we.WriteFrame(int16(d))
	}
}

See _example for more details

License

MIT, see LICENSE file for details.

About

Go binding for encodec.cpp

https://pkg.go.dev/github.com/octu0/go-encodec-cpp

License:MIT License


Languages

Language:Go 55.7%Language:C++ 31.1%Language:C 10.9%Language:Makefile 2.3%