Skip to content

Commit

Permalink
Update some NSMB code.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGameratorT committed Jun 29, 2020
1 parent 1470f70 commit c227e0f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 42 deletions.
47 changes: 44 additions & 3 deletions NSMBDS/functions.h → NSMBDS/declarations.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,49 @@
/*=============================================================\
| Simplified definitions of which declarations are required. |
\=============================================================*/

#define MATH_CLAMP(x, low, high) ( ( (x) > (high) ) ? (high) : ( ( (x) < (low) ) ? (low) : (x) ) )

typedef u8 OSThread[200];
typedef u8 OSMessageQueue[32];
typedef void *OSMessage;

typedef void (*SNDAlarmHandler)(void*);
typedef enum
{
SND_CHANNEL_DATASHIFT_NONE,
SND_CHANNEL_DATASHIFT_1BIT,
SND_CHANNEL_DATASHIFT_2BIT,
SND_CHANNEL_DATASHIFT_4BIT
} SNDChannelDataShift;
typedef enum
{
SND_WAVE_FORMAT_PCM8,
SND_WAVE_FORMAT_PCM16,
SND_WAVE_FORMAT_ADPCM,
SND_WAVE_FORMAT_PSG,
SND_WAVE_FORMAT_NOISE = SND_WAVE_FORMAT_PSG
} SNDWaveFormat;
typedef enum
{
SND_CHANNEL_LOOP_MANUAL,
SND_CHANNEL_LOOP_REPEAT,
SND_CHANNEL_LOOP_1SHOT
} SNDChannelLoop;

typedef u8 FSFile[72];
typedef enum
{
FS_SEEK_SET,
FS_SEEK_CUR,
FS_SEEK_END
} FSSeekFileMode;

extern "C"
{
void OS_Panic();
void OS_WakeupThreadDirect(OSThread *thread);
void OS_CreateThread(OSThread *thread, void (*func) (void *), void *arg, void *stack, u32 stackSize, u32 prio);
void OS_CreateThread(OSThread *thread, void (*func)(void *), void *arg, void *stack, u32 stackSize, u32 prio);
bool OS_ReceiveMessage(OSMessageQueue *mq, OSMessage *msg, s32 flags);
bool OS_SendMessage(OSMessageQueue *mq, OSMessage msg, s32 flags);
void OS_InitMessageQueue(OSMessageQueue *mq, OSMessage *msgArray, s32 msgCount);
Expand All @@ -22,8 +63,8 @@ extern "C"
bool FS_SeekFile(FSFile *p_file, s32 offset, FSSeekFileMode origin);
s32 FS_ReadFile(FSFile *p_file, void *dst, s32 len);
bool FS_CloseFile(FSFile *p_file);
bool FS_OpenFileDirect(FSFile *p_file, FSArchive *p_arc, u32 image_top, u32 image_bottom, u32 file_index);
bool FS_OpenFileFast(FSFile* p_file, void* archivePtr, int file_id);
void FS_InitFile(FSFile *p_file);
}

int getPlayerCount();
int getPlayerCount();
46 changes: 25 additions & 21 deletions NSMBDS/source/NWAVPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@

namespace NWAVPlayer
{
/*========================\
| Typedef declarations. |
\========================*/

typedef u8(*pStrmBufT)[2][STRM_BUF_SIZE];

/*==========================\
| Structure declarations. |
\==========================*/
Expand All @@ -57,7 +51,7 @@ namespace NWAVPlayer
int sampleRate;
int loopStart;
int loopEnd;
u8 format;
SNDWaveFormat format : 8;
u8 stereo;
u8 numEvents;
u8 padding;
Expand Down Expand Up @@ -91,8 +85,8 @@ namespace NWAVPlayer
EventHandler eventHandler;
};

const int headerSize = sizeof(Header);
const int eventInfoSize = sizeof(EventInfo);
constexpr int headerSize = sizeof(Header);
constexpr int eventInfoSize = sizeof(EventInfo);

