Choose this page for tracker music: SNESMOD plays Impulse Tracker modules, and plays sound effects from the same soundbank. For samples driven from C with DSP effects (ADSR, GAIN, echo), or for an SPC700 program of your own, read Samples, Voices and the Raw APU instead.
SNES Audio Architecture
The SNES has a dedicated SPC700 sound processor with:
- 8 audio channels
- 64KB dedicated audio RAM
- BRR (Bit Rate Reduction) sample compression
- Hardware ADSR envelopes
- Echo/reverb effects
Two engines, one per ROM
| Module | Best for | Size in audio RAM |
| snesmod (this page) | Music, and effects that share its soundbank | ~5.5KB driver |
| audio (Samples, Voices and the Raw APU) | One-shot effects, voice clips, DSP effects from C | per sample |
Pick one per ROM: audio and snesmod both own the SPC700, so they are never linked together. A one-shot .brr made from a WAV belongs to the audio module: see One-shot samples from WAV.
SNESMOD
SNESMOD is a tracker-based audio engine supporting Impulse Tracker (.it) modules.
Setup
- Create music in a tracker (OpenMPT recommended)
- Export as Impulse Tracker (.it) format
- List the modules in SOUNDBANK_SRC: the build converts them
- Link soundbank with your ROM
Makefile Configuration
# Enable SNESMOD
USE_SNESMOD := 1
USE_LIB := 1
LIB_MODULES := console sprite input
# Soundbank source files
SOUNDBANK_SRC := music/mymusic.it sfx/effects.it
Basic Music Playback
#include "soundbank.h"
while (1) {
}
return 0;
}
int main(void)
Definition main.c:73
void consoleInit(void)
Initialize SNES hardware.
void WaitForVBlank(void)
Wait for next VBlank period.
void setScreenOn(void)
Enable screen display.
void snesmodSetSoundbank(u8 bank)
Set the soundbank bank number.
void snesmodInit(void)
Initialize SNESMOD audio engine.
void snesmodPlay(u8 startPosition)
Start module playback.
void snesmodLoadModule(u16 moduleId)
Load a module from the soundbank.
void snesmodProcess(void)
Process audio commands.
OpenSNES common-case master header.
SNESMOD - Tracker-based Audio Engine.
Sound Effects
#include "soundbank.h"
void init_audio(void) {
}
void play_jump_sound(void) {
}
void play_coin_sound(void) {
}
#define SNESMOD_PITCH_NORMAL
Definition snesmod.h:339
#define SNESMOD_PITCH_HIGH
Definition snesmod.h:340
u8 snesmodLoadEffect(u16 sfxIndex)
Load a sound effect from the soundbank.
u8 snesmodPlayEffect(u16 effectId, u8 volume, u8 pan, u16 pitch)
Play a sound effect.
An effect has to fit. snesmodLoadEffect() copies the sample into the SPC700's RAM after the module, and the echo buffer sits at the top of that RAM. The driver does not check the room: an effect that does not fit is written over the echo buffer, and both are heard wrong, with no error (KNOWN_LIMITATIONS.md). opensnes-music inspect music.it prints what the module leaves free ("N free of M"); add up the effects before loading them. snesmodLoadModule() frees every effect, so load the effects after it. examples/audio/snesmod_music loads four effects beside a module with an echo, with the figures: what the module leaves, what each effect takes.
Volume Control
void snesmodStop(void)
Stop module playback.
void snesmodPause(void)
Pause module playback.
void snesmodResume(void)
Resume module playback.
void snesmodSetModuleVolume(u8 volume)
Set module volume.
void snesmodFadeVolume(u8 targetVolume, u8 speed)
Fade module volume.
Creating Music with OpenMPT
- Download OpenMPT (free)
- Create new Impulse Tracker module
- Keep within SNES limits:
- Max 8 channels
- Total samples < 58KB
- 32kHz max sample rate
- Export as .it file
The converter
The build runs this for you; by hand it is:
opensnes-music bank music.it effects.it --name soundbank --bank 1
# Output files:
# - soundbank.asm (link with ROM)
# - soundbank.h (module/effect IDs)
# - soundbank.bnk (binary data)
The build runs it from SOUNDBANK_SRC when USE_SNESMOD := 1 (opensnes-music — Impulse Tracker to soundbank), and opensnes-music inspect tells you what each module costs in the SPC700's 64 KB.
Pitch Constants
#define SNESMOD_PITCH_LOW 4
#define SNESMOD_PITCH_NORMAL 8
#define SNESMOD_PITCH_HIGH 12
Important: Call snesmodProcess()!
You MUST call snesmodProcess() every frame! Failure to do so causes:
- Audio glitches and stuttering
- Command buffer overflow
- Desynchronization
Following the music and managing the queue
snesmodGetPosition() returns the playback position the driver reports (the current row), for syncing game events to the music:
start_boss_flash();
}
u8 snesmodGetPosition(void)
Get current module position.
Every snesmod* command goes through a queue that snesmodProcess() drains a little each frame. snesmodFlush() blocks until the queue is empty, for the rare moment you need every command applied now, such as before loading another module.
The queue holds 85 commands and the SPC700 takes about two per frame. With several queued, snesmodProcess() waits on the audio CPU for up to five scanlines (about 2 % of a frame) before leaving the rest to the next frame. A command sent while the queue is full is dropped, so a burst of dozens of effects in one frame loses the last ones rather than corrupting the queue.
Example: Music + SFX
#include "soundbank.h"
u16 pad, pad_prev = 0, pad_pressed;
while (1) {
pad_pressed = pad & ~pad_prev;
pad_prev = pad;
if (pad == 0xFFFF) pad_pressed = 0;
if (pad_pressed &
KEY_A) {
}
if (pad_pressed &
KEY_B) {
}
}
return 0;
}
#define REG_JOY1H
Joypad 1 data high (R).
Definition registers.h:334
#define REG_JOY1L
Joypad 1 data low (R).
Definition registers.h:331
#define REG_HVBJOY
H/V blank and joypad status (R).
Definition registers.h:307
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:53
Memory Usage
| Component | Size |
| SPC700 Driver | ~5.5KB |
| Sample Data | Up to ~58KB |
| Echo Buffer | 2 KB per unit of delay, under $FF00 (none without an echo) |
Total audio RAM: 64KB
Tips
- Keep samples small - Use lower sample rates for less important sounds
- Reuse samples - Same sample at different pitches for variety
- Test on hardware - Emulator timing may differ
- Size the echo to what you hear - its ring takes 2 KB of audio RAM per 16 ms of delay
Example ROMs
- examples/audio/snesmod_music/ - Music playback with transport controls, and effects over it
Next Steps