OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
audio.h File Reference

OpenSNES Audio System. More...

#include <snes/types.h>

Go to the source code of this file.

Classes

struct  AudioSample
 BRR sample descriptor. More...
struct  AudioVoiceState
 Voice state information. More...

Macros

#define AUDIO_ATTACK_FAST   12
#define AUDIO_ATTACK_INSTANT   15
 ADSR attack rates (0=4.1s, 15=instant).
#define AUDIO_ATTACK_MEDIUM   8
#define AUDIO_ATTACK_SLOW   4
#define AUDIO_DECAY_FAST   7
#define AUDIO_DECAY_MEDIUM   4
#define AUDIO_DECAY_NONE   0
 ADSR decay rates.
#define AUDIO_DECAY_SLOW   1
#define AUDIO_ECHO_DELAY_MAX   7
#define AUDIO_ECHO_DELAY_MIN   1
 Echo delay (delay_ms = value * 16ms).
#define AUDIO_ERR_INVALID_ID   2
#define AUDIO_ERR_NO_MEMORY   1
#define AUDIO_ERR_NOT_LOADED   3
#define AUDIO_ERR_TIMEOUT   4
#define AUDIO_MAX_SAMPLES   64
 Maximum number of sample slots.
#define AUDIO_MAX_VOICES   8
 Maximum number of voices.
#define AUDIO_OK   0
 Error codes.
#define AUDIO_PAN_CENTER   8
#define AUDIO_PAN_LEFT   0
 Pan positions (0-15 scale; 8 is an exact centre, L = R = vol/2).
#define AUDIO_PAN_RIGHT   15
#define AUDIO_PITCH_C3   0x085F
 Common pitch values.
#define AUDIO_PITCH_C4   0x10BE
#define AUDIO_PITCH_C5   0x217C
#define AUDIO_PITCH_DEFAULT   0x1000
 Default pitch (1.0x playback rate).
#define AUDIO_RELEASE_FAST   24
#define AUDIO_RELEASE_INSTANT   31
 ADSR sustain RATE (ADSR2 bits 0-4): how fast the envelope keeps falling from the sustain level while the key is held. 0 holds the level for ever, 31 falls fastest. The S-DSP has no programmable release: on key-off the level drops at a fixed rate (fullsnes "Rate=31, Step=-8"; snesdev-wiki Errata). These names kept the "release" word of the API; AUDIO_RELEASE_INSTANT makes a held note die away quickly, it does not cut it. (Until 2026-10-04 this comment called the field the release rate — library audit D1.).
#define AUDIO_RELEASE_MEDIUM   16
#define AUDIO_RELEASE_SLOW   8
#define AUDIO_SUSTAIN_FULL   7
 ADSR sustain levels (0=1/8, 7=full).
#define AUDIO_SUSTAIN_HALF   3
#define AUDIO_SUSTAIN_QUARTER   1
#define AUDIO_VOICE_AUTO   0xFF
 Let audioPlaySampleOn() pick the voice (round-robin), as audioPlaySample() / audioPlaySampleEx() always do.
#define AUDIO_VOICE_NONE   0xFF
 Returned by audioPlaySample() / audioPlaySampleEx() when nothing was played (driver not ready, unknown or unloaded sample, command timeout). Was a bare 0xFF in the code until 2026-09-21.
#define AUDIO_VOL_MAX   127
 Maximum volume value.
#define AUDIO_VOL_MIN   0
#define audioSetPan(id, pan)
 Legacy pan conversion (0-127 to 0-15).
#define audioStop()
 Alias for audioStopAll.
#define audioStopSample(id)
 Alias for audioStopVoice.

Functions

u8 audioDisableEcho (void)
 Disable echo for all voices.
u8 audioEnableEcho (u8 voiceMask)
 Enable echo for specific voices.
u16 audioGetFreeMemory (void)
 Get available SPC700 RAM for samples.
u8 audioGetSampleInfo (u8 id, AudioSample *info)
 Get information about a loaded sample.
void audioGetVoiceState (u8 voice, AudioVoiceState *state)
 Get current state of a voice.
