summaryrefslogtreecommitdiff
path: root/internal/audio/fallbacks.go
blob: 587474dea2f50c4fc3b6e82bf0feaa06221d89d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package audio

import (
	"fmt"
	"strings"
)

// RunWithVoiceFallbacks tries the selected Gemini voice first, then the remaining known voices.
func RunWithVoiceFallbacks(initialVoice string, generate func(voice string) error, warnNoAudio func(voice string)) (usedVoice string, err error) {
	attempted := make([]string, 0, len(GeminiVoices))
	var lastErr error

	for _, voice := range GeminiVoiceFallbacks(initialVoice) {
		attempted = append(attempted, voice)

		err := generate(voice)
		if err == nil {
			return voice, nil
		}
		if !IsGeminiNoAudioDataError(err) {
			return "", err
		}

		lastErr = err
		if warnNoAudio != nil {
			warnNoAudio(voice)
		}
	}

	return "", fmt.Errorf("gemini returned no audio for voices %s: %w", strings.Join(attempted, ", "), lastErr)
}