diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-02 17:31:29 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-02 17:31:29 +0300 |
| commit | f3b5f58a171eeb47716e2d15a40c4c480d2d5ef9 (patch) | |
| tree | cf461894a613e0d7bad1a36e04dd420654514391 /internal/gui | |
| parent | 4d16684f92544d1cf90dfc5080378ca2f1abe18e (diff) | |
task 002: centralize Gemini voice fallbacks
Diffstat (limited to 'internal/gui')
| -rw-r--r-- | internal/gui/generator.go | 47 | ||||
| -rw-r--r-- | internal/gui/generator_test.go | 4 |
2 files changed, 20 insertions, 31 deletions
diff --git a/internal/gui/generator.go b/internal/gui/generator.go index a1ab22c..f3fe9b5 100644 --- a/internal/gui/generator.go +++ b/internal/gui/generator.go @@ -133,31 +133,6 @@ func (a *Application) generateAudioFile(ctx context.Context, text, outputFile, v return provider.GenerateAudio(ctx, text, outputFile) } -func (a *Application) generateGeminiAudioWithFallbacks(initialVoice string, generate func(voice string) error) (string, error) { - attempted := make([]string, 0, len(audio.GeminiVoices)) - var lastErr error - - for i, voice := range audio.GeminiVoiceFallbacks(initialVoice) { - if i > 0 { - fmt.Printf("Retrying Gemini audio with voice: %s\n", voice) - } - - attempted = append(attempted, voice) - err := generate(voice) - if err == nil { - return voice, nil - } - if !audio.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) -} - // translateWord translates a Bulgarian word to English func (a *Application) translateWord(word string) (string, error) { if a.translator == nil { @@ -207,7 +182,10 @@ func (a *Application) generateAudio(ctx context.Context, word string, cardDir st finalVoice := voice var err error if a.audioProviderName() == "gemini" && !a.geminiVoicePinned() { - finalVoice, err = a.generateGeminiAudioWithFallbacks(voice, func(candidate string) error { + finalVoice, err = audio.RunWithVoiceFallbacks(voice, func(candidate string) error { + if candidate != voice { + fmt.Printf("Retrying Gemini audio with voice: %s\n", candidate) + } return a.generateAudioFile(ctx, word, outputFile, candidate, speed) }) } else { @@ -246,7 +224,10 @@ func (a *Application) generateAudioFront(ctx context.Context, word string, cardD finalVoice := voice var err error if a.audioProviderName() == "gemini" && !a.geminiVoicePinned() { - finalVoice, err = a.generateGeminiAudioWithFallbacks(voice, func(candidate string) error { + finalVoice, err = audio.RunWithVoiceFallbacks(voice, func(candidate string) error { + if candidate != voice { + fmt.Printf("Retrying Gemini audio with voice: %s\n", candidate) + } return a.generateAudioFile(ctx, word, frontFile, candidate, speed) }) } else { @@ -283,7 +264,10 @@ func (a *Application) generateAudioBack(ctx context.Context, text string, cardDi finalVoice := voice var err error if a.audioProviderName() == "gemini" && !a.geminiVoicePinned() { - finalVoice, err = a.generateGeminiAudioWithFallbacks(voice, func(candidate string) error { + finalVoice, err = audio.RunWithVoiceFallbacks(voice, func(candidate string) error { + if candidate != voice { + fmt.Printf("Retrying Gemini audio with voice: %s\n", candidate) + } return a.generateAudioFile(ctx, text, backFile, candidate, speed) }) } else { @@ -336,7 +320,12 @@ func (a *Application) generateAudioBgBg(ctx context.Context, front, back, cardDi finalVoice := voice var err error if a.audioProviderName() == "gemini" && !a.geminiVoicePinned() { - finalVoice, err = a.generateGeminiAudioWithFallbacks(voice, runPair) + finalVoice, err = audio.RunWithVoiceFallbacks(voice, func(candidate string) error { + if candidate != voice { + fmt.Printf("Retrying Gemini audio with voice: %s\n", candidate) + } + return runPair(candidate) + }) } else { err = runPair(voice) } diff --git a/internal/gui/generator_test.go b/internal/gui/generator_test.go index 2828337..fcb8f51 100644 --- a/internal/gui/generator_test.go +++ b/internal/gui/generator_test.go @@ -398,11 +398,11 @@ func TestGenerateGeminiAudioWithFallbacksRetriesAlternateVoice(t *testing.T) { }, } - voice, err := app.generateGeminiAudioWithFallbacks("Charon", func(candidate string) error { + voice, err := audio.RunWithVoiceFallbacks("Charon", func(candidate string) error { return app.generateAudioFile(context.Background(), "ябълка", outputPath, candidate, 1.0) }) if err != nil { - t.Fatalf("generateGeminiAudioWithFallbacks() unexpected error: %v", err) + t.Fatalf("RunWithVoiceFallbacks() unexpected error: %v", err) } if voice != "Kore" { |
