-
Notifications
You must be signed in to change notification settings - Fork 63
Open
Description
Hi,
I am unable to capture from a specific device, but I am able to play back. The code structure remains the same and is derived from the examples.
package main
import (
"fmt"
"log"
"os"
"github.com/gen2brain/malgo"
)
func main() {
context, err := malgo.InitContext(nil, malgo.ContextConfig{}, nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer func() {
_ = context.Uninit()
context.Free()
}()
// Capture devices.
infos, err := context.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 := context.DeviceInfo(malgo.Capture, info.ID, malgo.Shared)
if err != nil {
e = err.Error()
}
fmt.Printf(" ID: [%d]: %v, %s, [%s], formats: %+v\n",
i, info.ID, info.Name(), e, full.Formats)
}
id := scanIdOption(infos)
fmt.Printf("Reading audio from: %s\n", infos[id].Name())
deviceConfig := malgo.DefaultDeviceConfig(malgo.Capture)
deviceConfig.Capture.Format = malgo.FormatS16
deviceConfig.Capture.Channels = 1
deviceConfig.Playback.Format = malgo.FormatS16
deviceConfig.Playback.Channels = 1
deviceConfig.SampleRate = 16000
deviceConfig.Alsa.NoMMap = 1
deviceConfig.PeriodSizeInMilliseconds = 40
deviceConfig.Capture.DeviceID = infos[id].ID.Pointer()
var playbackSampleCount uint32
var capturedSampleCount uint32
pCapturedSamples := make([]byte, 0)
sizeInBytes := uint32(malgo.SampleSizeInBytes(deviceConfig.Capture.Format))
onRecvFrames := func(pSample2, pSample []byte, framecount uint32) {
sampleCount := framecount * deviceConfig.Capture.Channels * sizeInBytes
log.Println(framecount)
newCapturedSampleCount := capturedSampleCount + sampleCount
pCapturedSamples = append(pCapturedSamples, pSample...)
capturedSampleCount = newCapturedSampleCount
}
fmt.Println("Recording...")
captureCallbacks := malgo.DeviceCallbacks{
Data: onRecvFrames,
}
device, err := malgo.InitDevice(context.Context, deviceConfig, captureCallbacks)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = device.Start()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Press Enter to stop recording...")
fmt.Scanln()
device.Uninit()
infos, err = context.Devices(malgo.Playback)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Playback Devices")
for i, info := range infos {
e := "ok"
full, err := context.DeviceInfo(malgo.Playback, 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)
}
id = scanIdOption(infos)
fmt.Printf("Playing audio to: %s\n", infos[id].Name())
deviceConfig = malgo.DefaultDeviceConfig(malgo.Playback)
deviceConfig.Capture.Format = malgo.FormatS16
deviceConfig.Capture.Channels = 1
deviceConfig.Playback.Format = malgo.FormatS16
deviceConfig.Playback.Channels = 1
deviceConfig.SampleRate = 16000
deviceConfig.Alsa.NoMMap = 1
deviceConfig.PeriodSizeInMilliseconds = 40
deviceConfig.Playback.DeviceID = infos[id].ID.Pointer()
onSendFrames := func(pSample, nil []byte, framecount uint32) {
samplesToRead := framecount * deviceConfig.Playback.Channels * sizeInBytes
if samplesToRead > capturedSampleCount-playbackSampleCount {
samplesToRead = capturedSampleCount - playbackSampleCount
}
copy(pSample, pCapturedSamples[playbackSampleCount:playbackSampleCount+samplesToRead])
playbackSampleCount += samplesToRead
if playbackSampleCount == uint32(len(pCapturedSamples)) {
playbackSampleCount = 0
}
}
fmt.Println("Playing...")
playbackCallbacks := malgo.DeviceCallbacks{
Data: onSendFrames,
}
device, err = malgo.InitDevice(context.Context, deviceConfig, playbackCallbacks)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = device.Start()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Press Enter to quit...")
fmt.Scanln()
device.Uninit()
}
func scanIdOption(infos []malgo.DeviceInfo) int {
fmt.Println("\r\nChoose an ID from the list above: ")
var id int
_, err := fmt.Scanf("%d", &id)
if err != nil {
log.Fatal(err)
} else {
fmt.Printf("\r\nChosen ID: [%d]\n", id)
}
if id >= len(infos) {
fmt.Println("\r\nInvalid ID")
panic("Invalid ID")
}
return id
}
If I comment deviceConfig.Capture.DeviceID = infos[id].ID.Pointer(), then I get audio captured from the default stream.
When this line is enabled, the callback is triggered and I received 640 bytes of zeroes. No data is received.
Am I missing something here?
Metadata
Metadata
Assignees
Labels
No labels