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). | |
| 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.
| 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.
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.
| 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).
| u8 audioQueueCount | ( | void | ) |
Orders queued and not yet sent.
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.
Queue a voice pitch (14-bit); see audioQueueStopVoices(). The mirrors (audioGetVolume(), audioGetVoiceState()) are not touched by the queued functions.
Queue a master volume (0..AUDIO_VOL_MAX); see audioQueueStopVoices().
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.