u8 audioGetVolume (void)
 Get current master volume.
u8 audioInit (void)
 Initialize the audio system.
u8 audioIsReady (void)
 Check if audio system is ready.
u8 audioLoadSample (u8 id, const u8 *brrData, u16 size, u16 loopPoint)
 Load a BRR sample into SPC700 RAM.
u8 audioPlaySample (u8 sampleId)
 Play a sample with default settings.
u8 audioPlaySampleEx (u8 sampleId, u8 volume, u8 pan, u16 pitch)
 Play a sample with custom settings.
u8 audioPlaySampleOn (u8 voice, u8 sampleId, u8 volume, u8 pan, u16 pitch)
 Play a sample on a voice YOU choose.
u8 audioSetADSR (u8 voice, u8 attack, u8 decay, u8 sustain, u8 release)
 Set ADSR envelope for a voice.
u8 audioSetEcho (u8 delay, s8 feedback, s8 volumeL, s8 volumeR)
 Configure echo parameters.
u8 audioSetEchoFilter (const s8 fir[8])
 Set FIR filter coefficients for echo.
u8 audioSetGain (u8 voice, u8 mode)
 Set GAIN mode for a voice (alternative to ADSR).
u8 audioSetVoicePitch (u8 voice, u16 pitch)
 Set pitch for a specific voice.
u8 audioSetVoiceVolume (u8 voice, u8 volumeL, u8 volumeR)
 Set volume for a specific voice.
u8 audioSetVolume (u8 volume)
 Set master volume.
u8 audioStopAll (void)
 Stop all audio playback.
u8 audioStopVoice (u8 voice)
 Stop a specific voice.
void audioUnloadSample (u8 id)
 Unload a sample from a slot.

Detailed Description

OpenSNES Audio System.

Note
v2 engine (2026-07): rebuilt on the raw-APU path — a pure C command layer (audio.c) driving a resident SPC700 driver built from source at lib build time (the legacy PVSnesLib-ABI audio.asm is gone). The full surface below is implemented: init/ready, master volume, per-voice control, dynamic BRR loading, playback with pan/pitch, echo with FIR filter, and live voice-state readback. Design notes: .claude/notes/chantiers/audio_v2.md.
Warning
Main-thread only — do not call audio functions from an NMI callback. One engine per ROM: do not link audio and snesmod together (both own the APU). apuReset() hot-swap works over this driver ($FE stays reserved).

Comprehensive audio API featuring:

  • 8 simultaneous voices with independent volume/pan/pitch
  • Dynamic BRR sample loading (up to 64 samples)
  • Echo/reverb effects with configurable FIR filter
  • Per-voice ADSR envelope control

Quick Start

// Makefile: LIB_MODULES := console audio input (audio pulls in apu)
#include <snes.h>
extern u8 beep_brr[];
extern u8 beep_brr_end[];
int main(void) {
audioInit(); // uploads + starts the driver
audioLoadSample(0, beep_brr, (u16)(beep_brr_end - beep_brr), 0);
while (1) {
if (padPressed(0) & KEY_A) {
}
}
}
int main(void)
Definition main.c:66
void consoleInit(void)
Initialize SNES hardware.
void WaitForVBlank(void)
Wait for next VBlank period.
u8 audioInit(void)
Initialize the audio system.
u8 audioPlaySample(u8 sampleId)
Play a sample with default settings.
u8 audioLoadSample(u8 id, const u8 *brrData, u16 size, u16 loopPoint)
Load a BRR sample into SPC700 RAM.
#define KEY_A
Definition input.h:84
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:53
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:47
#define padPressed(pad)
Definition input.h:159
OpenSNES common-case master header.

Features

  • Up to 8 simultaneous sound effects (SPC700 voices)
  • Up to 64 sample slots with dynamic loading/unloading
  • Per-voice volume, pan, and pitch control
  • Echo/reverb with configurable delay and FIR filter
  • Per-voice ADSR envelope control
  • Fully documented, readable SPC700 driver (~500 bytes)
Author
OpenSNES Team