Share prepared audio names and speaker identities

This commit is contained in:
2026-12-02 03:13:58 +00:00
parent 9b59fa16b2
commit f902e50def
9 changed files with 159 additions and 55 deletions

View File

@@ -48,8 +48,14 @@ func RequireRegularFile(name string) error {
return nil
}
// Source associates a selected local path with its prepared basename.
type Source struct {
Path string
PreparedName string
}
// DiscoverLocal selects explicit files or scans a directory without writing workspace state.
func DiscoverLocal(base string, files []string, dir string) ([]string, error) {
func DiscoverLocal(base string, files []string, dir string) ([]Source, error) {
var result []string
if len(files) != 0 {
seen := make(map[string]bool, len(files))
@@ -91,7 +97,15 @@ func DiscoverLocal(base string, files []string, dir string) ([]string, error) {
}
}
sort.Strings(result)
return result, nil
names := PreparedNames(result)
sources := make([]Source, 0, len(result))
for _, source := range result {
sources = append(sources, Source{Path: source, PreparedName: names[source]})
}
if err := ValidateSpeakerIDs(sources); err != nil {
return nil, err
}
return sources, nil
}
// ObjectLister is the read-only storage boundary required for audio selection.
@@ -124,5 +138,12 @@ func DiscoverS3(ctx context.Context, store ObjectLister, prefix string) ([]stora
if len(result) == 0 {
return nil, fmt.Errorf("no audio candidates found under s3 audio prefix %q", prefix)
}
sources := make([]Source, 0, len(result))
for _, obj := range result {
sources = append(sources, Source{Path: obj.Key, PreparedName: path.Base(obj.Key)})
}
if err := ValidateSpeakerIDs(sources); err != nil {
return nil, err
}
return result, nil
}

View File

@@ -2,6 +2,8 @@ package audio
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"os"
"path/filepath"
@@ -37,7 +39,7 @@ func TestDiscoverLocalSelection(t *testing.T) {
if err != nil {
t.Fatal(err)
}
want := []string{filepath.Join(root, "one.MP3"), filepath.Join(root, "two.flac")}
want := []Source{{filepath.Join(root, "one.MP3"), "one.MP3"}, {filepath.Join(root, "two.flac"), "two.flac"}}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %v, want %v", got, want)
}
@@ -45,7 +47,7 @@ func TestDiscoverLocalSelection(t *testing.T) {
if err != nil {
t.Fatal(err)
}
want = []string{filepath.Join(root, "extensionless"), filepath.Join(root, "unfamiliar.xyz")}
want = []Source{{filepath.Join(root, "extensionless"), "extensionless"}, {filepath.Join(root, "unfamiliar.xyz"), "unfamiliar.xyz"}}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %v, want %v", got, want)
}
@@ -104,3 +106,47 @@ func TestDiscoverS3Selection(t *testing.T) {
t.Fatalf("listing error: %v", err)
}
}
func TestDiscoveryPreparedNamesAndSpeakerCollisions(t *testing.T) {
root := t.TempDir()
first := filepath.Join(root, "first", "speaker.flac")
second := filepath.Join(root, "second", "speaker.flac")
for _, file := range []string{first, second} {
if err := os.MkdirAll(filepath.Dir(file), 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(file, []byte("audio"), 0600); err != nil {
t.Fatal(err)
}
}
digest := sha256.Sum256([]byte(filepath.Clean(first)))
occupiedName := "speaker-" + hex.EncodeToString(digest[:8]) + ".flac"
occupied := filepath.Join(root, occupiedName)
if err := os.WriteFile(occupied, []byte("audio"), 0600); err != nil {
t.Fatal(err)
}
sources, err := DiscoverLocal(root, []string{first, second, occupied}, "")
if err != nil {
t.Fatal(err)
}
names := map[string]string{}
for _, source := range sources {
names[source.Path] = source.PreparedName
}
if names[first] != strings.TrimSuffix(occupiedName, ".flac")+"-1.flac" || names[occupied] != occupiedName || names[first] == names[second] {
t.Fatalf("unexpected names: %v", names)
}
mp3, wav := filepath.Join(root, "speaker.mp3"), filepath.Join(root, "speaker.wav")
for _, file := range []string{mp3, wav} {
if err := os.WriteFile(file, nil, 0600); err != nil {
t.Fatal(err)
}
}
if _, err := DiscoverLocal(root, []string{mp3, wav}, ""); err == nil || !strings.Contains(err.Error(), mp3) || !strings.Contains(err.Error(), wav) {
t.Fatalf("collision error: %v", err)
}
store := &discoveryLister{objects: []storage.ObjectInfo{{Key: "x/speaker.mp3"}, {Key: "x/speaker.wav"}}}
if _, err := DiscoverS3(context.Background(), store, "x/"); err == nil || !strings.Contains(err.Error(), "x/speaker.mp3") || !strings.Contains(err.Error(), "x/speaker.wav") {
t.Fatalf("remote collision: %v", err)
}
}

69
internal/audio/names.go Normal file
View File

@@ -0,0 +1,69 @@
package audio
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"path/filepath"
"sort"
"strings"
)
// PreparedNames preserves existing basename disambiguation for local sources.
func PreparedNames(audioPaths []string) map[string]string {
byBase := make(map[string][]string, len(audioPaths))
occupied := make(map[string]bool, len(audioPaths))
for _, audioPath := range audioPaths {
base := filepath.Base(audioPath)
byBase[base] = append(byBase[base], audioPath)
occupied[base] = true
}
destinations := make(map[string]string, len(audioPaths))
ordered := append([]string(nil), audioPaths...)
sort.Strings(ordered)
nextSuffix := make(map[string]int)
for _, source := range ordered {
if _, assigned := destinations[source]; assigned {
continue
}
base := filepath.Base(source)
if len(byBase[base]) == 1 {
destinations[source] = base
continue
}
extension := filepath.Ext(base)
stem := strings.TrimSuffix(base, extension)
digest := sha256.Sum256([]byte(filepath.Clean(source)))
generatedStem := stem + "-" + hex.EncodeToString(digest[:8])
candidate := generatedStem + extension
for occupied[candidate] {
nextSuffix[generatedStem+extension]++
candidate = fmt.Sprintf("%s-%d%s", generatedStem, nextSuffix[generatedStem+extension], extension)
}
destinations[source] = candidate
occupied[candidate] = true
}
return destinations
}
// SpeakerID derives the speaker identity from a prepared filename.
func SpeakerID(name string) string {
return strings.TrimSuffix(filepath.Base(name), filepath.Ext(name))
}
// ValidateSpeakerIDs rejects ambiguous prepared names and reports their sources.
func ValidateSpeakerIDs(sources []Source) error {
seen := map[string]string{}
for _, source := range sources {
id := SpeakerID(source.PreparedName)
if id == "" {
return fmt.Errorf("could not derive speaker id from %q", source.Path)
}
if previous, ok := seen[id]; ok {
return fmt.Errorf("duplicate speaker id %q from %q and %q; choose one encoding or rename recordings", id, previous, source.Path)
}
seen[id] = source.Path
}
return nil
}