/*=======================\
| Static declarations. |
Expand All @@ -114,6 +108,8 @@ namespace NWAVPlayer
static OSMessage msgBuf;

static EventInfo* events;

typedef u8(*pStrmBufT)[2][STRM_BUF_SIZE];
static pStrmBufT pStrmBuf;

/*=========================\
Expand Down Expand Up @@ -374,17 +370,19 @@ namespace NWAVPlayer
s32 timerValue = SND_TIMER_CLOCK / sInfo.playRate;
u32 alarmPeriod = timerValue * (STRM_BUF_PAGESIZE / sInfo.bytesPerSample) / 32;

s32 loopLen = STRM_BUF_SIZE / sizeof(u32);

//Setup channels.
for (int i = 0; i < sInfo.chCount; i++)
{
bool left = i == 0;
SND_SetupChannelPcm(
left ? CHANNEL_L_NUM : CHANNEL_R_NUM,
static_cast<SNDWaveFormat>(hInfo.format),
hInfo.format,
left ? (*pStrmBuf)[0] : (*pStrmBuf)[1],
SND_CHANNEL_LOOP_REPEAT,
0,
STRM_BUF_SIZE / sizeof(u32),
loopLen,
sInfo.volume,
SND_CHANNEL_DATASHIFT_NONE,
timerValue,
Expand All @@ -396,6 +394,17 @@ namespace NWAVPlayer
SND_SetupAlarm(ALARM_NUM, alarmPeriod, alarmPeriod, SoundAlarmHandler, &sInfo);
}

//Reloads the current timers to apply new settings.
void reloadTimers()
{
bool notPaused = !sInfo.isPaused;
if (notPaused)
setPaused(true);
setup();
if (notPaused)
setPaused(false);
}

//Gets the music speed.
fx32 getSpeed() { return sInfo.speed; }

Expand All @@ -406,14 +415,7 @@ namespace NWAVPlayer
fx32 sampleRate = hInfo.sampleRate << FX32_SHIFT;
sInfo.playRate = FX_MulInline(sampleRate, speed) >> FX32_SHIFT;
sInfo.speed = speed;

//Update the music.
bool notPaused = !sInfo.isPaused;
if (notPaused)
setPaused(true);
setup();
if (notPaused)
setPaused(false);
reloadTimers();
}

//Loads the NWAV events that will be used to trigger the current callback function set.
Expand Down Expand Up @@ -520,12 +522,14 @@ namespace NWAVPlayer

//Startup stream thread.
OS_InitMessageQueue(&msgQ, &msgBuf, 1);
OS_CreateThread(&strmThread,
OS_CreateThread(
&strmThread,
StrmThread,
nullptr,
&strmThreadStack[THREAD_STACK_SIZE],
THREAD_STACK_SIZE,
STREAM_THREAD_PRIO);
STREAM_THREAD_PRIO
);
OS_WakeupThreadDirect(&strmThread);
}
}
40 changes: 22 additions & 18 deletions NSMBDS/source/NWAVPlayer_nsmb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
#include "nitro_if.h"
#include "Extras.h"

#ifdef __INTELLISENSE__
#define __attribute__(x)
#endif // __INTELLISENSE__

static bool& doBahp = *(bool*)0x02088B9C;

//Implement enemy dance animation events
void NWAV_UpdateBahps()
static void updateBahps()
{
if (!NWAVPlayer::getPaused())
{
Expand All @@ -43,7 +47,7 @@ void NWAV_UpdateBahps()
}
}

void NWAV_MainEventHandler(int eventID)
static void mainEventHandler(int eventID)
{
switch (eventID)
{
Expand All @@ -62,14 +66,14 @@ void repl_0204F2F0()
{
NNS_SndInit(); //Keep replaced instruction
NWAVPlayer::init();
NWAVPlayer::setEventHandler(NWAV_MainEventHandler);
NWAVPlayer::setEventHandler(mainEventHandler);
}

void repl_0204EC74()
{
NNS_SndMain(); //Keep replaced instruction
NWAVPlayer::updateFade();
NWAV_UpdateBahps();
updateBahps();
}

//Reduce sound heap size and init
Expand All @@ -93,15 +97,15 @@ void repl_02009434() {}
| This is where the Nitro WAV player is controlled. |
\=============================================================*/

