xyosc/audio/audio.go

74 lines
1.6 KiB
Go
Raw Permalink Normal View History

2024-10-20 22:58:09 +02:00
package audio
import (
"fmt"
2024-10-21 00:07:12 +02:00
"os"
2024-10-20 22:58:09 +02:00
"xyosc/config"
"xyosc/utils"
"github.com/gen2brain/malgo"
"github.com/smallnest/ringbuffer"
)
var SampleRingBuffer *ringbuffer.RingBuffer
var SampleSizeInBytes uint32
const format = malgo.FormatF32
func Init() {
2024-10-21 13:56:50 +02:00
SampleRingBuffer = ringbuffer.New(int(config.Config.RingBufferSize)).SetBlocking(true)
2024-10-21 00:07:12 +02:00
SampleSizeInBytes = uint32(malgo.SampleSizeInBytes(format))
2024-10-20 22:58:09 +02:00
}
func Start() {
ctx, err := malgo.InitContext(nil, malgo.ContextConfig{}, func(message string) {
fmt.Printf("LOG <%v>\n", message)
})
utils.CheckError(err)
defer func() {
_ = ctx.Uninit()
ctx.Free()
}()
2024-10-21 00:07:12 +02:00
// Capture devices.
infos, err := ctx.Devices(malgo.Capture)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Capture Devices")
for i, info := range infos {
e := "ok"
full, err := ctx.DeviceInfo(malgo.Capture, info.ID, malgo.Shared)
if err != nil {
e = err.Error()
}
fmt.Printf(" %d: %v, %s, [%s], formats: %+v\n",
i, info.ID, info.Name(), e, full.Formats)
}
deviceConfig := malgo.DefaultDeviceConfig(malgo.Capture)
2024-10-20 22:58:09 +02:00
deviceConfig.Capture.Format = format
deviceConfig.Capture.Channels = 2
2024-10-21 00:07:12 +02:00
deviceConfig.Capture.DeviceID = infos[config.Config.CaptureDeviceIndex].ID.Pointer()
2024-10-20 22:58:09 +02:00
deviceConfig.SampleRate = config.Config.SampleRate
deviceConfig.Alsa.NoMMap = 1
2024-10-21 00:07:12 +02:00
2024-10-20 22:58:09 +02:00
onRecvFrames := func(pSample2, pSample []byte, framecount uint32) {
SampleRingBuffer.Write(pSample)
}
captureCallbacks := malgo.DeviceCallbacks{
Data: onRecvFrames,
}
device, err := malgo.InitDevice(ctx.Context, deviceConfig, captureCallbacks)
utils.CheckError(err)
err = device.Start()
utils.CheckError(err)
2024-10-21 00:07:12 +02:00
select {}
2024-10-20 22:58:09 +02:00
//device.Uninit()
}