Files
romm/backend/utils/chd_cdrom.py
Sam Dornan 8c302ed2a4 feat(soundtrack): extract Dreamcast audio and name multi-disc tracks apart
Dreamcast discs come as .gdi sheets or GD-ROM CHDs; both now yield their
CD audio tracks.

- utils/gdi_sheet.py reads .gdi sheets: an audio track (type 0, 2352-byte
  sectors) fills its own file with little-endian PCM.
- GD-ROM CHDs ('CHGD' metadata) count each track's frames up to where the
  next one starts and record the unused part as PAD; the audio is the
  frames before the padding. Checked against chdman-built images, whose
  audio is stored big-endian like a CD CHD's.
- A lone .gdi gets the same 409 as a lone .cue.

Multi-disc sets:

- When two discs share an image name (Dreamcast sets often use disc.gdi
  in per-disc folders), their tracks take the disc's folder name too,
  instead of colliding and being skipped as already extracted.
- A ROM with several discs tags each track with DISCNUMBER, from "(Disc
  N)" in the name or the disc's order, and titles untitled tracks
  "Track NN (Disc N)".
- A lone CHD that an .m3u beside it lists isn't moved into a folder, since
  that would break the playlist; it gets a 409 naming the playlist.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014XHgJG3xRgUKwuHJHwKkPg
2026-09-25 22:26:33 +00:00

202 lines
6.5 KiB
Python