const int firstWavID = 2089;
constexpr int firstWavID = 2089;

//Backup the original functions
__attribute__((naked)) void Music_PlaySeqBak(int seqID, int sfxSetID) { asm("STMFD SP!, {R4,LR}"); asm("B 0x02011E80"); }
__attribute__((naked)) void Music_LoadSeqBak(int seqID) { asm("STMFD SP!, {R4,LR}"); asm("B 0x0204F198"); }
__attribute__((naked)) void Music_SetTempoBak(int ratio) { asm("STMFD SP!, {R4,LR}"); asm("B 0x0204E530"); }
__attribute__((naked)) void Music_SetVolumeBak(int targetVolume, int frames) { asm("STMFD SP!, {R4,LR}"); asm("B 0x0204DCB8"); }
__attribute__((naked)) void Music_PauseSeqBak(bool flag) { asm("STMFD SP!, {R4,LR}"); asm("B 0x0204E480"); }
__attribute__((naked)) void Music_StopSeqBak(int fadeFrame) { asm("STMFD SP!, {R4,LR}"); asm("B 0x0204E424"); }
__attribute__((naked)) static void playSeq(int seqID, int sfxSetID) { asm("STMFD SP!, {R4,LR}\nB 0x02011E80"); }
__attribute__((naked)) static void loadSeq(int seqID) { asm("STMFD SP!, {R4,LR}\nB 0x0204F198"); }
__attribute__((naked)) static void setTempo(int ratio) { asm("STMFD SP!, {R4,LR}\nB 0x0204E530"); }
__attribute__((naked)) static void setVolume(int targetVolume, int frames) { asm("STMFD SP!, {R4,LR}\nB 0x0204DCB8"); }
__attribute__((naked)) static void pauseSeq(bool flag) { asm("STMFD SP!, {R4,LR}\nB 0x0204E480"); }
__attribute__((naked)) static void stopSeq(int fadeFrame) { asm("STMFD SP!, {R4,LR}\nB 0x0204E424"); }

static bool GetIfSequenced(int seqID)
{
Expand Down Expand Up @@ -136,12 +140,12 @@ void nsub_02011E7C(int seqID, int sfxSetID)
{
if (currSeq != seqID)
NWAVPlayer::stop(0);
Music_PlaySeqBak(seqID, sfxSetID);
playSeq(seqID, sfxSetID);
}
else
{
if (currSeq != seqID)
Music_StopSeqBak(0);
stopSeq(0);

NWAVPlayer::play(wavID);

Expand All @@ -153,33 +157,33 @@ void nsub_02011E7C(int seqID, int sfxSetID)
void nsub_0204F194(int seqID)
{
if (GetIfSequenced(seqID))
Music_LoadSeqBak(seqID);
loadSeq(seqID);
}

//Replace set tempo
void nsub_0204E52C(int ratio)
{
Music_SetTempoBak(ratio);
setTempo(ratio);
NWAVPlayer::setSpeed(ratio << 12 >> 8);
}

//Replace set volume
void nsub_0204DCB4(int targetVolume, int frames)
{
Music_SetVolumeBak(targetVolume, frames);
setVolume(targetVolume, frames);
NWAVPlayer::setVolume(targetVolume, frames);
}

//Replace pause
void nsub_0204E47C(bool flag)
{
Music_PauseSeqBak(flag);
pauseSeq(flag);
NWAVPlayer::setPaused(flag);
}

//Replace stop
void nsub_0204E420(int fadeFrame)
{
Music_StopSeqBak(fadeFrame);
stopSeq(fadeFrame);
NWAVPlayer::stop(fadeFrame);
}
File renamed without changes.

0 comments on commit c227e0f

Please sign in to comment.