summaryrefslogtreecommitdiff
path: root/internal/audio
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-04-02 17:31:29 +0300
committerPaul Buetow <paul@buetow.org>2026-04-02 17:31:29 +0300
commitf3b5f58a171eeb47716e2d15a40c4c480d2d5ef9 (patch)
treecf461894a613e0d7bad1a36e04dd420654514391 /internal/audio
parent4d16684f92544d1cf90dfc5080378ca2f1abe18e (diff)
task 002: centralize Gemini voice fallbacks
Diffstat (limited to 'internal/audio')
-rw-r--r--internal/audio/fallbacks.go29
-rw-r--r--internal/audio/voices_test.go49
2 files changed, 78 insertions, 0 deletions
diff --git a/internal/audio/fallbacks.go b/internal/audio/fallbacks.go
new file mode 100644
index 0000000..bd0c86e
--- /dev/null
+++ b/internal/audio/fallbacks.go
@@ -0,0 +1,29 @@
+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) (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
+ fmt.Printf("Warning: Gemini returned no audio for voice %s\n", voice)
+ }
+
+ return "", fmt.Errorf("Gemini returned no audio for voices %s: %w", strings.Join(attempted, ", "), lastErr)
+}
diff --git a/internal/audio/voices_test.go b/internal/audio/voices_test.go
index ea0f797..5e940d0 100644
--- a/internal/audio/voices_test.go
+++ b/internal/audio/voices_test.go
@@ -1,10 +1,13 @@
package audio
import (
+ "errors"
"reflect"
"testing"
)
+var errTestSentinel = errors.New("test sentinel error")
+
func TestVoiceLists(t *testing.T) {
t.Parallel()
@@ -57,3 +60,49 @@ func TestGeminiVoiceFallbacks(t *testing.T) {
}
})
}
+
+func TestRunWithVoiceFallbacks(t *testing.T) {
+ originalVoices := append([]string(nil), GeminiVoices...)
+ t.Cleanup(func() {
+ GeminiVoices = originalVoices
+ })
+ GeminiVoices = []string{"Charon", "Kore", "Leda"}
+
+ t.Run("retries no-audio errors until success", func(t *testing.T) {
+ var attempted []string
+ usedVoice, err := RunWithVoiceFallbacks("Charon", func(voice string) error {
+ attempted = append(attempted, voice)
+ if voice == "Charon" {
+ return ErrGeminiNoAudioData
+ }
+ return nil
+ })
+ if err != nil {
+ t.Fatalf("RunWithVoiceFallbacks() unexpected error: %v", err)
+ }
+ if usedVoice != "Kore" {
+ t.Fatalf("used voice = %q, want %q", usedVoice, "Kore")
+ }
+ if got, want := attempted, []string{"Charon", "Kore"}; !reflect.DeepEqual(got, want) {
+ t.Fatalf("attempted voices = %#v, want %#v", got, want)
+ }
+ })
+
+ t.Run("returns non-retryable errors immediately", func(t *testing.T) {
+ sentinel := errTestSentinel
+ var attempted []string
+ usedVoice, err := RunWithVoiceFallbacks("Charon", func(voice string) error {
+ attempted = append(attempted, voice)
+ return sentinel
+ })
+ if !errors.Is(err, sentinel) {
+ t.Fatalf("error = %v, want %v", err, sentinel)
+ }
+ if usedVoice != "" {
+ t.Fatalf("used voice = %q, want empty", usedVoice)
+ }
+ if got, want := attempted, []string{"Charon"}; !reflect.DeepEqual(got, want) {
+ t.Fatalf("attempted voices = %#v, want %#v", got, want)
+ }
+ })
+}