summaryrefslogtreecommitdiff
path: root/internal/anki/audio_paths.go
blob: ca418cf66856a2aa5fc4714387a54035443aaff4 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package anki

import (
	"os"
	"path/filepath"
	"sort"
	"strings"
)

// ResolveAudioPaths returns the matching audio files for a logical base name.
// It prefers multi-voice outputs (audio_<voice>.<ext>) over a stale single file
// and uses metadata hints before falling back to common formats.
func ResolveAudioPaths(wordDir, baseName, preferredFormat string) []string {
	formats := audioFormatsToTry(wordDir, preferredFormat)

	// Prefer voice-specific files so multi-voice output wins over any stale
	// single-file audio that may be left behind in the directory.
	for _, format := range formats {
		globPattern := filepath.Join(wordDir, baseName+"_*."+format)
		matches, err := filepath.Glob(globPattern)
		if err == nil && len(matches) > 0 {
			sort.Strings(matches)
			return matches
		}
	}

	for _, format := range formats {
		exactPath := filepath.Join(wordDir, baseName+"."+format)
		if fileExists(exactPath) {
			return []string{exactPath}
		}
	}

	return nil
}

// ResolveAudioFile returns the first resolved audio file for a logical base name.
func ResolveAudioFile(wordDir, baseName, preferredFormat string) string {
	paths := ResolveAudioPaths(wordDir, baseName, preferredFormat)
	if len(paths) == 0 {
		return ""
	}

	return paths[0]
}

func audioFormatsToTry(wordDir, preferredFormat string) []string {
	var candidates []string
	appendFormat := func(format string) {
		format = strings.ToLower(strings.TrimSpace(format))
		if format == "" || containsString(candidates, format) {
			return
		}
		candidates = append(candidates, format)
	}

	appendFormat(readAudioFormatHint(wordDir))
	appendFormat(preferredFormat)
	appendFormat("wav")
	appendFormat("mp3")

	return candidates
}

func readAudioFormatHint(wordDir string) string {
	metadataFile := filepath.Join(wordDir, "audio_metadata.txt")
	data, err := os.ReadFile(metadataFile)
	if err != nil {
		return ""
	}

	for _, line := range strings.Split(string(data), "\n") {
		line = strings.TrimSpace(line)
		if strings.HasPrefix(line, "format=") {
			return strings.TrimSpace(strings.TrimPrefix(line, "format="))
		}
	}

	return ""
}

func containsString(values []string, want string) bool {
	for _, value := range values {
		if value == want {
			return true
		}
	}
	return false
}