fix: play M4A/AAC music on macOS via WAV conversion

- detect audio containers by content instead of file extension
- convert ISO media/M4A/AAC with afconvert to mono PCM WAV
- handle mislabeled .mp3 files and already referenced old routines
- show a clear not-playable state and add audio tests
This commit is contained in:
torsten 2026-11-10 20:04:25 +01:00
parent e28f8976f2
commit 2edd756920
13 changed files with 563 additions and 40 deletions

View file

@ -396,8 +396,11 @@ Fahrtrichtung.
sich innerhalb von zwei Sekunden aus. Steuerung: Icon-Knöpfe für sich innerhalb von zwei Sekunden aus. Steuerung: Icon-Knöpfe für
Start/Pause und Reset (Rekorder-Symbole, Tooltips nennen die Aktion), Start/Pause und Reset (Rekorder-Symbole, Tooltips nennen die Aktion),
Timeline-Scrubbing und Geschwindigkeit 0,25×–2,0×. Ein optionales Timeline-Scrubbing und Geschwindigkeit 0,25×–2,0×. Ein optionales
Musikstück (MP3/OGG/WAV) liegt neben der Kür-JSON in `user://routines/` Musikstück liegt neben der Kür-JSON in `user://routines/` und läuft über
und läuft über einen `AudioStreamPlayer` auf derselben Zeitbasis: Start, einen `AudioStreamPlayer` auf derselben Zeitbasis. `AudioFile` erkennt
MP3/OGG/WAV an den echten Dateibytes; M4A/AAC und falsch als `.mp3`
benannte ISO-Container werden auf macOS mit `afconvert` zu mono
PCM-WAV (44,1 kHz) konvertiert. 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
@ -527,7 +530,8 @@ ggf. eines nach `touchend` (300-ms-Guard).
Öffnen-Dialog zeigt `.json` und Legacy-`.tres`, wobei eine vorhandene Öffnen-Dialog zeigt `.json` und Legacy-`.tres`, wobei eine vorhandene
`.json` die gleichnamige `.tres` verdeckt. `.json` die gleichnamige `.tres` verdeckt.
`music_file` nennt nur den Dateinamen; die Audiodatei liegt flach daneben `music_file` nennt nur den Dateinamen; die Audiodatei liegt flach daneben
in `user://routines/` und wird vom Sync mitübertragen. in `user://routines/` und wird vom Sync mitübertragen. Nach einer
M4A/AAC-Konvertierung verweist `music_file` auf das erzeugte WAV.
`timeline_labels` speichert freie Timeline-Annotationen als `timeline_labels` speichert freie Timeline-Annotationen als
`{time_seconds, text}`. `{time_seconds, text}`.
- **Autosave** schreibt nach 1,5 s Inaktivität nur bei ungespeicherten - **Autosave** schreibt nach 1,5 s Inaktivität nur bei ungespeicherten

View file

@ -0,0 +1,59 @@
# Findings – Musikwiedergabe auf macOS (2026-11-10)
## Problem
Die Logs zeigten:
```text
ERROR: Failed to decode mp3 file. Make sure it is a valid mp3 audio file.
ERROR: This AudioStreamMP3 does not have an audio file assigned to it.
```
Die betroffenen Dateien hießen `.mp3`, waren aber tatsächlich
**M4A/AAC-Container** (`ftypM4A`). Godot spielt nur MP3, OGG Vorbis und WAV
nativ ab. Zusätzlich wählt `afconvert` seinen Demuxer über die
Dateiendung, sodass eine M4A-Datei mit `.mp3`-Endung auch dort zunächst
scheiterte.
## Umsetzung
- Neue Hilfsklasse `AudioFile` (`src/core/io/audio_file.gd`):
- erkennt MP3, OGG, WAV und ISO-Container (M4A/AAC/MP4) an den echten
Dateibytes, nicht an der Endung;
- lädt MP3/OGG/WAV über die Godot-Audio-Streams.
- macOS-Konvertierung mit `/usr/bin/afconvert`:
- Ausgabe als mono PCM-WAV, 44,1 kHz, 16 Bit;
- `-l 1` verhindert `WAVE_FORMAT_EXTENSIBLE`, das Godots WAV-Loader nicht
akzeptiert;
- `--no-filler` entfernt den `FLLR`-Block, den Godot ebenfalls nicht
erwartet;
- Dateien mit falscher Endung (z. B. M4A als `.mp3`) werden für die
Konvertierung als temporäre `.m4a`-Kopie bereitgestellt.
- `load_music_from_path()` konvertiert M4A/AAC beim Import und speichert das
erzeugte WAV in `user://routines/`.
- `_load_music_stream()` konvertiert auch bereits referenzierte Altdateien
beim Laden automatisch und verwendet danach das WAV.
- `FileDialog` bietet zusätzlich `.m4a` und `.aac` an; `RoutineFile`
akzeptiert diese Endungen.
- Nicht abspielbare Dateien zeigen in der Playback-Leiste
„(nicht abspielbar)“ und einen erklärenden Tooltip.
## Tests
Neue `tests/run_audio_tests.gd`:
- Header-Erkennung für MP3, OGG, WAV und als `.mp3` getarntes M4A.
- Laden eines PCM-WAV.
- macOS: WAV→M4A mit `afconvert`, M4A→WAV über `AudioFile`, Laden des
Ergebnisses.
- macOS: M4A als `.mp3` wird über den Endungs-Fallback korrekt konvertiert.
`./tools/test.sh` führt den neuen Audiotest mit aus.
## Grenzen
- M4A/AAC wird nur auf macOS automatisch konvertiert. Windows/Linux brauchen
weiterhin MP3, OGG oder WAV.
- 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
für übliche Kür-Musik.

View file

@ -143,9 +143,15 @@ die 2D-Funktionen sind davon nicht betroffen.
## Musik ## Musik
Ein Musikstück gehört optional zur Kür. **Musik…** in der Playback-Leiste Ein Musikstück gehört optional zur Kür. **Musik…** in der Playback-Leiste
öffnet die Dateiauswahl für **MP3** (auch OGG/WAV). Die Datei wird in den öffnet die Dateiauswahl für **MP3**, **OGG** und **WAV**. Auf macOS werden
Kür-Ordner kopiert und in der Kür-JSON als Dateiname vermerkt. zusätzlich **M4A/AAC** angeboten; sie werden beim Import automatisch in ein
PCM-WAV umgewandelt. Das erkennt auch Dateien, die nur falsch als `.mp3`
benannt sind. Auf anderen Systemen müssen M4A/AAC vorher extern konvertiert
werden. Die Datei wird in den Kür-Ordner kopiert und in der Kür-JSON als
Dateiname vermerkt.
- Kann eine Datei nicht geladen werden, zeigt die Leiste
„(nicht abspielbar)“ und einen Tooltip mit den unterstützten Formaten.
- Die Symbolknöpfe **Start/Pause** (▶/⏸) und **Reset** (⏮) steuern Bild und - Die Symbolknöpfe **Start/Pause** (▶/⏸) und **Reset** (⏮) steuern Bild und
Musik gemeinsam; die Tooltips nennen die jeweilige Aktion. Musik gemeinsam; die Tooltips nennen die jeweilige Aktion.
- **Timeline-Scrubbing** setzt auch die Musik an die entsprechende Stelle. - **Timeline-Scrubbing** setzt auch die Musik an die entsprechende Stelle.

View file

@ -32,9 +32,9 @@ Voraussetzung für Phase 1.
Session-Restore (Legacy-`.tres` lesbar) Session-Restore (Legacy-`.tres` lesbar)
- [x] Playback-Dot mit Blickrichtung, Timeline, Play/Pause/Reset und - [x] Playback-Dot mit Blickrichtung, Timeline, Play/Pause/Reset und
Geschwindigkeit 0,25×–2,0× Geschwindigkeit 0,25×–2,0×
- [x] Musikstück (MP3/OGG/WAV) je Kür laden; Wiedergabe, Scrubbing und - [x] Musikstück (MP3/OGG/WAV; M4A/AAC auf macOS automatisch nach WAV
Geschwindigkeit laufen synchron zum Läufer; der Cloud-Sync überträgt die konvertiert) je Kür laden; Wiedergabe, Scrubbing und Geschwindigkeit laufen
Musikdatei mit der Kür synchron zum Läufer; der Cloud-Sync überträgt die Musikdatei mit der Kür
- [x] Timeline-Labels: Kür-Labels schräg auf der Timeline, eigene Labels per - [x] Timeline-Labels: Kür-Labels schräg auf der Timeline, eigene Labels per
Klick anlegen, bearbeiten und löschen Klick anlegen, bearbeiten und löschen
- [x] Abschnittswechsel setzt Abspielfigur und Timeline an den - [x] Abschnittswechsel setzt Abspielfigur und Timeline an den

View file

@ -0,0 +1,133 @@
class_name AudioFile
extends RefCounted
## Detects audio containers by their actual bytes and loads the formats Godot
## can play. macOS can convert M4A/AAC/ISO media to PCM WAV via the built-in
## `afconvert`, which also covers files that merely use a `.mp3` extension.
enum Format {
UNKNOWN,
MP3,
OGG,
WAV,
ISO_MEDIA,
}
const AF_CONVERT := "/usr/bin/afconvert"
static func detect_format(path: String) -> Format:
var file := FileAccess.open(path, FileAccess.READ)
if file == null:
return Format.UNKNOWN
var header := file.get_buffer(mini(file.get_length(), 16))
file.close()
if header.size() >= 4:
var magic := header.slice(0, 4).get_string_from_ascii()
if magic == "OggS":
return Format.OGG
if magic == "RIFF" and _ascii_at(header, 8, 4) == "WAVE":
return Format.WAV
if _ascii_at(header, 4, 4) == "ftyp":
return Format.ISO_MEDIA
if _ascii_at(header, 0, 3) == "ID3":
return Format.MP3
if header.size() >= 2 and header[0] == 0xFF and (header[1] & 0xE0) == 0xE0:
return Format.MP3
return Format.UNKNOWN
static func load_stream(path: String) -> AudioStream:
match detect_format(path):
Format.MP3:
var file := FileAccess.open(path, FileAccess.READ)
if file == null:
return null
var data := file.get_buffer(file.get_length())
file.close()
var stream := AudioStreamMP3.new()
stream.data = data
return stream if stream.get_length() > 0.0 else null
Format.OGG:
return AudioStreamOggVorbis.load_from_file(path)
Format.WAV:
return AudioStreamWAV.load_from_file(path)
return null
static func can_convert_iso_media() -> bool:
return OS.get_name() == "macOS" and FileAccess.file_exists(AF_CONVERT)
## Converts any container `afconvert` understands (M4A/AAC/ISO media) to mono
## 44.1 kHz PCM WAV. Returns the process exit code.
static func convert_iso_media_to_wav(
source_absolute: String,
target_absolute: String
) -> int:
if not can_convert_iso_media():
return ERR_UNAVAILABLE
# afconvert chooses its demuxer from the file extension. A common macOS
# case is M4A/AAC content saved as ".mp3"; give the converter a copy with
# the correct extension.
var conversion_source := source_absolute
var temporary_source := ""
var extension := source_absolute.get_extension().to_lower()
if extension not in ["m4a", "mp4", "m4b", "mov", "aac"]:
temporary_source = target_absolute + ".source.m4a"
if FileAccess.file_exists(temporary_source):
DirAccess.remove_absolute(temporary_source)
var copy_error := DirAccess.copy_absolute(
source_absolute,
temporary_source
)
if copy_error != OK:
return copy_error
conversion_source = temporary_source
var output: Array = []
var exit_code := OS.execute(
AF_CONVERT,
PackedStringArray([
"-f",
"WAVE",
"-d",
"LEI16@44100",
"-c",
"1",
"-l",
"1",
"--no-filler",
conversion_source,
target_absolute,
]),
output,
true
)
if not temporary_source.is_empty():
DirAccess.remove_absolute(temporary_source)
return exit_code
static func format_name(format: Format) -> String:
match format:
Format.MP3:
return "MP3"
Format.OGG:
return "OGG"
Format.WAV:
return "WAV"
Format.ISO_MEDIA:
return "M4A/AAC"
return "unbekannt"
static func _ascii_at(
data: PackedByteArray,
start: int,
length: int
) -> String:
if start < 0 or length <= 0 or start + length > data.size():
return ""
return data.slice(start, start + length).get_string_from_ascii()

View file

@ -0,0 +1 @@
uid://6v5uyq33634n

View file

@ -34,7 +34,7 @@ const MAX_TIMELINE_LABEL_LENGTH := 60
## Audio files that may belong to a routine. They live next to the routine ## Audio files that may belong to a routine. They live next to the routine
## JSON in `user://routines/` and are transferred by the routine sync. ## JSON in `user://routines/` and are transferred by the routine sync.
const AUDIO_EXTENSIONS := [".mp3", ".ogg", ".wav"] const AUDIO_EXTENSIONS := [".mp3", ".ogg", ".wav", ".m4a", ".aac"]
## Only the model resources may be referenced by a legacy `.tres`. The list is ## Only the model resources may be referenced by a legacy `.tres`. The list is
## deliberately small; it contains no script with an `_init` side effect. ## deliberately small; it contains no script with an `_init` side effect.

View file

@ -3896,7 +3896,7 @@ func _setup_playback_controls() -> void:
play_button.disabled = true play_button.disabled = true
reset_playback_button.disabled = true reset_playback_button.disabled = true
music_file_dialog.filters = PackedStringArray([ music_file_dialog.filters = PackedStringArray([
"*.mp3 ; MP3-Musik", "*.mp3, *.m4a, *.aac ; MP3 / M4A / AAC",
"*.ogg ; OGG-Musik", "*.ogg ; OGG-Musik",
"*.wav ; WAV-Audio", "*.wav ; WAV-Audio",
]) ])
@ -4373,14 +4373,16 @@ func _on_music_file_selected(path: String) -> void:
## Copies the selected track into the routines folder and references it from ## Copies the selected track into the routines folder and references it from
## the routine. Keeping the file next to the JSON makes it part of the sync. ## the routine. M4A/AAC and mislabeled MP3 files are converted to WAV on macOS.
func load_music_from_path(path: String) -> bool: func load_music_from_path(path: String) -> bool:
if _routine == null: if _routine == null:
return false return false
var source := path.strip_edges() var source := path.strip_edges()
if not RoutineFile.is_audio_file(source): if not RoutineFile.is_audio_file(source):
_show_message("Bitte eine MP3-, OGG- oder WAV-Datei auswählen.") _show_message(
"Bitte eine MP3-, OGG-, WAV-, M4A- oder AAC-Datei auswählen."
)
return false return false
var absolute_source := ProjectSettings.globalize_path(source) var absolute_source := ProjectSettings.globalize_path(source)
@ -4388,19 +4390,33 @@ func load_music_from_path(path: String) -> bool:
_show_message("Die Musikdatei wurde nicht gefunden:\n%s" % source) _show_message("Die Musikdatei wurde nicht gefunden:\n%s" % source)
return false return false
var file := FileAccess.open(absolute_source, FileAccess.READ) var source_size := _file_size(absolute_source)
if file == null: if source_size <= 0:
_show_message("Die Musikdatei ist nicht lesbar:\n%s" % source)
return false
var size := file.get_length()
file.close()
if size <= 0:
_show_message("Die Musikdatei ist leer:\n%s" % source) _show_message("Die Musikdatei ist leer:\n%s" % source)
return false return false
if size > MAX_MUSIC_FILE_BYTES: if source_size > MAX_MUSIC_FILE_BYTES:
_show_message("Die Musikdatei ist zu groß (max. 64 MB).") _show_message("Die Musikdatei ist zu groß (max. 64 MB).")
return false return false
var format := AudioFile.detect_format(absolute_source)
var extension := source.get_extension().to_lower()
if (
format == AudioFile.Format.ISO_MEDIA
or extension == "m4a"
or extension == "aac"
):
return _import_converted_music(absolute_source, source)
if (
format != AudioFile.Format.MP3
and format != AudioFile.Format.OGG
and format != AudioFile.Format.WAV
):
_show_message(
"Die Datei ist keine gültige MP3-, OGG- oder WAV-Datei."
)
return false
_ensure_routines_dir() _ensure_routines_dir()
var file_name := RoutineFile.safe_music_file_name(source.get_file()) var file_name := RoutineFile.safe_music_file_name(source.get_file())
if file_name.is_empty(): if file_name.is_empty():
@ -4419,8 +4435,64 @@ func load_music_from_path(path: String) -> bool:
) )
return false return false
return _set_music_file(file_name)
func _import_converted_music(
absolute_source: String,
source_path: String
) -> bool:
if not AudioFile.can_convert_iso_media():
_show_message(
"Die Datei ist eine M4A/AAC-Datei. Auf diesem System bitte als "
+ "MP3, OGG oder WAV speichern."
)
return false
var base := source_path.get_file().get_basename()
if base.is_empty():
base = "musik"
var converted_name := RoutineFile.safe_music_file_name(base + ".wav")
if converted_name.is_empty():
_show_message("Der Dateiname der Musikdatei ist ungültig.")
return false
converted_name = _unique_music_file_name(converted_name)
_ensure_routines_dir()
var target := ProjectSettings.globalize_path(
ROUTINES_DIR.path_join(converted_name)
)
var exit_code := AudioFile.convert_iso_media_to_wav(
absolute_source,
target
)
if exit_code != 0 or not FileAccess.file_exists(target):
_show_message(
"Die Musikdatei konnte nicht konvertiert werden (Fehler %d)."
% exit_code
)
return false
var converted_size := _file_size(target)
if converted_size <= 0 or converted_size > MAX_MUSIC_FILE_BYTES:
DirAccess.remove_absolute(target)
_show_message(
"Die konvertierte Musikdatei ist leer oder zu groß (max. 64 MB)."
)
return false
return _set_music_file(converted_name)
func _set_music_file(file_name: String) -> bool:
var previous := _routine.music_file
_routine.music_file = file_name _routine.music_file = file_name
_load_music_stream() if not _load_music_stream():
_routine.music_file = previous
_load_music_stream()
_show_message(
"Die Musikdatei konnte nicht geladen werden:\n%s" % file_name
)
return false
_mark_dirty() _mark_dirty()
_update_music_ui() _update_music_ui()
if _playing: if _playing:
@ -4428,6 +4500,15 @@ func load_music_from_path(path: String) -> bool:
return true return true
static func _file_size(path: String) -> int:
var file := FileAccess.open(path, FileAccess.READ)
if file == null:
return 0
var size := file.get_length()
file.close()
return size
func _clear_music() -> void: func _clear_music() -> void:
if _routine == null or not _routine.has_music(): if _routine == null or not _routine.has_music():
return return
@ -4464,7 +4545,24 @@ func _load_music_stream() -> bool:
var stream := _read_audio_stream(path) var stream := _read_audio_stream(path)
if stream == null: if stream == null:
return false var converted_name := _converted_music_name(_routine.music_file)
if not _needs_audio_conversion(path, _routine.music_file):
return false
var converted_path := ROUTINES_DIR.path_join(converted_name)
if (
not FileAccess.file_exists(converted_path)
or _read_audio_stream(converted_path) == null
):
var exit_code := AudioFile.convert_iso_media_to_wav(
ProjectSettings.globalize_path(path),
ProjectSettings.globalize_path(converted_path)
)
if exit_code != 0 or not FileAccess.file_exists(converted_path):
return false
stream = _read_audio_stream(converted_path)
if stream == null:
return false
_routine.music_file = converted_name
music_player.stream = stream music_player.stream = stream
music_player.pitch_scale = _playback_speed music_player.pitch_scale = _playback_speed
@ -4472,21 +4570,26 @@ func _load_music_stream() -> bool:
func _read_audio_stream(path: String) -> AudioStream: func _read_audio_stream(path: String) -> AudioStream:
match path.get_extension().to_lower(): return AudioFile.load_stream(path)
"mp3":
var file := FileAccess.open(path, FileAccess.READ)
if file == null: func _needs_audio_conversion(path: String, file_name: String) -> bool:
return null var extension := file_name.get_extension().to_lower()
var data := file.get_buffer(file.get_length()) return (
file.close() extension == "m4a"
var stream := AudioStreamMP3.new() or extension == "aac"
stream.data = data or AudioFile.detect_format(path) == AudioFile.Format.ISO_MEDIA
return stream )
"ogg":
return AudioStreamOggVorbis.load_from_file(path)
"wav": func _converted_music_name(file_name: String) -> String:
return AudioStreamWAV.load_from_file(path) var base := file_name.get_basename()
return null if base.is_empty():
base = "musik"
var converted := RoutineFile.safe_music_file_name(
base + " (konvertiert).wav"
)
return converted if not converted.is_empty() else "musik (konvertiert).wav"
func _start_music() -> void: func _start_music() -> void:
@ -4557,8 +4660,16 @@ func _update_music_ui() -> void:
var path := ROUTINES_DIR.path_join(file_name) var path := ROUTINES_DIR.path_join(file_name)
if FileAccess.file_exists(path): if FileAccess.file_exists(path):
music_label.text = "♪ " + file_name if music_player.stream == null:
music_label.tooltip_text = "Musik: %s\n%s" % [file_name, path] music_label.text = "♪ " + file_name + " (nicht abspielbar)"
music_label.tooltip_text = (
"Die Musikdatei kann nicht geladen werden. Unterstützt werden "
+ "MP3, OGG und WAV; M4A/AAC wird auf macOS automatisch "
+ "nach WAV konvertiert.\n%s" % path
)
else:
music_label.text = "♪ " + file_name
music_label.tooltip_text = "Musik: %s\n%s" % [file_name, path]
else: else:
music_label.text = "♪ " + file_name + " (fehlt)" music_label.text = "♪ " + file_name + " (fehlt)"
music_label.tooltip_text = ( music_label.tooltip_text = (

View file

@ -52,6 +52,9 @@ $GODOT --headless --path rinkplanner --script res://tests/run_history_tests.gd
# Run playback dot/timeline tests # Run playback dot/timeline tests
$GODOT --headless --path rinkplanner --script res://tests/run_playback_tests.gd $GODOT --headless --path rinkplanner --script res://tests/run_playback_tests.gd
# Run audio format detection and macOS M4A/AAC conversion tests
$GODOT --headless --path rinkplanner --script res://tests/run_audio_tests.gd
# Run segment timing/presets/calibration tests # Run segment timing/presets/calibration tests
$GODOT --headless --path rinkplanner --script res://tests/run_timing_tests.gd $GODOT --headless --path rinkplanner --script res://tests/run_timing_tests.gd

View file

@ -0,0 +1,200 @@
extends SceneTree
## Headless tests for audio container detection and macOS M4A/AAC conversion.
##
## Run with:
## godot --headless --path rinkplanner --script res://tests/run_audio_tests.gd
const AF_CONVERT := "/usr/bin/afconvert"
const MP3_HEADER_PATH := "user://audio_test_header.mp3"
const OGG_HEADER_PATH := "user://audio_test_header.ogg"
const WAV_HEADER_PATH := "user://audio_test_header.wav"
const M4A_HEADER_PATH := "user://audio_test_header_disguised.mp3"
const WAV_PATH := "user://audio_test_tone.wav"
const M4A_PATH := "user://audio_test_tone.m4a"
const DISGUISED_PATH := "user://audio_test_disguised.mp3"
const CONVERTED_PATH := "user://audio_test_converted.wav"
var _assertions: int = 0
var _failures: PackedStringArray = PackedStringArray()
func _init() -> void:
_run.call_deferred()
func _run() -> void:
_test_header_detection()
_test_wav_loading()
_test_macos_conversion()
_cleanup()
_finish()
func _test_header_detection() -> void:
_write_bytes(MP3_HEADER_PATH, PackedByteArray([0x49, 0x44, 0x33, 0x04]))
_write_bytes(OGG_HEADER_PATH, "OggS".to_ascii_buffer())
var wav_header := PackedByteArray()
wav_header.append_array("RIFF".to_ascii_buffer())
wav_header.append_array(PackedByteArray([36, 0, 0, 0]))
wav_header.append_array("WAVE".to_ascii_buffer())
_write_bytes(WAV_HEADER_PATH, wav_header)
var m4a_header := PackedByteArray([0, 0, 0, 32])
m4a_header.append_array("ftyp".to_ascii_buffer())
m4a_header.append_array("M4A ".to_ascii_buffer())
_write_bytes(M4A_HEADER_PATH, m4a_header)
_check(
AudioFile.detect_format(MP3_HEADER_PATH) == AudioFile.Format.MP3,
"ID3 files are detected as MP3"
)
_check(
AudioFile.detect_format(OGG_HEADER_PATH) == AudioFile.Format.OGG,
"OggS files are detected as OGG"
)
_check(
AudioFile.detect_format(WAV_HEADER_PATH) == AudioFile.Format.WAV,
"RIFF/WAVE files are detected as WAV"
)
_check(
AudioFile.detect_format(M4A_HEADER_PATH) == AudioFile.Format.ISO_MEDIA,
"a mislabeled M4A file is detected by its ftyp header"
)
func _test_wav_loading() -> void:
_write_test_wav(WAV_PATH)
var stream := AudioFile.load_stream(WAV_PATH)
_check(stream != null, "a PCM WAV file loads into an audio stream")
if stream != null:
_check(stream.get_length() > 0.0, "the loaded WAV has a duration")
func _test_macos_conversion() -> void:
if OS.get_name() != "macOS" or not FileAccess.file_exists(AF_CONVERT):
print("SKIP: macOS afconvert not available")
return
var output: Array = []
var convert_to_m4a := OS.execute(
AF_CONVERT,
PackedStringArray([
"-f",
"m4af",
"-d",
"aac",
ProjectSettings.globalize_path(WAV_PATH),
ProjectSettings.globalize_path(M4A_PATH),
]),
output,
true
)
if convert_to_m4a != 0 or not FileAccess.file_exists(M4A_PATH):
_check(false, "test fixture M4A can be created with afconvert")
return
_check(
AudioFile.detect_format(M4A_PATH) == AudioFile.Format.ISO_MEDIA,
"the generated M4A file is detected correctly"
)
var convert_to_wav := AudioFile.convert_iso_media_to_wav(
ProjectSettings.globalize_path(M4A_PATH),
ProjectSettings.globalize_path(CONVERTED_PATH)
)
_check(convert_to_wav == 0, "M4A converts to WAV on macOS")
_check(
FileAccess.file_exists(CONVERTED_PATH),
"the converted WAV exists"
)
var stream := AudioFile.load_stream(CONVERTED_PATH)
_check(stream != null, "the converted WAV loads as an audio stream")
if stream != null:
_check(stream.get_length() > 0.0, "the converted WAV has a duration")
var copy_error := DirAccess.copy_absolute(
ProjectSettings.globalize_path(M4A_PATH),
ProjectSettings.globalize_path(DISGUISED_PATH)
)
_check(copy_error == OK, "the M4A fixture can be disguised as MP3")
if copy_error == OK:
_check(
AudioFile.detect_format(DISGUISED_PATH) == AudioFile.Format.ISO_MEDIA,
"the disguised M4A is still detected by content"
)
var disguised_convert := AudioFile.convert_iso_media_to_wav(
ProjectSettings.globalize_path(DISGUISED_PATH),
ProjectSettings.globalize_path(CONVERTED_PATH)
)
_check(
disguised_convert == 0,
"a mislabeled M4A file converts through the extension fallback"
)
_check(
AudioFile.load_stream(CONVERTED_PATH) != null,
"the converted disguised file loads"
)
func _write_bytes(path: String, data: PackedByteArray) -> void:
var file := FileAccess.open(path, FileAccess.WRITE)
if file == null:
return
file.store_buffer(data)
file.close()
func _write_test_wav(path: String) -> void:
const SAMPLE_RATE := 8000
const DATA_SIZE := SAMPLE_RATE * 2
var file := FileAccess.open(path, FileAccess.WRITE)
if file == null:
return
file.store_buffer("RIFF".to_ascii_buffer())
file.store_32(36 + DATA_SIZE)
file.store_buffer("WAVE".to_ascii_buffer())
file.store_buffer("fmt ".to_ascii_buffer())
file.store_32(16)
file.store_16(1)
file.store_16(1)
file.store_32(SAMPLE_RATE)
file.store_32(SAMPLE_RATE * 2)
file.store_16(2)
file.store_16(16)
file.store_buffer("data".to_ascii_buffer())
file.store_32(DATA_SIZE)
var silence := PackedByteArray()
silence.resize(DATA_SIZE)
file.store_buffer(silence)
file.close()
func _cleanup() -> void:
for path: String in [
MP3_HEADER_PATH,
OGG_HEADER_PATH,
WAV_HEADER_PATH,
M4A_HEADER_PATH,
WAV_PATH,
M4A_PATH,
DISGUISED_PATH,
CONVERTED_PATH,
]:
if FileAccess.file_exists(path):
DirAccess.remove_absolute(ProjectSettings.globalize_path(path))
func _check(condition: bool, message: String) -> void:
_assertions += 1
if not condition:
_failures.append(message)
func _finish() -> void:
if _failures.is_empty():
print("PASS: %d assertions" % _assertions)
quit(0)
return
for failure: String in _failures:
printerr("FAIL: %s" % failure)
printerr("%d of %d assertions failed" % [_failures.size(), _assertions])
quit(1)

View file

@ -0,0 +1 @@
uid://tl4wlfrrq5cg

View file

@ -236,7 +236,9 @@ func _test_json_rejects() -> void:
_check( _check(
RoutineFile.is_audio_file("song.MP3") RoutineFile.is_audio_file("song.MP3")
and RoutineFile.is_audio_file("song.ogg") and RoutineFile.is_audio_file("song.ogg")
and RoutineFile.is_audio_file("song.wav"), and RoutineFile.is_audio_file("song.wav")
and RoutineFile.is_audio_file("song.M4A")
and RoutineFile.is_audio_file("song.aac"),
"audio extensions are recognised case-insensitively" "audio extensions are recognised case-insensitively"
) )

View file

@ -57,6 +57,9 @@ fi
"$GODOT" --headless --path "$PROJECT_DIR" \ "$GODOT" --headless --path "$PROJECT_DIR" \
--script res://tests/run_playback_tests.gd --script res://tests/run_playback_tests.gd
"$GODOT" --headless --path "$PROJECT_DIR" \
--script res://tests/run_audio_tests.gd
"$GODOT" --headless --path "$PROJECT_DIR" \ "$GODOT" --headless --path "$PROJECT_DIR" \
--script res://tests/run_timing_tests.gd --script res://tests/run_timing_tests.gd