Skip to content

Commit

Permalink
Reinitializing audio when multiplayer session starts
Browse files Browse the repository at this point in the history
  • Loading branch information
afska committed Feb 26, 2023
1 parent 4a7f762 commit 6a5b2e8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/gameplay/multiplayer/Syncer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include "gameplay/Key.h"

extern "C" {
#include "player/player.h"
}

#define ASSERT(CONDITION, FAILURE_REASON) \
if (!(CONDITION)) { \
fail(FAILURE_REASON); \
Expand Down Expand Up @@ -182,6 +186,7 @@ void Syncer::startPlaying() {
SAVEFILE_write8(SRAM->memory.pageIndex, 0);
SAVEFILE_write8(SRAM->memory.songIndex, 0);

player_reinit();
setState(SyncState::SYNC_STATE_PLAYING);
}

Expand Down
13 changes: 7 additions & 6 deletions src/player/GSMPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@
\
static void unmute() { DSOUNDCTRL = DSOUNDCTRL | UNMUTE; }

#define PLAYER_TURN_ON_SOUND() \
SETSNDRES(1); \
SNDSTAT = SNDSTAT_ENABLE; \
DSOUNDCTRL = 0b1111101100001110;
#define PLAYER_LOAD() fs = find_first_gbfs_file(0);

#define PLAYER_TURN_ON_SOUND() \
SETSNDRES(1); \
SNDSTAT = SNDSTAT_ENABLE; \
DSOUNDCTRL = 0b1111101100001110; \
mute();

#define PLAYER_INIT(TM_CNT_L, TM_CNT_H) \
fs = find_first_gbfs_file(0); \
\
/* TMxCNT_L is count; TMxCNT_H is control */ \
TM_CNT_H = 0; \
TM_CNT_L = 0x10000 - (924 / 2); \
Expand Down
1 change: 1 addition & 0 deletions src/player/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define AUDIO_SYNC_LIMIT 2

void player_init();
void player_reinit();
void player_play(const char* name);
void player_loop(const char* name);
void player_seek(unsigned int msecs);
Expand Down
26 changes: 16 additions & 10 deletions src/player/player.iwram.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@ PLAYER_DEFINE(REG_DMA1CNT,
CHANNEL_A_MUTE,
CHANNEL_A_UNMUTE);

inline void player_init() {
void player_init() {
PLAYER_LOAD();
PLAYER_TURN_ON_SOUND();
PLAYER_INIT(REG_TM0CNT_L, REG_TM0CNT_H);
}

inline void player_play(const char* name) {
void player_reinit() {
mute();
PLAYER_INIT(REG_TM0CNT_L, REG_TM0CNT_H);
}

void player_play(const char* name) {
PLAYER_PLAY(name);
PlaybackState.msecs = 0;
PlaybackState.hasFinished = false;
Expand All @@ -48,12 +54,12 @@ inline void player_play(const char* name) {
currentAudioChunk = 0;
}

inline void player_loop(const char* name) {
void player_loop(const char* name) {
player_play(name);
PlaybackState.isLooping = true;
}

inline void player_seek(unsigned int msecs) {
void player_seek(unsigned int msecs) {
// (cursor must be a multiple of AUDIO_CHUNK_SIZE)
// cursor = src_pos - src
// msecs = cursor * msecsPerSample
Expand All @@ -69,13 +75,13 @@ inline void player_seek(unsigned int msecs) {
currentAudioChunk = 0;
}

inline void player_setRate(int newRate) {
void player_setRate(int newRate) {
rate = newRate;
rateCounter = 0;
currentAudioChunk = 0;
}

inline void player_stop() {
void player_stop() {
PLAYER_STOP();
PlaybackState.msecs = 0;
PlaybackState.hasFinished = false;
Expand All @@ -85,16 +91,16 @@ inline void player_stop() {
currentAudioChunk = 0;
}

inline bool player_isPlaying() {
bool player_isPlaying() {
return src_pos != NULL;
}

inline void player_onVBlank() {
void player_onVBlank() {
PLAYER_POST_UPDATE();
}

inline void player_forever(int (*update)(),
void (*onAudioChunks)(unsigned int current)) {
void player_forever(int (*update)(),
void (*onAudioChunks)(unsigned int current)) {
while (1) {
if (rate != 0) {
rateCounter++;
Expand Down

0 comments on commit 6a5b2e8

Please sign in to comment.