mirror of
https://github.com/extremscorner/not64.git
synced 2024-05-15 18:44:56 -04:00
Added tehpola's latest changes to gc_audio/main.c with some commented dumping functions, and also re-added the 50% volume decrease into the RSP.. it actually does sound better on some games
This commit is contained in:
parent
daa41a1163
commit
09db7fe648
120
gc_audio/audio.c
120
gc_audio/audio.c
|
@ -3,7 +3,7 @@
|
|||
----------------------------------------------------
|
||||
MEMORY USAGE:
|
||||
STATIC:
|
||||
Audio Buffer: 2x BUFFER_SIZE (currently 4kb each)
|
||||
Audio Buffer: 4x BUFFER_SIZE (currently ~3kb each)
|
||||
*/
|
||||
|
||||
#include "../main/winlnxdefs.h"
|
||||
|
@ -17,6 +17,13 @@
|
|||
#include <ogc/semaphore.h>
|
||||
#endif
|
||||
|
||||
//#define DUMP_AUDIO 1
|
||||
#ifdef DUMP_AUDIO
|
||||
#include "../main/rom.h"
|
||||
#include <fat.h>
|
||||
FILE* dump_audio_fp;
|
||||
#endif
|
||||
|
||||
#include "AudioPlugin.h"
|
||||
#include "Audio_#1.1.h"
|
||||
#include "../gui/DEBUG.h"
|
||||
|
@ -24,11 +31,17 @@
|
|||
AUDIO_INFO AudioInfo;
|
||||
|
||||
#define NUM_BUFFERS 4
|
||||
#define BUFFER_SIZE 2*1024
|
||||
#define BUFFER_SIZE 3840/2
|
||||
static char buffer[NUM_BUFFERS][BUFFER_SIZE] __attribute__((aligned(32)));
|
||||
static int which_buffer = 0;
|
||||
static unsigned int buffer_offset = 0;
|
||||
#define NEXT(x) (x=(x+1)%NUM_BUFFERS)
|
||||
static unsigned int freq;
|
||||
static unsigned int real_freq;
|
||||
static float freq_ratio;
|
||||
// NOTE: 32khz actually uses ~2136 bytes/frame @ 60hz
|
||||
static enum { BUFFER_SIZE_32_60 = 2176/2, BUFFER_SIZE_48_60 = 3200/2,
|
||||
BUFFER_SIZE_32_50 = 2560/2, BUFFER_SIZE_48_50 = 3840/2 } buffer_size;
|
||||
|
||||
#ifdef THREADED_AUDIO
|
||||
static lwp_t audio_thread;
|
||||
|
@ -51,27 +64,48 @@ EXPORT void CALL
|
|||
AiDacrateChanged( int SystemType )
|
||||
{
|
||||
// Taken from mupen_audio
|
||||
int f = 32000; //default to 32khz incase we get a bad systemtype
|
||||
freq = 32000; //default to 32khz incase we get a bad systemtype
|
||||
switch (SystemType){
|
||||
case SYSTEM_NTSC:
|
||||
f = 48681812 / (*AudioInfo.AI_DACRATE_REG + 1);
|
||||
freq = 48681812 / (*AudioInfo.AI_DACRATE_REG + 1);
|
||||
break;
|
||||
case SYSTEM_PAL:
|
||||
f = 49656530 / (*AudioInfo.AI_DACRATE_REG + 1);
|
||||
freq = 49656530 / (*AudioInfo.AI_DACRATE_REG + 1);
|
||||
break;
|
||||
case SYSTEM_MPAL:
|
||||
f = 48628316 / (*AudioInfo.AI_DACRATE_REG + 1);
|
||||
freq = 48628316 / (*AudioInfo.AI_DACRATE_REG + 1);
|
||||
break;
|
||||
}
|
||||
|
||||
if ( f == 32000 )
|
||||
// Calculate the absolute differences from 32 and 48khz
|
||||
int diff32 = freq - 32000;
|
||||
int diff48 = freq - 48000;
|
||||
diff32 = diff32 > 0 ? diff32 : -diff32;
|
||||
diff48 = diff48 > 0 ? diff48 : -diff48;
|
||||
// Choose the closest real frequency
|
||||
real_freq = (diff32 < diff48) ? 32000 : 48000;
|
||||
freq_ratio = (float)freq / real_freq;
|
||||
|
||||
if( real_freq == 32000 ){
|
||||
AUDIO_SetDSPSampleRate(AI_SAMPLERATE_32KHZ);
|
||||
else if ( f == 48000 )
|
||||
buffer_size = (SystemType != SYSTEM_PAL) ?
|
||||
BUFFER_SIZE_32_60 : BUFFER_SIZE_32_50;
|
||||
} else if( real_freq == 48000 ){
|
||||
AUDIO_SetDSPSampleRate(AI_SAMPLERATE_48KHZ);
|
||||
else {
|
||||
sprintf(txtbuffer,"error initializing frequency: %d", f);
|
||||
DEBUG_print(txtbuffer,DBG_AUDIOINFO);
|
||||
buffer_size = (SystemType != SYSTEM_PAL) ?
|
||||
BUFFER_SIZE_48_60 : BUFFER_SIZE_48_50;
|
||||
}
|
||||
|
||||
sprintf(txtbuffer, "Initializing frequency: %d (resampling ratio %f)",
|
||||
real_freq, freq_ratio);
|
||||
DEBUG_print(txtbuffer,DBG_AUDIOINFO);
|
||||
#ifdef DUMP_AUDIO
|
||||
char tmpname[256];
|
||||
memset(tmpname,0,256);
|
||||
sprintf(tmpname,"/AUDIO/%d - %s.pcm",real_freq,ROM_SETTINGS.goodname);
|
||||
if(!dump_audio_fp)
|
||||
dump_audio_fp = fopen(tmpname,"wb");
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef THREADED_AUDIO
|
||||
|
@ -92,42 +126,74 @@ static void inline play_buffer(void){
|
|||
// This thread will keep giving buffers to the audio as they come
|
||||
while(thread_running){
|
||||
|
||||
// Wait for a buffer to be processed and the audio to be ready
|
||||
// Wait for a buffer to be processed
|
||||
LWP_SemWait(buffer_full);
|
||||
#endif
|
||||
|
||||
// Make sure the buffer is in RAM, not the cache
|
||||
DCFlushRange(buffer[thread_buffer], buffer_size);
|
||||
|
||||
#ifdef THREADED_AUDIO
|
||||
// Wait for the audio interface to be free before playing
|
||||
LWP_SemWait(audio_free);
|
||||
#endif
|
||||
|
||||
DCFlushRange (buffer[thread_buffer], BUFFER_SIZE);
|
||||
AUDIO_InitDMA((unsigned int)&buffer[thread_buffer], BUFFER_SIZE);
|
||||
// Actually send the buffer out to be played
|
||||
AUDIO_InitDMA((unsigned int)&buffer[thread_buffer], buffer_size);
|
||||
AUDIO_StartDMA();
|
||||
|
||||
#ifdef THREADED_AUDIO
|
||||
// Move the index to the next buffer
|
||||
NEXT(thread_buffer);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void inline copy_to_buffer(char* buffer, char* stream, unsigned int length){
|
||||
memcpy(buffer, stream, length);
|
||||
static void inline copy_to_buffer(int* buffer, int* stream, unsigned int length){
|
||||
// NOTE: length is in samples (stereo (2) shorts)
|
||||
int di;
|
||||
float si;
|
||||
for(di = 0, si = 0.0f; di < length; ++di, si += freq_ratio){
|
||||
#if 1
|
||||
// Linear interpolation between current and next sample
|
||||
float t = si - floorf(si);
|
||||
short* osample = (short*)(buffer + di);
|
||||
short* isample1 = (short*)(stream + (int)si);
|
||||
short* isample2 = (short*)(stream + (int)ceilf(si));
|
||||
// Left and right
|
||||
osample[0] = (1.0f - t)*isample1[0] + t*isample2[0];
|
||||
osample[1] = (1.0f - t)*isample1[1] + t*isample2[1];
|
||||
#else
|
||||
// Quick and dirty resampling: skip over or repeat samples
|
||||
buffer[di] = stream[(int)si];
|
||||
#endif
|
||||
}
|
||||
#ifdef DUMP_AUDIO
|
||||
fwrite(buffer, 1, length*4, dump_audio_fp);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void inline add_to_buffer(void* stream, unsigned int length){
|
||||
// This shouldn't lose any data and works for any size
|
||||
unsigned int lengthi;
|
||||
unsigned int lengthLeft = length;
|
||||
unsigned int stream_offset = 0;
|
||||
// Length calculations are in samples (stereo (short) sample pairs)
|
||||
unsigned int lengthi, rlengthi;
|
||||
unsigned int lengthLeft = length >> 2;
|
||||
unsigned int rlengthLeft = ceilf(lengthLeft / freq_ratio);
|
||||
while(1){
|
||||
lengthi = (buffer_offset + lengthLeft < BUFFER_SIZE) ?
|
||||
lengthLeft : (BUFFER_SIZE - buffer_offset);
|
||||
rlengthi = (buffer_offset + (rlengthLeft << 2) < buffer_size) ?
|
||||
rlengthLeft : ((buffer_size - buffer_offset) >> 2);
|
||||
lengthi = rlengthi * freq_ratio;
|
||||
|
||||
#ifdef THREADED_AUDIO
|
||||
// Wait for a buffer we can copy into
|
||||
LWP_SemWait(buffer_empty);
|
||||
#endif
|
||||
copy_to_buffer(buffer[which_buffer] + buffer_offset,
|
||||
stream + stream_offset, lengthi);
|
||||
stream + stream_offset, rlengthi);
|
||||
|
||||
if(buffer_offset + lengthLeft < BUFFER_SIZE){
|
||||
buffer_offset += lengthi;
|
||||
if(buffer_offset + (rlengthLeft << 2) < buffer_size){
|
||||
buffer_offset += rlengthi << 2;
|
||||
#ifdef THREADED_AUDIO
|
||||
// This is a little weird, but we didn't fill this buffer.
|
||||
// So it is still considered 'empty', but since we 'decremented'
|
||||
|
@ -139,7 +205,8 @@ static void inline add_to_buffer(void* stream, unsigned int length){
|
|||
}
|
||||
|
||||
lengthLeft -= lengthi;
|
||||
stream_offset += lengthi;
|
||||
stream_offset += lengthi << 2;
|
||||
rlengthLeft -= rlengthi;
|
||||
|
||||
#ifdef THREADED_AUDIO
|
||||
// Let the audio thread know that we've filled a new buffer
|
||||
|
@ -245,6 +312,11 @@ RomClosed( void )
|
|||
audio_paused = 0;
|
||||
#endif
|
||||
AUDIO_StopDMA(); // So we don't have a buzzing sound when we exit the game
|
||||
#ifdef DUMP_AUDIO
|
||||
if(dump_audio_fp)
|
||||
fclose(dump_audio_fp);
|
||||
dump_audio_fp = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
EXPORT void CALL
|
||||
|
|
|
@ -135,13 +135,10 @@ static void adpcm_block(short *out,int src)
|
|||
mul2>>=2;
|
||||
downshift-=2;
|
||||
|
||||
/* if(1)
|
||||
{
|
||||
// decrease volume to 50%
|
||||
mul2*=2;
|
||||
mul1*=2;
|
||||
downshift+=1;
|
||||
}*/
|
||||
// decrease volume to 50%
|
||||
mul2*=2;
|
||||
mul1*=2;
|
||||
downshift+=1;
|
||||
|
||||
// init tmp buffer with last block
|
||||
last2=last[14];
|
||||
|
|
|
@ -112,6 +112,11 @@ static void adpcm_block(short *out,int src)
|
|||
mul2>>=2;
|
||||
downshift-=2;
|
||||
|
||||
// decrease volume to 50%
|
||||
mul2*=2;
|
||||
mul1*=2;
|
||||
downshift+=1;
|
||||
|
||||
// init tmp buffer with last block
|
||||
last2=last[14];
|
||||
last1=last[15];
|
||||
|
|
|
@ -125,6 +125,10 @@ static void adpcm_block(short *out,int src, int bits)
|
|||
mul2>>=2;
|
||||
downshift-=2;
|
||||
|
||||
// decrease volume to 50%
|
||||
mul2*=2;
|
||||
mul1*=2;
|
||||
downshift+=1;
|
||||
|
||||
// init tmp buffer with last block
|
||||
last2=last[14];
|
||||
|
|
Loading…
Reference in a new issue