// SPDX-License-Identifier: MIT // // spc_dump: the S-DSP referee driver. It plays an .spc file in blargg's // snes_spc 0.9.0 and writes every 32 kHz output sample, unfiltered, as // little-endian int16 left/right pairs. // // spc_dump FILE.spc SAMPLES OUT.raw [--regs OUT.regs] [--final OUT.spc] // [--clear-echo] // // --regs OUT.regs after every output sample, the 128 DSP registers as the // library saves them in an .spc snapshot (so ENVX, OUTX and // ENDX included): SAMPLES records of 128 bytes // --final OUT.spc the state at the end, saved as an .spc file // --clear-echo the library's optional echo-buffer clear after loading // // This file uses only the C interface that snes_spc's own documentation // (snes_spc.txt, "SPC Music Playback" and "Full SPC Emulation") describes: // spc_new, spc_load_spc, spc_clear_echo, spc_play, spc_save_spc, // spc_delete. The library's sources and headers were not read to write it; // the header is included only so the compiler can check the calls. The DSP // register bytes are taken from the saved .spc image at offset $10100, the // place the published SPC file format gives them. // // The program built from this file and the library is an LGPL-2.1 combined // work (snes_spc's licence; family charter rule 6). It is a closed-box // measuring instrument: it is never shipped in an MIT package and nothing // it links is copied into this repository. #include "spc.h" #include #include #include #include namespace { constexpr long kSpcFileSize = 0x10200; // header, 64 KiB RAM, DSP registers, extra constexpr long kDspRegisters = 0x10100; // offset of the 128 DSP registers in an .spc constexpr long kSaveBuffer = 0x20000; // generous: room for anything the save writes bool read_file(const char* path, std::vector& out) { FILE* in = std::fopen(path, "rb"); if (!in) { std::perror(path); return false; } unsigned char chunk[65536]; size_t got; while ((got = std::fread(chunk, 1, sizeof chunk, in)) > 0) out.insert(out.end(), chunk, chunk + got); std::fclose(in); return true; } void put_samples(FILE* out, const short* buf, int count) { for (int i = 0; i < count; i++) { unsigned short v = (unsigned short)buf[i]; std::fputc(v & 0xFF, out); std::fputc(v >> 8, out); } } } // namespace int main(int argc, char** argv) { if (argc < 4) { std::fprintf(stderr, "usage: spc_dump FILE.spc SAMPLES OUT.raw [--regs OUT.regs] [--final OUT.spc]" " [--clear-echo]\n"); return 2; } bool clear_echo = false; const char* regs_path = nullptr; const char* final_path = nullptr; for (int i = 4; i < argc; i++) { if (!std::strcmp(argv[i], "--clear-echo")) { clear_echo = true; } else if (!std::strcmp(argv[i], "--regs") && i + 1 < argc) { regs_path = argv[++i]; } else if (!std::strcmp(argv[i], "--final") && i + 1 < argc) { final_path = argv[++i]; } else { std::fprintf(stderr, "unknown argument: %s\n", argv[i]); return 2; } } std::vector file; if (!read_file(argv[1], file)) return 1; SNES_SPC* spc = spc_new(); if (!spc) { std::fprintf(stderr, "spc_new: out of memory\n"); return 1; } if (const char* err = spc_load_spc(spc, file.data(), (long)file.size())) { std::fprintf(stderr, "spc_load_spc: %s\n", err); return 1; } if (clear_echo) spc_clear_echo(spc); FILE* out = std::fopen(argv[3], "wb"); if (!out) { std::perror(argv[3]); return 1; } FILE* regs = nullptr; if (regs_path) { regs = std::fopen(regs_path, "wb"); if (!regs) { std::perror(regs_path); return 1; } } std::vector image(kSaveBuffer); long remaining = std::atol(argv[2]); // With --regs, one stereo sample per call so a snapshot follows each one. const int block = regs ? 1 : 1024; std::vector buf(block * 2); while (remaining > 0) { int n = remaining < block ? (int)remaining : block; if (const char* err = spc_play(spc, n * 2, buf.data())) { std::fprintf(stderr, "spc_play: %s\n", err); return 1; } put_samples(out, buf.data(), n * 2); if (regs) { spc_save_spc(spc, image.data()); std::fwrite(image.data() + kDspRegisters, 1, 128, regs); } remaining -= n; } std::fclose(out); if (regs) std::fclose(regs); if (final_path) { std::fill(image.begin(), image.end(), 0); spc_save_spc(spc, image.data()); FILE* fin = std::fopen(final_path, "wb"); if (!fin) { std::perror(final_path); return 1; } std::fwrite(image.data(), 1, kSpcFileSize, fin); std::fclose(fin); } spc_delete(spc); return 0; }