Skip to content

Commit db604c6

Browse files
authored
fix(CDDA): Refactor shared.mk to allow spaces in track names.
Fix by sftwninja
2 parents 34165b2 + b2ffe13 commit db604c6

File tree

9 files changed

+105
-20
lines changed

9 files changed

+105
-20
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

Samples/Sound - CDDA (simple)/cd/music/04BEGINGAME.mp3 renamed to Samples/Sound - CDDA (simple)/cd/music/04 BEGINGAME.mp3

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
sample01.mp3
1+
03 sample01.mp3
22
# Lines starting with "#" are ignored
33
# counting.mp3
44
# "normalize" is a general purpose filter for normalizing audio
55
# see saturnringlib/shared.mk
6-
Backbeat.ogg:normalize
7-
counting16.wav:radio
6+
01 Backbeat.ogg:normalize
7+
02 counting16.wav:radio

saturnringlib/shared.mk

Lines changed: 102 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -299,18 +299,8 @@ create_bin_cue: create_iso
299299
echo ' TRACK 01 MODE1/2352' >> $(BUILD_CUE)
300300
echo ' INDEX 01 00:00:00' >> $(BUILD_CUE)
301301

302-
# Check if tracklist file exists and determine audio file order
303-
ifneq (,$(wildcard $(MUSIC_DIR)/tracklist))
304-
# Read files from tracklist, prepend MUSIC_DIR to each line, filter out empty lines and comments
305-
TRACKLIST_LINES := $(shell sed 's/^\s*//;s/\s*$$//;/^$$/d;/^#/d' $(MUSIC_DIR)/tracklist)
306-
AUDIO_FILES := $(foreach line,$(TRACKLIST_LINES),$(word 1,$(subst :, ,$(line))))
307-
AUDIO_FILES := $(addprefix $(MUSIC_DIR)/,$(AUDIO_FILES))
308-
else
309-
# Fallback to original behavior - find all supported audio files
310-
AUDIO_FILES = $(patsubst ./%,%,$(shell find $(MUSIC_DIR) \( -name '*.mp3' -o -name '*.wav' -o -name '*.ogg' -o -name '*.flac' -o -name '*.aac' -o -name '*.m4a' -o -name '*.wma' \)))
311-
endif
312-
313-
AUDIO_FILES_RAW = $(patsubst %,%.raw,$(AUDIO_FILES))
302+
AUDIO_FILES =
303+
AUDIO_FILES_RAW =
314304

315305
# Function to get the filter option for a given audio file
316306
define get_filter_option
@@ -355,12 +345,105 @@ endef
355345
fi; \
356346
echo "Converted $< to $@ ($$size -> $$target_size bytes, $$target_sectors sectors)";
357347