from __future__ import annotations
import ctypes
import ctypes.util
import functools
from collections.abc import Iterator
from dataclasses import dataclass
from pathlib import Path
# A CD frame in a CHD: one raw sector followed by its subcode.
SECTOR_BYTES = 2352
FRAME_BYTES = SECTOR_BYTES + 96
# Each track's frames are padded up to a multiple of this.
TRACK_PADDING = 4
_OPEN_READ = 1
_METADATA_BUFFER_BYTES = 512
def _tag(name: str) -> int:
return int.from_bytes(name.encode("ascii"), "big")
# Current and legacy CD track metadata, then Dreamcast GD-ROM.
TRACK_METADATA_TAGS = (_tag("CHT2"), _tag("CHTR"), _tag("CHGD"))
@dataclass(frozen=True)
class ChdTrack:
number: int
type: str
frames: int
pregap: int
pregap_stored: bool
# GD-ROM counts a track to where the next starts and pads the rest.
pad: int = 0
@dataclass(frozen=True)
class ChdAudioTrack:
"""An audio track's playable frames, big-endian 44.1 kHz 16-bit stereo."""
number: int
first_frame: int
frame_count: int
def parse_track_metadata(text: str) -> ChdTrack | None:
"""Read one CHT2, CHTR or CHGD track entry, or None when it's malformed."""
fields = dict(
token.split(":", 1) for token in text.strip("\0 ").split() if ":" in token
)
try:
pregap = int(fields.get("PREGAP", "0"))
return ChdTrack(
number=int(fields["TRACK"]),
type=fields["TYPE"].upper(),
frames=int(fields["FRAMES"]),
pregap=pregap,
# A 'V' pregap type means the pregap's frames are in the image.
pregap_stored=fields.get("PGTYPE", "").upper().startswith("V"),
pad=int(fields.get("PAD", "0")),
)
except KeyError, ValueError:
return None
def audio_tracks(tracks: list[ChdTrack]) -> list[ChdAudioTrack]:
"""Locate each audio track's frames, skipping any pregap stored ahead of it
and any GD-ROM padding after it."""
located: list[ChdAudioTrack] = []
frame = 0
for track in sorted(tracks, key=lambda t: t.number):
if track.type == "AUDIO":
skip = track.pregap if track.pregap_stored else 0
count = track.frames - track.pad - skip
if count > 0:
located.append(
ChdAudioTrack(
number=track.number,
first_frame=frame + skip,
frame_count=count,
)
)
frame += -(-track.frames // TRACK_PADDING) * TRACK_PADDING
return located
class _Header(ctypes.Structure):
# The leading fields of libchdr's chd_header, which is all this reads.
_fields_ = [
("length", ctypes.c_uint32),
("version", ctypes.c_uint32),
("flags", ctypes.c_uint32),
("compression", ctypes.c_uint32 * 4),
("hunkbytes", ctypes.c_uint32),
("totalhunks", ctypes.c_uint32),
]
class ChdError(RuntimeError):
"""libchdr refused the file."""
@functools.cache
def load_libchdr() -> ctypes.CDLL | None:
"""libchdr, or None when it isn't installed."""
# find_library can't see musl's library paths, so the soname goes first.
for name in ("libchdr.so.0", ctypes.util.find_library("chdr")):
if not name:
continue
try:
lib = ctypes.CDLL(name)
except OSError:
continue
lib.chd_open.argtypes = [
ctypes.c_char_p,
ctypes.c_int,
ctypes.c_void_p,
ctypes.POINTER(ctypes.c_void_p),
]
lib.chd_close.argtypes = [ctypes.c_void_p]
lib.chd_get_header.argtypes = [ctypes.c_void_p]
lib.chd_get_header.restype = ctypes.POINTER(_Header)
lib.chd_get_metadata.argtypes = [
ctypes.c_void_p,
ctypes.c_uint32,
ctypes.c_uint32,
ctypes.c_void_p,
ctypes.c_uint32,
ctypes.POINTER(ctypes.c_uint32),
ctypes.POINTER(ctypes.c_uint32),
ctypes.POINTER(ctypes.c_uint8),
]
lib.chd_read.argtypes = [ctypes.c_void_p, ctypes.c_uint32, ctypes.c_void_p]
return lib
return None
class ChdImage:
"""A CD image opened read-only through libchdr."""
def __init__(self, lib: ctypes.CDLL, path: Path):
self._lib = lib
self._handle = ctypes.c_void_p()
error = lib.chd_open(str(path).encode(), _OPEN_READ, None, self._handle)
if error:
raise ChdError(f"libchdr could not open {path.name} (error {error})")
header = lib.chd_get_header(self._handle).contents
self.hunk_bytes: int = header.hunkbytes
self.total_hunks: int = header.totalhunks
def __enter__(self) -> ChdImage:
return self
def __exit__(self, *_: object) -> None:
if self._handle:
self._lib.chd_close(self._handle)
self._handle = ctypes.c_void_p()
def tracks(self) -> list[ChdTrack]:
buffer = ctypes.create_string_buffer(_METADATA_BUFFER_BYTES)
length = ctypes.c_uint32()
for tag in TRACK_METADATA_TAGS:
found: list[ChdTrack] = []
index = 0
while not self._lib.chd_get_metadata(
self._handle, tag, index, buffer, len(buffer), length, None, None
):
track = parse_track_metadata(buffer.value.decode("ascii", "replace"))
if track:
found.append(track)
index += 1
if found:
return found
return []
def audio_pcm(self, track: ChdAudioTrack) -> Iterator[bytes]:
"""Yield a track's samples a hunk at a time, dropping the subcode."""
if self.hunk_bytes % FRAME_BYTES:
raise ChdError("Image isn't laid out in CD frames")
frames_per_hunk = self.hunk_bytes // FRAME_BYTES
hunk = ctypes.create_string_buffer(self.hunk_bytes)
frame = track.first_frame
end = track.first_frame + track.frame_count
while frame < end:
number, offset = divmod(frame, frames_per_hunk)
if number >= self.total_hunks:
return
if self._lib.chd_read(self._handle, number, hunk):
raise ChdError(f"libchdr could not read hunk {number}")
count = min(frames_per_hunk - offset, end - frame)
raw = hunk.raw
yield b"".join(
raw[
(offset + i) * FRAME_BYTES : (offset + i) * FRAME_BYTES
+ SECTOR_BYTES
]
for i in range(count)
)
frame += count