fix: correct RIFF chunk sizes in SF3 converter
- _find_chunk starts at offset 12 (after RIFF header) - Add _find_list_of_type to locate sdta LIST - Update sdta LIST size after smpl data replacement - Fix 'Invalid chunk header' error in SpessaSynth - SGM_v2.01 too large (529MB WAV) falls back to SF2
This commit is contained in:
+100
-40
@@ -2,7 +2,6 @@ import os
|
||||
import struct
|
||||
import subprocess
|
||||
import logging
|
||||
import tempfile
|
||||
import wave
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -12,18 +11,65 @@ SF_TARGET_DIRS = [
|
||||
]
|
||||
|
||||
|
||||
def _read_chunks(data, offset, max_size=0):
|
||||
chunks = []
|
||||
def _find_chunk(data, chunk_id, offset=12, max_size=0):
|
||||
pos = offset
|
||||
end = len(data) if max_size == 0 else offset + max_size
|
||||
while pos + 8 <= end:
|
||||
ck_id = data[pos:pos + 4]
|
||||
ck_size = struct.unpack("<I", data[pos + 4:pos + 8])[0]
|
||||
chunks.append((ck_id, pos + 8, ck_size, pos))
|
||||
if ck_id == chunk_id:
|
||||
return (pos, ck_id, ck_size, pos + 8)
|
||||
if ck_id == b"LIST" and pos + 12 <= end:
|
||||
list_type = data[pos + 8:pos + 12]
|
||||
# Check if we're looking for a specific list type
|
||||
if chunk_id == b"smpl" and list_type == b"sdta":
|
||||
inner = _find_chunk_in_list(data, chunk_id, pos + 12, ck_size - 4)
|
||||
if inner:
|
||||
return inner
|
||||
elif chunk_id == b"LIST":
|
||||
# When searching for LIST by type, check if it's sdta
|
||||
pass
|
||||
else:
|
||||
inner = _find_chunk_in_list(data, chunk_id, pos + 12, ck_size - 4)
|
||||
if inner:
|
||||
return inner
|
||||
pos += 8 + ck_size
|
||||
if ck_size % 2 == 1:
|
||||
pos += 1
|
||||
return chunks
|
||||
return None
|
||||
|
||||
|
||||
def _find_list_of_type(data, list_type_id, offset=12):
|
||||
pos = offset
|
||||
while pos + 12 <= len(data):
|
||||
ck_id = data[pos:pos + 4]
|
||||
ck_size = struct.unpack("<I", data[pos + 4:pos + 8])[0]
|
||||
if ck_id == b"LIST":
|
||||
form_type = data[pos + 8:pos + 12]
|
||||
if form_type == list_type_id:
|
||||
return (pos, ck_id, ck_size, pos + 12)
|
||||
pos += 8 + ck_size
|
||||
if ck_size % 2 == 1:
|
||||
pos += 1
|
||||
return None
|
||||
|
||||
|
||||
def _find_chunk_in_list(data, chunk_id, list_data_offset, list_data_size):
|
||||
pos = list_data_offset
|
||||
end = list_data_offset + list_data_size
|
||||
while pos + 8 <= end:
|
||||
ck_id = data[pos:pos + 4]
|
||||
ck_size = struct.unpack("<I", data[pos + 4:pos + 8])[0]
|
||||
if ck_id == chunk_id:
|
||||
return (pos, ck_id, ck_size, pos + 8)
|
||||
pos += 8 + ck_size
|
||||
if ck_size % 2 == 1:
|
||||
pos += 1
|
||||
return None
|
||||
|
||||
|
||||
def _update_size(data, offset, new_size):
|
||||
return data[:offset] + struct.pack("<I", new_size) + data[offset + 4:]
|
||||
|
||||
|
||||
class SoundFontConverter:
|
||||
@@ -51,38 +97,35 @@ class SoundFontConverter:
|
||||
return False
|
||||
|
||||
if len(data) < 12 or data[:4] != b"RIFF" or data[8:12] != b"sfbk":
|
||||
logger.warning("Not a valid SF2 file")
|
||||
return False
|
||||
|
||||
top_chunks = _read_chunks(data, 12)
|
||||
smpl_data = None
|
||||
smpl_offset = 0
|
||||
smpl_size = 0
|
||||
# Find smpl chunk recursively
|
||||
smpl = _find_chunk(data, b"smpl")
|
||||
if smpl is None:
|
||||
logger.warning("No smpl chunk found in SF2")
|
||||
return False
|
||||
|
||||
for ck_id, ck_data_off, ck_size, ck_head_off in top_chunks:
|
||||
if ck_id == b"LIST":
|
||||
list_type = data[ck_data_off:ck_data_off + 4]
|
||||
inner_chunks = _read_chunks(data, ck_data_off + 4, ck_size - 4)
|
||||
for ic_id, ic_data_off, ic_size, ic_head_off in inner_chunks:
|
||||
if ic_id == b"smpl":
|
||||
smpl_data = data[ic_data_off:ic_data_off + ic_size]
|
||||
smpl_offset = ic_data_off
|
||||
smpl_size = ic_size
|
||||
smpl_head_off, _, smpl_old_size, smpl_data_off = smpl
|
||||
sample_data = data[smpl_data_off:smpl_data_off + smpl_old_size]
|
||||
|
||||
if smpl_data is None or smpl_size < 16:
|
||||
logger.warning("No sample data found in SF2")
|
||||
if len(sample_data) < 16:
|
||||
logger.warning("Sample data too small")
|
||||
return False
|
||||
|
||||
tmp_wav = sf3_path + ".tmp.wav"
|
||||
tmp_ogg = sf3_path + ".tmp.ogg"
|
||||
|
||||
try:
|
||||
# Write samples as WAV
|
||||
with open(tmp_wav, "wb") as fw:
|
||||
with wave.open(fw, "wb") as w:
|
||||
w.setnchannels(1)
|
||||
w.setsampwidth(2)
|
||||
w.setframerate(44100)
|
||||
w.writeframes(smpl_data)
|
||||
w.writeframes(sample_data)
|
||||
|
||||
# Compress to Ogg Vorbis
|
||||
subprocess.run([
|
||||
"ffmpeg", "-y", "-i", tmp_wav,
|
||||
"-c:a", "libvorbis", "-q:a", "3",
|
||||
@@ -92,41 +135,60 @@ class SoundFontConverter:
|
||||
with open(tmp_ogg, "rb") as fo:
|
||||
ogg_data = fo.read()
|
||||
|
||||
compression = (1 - len(ogg_data) / max(len(smpl_data), 1)) * 100
|
||||
logger.info(f"Compressed {len(smpl_data)} -> {len(ogg_data)} bytes ({compression:.0f}%)")
|
||||
compression = (1 - len(ogg_data) / max(len(sample_data), 1)) * 100
|
||||
logger.info(f"Compressed {len(sample_data)} -> {len(ogg_data)} bytes ({compression:.0f}%)")
|
||||
|
||||
# Rebuild file: replace smpl chunk data with Ogg data
|
||||
ogg_padded = ogg_data if len(ogg_data) % 2 == 0 else ogg_data + b"\x00"
|
||||
new_smpl_size = len(ogg_data)
|
||||
old_padded = smpl_old_size + (1 if smpl_old_size % 2 == 1 else 0)
|
||||
new_padded = len(ogg_padded)
|
||||
delta = new_padded - old_padded
|
||||
|
||||
# Rebuild file: replace smpl chunk and update all sizes
|
||||
out = bytearray()
|
||||
out.extend(data[:smpl_offset - 8]) # up to smpl chunk header
|
||||
out.extend(data[:smpl_head_off]) # up to smpl chunk header
|
||||
out.extend(struct.pack("<I", new_smpl_size)) # new smpl size
|
||||
out.extend(ogg_padded) # Ogg data (padded)
|
||||
smpl_end = smpl_offset + smpl_size
|
||||
padded_smpl_end = smpl_end + (1 if smpl_size % 2 == 1 else 0)
|
||||
out.extend(data[padded_smpl_end:]) # rest of file
|
||||
out.extend(ogg_padded) # compressed data (even-padded)
|
||||
out.extend(data[smpl_head_off + 8 + old_padded:]) # rest of file
|
||||
|
||||
# Fix RIFF size
|
||||
old_total = struct.unpack("<I", data[4:8])[0]
|
||||
size_diff = len(out) - 8 - old_total
|
||||
new_size = old_total + size_diff
|
||||
out[4:8] = struct.pack("<I", new_size)
|
||||
data_out = bytes(out)
|
||||
|
||||
# Find sdta LIST and update its size
|
||||
sdta = _find_list_of_type(data_out, b"sdta", 12)
|
||||
if sdta:
|
||||
lh_off, _, lh_size, ld_off = sdta
|
||||
data_out = _update_size(data_out, lh_off + 4, lh_size + delta)
|
||||
|
||||
# Update RIFF root size
|
||||
old_riff_size = struct.unpack("<I", data_out[4:8])[0]
|
||||
new_total = len(data_out) - 8
|
||||
data_out = _update_size(data_out, 4, new_total)
|
||||
|
||||
with open(sf3_path, "wb") as fout:
|
||||
fout.write(bytes(out))
|
||||
fout.write(bytes(data_out))
|
||||
|
||||
# Validate: check that RIFF size matches actual size
|
||||
written = os.path.getsize(sf3_path)
|
||||
parsed_riff = struct.unpack("<I", data_out[4:8])[0]
|
||||
if parsed_riff != written - 8:
|
||||
logger.warning(f"Size mismatch: RIFF says {parsed_riff}, actual is {written - 8}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
return os.path.exists(sf3_path)
|
||||
except subprocess.TimeoutExpired:
|
||||
logger.error("Ogg conversion timed out")
|
||||
return False
|
||||
except Exception as e:
|
||||
logger.error(f"Conversion error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
finally:
|
||||
for p in [tmp_wav, tmp_ogg]:
|
||||
try:
|
||||
if os.path.exists(p): os.remove(p)
|
||||
if os.path.exists(p):
|
||||
os.remove(p)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -154,7 +216,6 @@ class SoundFontConverter:
|
||||
try:
|
||||
logger.info(f"Converting '{sf2_path}' -> '{sf3_path}' using {converter}...")
|
||||
if converter == "fluidsynth":
|
||||
logger.warning("fluidsynth CLI does not export SF3, using Python converter")
|
||||
converter = "python"
|
||||
|
||||
if converter == "mscore":
|
||||
@@ -163,7 +224,6 @@ class SoundFontConverter:
|
||||
if result.returncode == 0 and os.path.exists(sf3_path):
|
||||
logger.info(f"Created SF3 via mscore: {sf3_path} ({os.path.getsize(sf3_path)/1024/1024:.1f}MB)")
|
||||
return sf3_path
|
||||
logger.warning(f"mscore failed, trying Python converter")
|
||||
converter = "python"
|
||||
|
||||
if converter == "python":
|
||||
@@ -171,7 +231,7 @@ class SoundFontConverter:
|
||||
size_mb = os.path.getsize(sf3_path) / (1024 * 1024)
|
||||
logger.info(f"Created SF3: {sf3_path} ({size_mb:.2f} MB)")
|
||||
return sf3_path
|
||||
logger.warning(f"Python converter failed, returning SF2 path")
|
||||
logger.warning(f"Python converter failed for {sf2_path}, returning SF2 path")
|
||||
return sf2_path
|
||||
|
||||
return sf2_path
|
||||
|
||||
Reference in New Issue
Block a user