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

Macros

#define AUDIO_LOAD_FAILED   0xFFFF
 What audioLoadSampleStep() returns when the driver stopped answering.

Functions

u16 audioGetFreeMemory (void)
 Get available SPC700 RAM for samples.
u8 audioGetSampleInfo (u8 id, AudioSample *info)
 Get information about a loaded sample.
u8 audioLoadSample (u8 id, const u8 *brrData, u16 size, u16 loopPoint)
 Load a BRR sample into SPC700 RAM.
u8 audioLoadSampleAt (u8 id, u16 spcAddr, const u8 *brrData, u16 size, u16 loopPoint)
 Load a sample at an address of the sound CPU's memory the game chooses.
u8 audioLoadSampleBegin (u8 id, const u8 *brrData, u16 size, u16 loopPoint)
 Start loading a sample in steps, while the game runs.
u8 audioLoadSampleBeginAt (u8 id, u16 spcAddr, const u8 *brrData, u16 size, u16 loopPoint)
 Start loading a sample in steps at an address the game chooses.
u16 audioLoadSampleStep (u16 maxBytes)
 Send the next piece of the sample audioLoadSampleBegin() started.
void audioUnloadSample (u8 id)
 Unload a sample from a slot.

Detailed Description

Macro Definition Documentation

◆ AUDIO_LOAD_FAILED

#define AUDIO_LOAD_FAILED   0xFFFF

What audioLoadSampleStep() returns when the driver stopped answering.

Function Documentation

◆ audioGetFreeMemory()

u16 audioGetFreeMemory ( void )

Get available SPC700 RAM for samples.

The sample area starts at $0B00 of the sound CPU's 64 KB (below it: the driver and the sample directory) and ends

  • at $FFC0, 62,656 bytes, as long as the game has configured no echo;
  • at the echo ring once audioSetEcho() has been called: the ring takes the top of the memory under $FF00, 2,048 bytes per unit of delay — 58,368 bytes of samples with a delay of 2, 48,128 with the longest, 7. audioSetEcho() answers AUDIO_ERR_NO_MEMORY when samples already loaded reach into the ring it is asked for, so a game that wants an echo calls audioSetEcho() before loading, or picks a delay its samples leave room for. A shorter delay gives memory back, and audioReleaseEcho() gives the whole ring back.
Returns
Free bytes available, under the limit that applies now

◆ audioGetSampleInfo()

u8 audioGetSampleInfo ( u8 id,
AudioSample * info )

Get information about a loaded sample.

Parameters
idSample ID
infoPointer to AudioSample struct to fill
Returns
AUDIO_OK if loaded, AUDIO_ERR_NOT_LOADED otherwise

◆ audioLoadSample()

u8 audioLoadSample ( u8 id,
const u8 * brrData,
u16 size,
u16 loopPoint )

Load a BRR sample into SPC700 RAM.

Parameters
idSample slot (0-63)
brrDataPointer to BRR sample data
sizeSize in bytes (must be multiple of 9)
loopPointLoop offset in bytes from the start of the sample (a multiple of 9, one BRR block). It is where a sample that loops comes back to — 0 for a loop from the very start. WHETHER the sample loops is the loop flag of its last BRR block (opensnes-sample sets it), not this value: a one-shot sample ignores it.
Returns
AUDIO_OK on success, or error code
extern u8 explosion_brr[];
extern u8 explosion_brr_end[];
audioLoadSample(0, explosion_brr, explosion_brr_end - explosion_brr, 0);
u8 audioLoadSample(u8 id, const u8 *brrData, u16 size, u16 loopPoint)
Load a BRR sample into SPC700 RAM.
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:47

◆ audioLoadSampleAt()

u8 audioLoadSampleAt ( u8 id,
u16 spcAddr,
const u8 * brrData,
u16 size,
u16 loopPoint )

Load a sample at an address of the sound CPU's memory the game chooses.

