Share prepared audio names and speaker identities
This commit is contained in:
@@ -105,6 +105,8 @@ workspace directories, copies no files, and writes no manifest.
|
|||||||
|
|
||||||
## Stage 2: Share prepared-name and speaker-identity calculation
|
## Stage 2: Share prepared-name and speaker-identity calculation
|
||||||
|
|
||||||
|
**Complete.** Shared prepared names and speaker collision checks retain existing naming; audio and stage tests pass.
|
||||||
|
|
||||||
Read `localAudioDestinations`, `buildTranscribeJobs`, and
|
Read `localAudioDestinations`, `buildTranscribeJobs`, and
|
||||||
`internal/stage/audio_destinations_test.go`.
|
`internal/stage/audio_destinations_test.go`.
|
||||||
|
|
||||||
|
|||||||
@@ -48,8 +48,14 @@ func RequireRegularFile(name string) error {
|
|||||||
return nil
|
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.
|
// 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
|
var result []string
|
||||||
if len(files) != 0 {
|
if len(files) != 0 {
|
||||||
seen := make(map[string]bool, len(files))
|
seen := make(map[string]bool, len(files))
|
||||||
@@ -91,7 +97,15 @@ func DiscoverLocal(base string, files []string, dir string) ([]string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
sort.Strings(result)
|
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.
|
// 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 {
|
if len(result) == 0 {
|
||||||
return nil, fmt.Errorf("no audio candidates found under s3 audio prefix %q", prefix)
|
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
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package audio
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -37,7 +39,7 @@ func TestDiscoverLocalSelection(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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) {
|
if !reflect.DeepEqual(got, want) {
|
||||||
t.Fatalf("got %v, want %v", got, want)
|
t.Fatalf("got %v, want %v", got, want)
|
||||||
}
|
}
|
||||||
@@ -45,7 +47,7 @@ func TestDiscoverLocalSelection(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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) {
|
if !reflect.DeepEqual(got, want) {
|
||||||
t.Fatalf("got %v, want %v", 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)
|
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 (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/audio"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -17,8 +18,8 @@ func TestLocalAudioDestinationsPreserveDistinctRecordings(t *testing.T) {
|
|||||||
generated := "one-" + hex.EncodeToString(digest[:8]) + ".flac"
|
generated := "one-" + hex.EncodeToString(digest[:8]) + ".flac"
|
||||||
third := filepath.Join(t.TempDir(), generated)
|
third := filepath.Join(t.TempDir(), generated)
|
||||||
inputs := []string{first, second, third}
|
inputs := []string{first, second, third}
|
||||||
destinations := localAudioDestinations(inputs)
|
destinations := audio.PreparedNames(inputs)
|
||||||
if reversed := localAudioDestinations([]string{third, second, first}); !reflect.DeepEqual(reversed, destinations) {
|
if reversed := audio.PreparedNames([]string{third, second, first}); !reflect.DeepEqual(reversed, destinations) {
|
||||||
t.Fatal("destination ownership depends on input order")
|
t.Fatal("destination ownership depends on input order")
|
||||||
}
|
}
|
||||||
if destinations[third] != generated {
|
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 {
|
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 {
|
for _, src := range resolvedAudio {
|
||||||
dst := filepath.Join(paths.AudioDir, destinations[src])
|
dst := filepath.Join(paths.AudioDir, destinations[src])
|
||||||
checksum, err := copyFileIfChanged(env.ArtifactStore, src, dst)
|
checksum, err := copyFileIfChanged(env.ArtifactStore, src, dst)
|
||||||
@@ -378,43 +378,6 @@ func materializeLocalAudioInputs(env *Env, paths artifacts.SessionPaths, resolve
|
|||||||
return nil
|
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 {
|
type s3AudioMaterializationStats struct {
|
||||||
Reused int
|
Reused int
|
||||||
Downloads int
|
Downloads int
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/audio"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -213,7 +214,7 @@ func currentPrepareAudioSourceInputs(ctx context.Context, env *Env, m *manifest.
|
|||||||
return expected, nil
|
return expected, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
destinations := localAudioDestinations(local)
|
destinations := audio.PreparedNames(local)
|
||||||
expected := make([]prepareExpectedInput, 0, len(local))
|
expected := make([]prepareExpectedInput, 0, len(local))
|
||||||
for _, source := range local {
|
for _, source := range local {
|
||||||
checksum, err := env.ArtifactStore.Checksum(source)
|
checksum, err := env.ArtifactStore.Checksum(source)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package stage
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/audio"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -138,7 +139,7 @@ func prepareAudioSemantics(cfg *config.Config) prepareAudioSelection {
|
|||||||
selection := prepareAudioSelection{Mode: "local_directory"}
|
selection := prepareAudioSelection{Mode: "local_directory"}
|
||||||
if len(inputs.AudioFiles) > 0 {
|
if len(inputs.AudioFiles) > 0 {
|
||||||
selection.Mode = "local_files"
|
selection.Mode = "local_files"
|
||||||
destinations := localAudioDestinations(inputs.AudioFiles)
|
destinations := audio.PreparedNames(inputs.AudioFiles)
|
||||||
for _, name := range destinations {
|
for _, name := range destinations {
|
||||||
selection.LocalNames = append(selection.LocalNames, filepath.ToSlash(name))
|
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/adapters/whisperx"
|
||||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/audio"
|
||||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -178,16 +179,15 @@ func buildTranscribeJobs(m *manifest.Manifest, paths artifacts.SessionPaths) ([]
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
jobs := make([]transcribeJob, 0, len(audioFiles))
|
jobs := make([]transcribeJob, 0, len(audioFiles))
|
||||||
seenSpeaker := map[string]string{}
|
sources := make([]audio.Source, 0, len(audioFiles))
|
||||||
for _, audioPath := range audioFiles {
|
for _, audioPath := range audioFiles {
|
||||||
base := strings.TrimSuffix(filepath.Base(audioPath), filepath.Ext(audioPath))
|
sources = append(sources, audio.Source{Path: audioPath, PreparedName: filepath.Base(audioPath)})
|
||||||
if base == "" {
|
}
|
||||||
return nil, fmt.Errorf("could not derive speaker id from %q", audioPath)
|
if err := audio.ValidateSpeakerIDs(sources); err != nil {
|
||||||
}
|
return nil, err
|
||||||
if previous, exists := seenSpeaker[base]; exists {
|
}
|
||||||
return nil, fmt.Errorf("duplicate speaker/audio basename %q from %q and %q", base, previous, audioPath)
|
for _, audioPath := range audioFiles {
|
||||||
}
|
base := audio.SpeakerID(audioPath)
|
||||||
seenSpeaker[base] = audioPath
|
|
||||||
jobs = append(jobs, transcribeJob{
|
jobs = append(jobs, transcribeJob{
|
||||||
speakerID: base,
|
speakerID: base,
|
||||||
audioPath: audioPath,
|
audioPath: audioPath,
|
||||||
|
|||||||
Reference in New Issue
Block a user