summaryrefslogtreecommitdiff
path: root/internal/processor
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-04-01 21:47:16 +0300
committerPaul Buetow <paul@buetow.org>2026-04-01 21:47:16 +0300
commitec8614a94b63cd58ae4aaeeac27f9c6d0612b215 (patch)
treed17760689651e731114616e04fd535ddf02a7c27 /internal/processor
parentf1c9f1a6294033e859641c0b82537aeb2866c69a (diff)
Fix multi-voice audio path resolution
Diffstat (limited to 'internal/processor')
-rw-r--r--internal/processor/processor.go52
-rw-r--r--internal/processor/processor_test.go39
2 files changed, 51 insertions, 40 deletions
diff --git a/internal/processor/processor.go b/internal/processor/processor.go
index 66b975e..813d5b3 100644
--- a/internal/processor/processor.go
+++ b/internal/processor/processor.go
@@ -550,10 +550,12 @@ func (p *Processor) GenerateAnkiFile() (string, error) {
// Find associated media files in the output directory
wordDir := p.findCardDirectory(bulgarian)
if wordDir != "" {
- // Look for audio file
- audioFile := filepath.Join(wordDir, fmt.Sprintf("audio.%s", audioFormat))
- if _, err := os.Stat(audioFile); err == nil {
- card.AudioFile = audioFile
+ cardType := internal.LoadCardType(wordDir)
+ if cardType.IsBgBg() {
+ card.AudioFile = anki.ResolveAudioFile(wordDir, "audio_front", audioFormat)
+ card.AudioFileBack = anki.ResolveAudioFile(wordDir, "audio_back", audioFormat)
+ } else {
+ card.AudioFile = anki.ResolveAudioFile(wordDir, "audio", audioFormat)
}
// Look for image file
@@ -812,35 +814,37 @@ func (p *Processor) isWordFullyProcessed(word string) bool {
audioFormat := p.effectiveAudioFormat()
if cardType.IsBgBg() {
- // For bg-bg cards, check for audio_front and audio_back
- frontAudio := filepath.Join(wordDir, fmt.Sprintf("audio_front.%s", audioFormat))
- backAudio := filepath.Join(wordDir, fmt.Sprintf("audio_back.%s", audioFormat))
- if _, err := os.Stat(frontAudio); os.IsNotExist(err) {
+ frontAudioFiles := anki.ResolveAudioPaths(wordDir, "audio_front", audioFormat)
+ backAudioFiles := anki.ResolveAudioPaths(wordDir, "audio_back", audioFormat)
+ if len(frontAudioFiles) == 0 || len(backAudioFiles) == 0 {
if os.Getenv("DEBUG_BATCH") != "" {
- fmt.Printf(" [DEBUG] No front audio file found: %s\n", frontAudio)
+ fmt.Printf(" [DEBUG] No bg-bg audio files found in %s\n", wordDir)
}
return false
}
- if _, err := os.Stat(backAudio); os.IsNotExist(err) {
+ for _, audioFile := range append(frontAudioFiles, backAudioFiles...) {
+ if _, err := os.Stat(audio.AttributionPath(audioFile)); os.IsNotExist(err) {
+ if os.Getenv("DEBUG_BATCH") != "" {
+ fmt.Printf(" [DEBUG] Missing attribution for audio file: %s\n", audioFile)
+ }
+ return false
+ }
+ }
+ } else {
+ // For en-bg cards, check for at least one resolved audio file and its metadata.
+ requiredFiles = append(requiredFiles, "audio_metadata.txt")
+
+ audioFiles := anki.ResolveAudioPaths(wordDir, "audio", audioFormat)
+ if len(audioFiles) == 0 {
if os.Getenv("DEBUG_BATCH") != "" {
- fmt.Printf(" [DEBUG] No back audio file found: %s\n", backAudio)
+ fmt.Printf(" [DEBUG] No audio files found in %s\n", wordDir)
}
return false
}
- } else {
- // For en-bg cards, check for standard audio file
- requiredFiles = append(requiredFiles,
- "audio_attribution.txt",
- "audio_metadata.txt",
- )
-
- audioFile := filepath.Join(wordDir, fmt.Sprintf("audio.%s", audioFormat))
- if _, err := os.Stat(audioFile); os.IsNotExist(err) {
- audioPattern := fmt.Sprintf("audio_*.%s", audioFormat)
- matches, _ := filepath.Glob(filepath.Join(wordDir, audioPattern))
- if len(matches) == 0 {
+ for _, audioFile := range audioFiles {
+ if _, err := os.Stat(audio.AttributionPath(audioFile)); os.IsNotExist(err) {
if os.Getenv("DEBUG_BATCH") != "" {
- fmt.Printf(" [DEBUG] No audio file found: %s or pattern %s\n", audioFile, audioPattern)
+ fmt.Printf(" [DEBUG] Missing attribution for audio file: %s\n", audioFile)
}
return false
}
diff --git a/internal/processor/processor_test.go b/internal/processor/processor_test.go
index 35ceb9b..77a9344 100644
--- a/internal/processor/processor_test.go
+++ b/internal/processor/processor_test.go
@@ -555,11 +555,18 @@ func TestGenerateAnkiFileUsesEffectiveAudioFormatForGemini(t *testing.T) {
p.translationCache.Add("ябълка", "apple")
wordDir := p.findOrCreateWordDirectory("ябълка")
- if err := os.WriteFile(filepath.Join(wordDir, "audio.wav"), []byte("audio data"), 0644); err != nil {
- t.Fatalf("failed to create wav audio file: %v", err)
- }
- if err := os.WriteFile(filepath.Join(wordDir, "phonetic.txt"), []byte("phonetic"), 0644); err != nil {
- t.Fatalf("failed to create phonetic file: %v", err)
+ for name, content := range map[string]string{
+ "audio_metadata.txt": "provider=gemini\nmodel=gemini-2.5-flash-preview-tts\nvoice=Kore\nspeed=1.00\nformat=wav\n",
+ "phonetic.txt": "phonetic",
+ "audio_alpha.wav": "audio data",
+ "audio_beta.wav": "audio data",
+ "audio_alpha_attribution.txt": "attribution",
+ "audio_beta_attribution.txt": "attribution",
+ "translation.txt": "ябълка = apple",
+ } {
+ if err := os.WriteFile(filepath.Join(wordDir, name), []byte(content), 0644); err != nil {
+ t.Fatalf("failed to create %s: %v", name, err)
+ }
}
outputPath, err := p.GenerateAnkiFile()
@@ -575,12 +582,12 @@ func TestGenerateAnkiFileUsesEffectiveAudioFormatForGemini(t *testing.T) {
t.Fatalf("failed to read generated CSV: %v", err)
}
cardID := filepath.Base(wordDir)
- if !strings.Contains(string(csvData), fmt.Sprintf("[sound:%s_audio.wav]", cardID)) {
- t.Fatalf("generated CSV did not reference wav audio for card %q: %s", cardID, csvData)
+ if !strings.Contains(string(csvData), fmt.Sprintf("[sound:%s_audio_alpha.wav]", cardID)) {
+ t.Fatalf("generated CSV did not reference the resolved multi-voice wav audio for card %q: %s", cardID, csvData)
}
}
-func TestIsWordFullyProcessedUsesEffectiveAudioFormatForGemini(t *testing.T) {
+func TestIsWordFullyProcessedUsesMultiVoiceAttributionFiles(t *testing.T) {
originalConfig := viper.New()
*originalConfig = *viper.GetViper()
defer func() {
@@ -598,22 +605,22 @@ func TestIsWordFullyProcessedUsesEffectiveAudioFormatForGemini(t *testing.T) {
p := NewProcessor(flags)
wordDir := p.findOrCreateWordDirectory("ябълка")
files := map[string]string{
- "translation.txt": "ябълка = apple",
- "phonetic.txt": "phonetic",
- "audio_metadata.txt": "provider=gemini\nmodel=gemini-2.5-flash-preview-tts\nvoice=Kore\nspeed=1.00\nformat=wav\n",
- "audio_attribution.txt": "attribution",
+ "translation.txt": "ябълка = apple",
+ "phonetic.txt": "phonetic",
+ "audio_metadata.txt": "provider=gemini\nmodel=gemini-2.5-flash-preview-tts\nvoice=Kore\nspeed=1.00\nformat=wav\n",
+ "audio_alpha.wav": "audio data",
+ "audio_beta.wav": "audio data",
+ "audio_alpha_attribution.txt": "attribution",
+ "audio_beta_attribution.txt": "attribution",
}
for name, content := range files {
if err := os.WriteFile(filepath.Join(wordDir, name), []byte(content), 0644); err != nil {
t.Fatalf("failed to create %s: %v", name, err)
}
}
- if err := os.WriteFile(filepath.Join(wordDir, "audio.wav"), []byte("audio data"), 0644); err != nil {
- t.Fatalf("failed to create wav audio file: %v", err)
- }
if !p.isWordFullyProcessed("ябълка") {
- t.Fatal("expected Gemini word with wav audio to be treated as fully processed")
+ t.Fatal("expected Gemini word with multi-voice wav audio to be treated as fully processed")
}
}