Permalink
Browse files

applied changes/fixes for use with ASNDLib. (c) Hermes

  • Loading branch information...
shagkur
shagkur committed Dec 9, 2008
1 parent dfbd0a0 commit 300427d0d0fcc1d4f653f3c8f453789dcd78ed6d
Showing with 143 additions and 62 deletions.
  1. +2 −2 gc/modplay/mixer.h
  2. +2 −1 gc/modplay/modplay.h
  3. +81 −7 libmodplay/gcmodplay.c
  4. +16 −13 libmodplay/mixer.c
  5. +42 −39 libmodplay/modplay.c
View
@@ -32,7 +32,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "defines.h"
#include "modplay.h"
-int mix_mono_16bit ( MOD * mod, u16 * buf, int numSamples );
-int mix_stereo_16bit ( MOD * mod, u16 * buf, int numSamples );
+int mix_mono_16bit ( MOD * mod, s16 * buf, int numSamples );
+int mix_stereo_16bit ( MOD * mod, s16 * buf, int numSamples );
#endif
View
@@ -119,7 +119,8 @@ typedef struct _mod
u8 sfxvolume;
BOOL set;
-
+ BOOL *notify;
+
} MOD;
s32 MOD_SetMOD ( MOD *, u8 * );
View
@@ -2,6 +2,7 @@
#include <stdio.h>
#include <string.h>
#include <gccore.h>
+#include <asndlib.h>
#include "gcmodplay.h"
//#define _GCMOD_DEBUG
@@ -13,6 +14,9 @@ static BOOL thr_running = FALSE;
static BOOL sndPlaying = FALSE;
static MODSNDBUF sndBuffer;
+static s32 have_samples = 0;
+static s32 mod_freq = 48000;
+
static u32 shiftVal = 0;
static vu32 curr_audio = 0;
static u32 curr_datalen[2] = {0,0};
@@ -44,7 +48,8 @@ static void* player(void *arg)
printf("player(run callback)\n\n");
start = gettime();
#endif
- sndBuffer.callback(sndBuffer.usr_data,(u8*)audioBuf[curr_audio],curr_datalen[curr_audio]);
+ sndBuffer.callback(sndBuffer.usr_data,((u8*)audioBuf[curr_audio]),curr_datalen[curr_audio]);
+ have_samples = 2;
#ifdef _GCMOD_DEBUG
end = gettime();
printf("player(end callback,%d - %d us)\n\n",curr_audio,diff_usec(start,end));
@@ -60,22 +65,43 @@ static void* player(void *arg)
static void dmaCallback()
{
+#ifndef __SNDLIB_H__
MODPlay *mp = (MODPlay*)sndBuffer.usr_data;
MOD *mod = &mp->mod;
+#endif
#ifdef _GCMOD_DEBUG
static long long start = 0,end = 0;
end = gettime();
if(start) printf("dmaCallback(%p,%d,%d - after %d ms)\n",(void*)audioBuf[curr_audio],curr_datalen,curr_audio,diff_msec(start,end));
#endif
+
+#ifndef __SNDLIB_H__
AUDIO_StopDMA();
AUDIO_InitDMA((u32)audioBuf[curr_audio],curr_datalen[curr_audio]);
AUDIO_StartDMA();
curr_audio ^= 1;
curr_datalen[curr_audio] = (mod->samplespertick<<shiftVal);
LWP_ThreadSignal(player_queue);
+#else
+ if(have_samples==0) {
+ have_samples = 1;
+ LWP_ThreadSignal(player_queue);
+ return;
+ }
+ if(have_samples==1) return;
+ if(have_samples==2) {
+ if(SND_AddVoice(0,audioBuf[curr_audio], curr_datalen[curr_audio])!=0) return; // Sorry I am busy: try again
+
+ curr_datalen[curr_audio]=0;
+ have_samples=0;
+ curr_audio ^= 1;
+ curr_datalen[curr_audio]=SNDBUFFERSIZE;
+ }
+#endif
+
#ifdef _GCMOD_DEBUG
start = gettime();
printf("dmaCallback(%p,%d,%d,%d us) leave\n",(void*)audioBuf[curr_audio],curr_datalen,curr_audio,diff_usec(end,start));
@@ -90,6 +116,11 @@ static void mixCallback(void *usrdata,u8 *stream,u32 len)
#ifdef _GCMOD_DEBUG
printf("mixCallback(%p,%p,%d) enter\n",stream,usrdata,len);
#endif
+ if(mp->manual_polling)
+ mod->notify = &mp->paused;
+ else
+ mod->notify = NULL;
+
mod->mixingbuf = stream;
mod->mixingbuflen = len;
@@ -132,10 +163,18 @@ static s32 SndBufStart(MODSNDBUF *sndbuf)
curr_datalen[0] = SNDBUFFERSIZE;
curr_datalen[1] = SNDBUFFERSIZE;
if(LWP_CreateThread(&hplayer,player,NULL,player_stack,STACKSIZE,80)!=-1) {
+#ifndef __SNDLIB_H__
AUDIO_RegisterDMACallback(dmaCallback);
AUDIO_InitDMA((u32)audioBuf[curr_audio],curr_datalen[curr_audio]);
AUDIO_StartDMA();
curr_audio ^= 1;
+#else
+ SND_SetVoice(0, VOICE_STEREO_16BIT, mod_freq,0, audioBuf[curr_audio], curr_datalen[curr_audio], 255, 255, dmaCallback);
+ have_samples=0;
+
+ curr_audio ^= 1;
+ SND_Pause(0);
+#endif
return 1;
}
sndPlaying = FALSE;
@@ -146,10 +185,12 @@ static s32 SndBufStart(MODSNDBUF *sndbuf)
static void SndBufStop()
{
if(!sndPlaying) return;
-
+#ifndef __SNDLIB_H__
AUDIO_StopDMA();
AUDIO_RegisterDMACallback(NULL);
-
+#else
+ SND_StopVoice(0);
+#endif
curr_audio = 0;
sndPlaying = FALSE;
curr_datalen[0] = 0;
@@ -197,7 +238,12 @@ void MODPlay_Init(MODPlay *mod)
{
memset(mod,0,sizeof(MODPlay));
- AUDIO_Init(NULL);
+#ifndef __SNDLIB_H__
+ AUDIO_Init(NULL);
+#else
+ SND_Pause(0);
+ SND_StopVoice(0);
+#endif
MODPlay_SetFrequency(mod,48000);
MODPlay_SetStereo(mod,TRUE);
@@ -215,13 +261,15 @@ void MODPlay_Init(MODPlay *mod)
s32 MODPlay_SetFrequency(MODPlay *mod,u32 freq)
{
if(freq==mod->playfreq) return 0;
-
+#ifndef __SNDLIB_H__
if(freq==32000 || freq==48000) {
if(freq==32000)
AUDIO_SetDSPSampleRate(AI_SAMPLERATE_32KHZ);
else
AUDIO_SetDSPSampleRate(AI_SAMPLERATE_48KHZ);
-
+#else
+ mod_freq = 48000;
+#endif
mod->playfreq = freq;
updateWaveFormat(mod);
return 0;
@@ -289,7 +337,7 @@ s32 MODPlay_AllocSFXChannels(MODPlay *mod,u32 sfxchans)
s32 MODPlay_Pause(MODPlay *mod,BOOL pause)
{
if(!mod->playing) return -1;
- mod->paused = TRUE;
+ mod->paused = pause;
return 0;
}
@@ -298,3 +346,29 @@ s32 MODPlay_TriggerNote(MODPlay *mod,u32 chan,u8 inst,u16 freq,u8 vol)
if(mod->mod.modraw==0) return -1;
return MOD_TriggerNote(&mod->mod,chan,inst,freq,vol);
}
+
+// add by Hermes
+
+/* void MODPlay_SetVolume(MODPlay *mod, s32 musicvolume, s32 sfxvolume)
+
+Set the volume levels for the MOD music (call it after MODPlay_SetMOD())
+
+mod: the MODPlay pointer
+
+musicvolume: in range 0 to 64
+sfxvolume: in range 0 to 64
+
+*/
+
+void MODPlay_SetVolume(MODPlay *mod, s32 musicvolume, s32 sfxvolume)
+{
+ if(musicvolume<0) musicvolume=0;
+ if(musicvolume>64) musicvolume=64;
+
+ if(sfxvolume<0) sfxvolume=0;
+ if(sfxvolume>64) sfxvolume=64;
+
+ mod->mod.musicvolume= musicvolume;
+ mod->mod.sfxvolume = sfxvolume;
+}
+
View
@@ -37,17 +37,18 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define PREFILL_WORD 32768
#elif defined(GP32)
#define PREFILL_WORD 32768
-#elif defined(GAMECUBE)
+#elif defined(GEKKO)
#define PREFILL_WORD 0
#else
#define PREFILL_WORD 32768
#endif
#define MIX_SAMPLES \
- b[i] += (s16) ((((u32)data[playpos.aword.high]*volume)>>6) << shiftval); \
+ accum = (s32)b[i] + (s32)((((s32)data[playpos.aword.high]*volume)>>6) << shiftval); \
+ if(accum<-32768) accum = -32768; if(accum>32767) accum = 32767; \
+ b[i] = accum; \
playpos.adword+=incval; \
- \
if ( playpos.adword>=loop_end ) \
{ \
if (mod->instrument[mod->instnum[voice]].looped) \
@@ -60,10 +61,11 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
} \
}
-s32 mix_mono_16bit ( MOD * mod, u16 * buf, s32 numSamples )
+s32 mix_mono_16bit ( MOD * mod, s16 * buf, s32 numSamples )
{
+ s32 accum;
s32 voice, i, j;
- u16 * b = buf;
+ s16 * b = buf;
s32 shiftval = mod->shiftval;
s32 numIterations;
s32 numIterationsRest;
@@ -107,13 +109,13 @@ s32 mix_mono_16bit ( MOD * mod, u16 * buf, s32 numSamples )
s8 * data = mod->instrument[mod->instnum[voice]].data;
union_dword playpos;
u32 loop_end = mod->instrument[mod->instnum[voice]].loop_end<<16;
- u32 volume = mod->volume[voice];
+ s32 volume = mod->volume[voice];
playpos.adword = mod->playpos[voice];
if ( voice<mod->num_voices )
- volume = (volume*(u32)mod->musicvolume)>>6;
+ volume = (volume*(s32)mod->musicvolume)>>6;
else
- volume = (volume*(u32)mod->sfxvolume)>>6;
+ volume = (volume*(s32)mod->sfxvolume)>>6;
if (mod->freq==32000 || mod->freq==48000)
incval >>= 2;
@@ -147,10 +149,11 @@ s32 mix_mono_16bit ( MOD * mod, u16 * buf, s32 numSamples )
return numSamples;
}
-s32 mix_stereo_16bit ( MOD * mod, u16 * buf, s32 numSamples )
+s32 mix_stereo_16bit ( MOD * mod, s16 * buf, s32 numSamples )
{
+ s32 accum;
s32 voice, i, j;
- u16 * b = buf;
+ s16 * b = buf;
s32 shiftval = mod->shiftval+1;
s32 numIterations, numIterationsRest;
@@ -183,12 +186,12 @@ s32 mix_stereo_16bit ( MOD * mod, u16 * buf, s32 numSamples )
s8 * data = mod->instrument[mod->instnum[voice]].data;
union_dword playpos;
u32 loop_end = mod->instrument[mod->instnum[voice]].loop_end<<16;
- u32 volume = mod->volume[voice];
+ s32 volume = mod->volume[voice];
if ( voice<mod->num_voices )
- volume = (volume*(u32)mod->musicvolume)>>6;
+ volume = (volume*(s32)mod->musicvolume)>>6;
else
- volume = (volume*(u32)mod->sfxvolume)>>6;
+ volume = (volume*(s32)mod->sfxvolume)>>6;
playpos.adword = mod->playpos[voice];
if (mod->freq==32000 || mod->freq==48000)
Oops, something went wrong.

0 comments on commit 300427d

Please sign in to comment.