vbagx/source/ngc/audio.cpp

141 lines
3.2 KiB
C++
Raw Normal View History

2008-09-14 22:40:26 +02:00
/****************************************************************************
2008-09-17 04:27:55 +02:00
* Visual Boy Advance GX
*
* Tantric September 2008
*
* audio.cpp
*
* Head and tail audio mixer
***************************************************************************/
2008-09-14 22:40:26 +02:00
#include <gccore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/** Locals **/
static int head = 0;
static int tail = 0;
#define MIXBUFFSIZE 0x10000
static u8 mixerdata[MIXBUFFSIZE];
#define MIXERMASK ((MIXBUFFSIZE >> 2) - 1)
2008-10-19 21:10:55 +02:00
static u8 soundbuffer[2][3840] ATTRIBUTE_ALIGN(32);
extern int ConfigRequested;
static int whichab = 0;
int IsPlaying = 0;
/****************************************************************************
* MIXER_GetSamples
***************************************************************************/
static int MIXER_GetSamples( u8 *dstbuffer, int maxlen )
{
u32 *src = (u32 *)mixerdata;
u32 *dst = (u32 *)dstbuffer;
u32 intlen = maxlen >> 2;
memset(dstbuffer, 0, maxlen);
while( ( head != tail ) && intlen )
{
*dst++ = src[tail++];
tail &= MIXERMASK;
intlen--;
}
return 3200;
}
/****************************************************************************
* MIXER_GetSamples
***************************************************************************/
static void AudioPlayer()
{
if ( !ConfigRequested )
{
int len = MIXER_GetSamples(soundbuffer[whichab], 3200);
DCFlushRange(soundbuffer[whichab],len);
AUDIO_InitDMA((u32)soundbuffer[whichab],len);
AUDIO_StartDMA();
whichab ^= 1;
IsPlaying = 1;
}
else
IsPlaying = 0;
}
2008-09-14 22:40:26 +02:00
/****************************************************************************
* MIXER_AddSamples
*
* Upsample from 11025 to 48000
* 11025 == 15052
* 22050 == 30106
* 44100 == 60211
*
* Audio officianados should look away now !
****************************************************************************/
void MIXER_AddSamples( u8 *sampledata, int len )
{
u32 *src = (u32 *)sampledata;
u32 *dst = (u32 *)mixerdata;
u32 intlen = (3200 >> 2);
u32 fixofs = 0;
u32 fixinc;
2008-09-14 22:40:26 +02:00
if ( !len )
fixinc = 30106;
else
fixinc = 60211;
2008-09-14 22:40:26 +02:00
do
{
// Do simple linear interpolate
dst[head++] = src[fixofs >> 16];
head &= MIXERMASK;
fixofs += fixinc;
}
while( --intlen );
2008-09-14 22:40:26 +02:00
2008-10-19 21:10:55 +02:00
// Restart Sound Processing if stopped
if (IsPlaying == 0)
{
2008-10-19 21:10:55 +02:00
ConfigRequested = 0;
AudioPlayer();
}
2008-09-14 22:40:26 +02:00
}
2008-10-19 21:10:55 +02:00
/****************************************************************************
* MIXER_GetSamples
***************************************************************************/
void InitialiseSound()
{
AUDIO_Init(NULL); // Start audio subsystem
AUDIO_SetDSPSampleRate(AI_SAMPLERATE_48KHZ);
AUDIO_RegisterDMACallback(AudioPlayer);
2008-10-19 21:10:55 +02:00
memset(soundbuffer, 0, 3840*2);
memset(mixerdata, 0, MIXBUFFSIZE);
}
2008-10-19 21:10:55 +02:00
/****************************************************************************
* MIXER_GetSamples
***************************************************************************/
2008-10-18 22:40:48 +02:00
void ResetAudio()
{
2008-10-19 21:10:55 +02:00
memset(soundbuffer, 0, 3840*2);
memset(mixerdata, 0, MIXBUFFSIZE);
2008-10-18 22:40:48 +02:00
}
2008-10-19 21:10:55 +02:00
/****************************************************************************
* MIXER_GetSamples
***************************************************************************/
void StopAudio()
{
AUDIO_StopDMA();
2008-10-19 21:10:55 +02:00
IsPlaying = 0;
}