OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
Initialization

Functions

u8 audioCommandDone (void)
 Has the sound CPU taken the last command? (never waits).
u8 audioInit (void)
 Initialize the audio system.
u8 audioIsReady (void)
 Check if audio system is ready.
u8 audioQueueCount (void)
 Orders queued and not yet sent.
u8 audioQueueDrain (u8 maxCmds)
 Send queued orders, at most maxCmds, without waiting.
u8 audioQueueSetVoicePitch (u8 voice, u16 pitch)
 Queue a voice pitch (14-bit); see audioQueueStopVoices(). The mirrors (audioGetVolume(), audioGetVoiceState()) are not touched by the queued functions.
u8 audioQueueSetVolume (u8 volume)
 Queue a master volume (0..AUDIO_VOL_MAX); see audioQueueStopVoices().
u8 audioQueueStopVoices (u8 mask)
 Queue a key-off of the voices of mask (bit n = voice n).

Detailed Description

Function Documentation

◆ audioCommandDone()

u8 audioCommandDone ( void )

Has the sound CPU taken the last command? (never waits).

A function that sends a command returns once the ports are written; the next command waits, bounded, for the sound CPU to acknowledge it before it writes its own (see audioInit()). This function is that wait without the wait: one read of the acknowledgement port. A caller that spreads its commands over several frames, from the main loop or from an nmiSetLag() callback, asks it first and sends only on a 1, so that no send ever spends the frame waiting for the previous one.

On a 1 for an acknowledged command it forgets the command, as the next send would have, so the send that follows does not look at the port again. It never loops, sends nothing, writes no port and counts no error (audio_error_count does not move on a 0).

Cost: a handful of instructions. Measured on luna (testing/fixtures/stress/audio_done): after audioStopVoices(), whose key-off the sound CPU holds about 2 scanlines, the function reads 0 for a few calls in a row before it reads 1.

Note
One sender only: the main loop or the callback, not both. Called in an interrupt that lands inside a send of the main loop, it could read the port before that send records its command.
Returns
1 when no command is pending or the sound CPU acknowledged it (the command is then forgotten), 0 while it has not yet
if (audioCommandDone()) { // never blocks
audioSetVoiceVolume(v, vol[v]); // posted at once, no wait inside
v = (u8)((v + 1) & 7);
}
u8 audioCommandDone(void)
Has the sound CPU taken the last command? (never waits).
u8 audioSetVoiceVolume(u8 voice, u8 volumeL, u8 volumeR)
Set volume for a specific voice.
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:47

◆ audioInit()

u8 audioInit ( void )

Initialize the audio system.

Uploads the SPC700 driver (built into the lib as audio_driver_blob) through the IPL boot-ROM protocol and starts it, then verifies the command channel with a PING. Call once during game initialization; check audioIsReady() afterwards.

Note
Blocks for the duration of the upload (~a frame for the small driver). Interrupts stay enabled; the NMI handler does not touch the APU ports.
Returns
AUDIO_OK, or AUDIO_ERR_TIMEOUT if the driver did not answer the handshake (audioIsReady() then reads 0). Returned nothing until 2026-09-21; so did the setters below, which now return the AUDIO_* code of the command they sent (AUDIO_ERR_INVALID_ID for a voice out of range) instead of swallowing it. Existing callers that ignore the value are unaffected.

Return codes of the functions that send a command (volume, pitch, envelope, key on / off, echo taps): a command is sent and the function returns at once; the driver's acknowledgement is awaited by the NEXT command, before it writes. So AUDIO_OK means "sent", and AUDIO_ERR_TIMEOUT from one of these functions means the command BEFORE it was never acknowledged (the one just asked for was then not sent). Loading a sample, audioSetEcho() and the functions that read a state back wait for their own acknowledgement, as before.

◆ audioIsReady()

u8 audioIsReady ( void )

Check if audio system is ready.

audioInit() blocks until the driver answers, so there is no "still initialising" state to poll: 0 after audioInit() means the SPC700 driver never answered (and every audio call is then a no-op).

Returns
Non-zero if the driver answered audioInit()'s ping, 0 otherwise

◆ audioQueueCount()

u8 audioQueueCount ( void )

Orders queued and not yet sent.

◆ audioQueueDrain()

u8 audioQueueDrain ( u8 maxCmds)

Send queued orders, at most maxCmds, without waiting.

Stops at the first order the driver cannot take yet (the previous command is not acknowledged) or when the ports are in use by the interrupted code. Call it once a frame from the nmiSetLag() callback.

Returns
orders still queued

◆ audioQueueSetVoicePitch()

u8 audioQueueSetVoicePitch ( u8 voice,
u16 pitch )

Queue a voice pitch (14-bit); see audioQueueStopVoices(). The mirrors (audioGetVolume(), audioGetVoiceState()) are not touched by the queued functions.

◆ audioQueueSetVolume()

u8 audioQueueSetVolume ( u8 volume)

Queue a master volume (0..AUDIO_VOL_MAX); see audioQueueStopVoices().

◆ audioQueueStopVoices()

u8 audioQueueStopVoices ( u8 mask)

Queue a key-off of the voices of mask (bit n = voice n).

The command queue lets a game loop slower than the music say what to do and have it sent at the driver's pace, without waiting: the queue holds 16 orders (AUDIO_ERR_NO_MEMORY when full, nothing queued), and audioQueueDrain() sends them. Only the three below are queued; every other function sends at once.

One sender, one lock. The queue is filled from the main thread and drained from one place (an nmiSetLag() callback, or the loop). While the main thread writes the ports ($2140-$2143: any command, a VSET, the block of a sample load) an interrupt-time drain sends nothing and tries again at its next call, so a load in steps (audioLoadSampleStep()) and a drain from a callback can share the ports. The queued orders keep their order; they are not dated.