Skip to content

Commit

Permalink
Wii: Move audio handling to callbacks (fixes #803)
Browse files Browse the repository at this point in the history
  • Loading branch information
endrift committed Aug 3, 2018
1 parent 20c99d2 commit 825c70b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Misc:
- FFmpeg: Support lossless h.264 encoding
- Feature: Added loading savestates from command line
- Qt: Allow pausing game at load (fixes mgba.io/i/1129)
- Wii: Move audio handling to callbacks (fixes mgba.io/i/803)

0.6.3: (2017-04-14)
Bugfixes:
Expand Down
46 changes: 29 additions & 17 deletions src/platform/wii/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ static enum VideoMode {

static void _retraceCallback(u32 count);

static void _postAudioBuffer(struct mAVStream* stream, blip_t* left, blip_t* right);
static void _audioDMA(void);
static void _setRumble(struct mRumble* rumble, int enable);
static void _sampleRotation(struct mRotationSource* source);
Expand All @@ -93,6 +94,7 @@ static s8 WPAD_StickX(u8 chan, u8 right);
static s8 WPAD_StickY(u8 chan, u8 right);

static void* outputBuffer;
static struct mAVStream stream;
static struct mRumble rumble;
static struct mRotationSource rotation;
static GXRModeObj* vmode;
Expand Down Expand Up @@ -222,7 +224,7 @@ static void reconfigureScreen(struct mGUIRunner* runner) {
runner->params.width = vmode->fbWidth * guiScale * wAdjust;
runner->params.height = vmode->efbHeight * guiScale * hAdjust;
if (runner->core) {
double ratio = GBAAudioCalculateRatio(1,audioSampleRate, 1);
double ratio = GBAAudioCalculateRatio(1, audioSampleRate, 1);
blip_set_rates(runner->core->getAudioChannel(runner->core, 0), runner->core->frequency(runner->core), 48000 * ratio);
blip_set_rates(runner->core->getAudioChannel(runner->core, 1), runner->core->frequency(runner->core), 48000 * ratio);

Expand Down Expand Up @@ -317,6 +319,11 @@ int main(int argc, char* argv[]) {
rotation.readTiltY = _readTiltY;
rotation.readGyroZ = _readGyroZ;

stream.videoDimensionsChanged = NULL;
stream.postVideoFrame = NULL;
stream.postAudioFrame = NULL;
stream.postAudioBuffer = _postAudioBuffer;

struct mGUIRunner runner = {
.params = {
720, 480,
Expand Down Expand Up @@ -554,6 +561,25 @@ static void _audioDMA(void) {
audioBufferSize = 0;
}

static void _postAudioBuffer(struct mAVStream* stream, blip_t* left, blip_t* right) {
UNUSED(stream);
int available = blip_samples_avail(left);
if (available + audioBufferSize > SAMPLES) {
available = SAMPLES - audioBufferSize;
}
available &= ~((32 / sizeof(struct GBAStereoSample)) - 1); // Force align to 32 bytes
if (available > 0) {
// These appear to be reversed for AUDIO_InitDMA
blip_read_samples(left, &audioBuffer[currentAudioBuffer][audioBufferSize].right, available, true);
blip_read_samples(right, &audioBuffer[currentAudioBuffer][audioBufferSize].left, available, true);
audioBufferSize += available;
}
if (audioBufferSize == SAMPLES && !AUDIO_GetDMAEnableFlag()) {
_audioDMA();
AUDIO_StartDMA();
}
}

static void _drawStart(void) {
VIDEO_SetBlack(false);

Expand Down Expand Up @@ -665,6 +691,7 @@ void _guiPrepare(void) {
void _setup(struct mGUIRunner* runner) {
runner->core->setPeripheral(runner->core, mPERIPH_ROTATION, &rotation);
runner->core->setPeripheral(runner->core, mPERIPH_RUMBLE, &rumble);
runner->core->setAVStream(runner->core, &stream);

_mapKey(&runner->core->inputMap, GCN1_INPUT, PAD_BUTTON_A, GBA_KEY_A);
_mapKey(&runner->core->inputMap, GCN1_INPUT, PAD_BUTTON_B, GBA_KEY_B);
Expand Down Expand Up @@ -783,22 +810,7 @@ void _unpaused(struct mGUIRunner* runner) {
}

void _drawFrame(struct mGUIRunner* runner, bool faded) {
int available = blip_samples_avail(runner->core->getAudioChannel(runner->core, 0));
if (available + audioBufferSize > SAMPLES) {
available = SAMPLES - audioBufferSize;
}
available &= ~((32 / sizeof(struct GBAStereoSample)) - 1); // Force align to 32 bytes
if (available > 0) {
// These appear to be reversed for AUDIO_InitDMA
blip_read_samples(runner->core->getAudioChannel(runner->core, 0), &audioBuffer[currentAudioBuffer][audioBufferSize].right, available, true);
blip_read_samples(runner->core->getAudioChannel(runner->core, 1), &audioBuffer[currentAudioBuffer][audioBufferSize].left, available, true);
audioBufferSize += available;
}
if (audioBufferSize == SAMPLES && !AUDIO_GetDMAEnableFlag()) {
_audioDMA();
AUDIO_StartDMA();
}

UNUSED(runner);
uint32_t color = 0xFFFFFF3F;
if (!faded) {
color |= 0xC0;
Expand Down

0 comments on commit 825c70b

Please sign in to comment.