https://github.com/octu0/go-encodec-cpp
Go binding for encodec.cpp
https://github.com/octu0/go-encodec-cpp
audio-codec bindings encodec
Last synced: 5 months ago
JSON representation
Go binding for encodec.cpp
- Host: GitHub
- URL: https://github.com/octu0/go-encodec-cpp
- Owner: octu0
- License: mit
- Created: 2023-10-28T13:01:46.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-01-10T09:48:30.000Z (over 2 years ago)
- Last Synced: 2024-12-30T03:43:23.318Z (over 1 year ago)
- Topics: audio-codec, bindings, encodec
- Language: Go
- Homepage: https://pkg.go.dev/github.com/octu0/go-encodec-cpp
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `go-encodec-cpp`
[](https://github.com/octu0/go-encodec-cpp/blob/master/LICENSE)
[](https://godoc.org/github.com/octu0/go-encodec-cpp)
[](https://goreportcard.com/report/github.com/octu0/go-encodec-cpp)
[](https://github.com/octu0/go-encodec-cpp/releases)
Go binding for [encodec.cpp](https://github.com/PABannier/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
```go
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
```go
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](https://github.com/octu0/go-encodec-cpp/tree/master/_example) for more details
## License
MIT, see LICENSE file for details.