PVSnesLib  4.3.0
Documentation to code in C or ASM for the Nintendo SNES
audio/effectsandmusic/effectsandmusic.c
/*---------------------------------------------------------------------------------
Simple effect & music sound demo
/*\ WARNING effects must be 1st IT file in convertion
all musics must be after IT file
each combination of a music + effect must be lower than 60K (not 64K because of size of sound drivers)
-- alekmaul
---------------------------------------------------------------------------------*/
#include <snes.h>
#include "res/soundbank.h"
extern char snesfont, snespal;
extern char SOUNDBANK__0,SOUNDBANK__1;
u8 i,j, keyapressed = 0,keybpressed = 0,keyxpressed = 0,keylpressed = 0,keyrpressed = 0;
//---------------------------------------------------------------------------------
int main(void)
{
// Initialize sound engine (take some time)
// Initialize SNES
// Initialize text console with our font
consoleInitText(0, 16 * 2, &snesfont, &snespal);
// Set soundbanks in reverse order
spcSetBank(&SOUNDBANK__1);
spcSetBank(&SOUNDBANK__0);
// Load effects
spcLoad(MOD_EFFECTSSFX);
// Init background
bgSetGfxPtr(0, 0x2000);
bgSetMapPtr(0, 0x6800, SC_32x32);
// Now Put in 16 color mode and disable Bgs except current
setMode(BG_MODE1, 0);
// Load all effects
spcStop(); spcLoad(0);
for (j=0;j<5;j++) {
}
// Draw a wonderfull text :P
consoleDrawText(5, 11, "Press A to play effect !");
consoleDrawText(5, 12, "Press B to play music !");
consoleDrawText(5, 13, "Press X to stop music !");
consoleDrawText(5, 18, "Press L and R to change !");
// Wait for nothing :P
// Wait for nothing :D !
i=0;
consoleDrawText(5, 17, "Effect: tada");
while (1)
{
// Refresh pad values and test key a (without repeating sound if still pressed)
if (padsCurrent(0) & KEY_A)
{
if (keyapressed == 0)
{
keyapressed = 1;
// Play effect
spcEffect(8,i,15*16+8); // (u16 pitch,u16 sfxIndex, u8 volpan); pitch=1=4Khz , 2=8khz, 8=32Khz
}
}
else
keyapressed = 0;
if (padsCurrent(0) & KEY_B)
{
if (keybpressed == 0)
{
keybpressed = 1;
// stop current music and load music. We load 1st music just after effects, so number 1)
spcStop(); spcLoad(1);
// reload effects in memory
for (j=0;j<5;j++) {
}
// play music from the beginning
spcPlay(0);
}
}
else
keybpressed = 0;
if (padsCurrent(0) & KEY_X)
{
if (keyxpressed == 0)
{
keyxpressed = 1;
// Stop music
spcStop();
}
}
else
keyxpressed = 0;
{
if (keylpressed == 0)
{
keylpressed = 1;
if (i>0) i--;
if (i==0)
consoleDrawText(5, 17, "Effect: tada ");
else if (i==1)
consoleDrawText(5, 17, "Effect: Hall Strings ");
else if (i==2)
consoleDrawText(5, 17, "Effect: Honky Tonk Piano");
else if (i==3)
consoleDrawText(5, 17, "Effect: Marimba 1 ");
else if (i==4)
consoleDrawText(5, 17, "Effect: Cowbell ");
}
}
else
keylpressed = 0;
{
if (keyrpressed == 0)
{
keyrpressed = 1;
if (i<5) i++;
if (i==0)
consoleDrawText(5, 14, "Effect: tada ");
else if (i==1)
consoleDrawText(5, 17, "Effect: Hall Strings ");
else if (i==2)
consoleDrawText(5, 17, "Effect: Honky Tonk Piano");
else if (i==3)
consoleDrawText(5, 17, "Effect: Marimba 1 ");
else if (i==4)
consoleDrawText(5, 17, "Effect: Cowbell ");
}
}
else
keyrpressed = 0;
// Update music / sfx stream and wait vbl
}
return 0;
}
void bgSetGfxPtr(u8 bgNumber, u16 address)
Definition of each background address.
void bgSetDisable(u8 bgNumber)
Disable a BG in the actual SNES mode.
void bgSetMapPtr(u8 bgNumber, u16 address, u8 mapSize)
Change Background Map address.
void consoleSetTextVramAdr(u16 vramfont)
Change text graphics address (4K aligned)
void consoleInitText(u8 palnum, u8 palsize, u8 *tilfont, u8 *palfont)
Initialize the Text System.
void consoleInit(void)
Initialize console.
void consoleDrawText(u16 x, u16 y, char *fmt,...)
Output formatted string on a screen (tiles mode)
void consoleSetTextOffset(u16 offsetfont)
Change text Background map address for display (must be BG address)
void consoleSetTextVramBGAdr(u16 offsetfont)
Change text Background map address for display (must be BG address)
void scanPads(void)
Wait for pad ready and read pad values in.
#define padsCurrent(value)
Return current value of selected pad.
Definition: input.h:161
@ KEY_B
pad B button.
Definition: input.h:49
@ KEY_X
pad X button.
Definition: input.h:58
@ KEY_LEFT
pad LEFT button.
Definition: input.h:53
@ KEY_RIGHT
pad RIGHT button.
Definition: input.h:52
@ KEY_A
pad A button.
Definition: input.h:48
void WaitForVBlank(void)
Wait for vblank interrupt
the master include file for snes applications.
void spcLoad(u16 musIndex)
load module into sm-spc. this function may take some time to execute
void spcLoadEffect(u16 sfxIndex)
load sound effect into sm-spc. this function may take some time to execute
void spcEffect(u16 pitch, u16 sfxIndex, u8 volpan)
Play sound effect (load with spcLoadEffect)
void spcSetBank(u8 *bank)
set soundbank origin. soundbank must have dedicated bank(s)
void spcProcess(void)
Process messages This function will try to give messages to the spc until a few scanlines pass.
void spcBoot(void)
boots the spc700 with sm-spc. call once at startup
void spcPlay(u8 startPos)
play module. note: this simply queues a message, use spcFlush if you want to wait until the message i...
void spcStop(void)
stop playing the current module.
void setScreenOn(void)
Put screen On.
void setMode(u8 mode, u8 size)
Set the SNES hardware to operate in new display mode.