X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=pico%2Fsound%2Fsound.c;h=a67ebcca09288d0c54f3964e305061a35dd2b7f5;hb=e42a47e2086e6512519dd86af420363498302f49;hp=69c1be0397b7ea2527f44d0f92487cbb30469016;hpb=274fcc35aa20e9777a8e09630a94088757384329;p=picodrive.git diff --git a/pico/sound/sound.c b/pico/sound/sound.c index 69c1be0..a67ebcc 100644 --- a/pico/sound/sound.c +++ b/pico/sound/sound.c @@ -14,15 +14,13 @@ #include "../cd/cue.h" #include "mix.h" -#define SIMPLE_WRITE_SOUND 0 - void (*PsndMix_32_to_16l)(short *dest, int *src, int count) = mix_32_to_16l_stereo; // master int buffer to mix to static int PsndBuffer[2*(44100+100)/50]; -// dac -static unsigned short dac_info[312+4]; // pppppppp ppppllll, p - pos in buff, l - length to write for this sample +// dac, psg +static unsigned short dac_info[312+4]; // pos in sample buffer // cdda output buffer short cdda_out_buffer[2*1152]; @@ -32,8 +30,9 @@ 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; -int PsndDacLine=0; +int PsndDacLine, PsndPsgLine; short *PsndOut=NULL; // PCM data buffer +static int PsndLen_use; // timers int timer_a_next_oflow, timer_a_step; // in z80 cycles @@ -45,7 +44,7 @@ extern int *sn76496_regs; static void dac_recalculate(void) { - int i, dac_cnt, pos, len, lines = Pico.m.pal ? 312 : 262, mid = Pico.m.pal ? 68 : 93; + int i, dac_cnt, pos, len, lines = Pico.m.pal ? 313 : 262, mid = Pico.m.pal ? 68 : 93; if (PsndLen <= lines) { @@ -57,14 +56,12 @@ static void dac_recalculate(void) for(i=226; i != 225; i++) { if (i >= lines) i = 0; - len = 0; if(dac_cnt < 0) { - len=1; pos++; dac_cnt += lines; } dac_cnt -= PsndLen; - dac_info[i] = (pos<<4)|len; + dac_info[i] = pos; } } else @@ -86,24 +83,12 @@ static void dac_recalculate(void) len++; } dac_cnt += PsndLen; - dac_info[i] = (pos<<4)|len; - pos+=len; + pos += len; + dac_info[i] = pos; } - // last sample - for(len = 0, i = pos; i < PsndLen; i++) len++; - if (PsndLen_exc_add) len++; - dac_info[224] = (pos<<4)|len; } - mid = (dac_info[lines-1] & 0xfff0) + ((dac_info[lines-1] & 0xf) << 4); for (i = lines; i < sizeof(dac_info) / sizeof(dac_info[0]); i++) - dac_info[i] = mid; - //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); + dac_info[i] = dac_info[0]; } @@ -163,29 +148,77 @@ void PsndRerate(int preserve_state) } +PICO_INTERNAL void PsndStartFrame(void) +{ + // compensate for float part of PsndLen + PsndLen_use = PsndLen; + PsndLen_exc_cnt += PsndLen_exc_add; + if (PsndLen_exc_cnt >= 0x10000) { + PsndLen_exc_cnt -= 0x10000; + PsndLen_use++; + } + + PsndDacLine = PsndPsgLine = 0; + emustatus &= ~1; + dac_info[224] = PsndLen_use; +} + PICO_INTERNAL void PsndDoDAC(int line_to) { int pos, pos1, len; int dout = ym2612.dacout; int line_from = PsndDacLine; - if (line_to >= 312) - line_to = 311; + if (line_to >= 313) + line_to = 312; + + pos = dac_info[line_from]; + pos1 = dac_info[line_to + 1]; + len = pos1 - pos; + if (len <= 0) + return; PsndDacLine = line_to + 1; - pos =dac_info[line_from]>>4; - pos1=dac_info[line_to]; - len = ((pos1>>4)-pos) + (pos1&0xf); - if (!len) return; + if (!PsndOut) + return; if (PicoOpt & POPT_EN_STEREO) { short *d = PsndOut + pos*2; - for (; len > 0; len--, d+=2) *d = dout; + for (; len > 0; len--, d+=2) *d += dout; } else { short *d = PsndOut + pos; - for (; len > 0; len--, d++) *d = dout; + for (; len > 0; len--, d++) *d += dout; + } +} + +PICO_INTERNAL void PsndDoPSG(int line_to) +{ + int line_from = PsndPsgLine; + int pos, pos1, len; + int stereo = 0; + + if (line_to >= 313) + line_to = 312; + + pos = dac_info[line_from]; + pos1 = dac_info[line_to + 1]; + len = pos1 - pos; + //elprintf(EL_STATUS, "%3d %3d %3d %3d %3d", + // pos, pos1, len, line_from, line_to); + if (len <= 0) + return; + + PsndPsgLine = line_to + 1; + + if (!PsndOut || !(PicoOpt & POPT_EN_PSG)) + return; + + if (PicoOpt & POPT_EN_STEREO) { + stereo = 1; + pos <<= 1; } + SN76496Update(PsndOut + pos, len, stereo); } // cdda @@ -260,21 +293,6 @@ static int PsndRender(int offset, int length) pprof_start(sound); -#if !SIMPLE_WRITE_SOUND - 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++; - } - } -#endif - - // PSG - if (PicoOpt & POPT_EN_PSG) - SN76496Update(PsndOut+offset, length, stereo); - if (PicoAHW & PAHW_PICO) { PicoPicoPCMUpdate(PsndOut+offset, length, stereo); return length; @@ -322,20 +340,17 @@ static int PsndRender(int offset, int length) // to be called on 224 or line_sample scanlines only PICO_INTERNAL void PsndGetSamples(int y) { -#if SIMPLE_WRITE_SOUND - if (y != 224) return; - PsndRender(0, PsndLen); - if (PicoWriteSound) - PicoWriteSound(PsndLen * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2)); - PsndClear(); -#else static int curr_pos = 0; + if (ym2612.dacen && PsndDacLine < y) + PsndDoDAC(y - 1); + PsndDoPSG(y - 1); + if (y == 224) { if (emustatus & 2) curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2); - else curr_pos = PsndRender(0, PsndLen); + else curr_pos = PsndRender(0, PsndLen_use); if (emustatus & 1) emustatus |= 2; else emustatus &= ~2; @@ -343,28 +358,20 @@ PICO_INTERNAL void PsndGetSamples(int y) PicoWriteSound(curr_pos * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2)); // clear sound buffer PsndClear(); + PsndDacLine = 224; + dac_info[224] = 0; } else if (emustatus & 3) { emustatus|= 2; emustatus&=~1; curr_pos = PsndRender(0, PsndLen/2); } -#endif } PICO_INTERNAL void PsndGetSamplesMS(void) { int stereo = (PicoOpt & 8) >> 3; - int length = PsndLen; - -#if !SIMPLE_WRITE_SOUND - // compensate for float part of PsndLen - PsndLen_exc_cnt += PsndLen_exc_add; - if (PsndLen_exc_cnt >= 0x10000) { - PsndLen_exc_cnt -= 0x10000; - length++; - } -#endif + int length = PsndLen_use; // PSG if (PicoOpt & POPT_EN_PSG) @@ -382,3 +389,4 @@ PICO_INTERNAL void PsndGetSamplesMS(void) PsndClear(); } +// vim:shiftwidth=2:ts=2:expandtab