Share prepared audio names and speaker identities
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
69
internal/audio/names.go
Normal 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
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package stage
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/audio"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
@@ -17,8 +18,8 @@ func TestLocalAudioDestinationsPreserveDistinctRecordings(t *testing.T) {
|
||||
generated := "one-" + hex.EncodeToString(digest[:8]) + ".flac"
|
||||
third := filepath.Join(t.TempDir(), generated)
|
||||
inputs := []string{first, second, third}
|
||||
destinations := localAudioDestinations(inputs)
|
||||
if reversed := localAudioDestinations([]string{third, second, first}); !reflect.DeepEqual(reversed, destinations) {
|
||||
destinations := audio.PreparedNames(inputs)
|
||||
if reversed := audio.PreparedNames([]string{third, second, first}); !reflect.DeepEqual(reversed, destinations) {
|
||||
t.Fatal("destination ownership depends on input order")
|
||||
}
|
||||
if destinations[third] != generated {
|
||||
|
||||
@@ -366,7 +366,7 @@ func resolveLocalAudioFiles(sessionDir string, inputs config.SessionInputsConfig
|
||||
}
|
||||
|
||||
func materializeLocalAudioInputs(env *Env, paths artifacts.SessionPaths, resolvedAudio []string, registerInput func(kind, path, checksum string)) error {
|
||||
destinations := localAudioDestinations(resolvedAudio)
|
||||
destinations := audio.PreparedNames(resolvedAudio)
|
||||
for _, src := range resolvedAudio {
|
||||
dst := filepath.Join(paths.AudioDir, destinations[src])
|
||||
checksum, err := copyFileIfChanged(env.ArtifactStore, src, dst)
|
||||
@@ -378,43 +378,6 @@ func materializeLocalAudioInputs(env *Env, paths artifacts.SessionPaths, resolve
|
||||
return nil
|
||||
}
|
||||
|
||||
func localAudioDestinations(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
|
||||
}
|
||||
|
||||
type s3AudioMaterializationStats struct {
|
||||
Reused int
|
||||
Downloads int
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/audio"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
@@ -213,7 +214,7 @@ func currentPrepareAudioSourceInputs(ctx context.Context, env *Env, m *manifest.
|
||||
return expected, nil
|
||||
}
|
||||
|
||||
destinations := localAudioDestinations(local)
|
||||
destinations := audio.PreparedNames(local)
|
||||
expected := make([]prepareExpectedInput, 0, len(local))
|
||||
for _, source := range local {
|
||||
checksum, err := env.ArtifactStore.Checksum(source)
|
||||
|
||||
@@ -2,6 +2,7 @@ package stage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/audio"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -138,7 +139,7 @@ func prepareAudioSemantics(cfg *config.Config) prepareAudioSelection {
|
||||
selection := prepareAudioSelection{Mode: "local_directory"}
|
||||
if len(inputs.AudioFiles) > 0 {
|
||||
selection.Mode = "local_files"
|
||||
destinations := localAudioDestinations(inputs.AudioFiles)
|
||||
destinations := audio.PreparedNames(inputs.AudioFiles)
|
||||
for _, name := range destinations {
|
||||
selection.LocalNames = append(selection.LocalNames, filepath.ToSlash(name))
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/whisperx"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/audio"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
)
|
||||
|
||||
@@ -178,16 +179,15 @@ func buildTranscribeJobs(m *manifest.Manifest, paths artifacts.SessionPaths) ([]
|
||||
return nil, err
|
||||
}
|
||||
jobs := make([]transcribeJob, 0, len(audioFiles))
|
||||
seenSpeaker := map[string]string{}
|
||||
sources := make([]audio.Source, 0, len(audioFiles))
|
||||
for _, audioPath := range audioFiles {
|
||||
base := strings.TrimSuffix(filepath.Base(audioPath), filepath.Ext(audioPath))
|
||||
if base == "" {
|
||||
return nil, fmt.Errorf("could not derive speaker id from %q", audioPath)
|
||||
}
|
||||
if previous, exists := seenSpeaker[base]; exists {
|
||||
return nil, fmt.Errorf("duplicate speaker/audio basename %q from %q and %q", base, previous, audioPath)
|
||||
}
|
||||
seenSpeaker[base] = audioPath
|
||||
sources = append(sources, audio.Source{Path: audioPath, PreparedName: filepath.Base(audioPath)})
|
||||
}
|
||||
if err := audio.ValidateSpeakerIDs(sources); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, audioPath := range audioFiles {
|
||||
base := audio.SpeakerID(audioPath)
|
||||
jobs = append(jobs, transcribeJob{
|
||||
speakerID: base,
|
||||
audioPath: audioPath,
|
||||
|
||||
Reference in New Issue
Block a user