59 lines
3.0 KiB
Python
59 lines
3.0 KiB
Python
"""Prepare the original default SoundEffects.omt bank (EXE423642..656).
|
|
|
|
Numeric effect IDs are archive identities, not offsets into the four-entry
|
|
sounds.omt archive. Preserve the original PCM/RIFF bytes and explicit metadata.
|
|
"""
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import struct
|
|
|
|
from entities import _decode, resolve_asset
|
|
from omt import Database, FormatError
|
|
|
|
|
|
def export_sound_bank(original: Path, output: Path) -> dict:
|
|
original, output = original.resolve(), output.resolve()
|
|
if output.is_relative_to(original) or original.is_relative_to(output):
|
|
raise FormatError('Sound output must be separate from original files')
|
|
source = resolve_asset(original, 'omt/SoundEffects.omt')
|
|
database = Database(source)
|
|
source_hash = hashlib.sha256(database.data).hexdigest()
|
|
resources = []
|
|
for entry in database.entries:
|
|
if entry.type != 'Wave':
|
|
continue
|
|
resource = {'database': source.relative_to(original).as_posix(), 'type': 'Wave',
|
|
'id': entry.id, 'name': entry.name, 'offset': entry.offset,
|
|
'size': entry.size, 'databaseSha256': source_hash}
|
|
_decode(database, entry, resource, output)
|
|
if 'seconds' not in resource['audio']:
|
|
# Some original resources contain MPEG Layer3 in RIFF. Preserve the
|
|
# encoded bytes; duration comes from the explicit fact sample count.
|
|
raw = (output/resource['path']).read_bytes()
|
|
position, rate, frames, encoding = 12, None, None, None
|
|
while position + 8 <= len(raw):
|
|
tag = raw[position:position+4]
|
|
size = struct.unpack_from('<I', raw, position+4)[0]
|
|
begin, end = position+8, position+8+size
|
|
if end > len(raw):
|
|
raise FormatError('Sound RIFF chunk escapes resource')
|
|
if tag == b'fmt ' and size >= 16:
|
|
encoding, channels, rate = struct.unpack_from('<HHI', raw, begin)
|
|
elif tag == b'fact' and size >= 4:
|
|
frames = struct.unpack_from('<I', raw, begin)[0]
|
|
position = end+(size & 1)
|
|
if not rate or frames is None:
|
|
raise FormatError(f'No explicit sound duration for encoding: {entry.id}')
|
|
resource['audio'] = {'formatTag': encoding, 'channels': channels, 'rate': rate,
|
|
'frames': frames, 'seconds': frames/rate,
|
|
'encoding': 'encoded RIFF preserved; decoder support is separate'}
|
|
resource['sha256'] = hashlib.sha256((output/resource['path']).read_bytes()).hexdigest()
|
|
resources.append(resource)
|
|
if not resources or len({r['id'] for r in resources}) != len(resources):
|
|
raise FormatError('Missing or duplicate original sound identities')
|
|
report = {'schema': 1, 'evidence': 'Neutron.exe423642..423656; default current sound database',
|
|
'resources': resources}
|
|
(output/'effects.json').write_text(json.dumps(report, indent=2)+'\n', encoding='utf8')
|
|
return report
|