From 3ef975c9e4839978545919a2684ba97d1a2a881a Mon Sep 17 00:00:00 2001 From: notaz Date: Tue, 6 Feb 2007 22:25:53 +0000 Subject: [PATCH] sound code rewrite for lower sample rates (except mp3s) git-svn-id: file:///home/notaz/opt/svn/PicoDrive/platform@30 be3aeb3a-fb24-0410-a615-afba39da0efa --- gp2x/940ctl_ym2612.c | 6 ++++++ gp2x/emu.c | 31 +++++++------------------------ 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/gp2x/940ctl_ym2612.c b/gp2x/940ctl_ym2612.c index 1a8160d..29fd8bf 100644 --- a/gp2x/940ctl_ym2612.c +++ b/gp2x/940ctl_ym2612.c @@ -537,6 +537,12 @@ int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty) } writebuff_ptr = 0; + /* predict sample counter for next frame */ + if (PsndLen_exc_add) { + if (PsndLen_exc_cnt + PsndLen_exc_add >= 0x10000) length = PsndLen + 1; + else length = PsndLen; + } + /* give 940 ym job */ shared_ctl->writebuffsel ^= 1; shared_ctl->length = length; diff --git a/gp2x/emu.c b/gp2x/emu.c index bbb78b2..871d05f 100644 --- a/gp2x/emu.c +++ b/gp2x/emu.c @@ -48,6 +48,7 @@ unsigned char *rom_data = NULL; extern int crashed_940; +static short sndBuffer[2*44100/50]; static char noticeMsg[64]; // notice msg to draw static struct timeval noticeMsgTime = { 0, 0 }; // when started showing static int reset_timing, osd_fps_x; @@ -927,24 +928,10 @@ static void updateKeys(void) prevEvents = (allActions[0] | allActions[1]) >> 16; } -static int snd_excess_add = 0, snd_excess_cnt = 0; // hack -static void updateSound(void) +static void updateSound(int len) { - int len = (PicoOpt&8)?PsndLen*2:PsndLen; - - snd_excess_cnt += snd_excess_add; - if (snd_excess_cnt >= 0x10000) { - snd_excess_cnt -= 0x10000; - if (PicoOpt&8) { - PsndOut[len] = PsndOut[len-2]; - PsndOut[len+1] = PsndOut[len-1]; - len+=2; - } else { - PsndOut[len] = PsndOut[len-1]; - len++; - } - } + if (PicoOpt&8) len<<=1; gp2x_sound_write(PsndOut, len<<1); } @@ -1024,22 +1011,23 @@ void emu_Loop(void) // prepare sound stuff if(currentConfig.EmuOpt & 4) { + int snd_excess_add; if(PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old || crashed_940) { /* if 940 is turned off, we need it to be put back to sleep */ if (!(PicoOpt&0x200) && ((PicoOpt^PicoOpt_old)&0x200)) { Reset940(1, 2); Pause940(1); } - sound_rerate(); + sound_rerate(1); } //excess_samples = PsndRate - PsndLen*target_fps; - snd_excess_cnt = 0; snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps; printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n", PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal); gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3); gp2x_sound_volume(currentConfig.volume, currentConfig.volume); PicoWriteSound = updateSound; - PsndOut = calloc((PicoOpt&8) ? (PsndLen*4+4) : (PsndLen*2+2), 1); + memset(sndBuffer, 0, sizeof(sndBuffer)); + PsndOut = sndBuffer; PsndRate_old = PsndRate; PsndLen_real = PsndLen; PicoOpt_old = PicoOpt; @@ -1239,11 +1227,6 @@ if (Pico.m.frame_count == 31563) { emu_SaveLoadGame(0, 1); SRam.changed = 0; } - - if (PsndOut != 0) { - free(PsndOut); - PsndOut = 0; - } } -- 2.39.2