Skip to content

Commit

Permalink
refactor: Improve audio device selection and validation logic
Browse files Browse the repository at this point in the history
- Modify device selection to include all devices, not just hardware devices
- Update device matching and testing to work with a broader range of audio sources
- Remove commented-out device source reset to prevent unintended clearing
- Improve logging and device selection output
  • Loading branch information
tphakala committed Feb 16, 2025
1 parent 98d1553 commit c0cad67
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions internal/myaudio/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,30 +431,30 @@ func ValidateAudioDevice(settings *conf.Settings) error {
return fmt.Errorf("failed to get capture devices: %w", err)
}

// Filter to get only hardware devices
// Filter to get only hardware devices to check if any are available
hardwareDevices := getHardwareDevices(infos)
if len(hardwareDevices) == 0 {
settings.Realtime.Audio.Source = ""
return fmt.Errorf("no hardware audio capture devices found")
}

// Try to find and test the configured device
for i := range hardwareDevices {
decodedID, err := hexToASCII(hardwareDevices[i].ID.String())
// Try to find and test the configured device, in this we also accept alsa speudo devices
for i := range infos {
decodedID, err := hexToASCII(infos[i].ID.String())
if err != nil {
continue
}

if matchesDeviceSettings(decodedID, &hardwareDevices[i], settings.Realtime.Audio.Source) {
if TestCaptureDevice(malgoCtx, &hardwareDevices[i]) {
if matchesDeviceSettings(decodedID, &infos[i], settings.Realtime.Audio.Source) {
if TestCaptureDevice(malgoCtx, &infos[i]) {
return nil
}
settings.Realtime.Audio.Source = ""
return fmt.Errorf("configured audio device '%s' failed hardware test", settings.Realtime.Audio.Source)
}
}

settings.Realtime.Audio.Source = ""
//settings.Realtime.Audio.Source = ""
return fmt.Errorf("configured audio device '%s' not found", settings.Realtime.Audio.Source)
}

Expand Down Expand Up @@ -486,29 +486,26 @@ func selectCaptureSource(settings *conf.Settings) (captureSource, error) {
return captureSource{}, fmt.Errorf("failed to get capture devices: %w", err)
}

// Filter to get only hardware devices
hardwareDevices := getHardwareDevices(infos)

fmt.Println("Available Hardware Capture Sources:")
for i := range hardwareDevices {
decodedID, err := hexToASCII(hardwareDevices[i].ID.String())
fmt.Println("Available Capture Sources:")
for i := range infos {
decodedID, err := hexToASCII(infos[i].ID.String())
if err != nil {
fmt.Printf("❌ Error decoding ID for device %d: %v\n", i, err)
continue
}

output := fmt.Sprintf(" %d: %s", i, hardwareDevices[i].Name())
output := fmt.Sprintf(" %d: %s", i, infos[i].Name())
if runtime.GOOS == "linux" {
output = fmt.Sprintf("%s, %s", output, decodedID)
}

if matchesDeviceSettings(decodedID, &hardwareDevices[i], settings.Realtime.Audio.Source) {
if TestCaptureDevice(malgoCtx, &hardwareDevices[i]) {
if matchesDeviceSettings(decodedID, &infos[i], settings.Realtime.Audio.Source) {
if TestCaptureDevice(malgoCtx, &infos[i]) {
fmt.Printf("%s (✅ selected)\n", output)
return captureSource{
Name: hardwareDevices[i].Name(),
Name: infos[i].Name(),
ID: decodedID,
Pointer: hardwareDevices[i].ID.Pointer(),
Pointer: infos[i].ID.Pointer(),
}, nil
}
fmt.Printf("%s (❌ device test failed)\n", output)
Expand Down

0 comments on commit c0cad67

Please sign in to comment.