From 7a93adeb29f01f5867ff36606cd73a382b6ee0e3 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@30 be3aeb3a-fb24-0410-a615-afba39da0efa --- Pico/Pico.c | 19 +++---- Pico/Pico.h | 4 +- Pico/PicoInt.h | 4 ++ Pico/cd/Pico.c | 31 +++-------- Pico/sound/mix.s | 12 ++--- Pico/sound/sound.c | 96 ++++++++++++++++++++--------------- platform/gp2x/940ctl_ym2612.c | 6 +++ platform/gp2x/code940/940.c | 3 +- platform/gp2x/emu.c | 31 +++-------- 9 files changed, 98 insertions(+), 108 deletions(-) diff --git a/Pico/Pico.c b/Pico/Pico.c index 004a247..3cee0c2 100644 --- a/Pico/Pico.c +++ b/Pico/Pico.c @@ -18,7 +18,7 @@ int PicoSkipFrame=0; // skip rendering frame? int PicoRegionOverride = 0; // override the region detection 0: Auto, 1: Japan NTSC, 2: Japan PAL, 4: US, 8: Europe int PicoAutoRgnOrder = 0; int emustatus = 0; -void (*PicoWriteSound)(void) = 0; // called once per frame at the best time to send sound buffer (PsndOut) to hardware +void (*PicoWriteSound)(int len) = 0; // called once per frame at the best time to send sound buffer (PsndOut) to hardware struct PicoSRAM SRam; int z80startCycle = 0, z80stopCycle = 0; // in 68k cycles @@ -315,21 +315,22 @@ static int CheckIdle(void) // to be called on 224 or line_sample scanlines only static __inline void getSamples(int y) { + static int curr_pos = 0; + if(y == 224) { //dprintf("sta%i: %i [%i]", (emustatus & 2), emustatus, y); if(emustatus & 2) - sound_render(PsndLen/2, PsndLen-PsndLen/2); - else sound_render(0, PsndLen); + curr_pos += sound_render(curr_pos, PsndLen-PsndLen/2); + else curr_pos = sound_render(0, PsndLen); if (emustatus&1) emustatus|=2; else emustatus&=~2; - if (PicoWriteSound) PicoWriteSound(); + if (PicoWriteSound) PicoWriteSound(curr_pos); // clear sound buffer sound_clear(); - //memset(PsndOut, 0, (PicoOpt & 8) ? (PsndLen<<2) : (PsndLen<<1)); } else if(emustatus & 3) { emustatus|= 2; emustatus&=~1; - sound_render(0, PsndLen/2); + curr_pos = sound_render(0, PsndLen/2); } } @@ -538,10 +539,10 @@ static int PicoFrameSimple(void) // here we render sound if ym2612 is disabled if(!(PicoOpt&1) && PsndOut) { - sound_render(0, PsndLen); - if(PicoWriteSound) PicoWriteSound(); + int len = sound_render(0, PsndLen); + if(PicoWriteSound) PicoWriteSound(len); // clear sound buffer - memset(PsndOut, 0, (PicoOpt & 8) ? (PsndLen<<2) : (PsndLen<<1)); + sound_clear(); } // render screen diff --git a/Pico/Pico.h b/Pico/Pico.h index ef2db10..7e7633c 100644 --- a/Pico/Pico.h +++ b/Pico/Pico.h @@ -42,7 +42,7 @@ int PicoReset(int hard); int PicoFrame(void); extern int PicoPad[2]; // Joypads, format is MXYZ SACB RLDU extern int (*PicoCram)(int cram); // Callback to convert colour ram 0000bbb0 ggg0rrr0 -extern void (*PicoWriteSound)(void); // called once per frame at the best time to send sound buffer (PsndOut) to hardware +extern void (*PicoWriteSound)(int len); // called once per frame at the best time to send sound buffer (PsndOut) to hardware int PicoFrameMCD(void); @@ -89,7 +89,7 @@ extern void (*PicoPrepareCram)(); // prepares PicoCramHigh for renderer to us extern int PsndRate,PsndLen; extern short *PsndOut; void sound_reset(); -void sound_rerate(); +void sound_rerate(int preserve_state); void z80_pack(unsigned char *data); void z80_unpack(unsigned char *data); void z80_reset(); diff --git a/Pico/PicoInt.h b/Pico/PicoInt.h index c7b72a2..b17277f 100644 --- a/Pico/PicoInt.h +++ b/Pico/PicoInt.h @@ -277,6 +277,10 @@ int SekInitS68k(void); int SekResetS68k(void); int SekInterruptS68k(int irq); +// sound/sound.c +extern int PsndLen_exc_cnt; +extern int PsndLen_exc_add; + // VideoPort.c void PicoVideoWrite(unsigned int a,unsigned short d); unsigned int PicoVideoRead(unsigned int a); diff --git a/Pico/cd/Pico.c b/Pico/cd/Pico.c index d643cb7..dfdbf5e 100644 --- a/Pico/cd/Pico.c +++ b/Pico/cd/Pico.c @@ -131,27 +131,6 @@ static __inline void update_chips(void) gfx_cd_update(); } -// to be called on 224 or line_sample scanlines only -static __inline void getSamples(int y) -{ - if(y == 224) { - //dprintf("sta%i: %i [%i]", (emustatus & 2), emustatus, y); - if(emustatus & 2) - sound_render(PsndLen/2, PsndLen-PsndLen/2); - else sound_render(0, PsndLen); - if (emustatus&1) emustatus|=2; else emustatus&=~2; - if (PicoWriteSound) PicoWriteSound(); - // clear sound buffer - sound_clear(); - //memset(PsndOut, 0, (PicoOpt & 8) ? (PsndLen<<2) : (PsndLen<<1)); - } - else if(emustatus & 3) { - emustatus|= 2; - emustatus&=~1; - sound_render(0, PsndLen/2); - } -} - static int PicoFrameHintsMCD(void) { @@ -240,10 +219,12 @@ static int PicoFrameHintsMCD(void) sound_timers_and_dac(y); // get samples from sound chips - if(y == 32 && PsndOut) - emustatus &= ~1; - else if((y == 224 || y == line_sample) && PsndOut) - getSamples(y); + if (y == 224 && PsndOut) { + int len = sound_render(0, PsndLen); + if (PicoWriteSound) PicoWriteSound(len); + // clear sound buffer + sound_clear(); + } // Run scanline: //dprintf("m68k starting exec @ %06x", SekPc); diff --git a/Pico/sound/mix.s b/Pico/sound/mix.s index ab0853e..d34a273 100644 --- a/Pico/sound/mix.s +++ b/Pico/sound/mix.s @@ -206,10 +206,10 @@ m16_32_s2_no_unal2: @ limit @ reg=int_sample, lr=1, r3=tmp, kills flags .macro Limit reg - add r3, lr, \reg, asr #16 + add r3, lr, \reg, asr #15 bics r3, r3, #1 @ in non-overflow conditions r3 is 0 or 1 movne \reg, #0x8000 - submi \reg, \reg, #1 + subpl \reg, \reg, #1 .endm @@ -223,11 +223,11 @@ m16_32_s2_no_unal2: @ mov r3, #0x8000 @ subpl r3, r3, #1 - add r3, lr, \reg, asr #16 + add r3, lr, \reg, asr #15 bics r3, r3, #1 @ in non-overflow conditions r3 is 0 or 1 moveq \reg, \reg, lsl #16 movne \reg, #0x80000000 - submi \reg, \reg, #0x00010000 + subpl \reg, \reg, #0x00010000 .endm @@ -292,7 +292,7 @@ mix_32_to_16_mono: @ check if dest is word aligned tst r0, #2 beq m32_16_mo_no_unalw - ldrsh r5, [r0], #2 + ldrsh r5, [r0] ldr r4, [r1], #4 sub r2, r2, #1 add r4, r4, r5 @@ -339,7 +339,7 @@ m32_16_mo_end: m32_16_mo_no_unal2: tst r2, #1 ldmeqfd sp!, {r4-r8,pc} - ldrsh r5, [r0], #2 + ldrsh r5, [r0] ldr r4, [r1], #4 add r4, r4, r5 Limit r4 diff --git a/Pico/sound/sound.c b/Pico/sound/sound.c index 6ea966b..18cc46e 100644 --- a/Pico/sound/sound.c +++ b/Pico/sound/sound.c @@ -38,6 +38,8 @@ unsigned short dac_info[312]; // pppppppp ppppllll, p - pos in buff, l - length // for Pico int PsndRate=0; int PsndLen=0; // number of mono samples, multiply by 2 for stereo +int PsndLen_exc_add=0; // this is for non-integer sample counts per line, eg. 22050/60 +int PsndLen_exc_cnt=0; short *PsndOut=NULL; // PCM data buffer // from ym2612.c @@ -55,7 +57,7 @@ static void dac_recalculate() if(PsndLen <= lines) { // shrinking algo - dac_cnt = 0;//lines - PsndLen; + dac_cnt = -PsndLen; len=1; pos=0; dac_info[225] = 1; @@ -72,7 +74,7 @@ static void dac_recalculate() } } else { // stretching - dac_cnt = PsndLen/2; + dac_cnt = PsndLen; pos=0; for(i = 225; i != 224; i++) { if (i >= lines) i = 0; @@ -92,12 +94,16 @@ static void dac_recalculate() } // last sample for(len = 0, i = pos; i < PsndLen; i++) len++; + if (PsndLen_exc_add) len++; dac_info[224] = (pos<<4)|len; } -// dprintf("rate is %i, len %i", PsndRate, PsndLen); -// for(i=0; i < lines; i++) -// dprintf("%03i : %03i : %i", i, dac_info[i]>>4, dac_info[i]&0xf); -//exit(8); + //for(i=len=0; i < lines; i++) { + // printf("%03i : %03i : %i\n", i, dac_info[i]>>4, dac_info[i]&0xf); + // len+=dac_info[i]&0xf; + //} + //printf("rate is %i, len %f\n", PsndRate, (double)PsndRate/(Pico.m.pal ? 50.0 : 60.0)); + //printf("len total: %i, last pos: %i\n", len, pos); + //exit(8); } @@ -106,53 +112,58 @@ void sound_reset() extern int z80stopCycle; void *ym2612_regs; - // init even if we are not going to use them, just in case we ever enable it - YM2612Init(Pico.m.pal ? OSC_PAL/7 : OSC_NTSC/7, PsndRate); // also clear the internal registers+addr line ym2612_regs = YM2612GetRegs(); memset(ym2612_regs, 0, 0x200+4); - - SN76496_init(Pico.m.pal ? OSC_PAL/15 : OSC_NTSC/15, PsndRate); - - // calculate PsndLen - PsndLen=PsndRate/(Pico.m.pal ? 50 : 60); - - // recalculate dac info - dac_recalculate(); z80stopCycle = 0; + + sound_rerate(0); } // to be called after changing sound rate or chips -void sound_rerate() +void sound_rerate(int preserve_state) { unsigned int state[28]; + int target_fps = Pico.m.pal ? 50 : 60; - // not all rates are supported in MCD mode due to mp3 decoder + // not all rates are supported in MCD mode due to mp3 decoder limitations if (PicoMCD & 1) { if (PsndRate != 11025 && PsndRate != 22050 && PsndRate != 44100) PsndRate = 22050; - if (!(PicoOpt & 8)) PicoOpt |= 8; + PicoOpt |= 8; // force stereo } - if ((PicoMCD & 1) && Pico_mcd->m.audio_track) Pico_mcd->m.audio_offset = mp3_get_offset(); + if (preserve_state) { + if ((PicoMCD & 1) && Pico_mcd->m.audio_track) + Pico_mcd->m.audio_offset = mp3_get_offset(); + } YM2612Init(Pico.m.pal ? OSC_PAL/7 : OSC_NTSC/7, PsndRate); - // feed it back it's own registers, just like after loading state - YM2612PicoStateLoad(); - if ((PicoMCD & 1) && Pico_mcd->m.audio_track) - mp3_start_play(Pico_mcd->TOC.Tracks[Pico_mcd->m.audio_track].F, Pico_mcd->m.audio_offset); + if (preserve_state) { + // feed it back it's own registers, just like after loading state + YM2612PicoStateLoad(); + if ((PicoMCD & 1) && Pico_mcd->m.audio_track) + mp3_start_play(Pico_mcd->TOC.Tracks[Pico_mcd->m.audio_track].F, Pico_mcd->m.audio_offset); + } - memcpy(state, sn76496_regs, 28*4); // remember old state + if (preserve_state) memcpy(state, sn76496_regs, 28*4); // remember old state SN76496_init(Pico.m.pal ? OSC_PAL/15 : OSC_NTSC/15, PsndRate); - memcpy(sn76496_regs, state, 28*4); // restore old state + if (preserve_state) memcpy(sn76496_regs, state, 28*4); // restore old state // calculate PsndLen - PsndLen=PsndRate/(Pico.m.pal ? 50 : 60); + PsndLen=PsndRate / target_fps; + PsndLen_exc_add=((PsndRate - PsndLen*target_fps)<<16) / target_fps; + PsndLen_exc_cnt=0; // recalculate dac info dac_recalculate(); if (PicoMCD & 1) pcm_set_rate(PsndRate); + + // clear all buffers + memset32(PsndBuffer, 0, sizeof(PsndBuffer)/4); + if (PsndOut) + sound_clear(); } @@ -181,22 +192,16 @@ void sound_timers_and_dac(int raster) d[0] = dout; if (len > 1) { d[2] = dout; - if (len > 2) { + if (len > 2) d[4] = dout; - if (len > 3) - d[6] = dout; - } } } else { short *d = PsndOut + pos; d[0] = dout; if (len > 1) { d[1] = dout; - if (len > 2) { + if (len > 2) d[2] = dout; - if (len > 3) - d[3] = dout; - } } } } @@ -213,9 +218,10 @@ void sound_timers_and_dac(int raster) void sound_clear(void) { - if (PicoOpt & 8) memset32((int *) PsndOut, 0, PsndLen); - else memset(PsndOut, 0, PsndLen<<1); -// memset(PsndBuffer, 0, (PicoOpt & 8) ? (PsndLen<<3) : (PsndLen<<2)); + int len = PsndLen; + if (PsndLen_exc_add) len++; + if (PicoOpt & 8) memset32((int *) PsndOut, 0, len); // clear both channels at once + else memset(PsndOut, 0, len<<1); } @@ -227,6 +233,15 @@ int sound_render(int offset, int length) int do_pcm = (PicoMCD&1) && (PicoOpt&0x400) && (Pico_mcd->pcm.control & 0x80) && Pico_mcd->pcm.enabled; offset <<= stereo; + if (offset == 0) { // should happen once per frame + // compensate for float part of PsndLen + PsndLen_exc_cnt += PsndLen_exc_add; + if (PsndLen_exc_cnt >= 0x10000) { + PsndLen_exc_cnt -= 0x10000; + length++; + } + } + // PSG if (PicoOpt & 2) SN76496Update(PsndOut+offset, length, stereo); @@ -235,10 +250,11 @@ int sound_render(int offset, int length) if (PicoOpt & 1) YM2612UpdateOne(buf32, length, stereo, 1); + // CD: PCM sound if (do_pcm) pcm_update(buf32, length, stereo); - // CDDA audio + // CD: CDDA audio // if ((PicoMCD & 1) && (PicoOpt & 0x800)) // mp3_update(PsndBuffer+offset, length, stereo); @@ -247,7 +263,7 @@ int sound_render(int offset, int length) mix_32_to_16l_stereo(PsndOut+offset, buf32, length); else mix_32_to_16_mono (PsndOut+offset, buf32, length); - return 0; + return length; } diff --git a/platform/gp2x/940ctl_ym2612.c b/platform/gp2x/940ctl_ym2612.c index 1a8160d..29fd8bf 100644 --- a/platform/gp2x/940ctl_ym2612.c +++ b/platform/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/platform/gp2x/code940/940.c b/platform/gp2x/code940/940.c index 3f7130a..18d282c 100644 --- a/platform/gp2x/code940/940.c +++ b/platform/gp2x/code940/940.c @@ -4,7 +4,6 @@ static _940_data_t *shared_data = (_940_data_t *) 0x00100000; static _940_ctl_t *shared_ctl = (_940_ctl_t *) 0x00200000; static unsigned char *mp3_data = (unsigned char *) 0x01000000; YM2612 *ym2612_940; -int *mix_buffer; // from init.s void wait_irq(void); @@ -59,8 +58,8 @@ static void mp3_decode(void) void Main940(int startvector, int pc_at_irq) { + int mix_buffer = shared_data->mix_buffer; ym2612_940 = &shared_data->ym2612; - mix_buffer = shared_data->mix_buffer; // debug shared_ctl->vstarts[startvector]++; diff --git a/platform/gp2x/emu.c b/platform/gp2x/emu.c index bbb78b2..871d05f 100644 --- a/platform/gp2x/emu.c +++ b/platform/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