audioLoadSample() places each sample where the last one ended. A game whose samples do not all fit keeps a resident set and replaces others, during play, in places of its own: a swap area. This call writes the sample at spcAddr, whole, then its directory entry, as audioLoadSample() does.

  • Any address in the sample area: $0B00 up to the end audioGetFreeMemory() counts to ($FFC0, or the echo ring). A directory entry is two 16-bit addresses, and the S-DSP reads a sample from the byte its entry names, 9 bytes a block, whatever that byte is: no alignment (audioLoadSample() itself places samples on any byte).
  • No overlap with a loaded sample or with the load in steps under way, except slot id's own place: a slot can be replaced where it was, by a sample of any size the area allows.
  • The allocator never goes back. When the sample ends above the place audioLoadSample() would use next, that place moves up to its end, so a later audioLoadSample() goes above it; a sample placed lower leaves it where it was. audioUnloadSample() gives memory back only for the sample that ends exactly there (the bump allocator's rule): a sample loaded at an address below it gives nothing back, and its place is reused by loading another sample there with this call.
  • From the first byte to the directory entry, slot id is "not loaded" (playing it is refused); if the driver stops answering half way it stays so.
  • A voice still playing the old sample of this place reads the new bytes as they arrive: see audioLoadSampleBeginAt() for what to do first.

The place is checked against the 64 slots before anything is sent: about 20 scanlines, measured (audioLoadSampleBeginAt() alike), on top of the bytes' own time (about 650 master cycles a byte, as audioLoadSample()).

Errors (nothing is sent and nothing changes):

  • AUDIO_ERR_INVALID_ID: id is 64 or more, or is the slot being loaded in steps;
  • AUDIO_ERR_TIMEOUT: audioInit() did not start the driver (or it stopped answering during the load: then the slot is "not loaded");
  • AUDIO_ERR_NO_MEMORY: size is 0, or the place is refused: below $0B00, at or above the end of the sample area, running past that end (a place that would wrap past $FFFF included), or overlapping another loaded sample or the load in steps.
Parameters
idSample slot (0-63)
spcAddrWhere the sample's first byte goes
brrDataPointer to the BRR data
sizeSize in bytes (1 at least; a BRR sample is a multiple of 9)
loopPointAs for audioLoadSample()
Returns
AUDIO_OK or one of the errors above
#define SWAP_AREA 0x8000
audioLoadSampleAt(SMP_BOSS, SWAP_AREA, boss_brr, (u16)(boss_brr_end - boss_brr), 0);
u8 audioLoadSampleAt(u8 id, u16 spcAddr, const u8 *brrData, u16 size, u16 loopPoint)
Load a sample at an address of the sound CPU's memory the game chooses.
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:53

◆ audioLoadSampleBegin()

u8 audioLoadSampleBegin ( u8 id,
const u8 * brrData,
u16 size,
u16 loopPoint )

Start loading a sample in steps, while the game runs.

audioLoadSample() does not return before the whole sample is in the sound CPU's memory: about 560 bytes a frame, ten frames of a stopped game for 5.6 KB. This pair sends the same bytes a piece at a time, when the game says so: voices that are playing go on, and voice commands (play, volume, pitch, stop) pass between two steps.

audioLoadSampleBegin() takes the place in memory and sends nothing; each audioLoadSampleStep() sends a piece. The slot is "not loaded" — playing it is refused — until a step returns 0.

  • One sample at a time: a second audioLoadSampleBegin() before the first is whole is refused (AUDIO_ERR_INVALID_ID).
  • audioLoadSample() on other slots stays allowed meanwhile; those samples go above the place taken here.
  • audioUnloadSample(id) before the last step gives the load up; the memory comes back if nothing was loaded above it.
  • brrData must stay valid until the last step (it is ROM data in every case we know).
Parameters
idSample slot (0-63)
brrDataPointer to the BRR data
sizeSize in bytes (3 at least)
loopPointAs for audioLoadSample()
Returns
AUDIO_OK; AUDIO_ERR_NO_MEMORY; AUDIO_ERR_INVALID_ID (bad slot, or a load in steps is already under way); AUDIO_ERR_TIMEOUT before audioInit()
audioLoadSampleBegin(SMP_VOICE, voice_brr, (u16)(voice_brr_end - voice_brr), 0);
// then once a frame, in the game loop:
if (voice_loading && audioLoadSampleStep(64) == 0) voice_loading = 0;
u16 audioLoadSampleStep(u16 maxBytes)
Send the next piece of the sample audioLoadSampleBegin() started.
u8 audioLoadSampleBegin(u8 id, const u8 *brrData, u16 size, u16 loopPoint)
Start loading a sample in steps, while the game runs.

◆ audioLoadSampleBeginAt()

u8 audioLoadSampleBeginAt ( u8 id,
u16 spcAddr,
const u8 * brrData,
u16 size,
u16 loopPoint )

Start loading a sample in steps at an address the game chooses.

audioLoadSampleBegin() at the address of audioLoadSampleAt(), with the same rules for the place; then audioLoadSampleStep() sends it and its last step writes the directory entry, and audioUnloadSample(id) before that gives the load up, as for audioLoadSampleBegin().

Replacing a slot that is loaded: from this call to the last step the slot is "not loaded", so a note asked of it meanwhile is refused (audioPlaySampleOn() returns AUDIO_VOICE_NONE); a voice that was already playing it goes on reading the place, which now holds the new bytes as they arrive.

Silence the place before its first byte goes (here and for audioLoadSampleAt()). A key-off is a release, not a cut: the S-DSP takes 8 off the voice's envelope every sample, from at most $7FF, so 256 samples, 8 ms, and the voice reads its sample all that time (anomie's S-DSP doc: release E-=8 every sample; BRR decoding never stops for a voice, KOFF only sets the envelope). Measured on luna with examples/audio/sample_swap, against a build whose replacement holds the old bytes: audioStopVoice() then the first step at once (36 scanlines later), the output differs for 8 ms; stopped at scanline 200 then one WaitForVBlank() (62 scanlines), it differs for 2 ms; stopped one frame (294 scanlines) or two before, it does not. The envelope reads 0 133 scanlines after audioStopVoice(). So:

  • call audioStopVoice() for every voice that may be playing the place,
  • then let 8 ms pass before the first step: two WaitForVBlank() always do, wherever in the frame the stop was sent; one does only when the stop went at least 126 scanlines before the VBlank.

