fix: reuse identical music files instead of numbered copies
- compare size and SHA-256 before copying a track into the routine folder - keep the file the current routine already references when content matches - convert M4A/AAC into a scratch file and dedupe it by content - extend playback tests for repeated loads and same-name variants
This commit is contained in:
parent
2edd756920
commit
e23520e630
5 changed files with 169 additions and 34 deletions
|
|
@ -400,7 +400,10 @@ Fahrtrichtung.
|
||||||
einen `AudioStreamPlayer` auf derselben Zeitbasis. `AudioFile` erkennt
|
einen `AudioStreamPlayer` auf derselben Zeitbasis. `AudioFile` erkennt
|
||||||
MP3/OGG/WAV an den echten Dateibytes; M4A/AAC und falsch als `.mp3`
|
MP3/OGG/WAV an den echten Dateibytes; M4A/AAC und falsch als `.mp3`
|
||||||
benannte ISO-Container werden auf macOS mit `afconvert` zu mono
|
benannte ISO-Container werden auf macOS mit `afconvert` zu mono
|
||||||
PCM-WAV (44,1 kHz) konvertiert. Start,
|
PCM-WAV (44,1 kHz) konvertiert. Beim Kopieren in den Kür-Ordner wird der
|
||||||
|
Inhalt verglichen: identische Tracks werden wiederverwendet (auch die
|
||||||
|
bereits referenzierte Datei der Kür bleibt erhalten), nur bei
|
||||||
|
abweichendem Inhalt gleichen Namens entsteht ein nummerierter Name. Start,
|
||||||
Pause, Reset, Scrubbing und Geschwindigkeit wirken auf Bild und Ton. Über
|
Pause, Reset, Scrubbing und Geschwindigkeit wirken auf Bild und Ton. Über
|
||||||
der Timeline liegt eine Label-Spur: Labels von Segmenten und Kürzel von
|
der Timeline liegt eine Label-Spur: Labels von Segmenten und Kürzel von
|
||||||
Elementen ohne Freitext erscheinen schräg an ihrem Startzeitpunkt (auch
|
Elementen ohne Freitext erscheinen schräg an ihrem Startzeitpunkt (auch
|
||||||
|
|
|
||||||
|
|
@ -57,3 +57,23 @@ Neue `tests/run_audio_tests.gd`:
|
||||||
- Die konvertierte WAV-Datei ist deutlich größer als das M4A-Original
|
- Die konvertierte WAV-Datei ist deutlich größer als das M4A-Original
|
||||||
(mono 44,1 kHz, ca. 5 MB pro Minute), bleibt aber unter dem 64-MB-Limit
|
(mono 44,1 kHz, ca. 5 MB pro Minute), bleibt aber unter dem 64-MB-Limit
|
||||||
für übliche Kür-Musik.
|
für übliche Kür-Musik.
|
||||||
|
|
||||||
|
## Nachtrag: keine nummerierten Musik-Duplikate
|
||||||
|
|
||||||
|
Jedes erneute Laden desselben Stücks legte bisher eine neue Kopie an
|
||||||
|
(`… 2.mp3`, `… 3.mp3`, …), weil der Zielname nur auf Existenz geprüft wurde.
|
||||||
|
|
||||||
|
- `_resolve_music_copy_name()` vergleicht jetzt Größe und SHA-256 des
|
||||||
|
vorhandenen Ziels mit der Quelle.
|
||||||
|
- Identischer Inhalt → die vorhandene Datei wird weiterverwendet
|
||||||
|
(`needs_copy = false`). Das gilt auch für die Datei, die die aktuelle Kür
|
||||||
|
bereits referenziert, sodass ein erneutes Laden den Namen gar nicht ändert.
|
||||||
|
- Nur bei gleichem Namen und abweichendem Inhalt entsteht weiterhin ein
|
||||||
|
nummerierter Name.
|
||||||
|
- M4A/AAC wird zuerst in eine Scratch-Datei (`user://_music_import_*.wav`)
|
||||||
|
konvertiert und danach inhaltsbasiert einsortiert; die Scratch-Datei wird
|
||||||
|
immer entfernt.
|
||||||
|
|
||||||
|
Tests in `run_playback_tests.gd`: zweimal dasselbe Stück laden behält den
|
||||||
|
Namen und erzeugt kein `… 2.wav`; ein anderes Stück gleichen Namens bekommt
|
||||||
|
eine Nummer, die beim erneuten Laden stabil bleibt.
|
||||||
|
|
|
||||||
|
|
@ -159,6 +159,10 @@ Dateiname vermerkt.
|
||||||
(und tiefer), bei 2× schneller.
|
(und tiefer), bei 2× schneller.
|
||||||
- **×** entfernt die Musik aus der Kür, löscht die Datei aber nicht. So
|
- **×** entfernt die Musik aus der Kür, löscht die Datei aber nicht. So
|
||||||
bleibt sie für andere Kürdateien erhalten.
|
bleibt sie für andere Kürdateien erhalten.
|
||||||
|
- Dasselbe Stück kann beliebig oft geladen werden: Ist der Inhalt schon im
|
||||||
|
Kür-Ordner vorhanden, wird die vorhandene Datei weiterverwendet. Eine
|
||||||
|
Nummer im Namen (`… 2.mp3`) entsteht nur bei echt unterschiedlichem Inhalt
|
||||||
|
gleichen Namens.
|
||||||
- Beim **Sync** wird die Musikdatei wie die Kür-JSON hoch- und
|
- Beim **Sync** wird die Musikdatei wie die Kür-JSON hoch- und
|
||||||
heruntergeladen. Fehlt sie auf einem Gerät, zeigt die Leiste
|
heruntergeladen. Fehlt sie auf einem Gerät, zeigt die Leiste
|
||||||
„(fehlt)“; nach dem nächsten Sync ist sie verfügbar.
|
„(fehlt)“; nach dem nächsten Sync ist sie verfügbar.
|
||||||
|
|
|
||||||
|
|
@ -4425,15 +4425,19 @@ func load_music_from_path(path: String) -> bool:
|
||||||
|
|
||||||
var routines_absolute := ProjectSettings.globalize_path(ROUTINES_DIR)
|
var routines_absolute := ProjectSettings.globalize_path(ROUTINES_DIR)
|
||||||
if absolute_source.get_base_dir() != routines_absolute:
|
if absolute_source.get_base_dir() != routines_absolute:
|
||||||
file_name = _unique_music_file_name(file_name)
|
var resolved := _resolve_music_copy_name(absolute_source, file_name)
|
||||||
var target := ProjectSettings.globalize_path(ROUTINES_DIR.path_join(file_name))
|
file_name = resolved.name
|
||||||
var copy_error := DirAccess.copy_absolute(absolute_source, target)
|
if resolved.needs_copy:
|
||||||
if copy_error != OK:
|
var target := ProjectSettings.globalize_path(
|
||||||
_show_message(
|
ROUTINES_DIR.path_join(file_name)
|
||||||
"Die Musikdatei konnte nicht kopiert werden (Fehler %d)."
|
|
||||||
% copy_error
|
|
||||||
)
|
)
|
||||||
return false
|
var copy_error := DirAccess.copy_absolute(absolute_source, target)
|
||||||
|
if copy_error != OK:
|
||||||
|
_show_message(
|
||||||
|
"Die Musikdatei konnte nicht kopiert werden (Fehler %d)."
|
||||||
|
% copy_error
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
|
||||||
return _set_music_file(file_name)
|
return _set_music_file(file_name)
|
||||||
|
|
||||||
|
|
@ -4456,30 +4460,52 @@ func _import_converted_music(
|
||||||
if converted_name.is_empty():
|
if converted_name.is_empty():
|
||||||
_show_message("Der Dateiname der Musikdatei ist ungültig.")
|
_show_message("Der Dateiname der Musikdatei ist ungültig.")
|
||||||
return false
|
return false
|
||||||
converted_name = _unique_music_file_name(converted_name)
|
|
||||||
_ensure_routines_dir()
|
_ensure_routines_dir()
|
||||||
var target := ProjectSettings.globalize_path(
|
# Convert into a scratch file first so an already converted copy can be
|
||||||
ROUTINES_DIR.path_join(converted_name)
|
# reused by content instead of piling up numbered duplicates.
|
||||||
)
|
var scratch_name := "user://_music_import_%d.wav" % Time.get_ticks_usec()
|
||||||
|
var scratch_path := ProjectSettings.globalize_path(scratch_name)
|
||||||
var exit_code := AudioFile.convert_iso_media_to_wav(
|
var exit_code := AudioFile.convert_iso_media_to_wav(
|
||||||
absolute_source,
|
absolute_source,
|
||||||
target
|
scratch_path
|
||||||
)
|
)
|
||||||
if exit_code != 0 or not FileAccess.file_exists(target):
|
if exit_code != 0 or not FileAccess.file_exists(scratch_path):
|
||||||
|
if FileAccess.file_exists(scratch_path):
|
||||||
|
DirAccess.remove_absolute(scratch_path)
|
||||||
_show_message(
|
_show_message(
|
||||||
"Die Musikdatei konnte nicht konvertiert werden (Fehler %d)."
|
"Die Musikdatei konnte nicht konvertiert werden (Fehler %d)."
|
||||||
% exit_code
|
% exit_code
|
||||||
)
|
)
|
||||||
return false
|
return false
|
||||||
|
|
||||||
var converted_size := _file_size(target)
|
var converted_size := _file_size(scratch_path)
|
||||||
if converted_size <= 0 or converted_size > MAX_MUSIC_FILE_BYTES:
|
if converted_size <= 0 or converted_size > MAX_MUSIC_FILE_BYTES:
|
||||||
DirAccess.remove_absolute(target)
|
DirAccess.remove_absolute(scratch_path)
|
||||||
_show_message(
|
_show_message(
|
||||||
"Die konvertierte Musikdatei ist leer oder zu groß (max. 64 MB)."
|
"Die konvertierte Musikdatei ist leer oder zu groß (max. 64 MB)."
|
||||||
)
|
)
|
||||||
return false
|
return false
|
||||||
|
|
||||||
|
var resolved := _resolve_music_copy_name(scratch_path, converted_name)
|
||||||
|
converted_name = resolved.name
|
||||||
|
if resolved.needs_copy:
|
||||||
|
var target := ProjectSettings.globalize_path(
|
||||||
|
ROUTINES_DIR.path_join(converted_name)
|
||||||
|
)
|
||||||
|
var move_error := DirAccess.rename_absolute(scratch_path, target)
|
||||||
|
if move_error != OK:
|
||||||
|
move_error = DirAccess.copy_absolute(scratch_path, target)
|
||||||
|
DirAccess.remove_absolute(scratch_path)
|
||||||
|
if move_error != OK:
|
||||||
|
_show_message(
|
||||||
|
"Die Musikdatei konnte nicht gespeichert werden (Fehler %d)."
|
||||||
|
% move_error
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
else:
|
||||||
|
DirAccess.remove_absolute(scratch_path)
|
||||||
|
|
||||||
return _set_music_file(converted_name)
|
return _set_music_file(converted_name)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -4519,18 +4545,50 @@ func _clear_music() -> void:
|
||||||
_update_music_ui()
|
_update_music_ui()
|
||||||
|
|
||||||
|
|
||||||
func _unique_music_file_name(file_name: String) -> String:
|
## Picks the file name a new music copy is stored under. A file that already
|
||||||
if not FileAccess.file_exists(ROUTINES_DIR.path_join(file_name)):
|
## exists with identical content is reused instead of creating a numbered
|
||||||
return file_name
|
## duplicate, so loading the same track twice stays idempotent.
|
||||||
|
func _resolve_music_copy_name(
|
||||||
|
absolute_source: String,
|
||||||
|
file_name: String
|
||||||
|
) -> Dictionary:
|
||||||
|
# Keep the attachment this routine already references when it holds the
|
||||||
|
# same audio, so reloading a track does not even rename it.
|
||||||
|
var current := _routine.music_file if _routine != null else ""
|
||||||
|
if not current.is_empty():
|
||||||
|
var current_path := ROUTINES_DIR.path_join(current)
|
||||||
|
if (
|
||||||
|
current != file_name
|
||||||
|
and FileAccess.file_exists(current_path)
|
||||||
|
and _same_file_content(absolute_source, current_path)
|
||||||
|
):
|
||||||
|
return {"name": current, "needs_copy": false}
|
||||||
|
|
||||||
|
var target_path := ROUTINES_DIR.path_join(file_name)
|
||||||
|
if not FileAccess.file_exists(target_path):
|
||||||
|
return {"name": file_name, "needs_copy": true}
|
||||||
|
if _same_file_content(absolute_source, target_path):
|
||||||
|
return {"name": file_name, "needs_copy": false}
|
||||||
|
|
||||||
var base := file_name.get_basename()
|
var base := file_name.get_basename()
|
||||||
var extension := "." + file_name.get_extension()
|
var extension := "." + file_name.get_extension()
|
||||||
var index := 2
|
var index := 2
|
||||||
while true:
|
while true:
|
||||||
var candidate := "%s %d%s" % [base, index, extension]
|
var candidate := "%s %d%s" % [base, index, extension]
|
||||||
if not FileAccess.file_exists(ROUTINES_DIR.path_join(candidate)):
|
var candidate_path := ROUTINES_DIR.path_join(candidate)
|
||||||
return candidate
|
if not FileAccess.file_exists(candidate_path):
|
||||||
|
return {"name": candidate, "needs_copy": true}
|
||||||
|
if _same_file_content(absolute_source, candidate_path):
|
||||||
|
return {"name": candidate, "needs_copy": false}
|
||||||
index += 1
|
index += 1
|
||||||
return file_name
|
return {"name": file_name, "needs_copy": true}
|
||||||
|
|
||||||
|
|
||||||
|
static func _same_file_content(path_a: String, path_b: String) -> bool:
|
||||||
|
var size := _file_size(path_a)
|
||||||
|
if size <= 0 or size != _file_size(path_b):
|
||||||
|
return false
|
||||||
|
return FileAccess.get_sha256(path_a) == FileAccess.get_sha256(path_b)
|
||||||
|
|
||||||
|
|
||||||
func _load_music_stream() -> bool:
|
func _load_music_stream() -> bool:
|
||||||
|
|
|
||||||
|
|
@ -562,6 +562,52 @@ func _run() -> void:
|
||||||
"playback bar shows the music file"
|
"playback bar shows the music file"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Loading the very same track again must not create a numbered duplicate.
|
||||||
|
_check(
|
||||||
|
controller.load_music_from_path(music_source),
|
||||||
|
"the same music file can be loaded again"
|
||||||
|
)
|
||||||
|
_check(
|
||||||
|
controller.get_music_file_name() == "test_music.wav",
|
||||||
|
"reloading identical music keeps the existing file name"
|
||||||
|
)
|
||||||
|
_check(
|
||||||
|
not FileAccess.file_exists("user://routines/test_music 2.wav"),
|
||||||
|
"reloading identical music creates no numbered duplicate"
|
||||||
|
)
|
||||||
|
|
||||||
|
# A different track with the same name still gets its own numbered copy.
|
||||||
|
var alt_dir := "user://music_alt"
|
||||||
|
DirAccess.make_dir_recursive_absolute(
|
||||||
|
ProjectSettings.globalize_path(alt_dir)
|
||||||
|
)
|
||||||
|
var alt_source := alt_dir.path_join("test_music.wav")
|
||||||
|
_write_test_wav(alt_source, 16000)
|
||||||
|
_check(
|
||||||
|
controller.load_music_from_path(alt_source),
|
||||||
|
"a different track with the same name loads"
|
||||||
|
)
|
||||||
|
_check(
|
||||||
|
controller.get_music_file_name() == "test_music 2.wav",
|
||||||
|
"a different track is stored under a numbered name"
|
||||||
|
)
|
||||||
|
_check(
|
||||||
|
controller.load_music_from_path(alt_source),
|
||||||
|
"the numbered track can be loaded again"
|
||||||
|
)
|
||||||
|
_check(
|
||||||
|
controller.get_music_file_name() == "test_music 2.wav",
|
||||||
|
"reloading the numbered track keeps its name"
|
||||||
|
)
|
||||||
|
_check(
|
||||||
|
controller.load_music_from_path(music_source),
|
||||||
|
"switching back to the first track works"
|
||||||
|
)
|
||||||
|
_check(
|
||||||
|
controller.get_music_file_name() == "test_music.wav",
|
||||||
|
"switching back reuses the existing first copy"
|
||||||
|
)
|
||||||
|
|
||||||
controller.seek_playback(0.5)
|
controller.seek_playback(0.5)
|
||||||
controller.play()
|
controller.play()
|
||||||
_check(controller.music_player.playing, "music starts with routine playback")
|
_check(controller.music_player.playing, "music starts with routine playback")
|
||||||
|
|
@ -598,9 +644,14 @@ func _run() -> void:
|
||||||
FileAccess.file_exists("user://routines/test_music.wav"),
|
FileAccess.file_exists("user://routines/test_music.wav"),
|
||||||
"clearing keeps the file for other routines"
|
"clearing keeps the file for other routines"
|
||||||
)
|
)
|
||||||
DirAccess.remove_absolute(
|
for leftover: String in [
|
||||||
ProjectSettings.globalize_path("user://routines/test_music.wav")
|
"user://routines/test_music.wav",
|
||||||
)
|
"user://routines/test_music 2.wav",
|
||||||
|
]:
|
||||||
|
if FileAccess.file_exists(leftover):
|
||||||
|
DirAccess.remove_absolute(ProjectSettings.globalize_path(leftover))
|
||||||
|
DirAccess.remove_absolute(ProjectSettings.globalize_path(alt_source))
|
||||||
|
DirAccess.remove_absolute(ProjectSettings.globalize_path(alt_dir))
|
||||||
DirAccess.remove_absolute(ProjectSettings.globalize_path(music_source))
|
DirAccess.remove_absolute(ProjectSettings.globalize_path(music_source))
|
||||||
|
|
||||||
scene.queue_free()
|
scene.queue_free()
|
||||||
|
|
@ -613,27 +664,26 @@ func _run() -> void:
|
||||||
_finish()
|
_finish()
|
||||||
|
|
||||||
|
|
||||||
func _write_test_wav(path: String) -> void:
|
func _write_test_wav(path: String, sample_rate: int = 8000) -> void:
|
||||||
const SAMPLE_RATE := 8000
|
var data_size := sample_rate * 2
|
||||||
const DATA_SIZE := SAMPLE_RATE * 2
|
|
||||||
var file := FileAccess.open(path, FileAccess.WRITE)
|
var file := FileAccess.open(path, FileAccess.WRITE)
|
||||||
if file == null:
|
if file == null:
|
||||||
return
|
return
|
||||||
file.store_buffer("RIFF".to_ascii_buffer())
|
file.store_buffer("RIFF".to_ascii_buffer())
|
||||||
file.store_32(36 + DATA_SIZE)
|
file.store_32(36 + data_size)
|
||||||
file.store_buffer("WAVE".to_ascii_buffer())
|
file.store_buffer("WAVE".to_ascii_buffer())
|
||||||
file.store_buffer("fmt ".to_ascii_buffer())
|
file.store_buffer("fmt ".to_ascii_buffer())
|
||||||
file.store_32(16)
|
file.store_32(16)
|
||||||
file.store_16(1)
|
file.store_16(1)
|
||||||
file.store_16(1)
|
file.store_16(1)
|
||||||
file.store_32(SAMPLE_RATE)
|
file.store_32(sample_rate)
|
||||||
file.store_32(SAMPLE_RATE * 2)
|
file.store_32(sample_rate * 2)
|
||||||
file.store_16(2)
|
file.store_16(2)
|
||||||
file.store_16(16)
|
file.store_16(16)
|
||||||
file.store_buffer("data".to_ascii_buffer())
|
file.store_buffer("data".to_ascii_buffer())
|
||||||
file.store_32(DATA_SIZE)
|
file.store_32(data_size)
|
||||||
var silence := PackedByteArray()
|
var silence := PackedByteArray()
|
||||||
silence.resize(DATA_SIZE)
|
silence.resize(data_size)
|
||||||
file.store_buffer(silence)
|
file.store_buffer(silence)
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue