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

@ -52,6 +52,9 @@ $GODOT --headless --path rinkplanner --script res://tests/run_history_tests.gd
# Run playback dot/timeline tests
$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
$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(
RoutineFile.is_audio_file("song.MP3")
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"
)