OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
main.c File Reference

Sound effects the way a game engine drives them: voices by number. More...

#include <snes.h>
#include <snes/audio.h>
#include <snes/profile.h>
#include "res/wave.h"

Macros

#define LOAD_STEP   192
 Bytes sent per frame while loading in pieces.
#define PH_DONE   5
#define PH_LOAD   1
 Phases, written to phase so a manifest knows where the run is.
#define PH_RESTART   3
#define PH_SEVEN   4
#define PH_SWEEP   2
#define SMP_FILL_A   1
#define SMP_FILL_B   2
#define SMP_FILL_C   3
#define SMP_WAVE   0
 Sample slots: the waveform, then the three loaded in pieces.
#define VOICE_RESTART   3
#define VOICE_SWEEP   2
 Voice of the pitch sweep and voice of the restarts.

Functions

static void enter (u8 p, u16 c)
 Start a phase: set the oracle, zero its frame counter.
static void load_in_pieces (u8 slot, u8 *data, u8 *end)
 Load one filler sample in pieces, one step per frame.
int main (void)
static void tick (void)
 Wait one frame and count it into the current phase.
static void tint (u16 c)
 Backdrop colour per phase, so the run can be followed by eye.
static void update_voices (u8 count, u16 pitch)
 The seven-voice update, one command per voice.

Variables

u8 fill_a []
 Filler samples (data.asm): fixed byte patterns, never played.
u8 fill_a_end []
u8 fill_b []
u8 fill_b_end []
u8 fill_c []
u8 fill_c_end []
u16 frames_in_phase
u16 free_after
u16 free_before
u16 idle_lines_max
u16 load_frames
u16 load_lines_max
u8 phase
 Oracles (WRAM): read by testing/manifests/audio_sfx_voices.toml.
u16 restart_lines_max
u16 restart_pitch
u8 restarts_ok
u16 seven_frames
u16 seven_lines_max
u16 seven_lines_sum
u16 sweep_lines_max
u16 sweep_pitch

Detailed Description

Sound effects the way a game engine drives them: voices by number.

Three recipes a game-side sound player is built from, each measured by a luna manifest, with the audio module as it is (no extra layer):

  1. A looped waveform whose pitch moves every frame - a 64-sample cycle loops on voice 2 and audioSetVoiceVolumePitch() sends the note each frame.
  2. Stop, then play, on the same voice inside one frame - the "last arrival cuts the sound in progress" rule: audioStopVoice() followed by audioPlaySampleOn() on the same voice, with no frame in between.
  3. A sample loaded in pieces - audioLoadSampleBegin() then a small audioLoadSampleStep() per frame while the game keeps running.

A fourth block times seven voices updated every frame. Each block counts the scanlines it used with profileScanlineStart()/End() (1 line = 1364 master cycles) so the cost is read from RAM, not guessed.

Nothing to press: the phases run on their own and the backdrop colour names the phase.

SNES Concepts
  • Looped one-cycle waveform: pitch is the note
  • Voice ownership: the game picks the voice, the module just obeys
  • Piecewise upload over the APU ports: bounded cost per frame
What to Observe
Backdrop: blue while loading, green during the waveform sweep, yellow for the restarts, red for the seven-voice block, then grey and silence.
Modules Used
console, dma, audio, profile
See also
lib/include/snes/audio.h
docs/tutorials/audio_samples.md

Macro Definition Documentation

◆ LOAD_STEP

#define LOAD_STEP   192

Bytes sent per frame while loading in pieces.

◆ PH_DONE

#define PH_DONE   5

◆ PH_LOAD

#define PH_LOAD   1

Phases, written to phase so a manifest knows where the run is.

◆ PH_RESTART

#define PH_RESTART   3

◆ PH_SEVEN

#define PH_SEVEN   4

◆ PH_SWEEP

#define PH_SWEEP   2

◆ SMP_FILL_A

#define SMP_FILL_A   1

◆ SMP_FILL_B

#define SMP_FILL_B   2

◆ SMP_FILL_C

#define SMP_FILL_C   3

◆ SMP_WAVE

#define SMP_WAVE   0

Sample slots: the waveform, then the three loaded in pieces.

◆ VOICE_RESTART

#define VOICE_RESTART   3

◆ VOICE_SWEEP

#define VOICE_SWEEP   2

Voice of the pitch sweep and voice of the restarts.

Function Documentation

◆ enter()

void enter ( u8 p,
u16 c )
static

Start a phase: set the oracle, zero its frame counter.

◆ load_in_pieces()

void load_in_pieces ( u8 slot,
u8 * data,
u8 * end )
static

Load one filler sample in pieces, one step per frame.

◆ main()

int main ( void )

◆ tick()

void tick ( void )
static

Wait one frame and count it into the current phase.

◆ tint()

void tint ( u16 c)
static

Backdrop colour per phase, so the run can be followed by eye.

◆ update_voices()

void update_voices ( u8 count,
u16 pitch )
static

The seven-voice update, one command per voice.

Variable Documentation

◆ fill_a

u8 fill_a[]
extern

Filler samples (data.asm): fixed byte patterns, never played.

◆ fill_a_end

u8 fill_a_end[]

◆ fill_b

u8 fill_b[]
extern

◆ fill_b_end

u8 fill_b_end[]

◆ fill_c

u8 fill_c[]
extern

◆ fill_c_end

u8 fill_c_end[]

◆ frames_in_phase

u16 frames_in_phase

◆ free_after

u16 free_after

...and after the three samples are whole

◆ free_before

u16 free_before

audioGetFreeMemory() before loading in pieces

◆ idle_lines_max

u16 idle_lines_max

the same loop with nothing sent: the floor

◆ load_frames

u16 load_frames

frames from the first Begin to the last Step

◆ load_lines_max

u16 load_lines_max

worst scanlines of one audioLoadSampleStep()

◆ phase

u8 phase

Oracles (WRAM): read by testing/manifests/audio_sfx_voices.toml.

◆ restart_lines_max

u16 restart_lines_max

worst scanlines of one stop+play pair

◆ restart_pitch

u16 restart_pitch

pitch of the last restart

◆ restarts_ok

u8 restarts_ok

stop+play pairs that answered the right voice

◆ seven_frames

u16 seven_frames

frames measured in the seven-voice block

◆ seven_lines_max

u16 seven_lines_max

worst scanlines of seven voice updates

◆ seven_lines_sum

u16 seven_lines_sum

their sum over seven_frames

◆ sweep_lines_max

u16 sweep_lines_max

worst scanlines of one pitch+volume update

◆ sweep_pitch

u16 sweep_pitch

last pitch sent by the sweep