A note on another voice in the frame of a step goes at its usual time if it is sent before the step, and later by the step's own time if sent after it (105 scanlines for 192 bytes, measured): send the frame's notes first.

Errors (nothing is taken and nothing changes):

  • AUDIO_ERR_INVALID_ID: id is 64 or more, or a load in steps is already under way;
  • AUDIO_ERR_TIMEOUT: audioInit() did not start the driver;
  • AUDIO_ERR_NO_MEMORY: size is under 3, or the place is refused, as for audioLoadSampleAt().
Parameters
idSample slot (0-63)
spcAddrWhere the sample's first byte goes
brrDataPointer to the BRR data; must stay valid until the last step
sizeSize in bytes (3 at least)
loopPointAs for audioLoadSample()
Returns
AUDIO_OK or one of the errors above

◆ audioLoadSampleStep()

u16 audioLoadSampleStep ( u16 maxBytes)

Send the next piece of the sample audioLoadSampleBegin() started.

Parameters
maxBytesHow much to send at most. Bytes travel three at a time, so the step is maxBytes rounded down to a multiple of 3 (3 at least); the last step takes up to two bytes more rather than leave them alone. The cost of a step is what the game chooses here, plus a fixed part: see docs/tutorials/audio_samples.md for the measured figures.
Returns
Bytes still to send; 0 when the sample is whole and playable (and when no load is under way); AUDIO_LOAD_FAILED if the driver stopped answering — the load is given up

◆ audioUnloadSample()

void audioUnloadSample ( u8 id)

Unload a sample from a slot.

Parameters
idSample slot (0-63)

On the sample audioLoadSampleBegin() started and no step has finished: gives that load up.

It does NOT stop a voice that is playing the sample — stop it first. The allocator is a bump pointer: SPC memory comes back only when the sample unloaded ends where the next audioLoadSample() would go (the most recently loaded one, among the samples audioLoadSample() placed), and loading a slot that is already loaded with audioLoadSample() leaks the old block. A sample placed with audioLoadSampleAt() below that point gives nothing back: its place is reused by loading another sample there with audioLoadSampleAt(). (The "voices will be stopped" sentence that stood here was never implemented.)