Add in a failsafe when extracting custom sound files

This commit is contained in:
MysterD 2023-04-01 03:15:41 -07:00
parent 213af9b2dc
commit 74b773d5fd
2 changed files with 94 additions and 1 deletions

View file

@ -1115,6 +1115,7 @@ N64CKSUM := $(TOOLS_DIR)/n64cksum
N64GRAPHICS := $(TOOLS_DIR)/n64graphics N64GRAPHICS := $(TOOLS_DIR)/n64graphics
N64GRAPHICS_CI := $(TOOLS_DIR)/n64graphics_ci N64GRAPHICS_CI := $(TOOLS_DIR)/n64graphics_ci
TEXTCONV := $(TOOLS_DIR)/textconv TEXTCONV := $(TOOLS_DIR)/textconv
AIFF_EXTRACT_FAILSAFE := $(TOOLS_DIR)/aiff_extract_codebook_failsafe.py
AIFF_EXTRACT_CODEBOOK := $(TOOLS_DIR)/aiff_extract_codebook AIFF_EXTRACT_CODEBOOK := $(TOOLS_DIR)/aiff_extract_codebook
VADPCM_ENC := $(TOOLS_DIR)/vadpcm_enc VADPCM_ENC := $(TOOLS_DIR)/vadpcm_enc
EXTRACT_DATA_FOR_MIO := $(TOOLS_DIR)/extract_data_for_mio EXTRACT_DATA_FOR_MIO := $(TOOLS_DIR)/extract_data_for_mio
@ -1371,7 +1372,7 @@ endif
$(BUILD_DIR)/%.table: %.aiff $(BUILD_DIR)/%.table: %.aiff
$(call print,Extracting codebook:,$<,$@) $(call print,Extracting codebook:,$<,$@)
$(V)$(AIFF_EXTRACT_CODEBOOK) $< >$@ $(V)$(PYTHON) $(AIFF_EXTRACT_FAILSAFE) $(AIFF_EXTRACT_CODEBOOK) $< $@
$(call print,Piping:,$<,$@.inc.c) $(call print,Piping:,$<,$@.inc.c)
$(V)hexdump -v -e '1/1 "0x%X,"' $< > $@.inc.c $(V)hexdump -v -e '1/1 "0x%X,"' $< > $@.inc.c
$(V)echo >> $@.inc.c $(V)echo >> $@.inc.c

View file

@ -0,0 +1,92 @@
#!/usr/bin/env python3
import sys
import os
import subprocess
# example before: tools/aiff_extract_codebook sound/samples/sfx_custom_luigi/00.aiff >build/us_pc/sound/samples/sfx_custom_luigi/00.table
s_size = 0
# check arguments
if len(sys.argv) != 4:
print(sys.argv[0] + ': was passed the incorrect number of arguments: ' + str(sys.argv), file=sys.stderr)
exit(1)
# get arguments
a_cmd = sys.argv[1]
a_input = sys.argv[2]
a_output = sys.argv[3]
# validate input
if not os.path.isfile(a_input):
print(sys.argv[0] + ': original input file does not exist "' + a_input + '"', file=sys.stderr)
else:
# run original command
s_cmd = [ a_cmd, a_input ]
with open(a_output, 'w') as outfile:
subprocess.call(s_cmd, stdout=outfile, shell=False)
# get size
if not os.path.isfile(a_output):
print(sys.argv[0] + ': original output file does not exist "' + a_output + '"', file=sys.stderr)
else:
with open(a_output, 'r') as outfile:
s_text = outfile.read().strip()
s_size = len(s_text)
# check size
if s_size > 6:
exit(0)
print(sys.argv[0] + ': original output file has a size of zero "' + a_output + '"', file=sys.stderr)
# only override custom
if 'custom' not in a_input:
print(sys.argv[0] + ': original input file is not custom "' + a_input + '"', file=sys.stderr)
exit(1)
# figure out which mario sound path to try to grab
m_path = 'sound/samples/sfx_mario/'
if '_peach' in a_input:
m_path = 'sound/samples/sfx_mario_peach/'
# make sure mario path exists
if not os.path.isdir(m_path):
print(sys.argv[0] + ': could not find override mario path "' + m_path + '" for "' + a_input + '"', file=sys.stderr)
exit(1)
# generate table of mario sounds
m_table = {}
m_any_sound = None
for m_file in os.listdir(m_path):
m_filename = m_path + os.path.basename(m_file)
m_hex = m_file.split('_')[0].upper()
m_table[m_hex] = m_filename
m_any_sound = m_filename
# figure out input hex
n_hex = os.path.basename(a_input).split('.')[0].split('_')[0].upper()
if n_hex not in m_table:
print(sys.argv[0] + ': could not find hex in mario table "' + m_path + '" for "' + a_input + '"', file=sys.stderr)
n_input = m_any_sound
else:
# override input file
n_input = m_table[n_hex]
# run override command
s_cmd = [ a_cmd, n_input ]
with open(a_output, 'w') as outfile:
subprocess.call(s_cmd, stdout=outfile, shell=False)
# get size
if not os.path.isfile(a_output):
print(sys.argv[0] + ': override output file does not exist "' + a_output + '"', file=sys.stderr)
else:
with open(a_output, 'r') as outfile:
s_text = outfile.read().strip()
s_size = len(s_text)
# check size
if s_size <= 0:
print(sys.argv[0] + ': override output file has a size of zero "' + a_output + '"', file=sys.stderr)
exit(1)