358-
add_audio_to_bin_cue: $(AUDIO_FILES_RAW)
348+
add_audio_to_bin_cue: create_bin_cue
359349
track=2; \
360350
total_size=$$(stat -c%s "$(BUILD_BIN)"); \
361351
sectors=$$((total_size / 2352)); \
362352
echo "Starting with $$total_size bytes ($$sectors sectors)"; \
363-
for i in $^; do \
353+
# Find audio files and convert them to raw \
354+
if [ -f "$(MUSIC_DIR)/tracklist" ]; then \
355+
# Parse tracklist and handle files with spaces \
356+
# Use a temp file to avoid pipeline subshell issues \
357+
# Ensure file ends with newline, then filter \
358+
( cat "$(MUSIC_DIR)/tracklist"; echo ) | sed 's/^\s*//;s/\s*$$//;/^$$/d;/^#/d' > .tracklist_temp; \
359+
while IFS= read -r line; do \
360+
# Extract filename part before colon (if any) \
361+
if echo "$$line" | grep -q ':'; then \
362+
audiofile=$${line%%:*}; \
363+
else \
364+
audiofile=$$line; \
365+
fi; \
366+
# Prepend music directory \
367+
audiofile="$(MUSIC_DIR)/$$audiofile"; \
368+
rawfile="$$audiofile.raw"; \
369+
if [ -f "$$audiofile" ]; then \
370+
if [ ! -f "$$rawfile" ]; then \
371+
echo "Converting $$audiofile to $$rawfile"; \
372+
# Get filter option for this file \
373+
filter_option=""; \
374+
if echo "$$line" | grep -q ':'; then \
375+
filter_option=$${line#*:}; \
376+
fi; \
377+
# Apply sox conversion with or without filters \
378+
if [ -n "$$filter_option" ]; then \
379+
filter_var="SRL_SOX_FILTERS_$$filter_option"; \
380+
filter_cmd=$$(eval echo \$$$$filter_var); \
381+
if [ -n "$$filter_cmd" ]; then \
382+
sox "$$audiofile" -t raw -r 44100 -e signed-integer -b 16 -c 2 "$$rawfile" $$filter_cmd; \
383+
else \
384+
echo "Warning: No SOX_FILTERS_$$filter_option defined, using no filters"; \
385+
sox "$$audiofile" -t raw -r 44100 -e signed-integer -b 16 -c 2 "$$rawfile"; \
386+
fi; \
387+
else \
388+
sox "$$audiofile" -t raw -r 44100 -e signed-integer -b 16 -c 2 "$$rawfile"; \
389+
fi; \
390+
# Check to ensure the raw file is sector aligned \
391+
size=$$(stat -c%s "$$rawfile"); \
392+
target_sectors=$$((size / 2352)); \
393+
if [ $$((size % 2352)) -ne 0 ]; then \
394+
target_sectors=$$((target_sectors + 1)); \
395+
fi; \
396+
target_size=$$((target_sectors * 2352)); \
397+
if [ $$size -lt $$target_size ]; then \
398+
mv "$$rawfile" "$$rawfile.unpadded"; \
399+
dd if=/dev/zero bs=1 count=$$((target_size - size)) of=padding.tmp status=none; \
400+
cat "$$rawfile.unpadded" padding.tmp > "$$rawfile"; \
401+
rm -f padding.tmp "$$rawfile.unpadded"; \
402+
fi; \
403+
echo "Converted $$audiofile to $$rawfile ($$size -> $$target_size bytes, $$target_sectors sectors)"; \
404+
fi; \
405+
echo "$$rawfile" >> .audio_files_temp; \
406+
else \
407+
echo "Warning: Audio file not found: $$audiofile"; \
408+
fi; \
409+
done < .tracklist_temp; \
410+
rm -f .tracklist_temp; \
411+
else \
412+
# Auto-discover audio files and convert to raw \
413+
# Use xargs to handle spaces in filenames properly \
414+
find $(MUSIC_DIR) \( -name '*.mp3' -o -name '*.wav' -o -name '*.ogg' -o -name '*.flac' -o -name '*.aac' -o -name '*.m4a' -o -name '*.wma' \) -print0 | \
415+
xargs -0 -I {} sh -c ' \
416+
audiofile="{}"; \
417+
rawfile="$$audiofile.raw"; \
418+
if [ ! -f "$$rawfile" ]; then \
419+
echo "Converting $$audiofile to $$rawfile"; \
420+
sox "$$audiofile" -t raw -r 44100 -e signed-integer -b 16 -c 2 "$$rawfile"; \
421+
size=$$(stat -c%s "$$rawfile"); \
422+
target_sectors=$$((size / 2352)); \
423+
if [ $$((size % 2352)) -ne 0 ]; then \
424+
target_sectors=$$((target_sectors + 1)); \
425+
fi; \
426+
target_size=$$((target_sectors * 2352)); \
427+
if [ $$size -lt $$target_size ]; then \
428+
mv "$$rawfile" "$$rawfile.unpadded"; \
429+
dd if=/dev/zero bs=1 count=$$((target_size - size)) of=padding.tmp status=none; \
430+
cat "$$rawfile.unpadded" padding.tmp > "$$rawfile"; \
431+
rm -f padding.tmp "$$rawfile.unpadded"; \
432+
fi; \
433+
echo "Converted $$audiofile to $$rawfile ($$size -> $$target_size bytes, $$target_sectors sectors)"; \
434+
fi; \
435+
echo "$$rawfile" >> .audio_files_temp; \
436+
'; \
437+
fi; \
438+
if [ ! -f .audio_files_temp ]; then \
439+
echo "No audio files found"; \
440+
touch .audio_files_temp; \
441+
fi; \
442+
while IFS= read -r i; do \
443+
[ -z "$$i" ] && continue; \
444+
# Remove quotes if present \
445+
i=$${i#\"}; i=$${i%\"}; \
446+
[ ! -f "$$i" ] && continue; \
364447
echo "Track $$track: starts at sector $$sectors"; \
365448
echo ' TRACK' $$(printf "%02d" $$track) 'AUDIO' >> $(BUILD_CUE); \
366449
# 150 frames are required to gap the audio track when directly following data \
@@ -394,10 +477,12 @@ add_audio_to_bin_cue: $(AUDIO_FILES_RAW)
394477
echo " New total: $$total_size bytes ($$((total_size / 2352)) sectors)"; \
395478
track=$$((track + 1)); \
396479
sectors=$$((sectors + $$sectors_in_file)); \
397-
done
398-
rm -f $(AUDIO_FILES_RAW)
480+
done < .audio_files_temp; \
481+
rm -f .audio_files_temp
482+
# Clean up raw files \
483+
find $(MUSIC_DIR) -name '*.raw' -delete
399484

400-
build_bin_cue: create_bin_cue add_audio_to_bin_cue
485+
build_bin_cue: add_audio_to_bin_cue
401486

402487
# CLONE_CD_PATH = $(BUILD_DROP)/CloneCdFiles
403488
# CLONE_CD_CCD = $(CLONE_CD_PATH)/$(CD_NAME).ccd

0 commit comments

Comments
 (0)