From 2cc88f4b8eca9da063edc532443f99d227d17dbb Mon Sep 17 00:00:00 2001 From: "simon.kagstrom" Date: Mon, 5 Jan 2009 09:43:37 +0000 Subject: [PATCH] Use gc audio (not working yet) --- Makefile | 2 +- Src/SID.cpp | 8 +-- Src/SID_wii.i | 93 ++++++++++++++++++++++++++++++++ Src/gcaudio.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++++ Src/gcaudio.h | 27 ++++++++++ Src/main_wii.i | 2 +- 6 files changed, 267 insertions(+), 5 deletions(-) create mode 100644 Src/SID_wii.i create mode 100644 Src/gcaudio.c create mode 100644 Src/gcaudio.h diff --git a/Makefile b/Makefile index 38616e8..f359c18 100644 --- a/Makefile +++ b/Makefile @@ -58,7 +58,7 @@ export DEPSDIR := $(CURDIR)/$(BUILD) #--------------------------------------------------------------------------------- # automatically build a list of object files for our project #--------------------------------------------------------------------------------- -CFILES := char_to_kc.c +CFILES := char_to_kc.c gcaudio.c CPPFILES := Display.cpp main.cpp Prefs.cpp SID.cpp REU.cpp IEC.cpp 1541fs.cpp \ 1541d64.cpp 1541t64.cpp 1541job.cpp SAM.cpp C64.cpp CPUC64.cpp VIC.cpp \ CIA.cpp CPU1541.cpp menu.cpp diff --git a/Src/SID.cpp b/Src/SID.cpp index 1c87c9e..eff9fe3 100644 --- a/Src/SID.cpp +++ b/Src/SID.cpp @@ -269,6 +269,8 @@ void MOS6581::SetState(MOS6581State *ss) #if defined(AMIGA) || defined(__riscos__) const uint32 SAMPLE_FREQ = 22050; // Sample output frequency in Hz +#elif defined(GEKKO) +const uint32 SAMPLE_FREQ = 48000; #else const uint32 SAMPLE_FREQ = 44100; // Sample output frequency in Hz #endif @@ -1362,6 +1364,9 @@ void DigitalRenderer::calc_buffer(int16 *buf, long count) #elif defined(AMIGA) #include "SID_Amiga.i" +#elif defined(GEKKO) +#include "SID_wii.i" + #elif defined(HAVE_SDL) #include "SID_SDL.i" @@ -1383,9 +1388,6 @@ void DigitalRenderer::calc_buffer(int16 *buf, long count) #elif defined(__riscos__) #include "SID_Acorn.i" -#elif defined(GEKKO) -#include "SID_wii.i" - #else // No sound void DigitalRenderer::init_sound(void) {ready = false;} DigitalRenderer::~DigitalRenderer() {} diff --git a/Src/SID_wii.i b/Src/SID_wii.i new file mode 100644 index 0000000..ff620e0 --- /dev/null +++ b/Src/SID_wii.i @@ -0,0 +1,93 @@ +/* + * SID_linux.i - 6581 emulation, Linux specific stuff + * + * Frodo (C) 1994-1997,2002 Christian Bauer + * Linux sound stuff by Bernd Schmidt + */ + +#include "gcaudio.h" + +#include "VIC.h" + +/* + * Initialization + */ + +void DigitalRenderer::init_sound(void) +{ + this->sndbufsize = 1024; + + ready = false; + InitialiseAudio(); + ResetAudio(); + + this->sound_buffer = new int16[this->sndbufsize]; + ready = true; +} + + +/* + * Destructor + */ + +DigitalRenderer::~DigitalRenderer() +{ + StopAudio(); +} + + +/* + * Pause sound output + */ + +void DigitalRenderer::Pause(void) +{ + StopAudio(); +} + + +/* + * Resume sound output + */ + +void DigitalRenderer::Resume(void) +{ + /* Done by PlayAudio() */ +} + + +/* + * Fill buffer, sample volume (for sampled voice) + */ + +void DigitalRenderer::EmulateLine(void) +{ + static int divisor = 0; + static int to_output = 0; + static int buffer_pos = 0; + + if (!ready) + return; + + sample_buf[sample_in_ptr] = volume; + sample_in_ptr = (sample_in_ptr + 1) % SAMPLE_BUF_SIZE; + + /* + * Now see how many samples have to be added for this line + */ + divisor += SAMPLE_FREQ; + while (divisor >= 0) + divisor -= TOTAL_RASTERS*SCREEN_FREQ, to_output++; + + /* + * Calculate the sound data only when we have enough to fill + * the buffer entirely. + */ + if ((buffer_pos + to_output) >= sndbufsize) { + int datalen = sndbufsize - buffer_pos; + to_output -= datalen; + calc_buffer(sound_buffer + buffer_pos, datalen*2); + PlaySound((uint16_t*)sound_buffer, sndbufsize); + buffer_pos = 0; + } +} diff --git a/Src/gcaudio.c b/Src/gcaudio.c new file mode 100644 index 0000000..9035378 --- /dev/null +++ b/Src/gcaudio.c @@ -0,0 +1,140 @@ +/**************************************************************************** + * FCE Ultra 0.98.12 + * Nintendo Wii/Gamecube Port + * + * Tantric September 2008 + * eke-eke October 2008 + * + * gcaudio.c + * + * Audio driver + ****************************************************************************/ + +#include +#include + +#define SAMPLERATE 48000 + +static u8 ConfigRequested = 0; +static u8 soundbuffer[2][3840] ATTRIBUTE_ALIGN(32); +static u8 mixbuffer[16000]; +static int mixhead = 0; +static int mixtail = 0; +static int whichab = 0; +static int IsPlaying = 0; + +/**************************************************************************** + * MixerCollect + * + * Collects sound samples from mixbuffer and puts them into outbuffer + * Makes sure to align them to 32 bytes for AUDIO_InitDMA + ***************************************************************************/ +static int MixerCollect( u8 *outbuffer, int len ) +{ + u32 *dst = (u32 *)outbuffer; + u32 *src = (u32 *)mixbuffer; + int done = 0; + + // Always clear output buffer + memset(outbuffer, 0, len); + + while ( ( mixtail != mixhead ) && ( done < len ) ) + { + *dst++ = src[mixtail++]; + if (mixtail == 4000) mixtail = 0; + done += 4; + } + + // Realign to 32 bytes for DMA + mixtail -= ((done&0x1f) >> 2); + if (mixtail < 0) + mixtail += 4000; + done &= ~0x1f; + if (!done) + return len >> 1; + + return done; +} + +/**************************************************************************** + * AudioSwitchBuffers + * + * Manages which buffer is played next + ***************************************************************************/ +static void AudioSwitchBuffers() +{ + if ( !ConfigRequested ) + { + int len = MixerCollect( soundbuffer[whichab], 3840 ); + DCFlushRange(soundbuffer[whichab], len); + AUDIO_InitDMA((u32)soundbuffer[whichab], len); + AUDIO_StartDMA(); + whichab ^= 1; + IsPlaying = 1; + } + else IsPlaying = 0; +} + +/**************************************************************************** + * InitialiseAudio + * + * Initializes sound system on first load of emulator + ***************************************************************************/ +void InitialiseAudio() +{ + AUDIO_Init(NULL); // Start audio subsystem + AUDIO_SetDSPSampleRate(AI_SAMPLERATE_48KHZ); + AUDIO_RegisterDMACallback( AudioSwitchBuffers ); + memset(soundbuffer, 0, 3840*2); + memset(mixbuffer, 0, 16000); +} + +/**************************************************************************** + * StopAudio + * + * Pause audio output when returning to menu + ***************************************************************************/ +void StopAudio() +{ + AUDIO_StopDMA(); + IsPlaying = 0; +} + +/**************************************************************************** + * ResetAudio + * + * Reset audio output when loading a new game + ***************************************************************************/ +void ResetAudio() +{ + memset(soundbuffer, 0, 3840*2); + memset(mixbuffer, 0, 16000); + mixhead = mixtail = 0; +} + +/**************************************************************************** + * PlaySound + * + * Puts incoming mono samples into mixbuffer + * Splits mono samples into two channels (stereo) + ****************************************************************************/ +void PlaySound( uint16_t *Buffer, int count ) +{ + int i; + u16 sample; + u32 *dst = (u32 *)mixbuffer; + + for( i = 0; i < count; i++ ) + { + sample = Buffer[i]; + dst[mixhead++] = sample | ( sample << 16); + if (mixhead == 4000) + mixhead = 0; + } + + // Restart Sound Processing if stopped + if (IsPlaying == 0) + { + AudioSwitchBuffers (); + } +} diff --git a/Src/gcaudio.h b/Src/gcaudio.h new file mode 100644 index 0000000..cfd7741 --- /dev/null +++ b/Src/gcaudio.h @@ -0,0 +1,27 @@ +/**************************************************************************** + * From the FCE Ultra 0.98.12 + * Nintendo Wii/Gamecube Port + * + * Tantric September 2008 + * eke-eke October 2008 + * Simon Kagstrom Jan 2009 + * + * gcaudio.h + * + * Audio driver + ****************************************************************************/ + +#include + +#if defined(__cplusplus) +extern "C" { +#endif + +void InitialiseAudio(); +void StopAudio(); +void ResetAudio(); +void PlaySound( uint16_t *Buffer, int samples ); + +#if defined(__cplusplus) +}; +#endif diff --git a/Src/main_wii.i b/Src/main_wii.i index 9958e3e..e5a5689 100644 --- a/Src/main_wii.i +++ b/Src/main_wii.i @@ -34,7 +34,7 @@ extern "C" int main(int argc, char **argv) fatInitDefault(); // Init SDL - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { + if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "Couldn't initialize SDL (%s)\n", SDL_GetError()); return 0; }