X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?p=picodrive.git;a=blobdiff_plain;f=pico%2Fsound%2Fsound.c;h=8f88dd7c5ca2972deb4b3f730d641f51ab724f55;hp=ec0e20590f06f27c68a59060523c379fb0c00e30;hb=4f2cdbf551ad1a7f487b65b4754cbf7983e80b8a;hpb=33be04ca5fee314271f1959672e22cf94d670ea6 diff --git a/pico/sound/sound.c b/pico/sound/sound.c index ec0e205..8f88dd7 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 +static unsigned short dac_info[312+4]; // pos in sample buffer // cdda output buffer short cdda_out_buffer[2*1152]; @@ -34,6 +32,7 @@ int PsndLen_exc_add=0; // this is for non-integer sample counts per line, eg. 22 int PsndLen_exc_cnt=0; int PsndDacLine=0; short *PsndOut=NULL; // PCM data buffer +static int PsndLen_use; // timers int timer_a_next_oflow, timer_a_step; // in z80 cycles @@ -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]; } @@ -121,15 +106,6 @@ void PsndRerate(int preserve_state) void *state = NULL; int target_fps = Pico.m.pal ? 50 : 60; - // not all rates are supported in MCD mode due to mp3 decoder limitations - if (PicoAHW & PAHW_MCD) { - if (!(11025-100 <= PsndRate && PsndRate <= 11025+100) && - !(22050-100 <= PsndRate && PsndRate <= 22050+100) && - !(44100-100 <= PsndRate && PsndRate <= 44100+100)) - PsndRate = 22050; - PicoOpt |= POPT_EN_STEREO; // force stereo - } - if (preserve_state) { state = malloc(0x204); if (state == NULL) return; @@ -141,8 +117,6 @@ void PsndRerate(int preserve_state) // feed it back it's own registers, just like after loading state memcpy(YM2612GetRegs(), state, 0x204); ym2612_unpack_state(); - if ((PicoAHW & PAHW_MCD) && !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1)) - cdda_start_play(); } if (preserve_state) memcpy(state, sn76496_regs, 28*4); // remember old state @@ -174,6 +148,21 @@ 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 = 0; + emustatus &= ~1; + dac_info[224] = PsndLen_use; +} + PICO_INTERNAL void PsndDoDAC(int line_to) { int pos, pos1, len; @@ -183,12 +172,16 @@ PICO_INTERNAL void PsndDoDAC(int line_to) if (line_to >= 312) line_to = 311; + 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; @@ -200,23 +193,19 @@ PICO_INTERNAL void PsndDoDAC(int line_to) } // cdda -static pm_file *cdda_stream = NULL; - static void cdda_raw_update(int *buffer, int length) { int ret, cdda_bytes, mult = 1; - if (cdda_stream == NULL) - return; cdda_bytes = length*4; if (PsndRate <= 22050 + 100) mult = 2; if (PsndRate < 22050 - 100) mult = 4; cdda_bytes *= mult; - ret = pm_read(cdda_out_buffer, cdda_bytes, cdda_stream); + ret = pm_read(cdda_out_buffer, cdda_bytes, Pico_mcd->cdda_stream); if (ret < cdda_bytes) { memset((char *)cdda_out_buffer + ret, 0, cdda_bytes - ret); - cdda_stream = NULL; + Pico_mcd->cdda_stream = NULL; return; } @@ -228,51 +217,24 @@ static void cdda_raw_update(int *buffer, int length) } } -PICO_INTERNAL void cdda_start_play(void) +void cdda_start_play(int lba_base, int lba_offset, int lb_len) { - int lba_offset, index, lba_length, i; - - elprintf(EL_STATUS, "cdda play track #%i", Pico_mcd->scd.Cur_Track); - - index = Pico_mcd->scd.Cur_Track - 1; - - lba_offset = Pico_mcd->scd.Cur_LBA - Track_to_LBA(index + 1); - if (lba_offset < 0) lba_offset = 0; - lba_offset += Pico_mcd->TOC.Tracks[index].Offset; - - // find the actual file for this track - for (i = index; i > 0; i--) - if (Pico_mcd->TOC.Tracks[i].F != NULL) break; - - if (Pico_mcd->TOC.Tracks[i].F == NULL) { - elprintf(EL_STATUS|EL_ANOMALY, "no track?!"); - return; - } - - if (Pico_mcd->TOC.Tracks[i].ftype == CT_MP3) + if (Pico_mcd->cdda_type == CT_MP3) { int pos1024 = 0; - lba_length = Pico_mcd->TOC.Tracks[i].Length; - for (i++; i < Pico_mcd->TOC.Last_Track; i++) { - if (Pico_mcd->TOC.Tracks[i].F != NULL) break; - lba_length += Pico_mcd->TOC.Tracks[i].Length; - } - if (lba_offset) - pos1024 = lba_offset * 1024 / lba_length; + pos1024 = lba_offset * 1024 / lb_len; - mp3_start_play(Pico_mcd->TOC.Tracks[index].F, pos1024); + mp3_start_play(Pico_mcd->cdda_stream, pos1024); return; } - cdda_stream = Pico_mcd->TOC.Tracks[i].F; - PicoCDBufferFlush(); // buffering relies on fp not being touched - pm_seek(cdda_stream, lba_offset * 2352, SEEK_SET); - if (Pico_mcd->TOC.Tracks[i].ftype == CT_WAV) + pm_seek(Pico_mcd->cdda_stream, (lba_base + lba_offset) * 2352, SEEK_SET); + if (Pico_mcd->cdda_type == CT_WAV) { // skip headers, assume it's 44kHz stereo uncompressed - pm_seek(cdda_stream, 44, SEEK_CUR); + pm_seek(Pico_mcd->cdda_stream, 44, SEEK_CUR); } } @@ -302,17 +264,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); @@ -339,13 +290,12 @@ static int PsndRender(int offset, int length) // CD: CDDA audio // CD mode, cdda enabled, not data track, CDC is reading - if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_CDDA) && - !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1)) + if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_CDDA) + && Pico_mcd->cdda_stream != NULL + && !(Pico_mcd->s68k_regs[0x36] & 1)) { // note: only 44, 22 and 11 kHz supported, with forced stereo - int index = Pico_mcd->scd.Cur_Track - 1; - - if (Pico_mcd->TOC.Tracks[index].ftype == CT_MP3) + if (Pico_mcd->cdda_type == CT_MP3) mp3_update(buf32, length, stereo); else cdda_raw_update(buf32, length); @@ -365,20 +315,16 @@ 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); + 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; @@ -386,28 +332,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) @@ -425,3 +363,4 @@ PICO_INTERNAL void PsndGetSamplesMS(void) PsndClear(); } +// vim:shiftwidth=2:ts=2:expandtab