From f3b5f58a171eeb47716e2d15a40c4c480d2d5ef9 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 2 Apr 2026 17:31:29 +0300 Subject: task 002: centralize Gemini voice fallbacks --- internal/audio/fallbacks.go | 29 +++++++++++++++++++++++++ internal/audio/voices_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 internal/audio/fallbacks.go (limited to 'internal/audio') 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) + } + }) +} -- cgit v1.2.3