* PicoDrive\r
* (c) Copyright Dave, 2004\r
* (C) notaz, 2006-2009\r
+ * (C) irixxxx, 2019-2024\r
*\r
* This work is licensed under the terms of MAME license.\r
* See COPYING file in the top-level directory.\r
*/\r
\r
#include <string.h>\r
+#include "../pico_int.h"\r
#include "ym2612.h"\r
+#include "ym2413.h"\r
#include "sn76496.h"\r
-#include "../pico_int.h"\r
-#include "../cd/cue.h"\r
+#include "../cd/megasd.h"\r
+#include "resampler.h"\r
#include "mix.h"\r
\r
-void (*PsndMix_32_to_16l)(short *dest, int *src, int count) = mix_32_to_16l_stereo;\r
+#define YM2612_CH6PAN 0x1b6 // panning register for channel 6 (used for DAC)\r
\r
-// master int buffer to mix to\r
-static int PsndBuffer[2*(44100+100)/50];\r
+void (*PsndMix_32_to_16)(s16 *dest, s32 *src, int count) = mix_32_to_16_stereo;\r
\r
-// dac, psg\r
-static unsigned short dac_info[312+4]; // pos in sample buffer\r
+// master int buffer to mix to\r
+// +1 for a fill triggered by an instruction overhanging into the next scanline\r
+static s32 PsndBuffer[2*(54000+100)/50+2];\r
\r
// cdda output buffer\r
-short cdda_out_buffer[2*1152];\r
+s16 cdda_out_buffer[2*1152];\r
\r
-// sn76496\r
-extern int *sn76496_regs;\r
+// FM resampling polyphase FIR\r
+static resampler_t *fmresampler;\r
+static int (*PsndFMUpdate)(s32 *buffer, int length, int stereo, int is_buf_empty);\r
\r
+PICO_INTERNAL void PsndInit(void)\r
+{\r
+ opll = OPLL_new(OSC_NTSC/15, OSC_NTSC/15/72);\r
+ OPLL_setChipType(opll,0);\r
+ OPLL_reset(opll);\r
+}\r
\r
-static void dac_recalculate(void)\r
+PICO_INTERNAL void PsndExit(void)\r
{\r
- int lines = Pico.m.pal ? 313 : 262;\r
- int mid = Pico.m.pal ? 68 : 93;\r
- int i, dac_cnt, pos, len;\r
+ OPLL_delete(opll);\r
+ opll = NULL;\r
\r
- if (Pico.snd.len <= lines)\r
- {\r
- // shrinking algo\r
- dac_cnt = -Pico.snd.len;\r
- len=1; pos=0;\r
- dac_info[225] = 1;\r
-\r
- for(i=226; i != 225; i++)\r
- {\r
- if (i >= lines) i = 0;\r
- if(dac_cnt < 0) {\r
- pos++;\r
- dac_cnt += lines;\r
- }\r
- dac_cnt -= Pico.snd.len;\r
- dac_info[i] = pos;\r
- }\r
- }\r
- else\r
- {\r
- // stretching\r
- dac_cnt = Pico.snd.len;\r
- pos=0;\r
- for(i = 225; i != 224; i++)\r
- {\r
- if (i >= lines) i = 0;\r
- len=0;\r
- while(dac_cnt >= 0) {\r
- dac_cnt -= lines;\r
- len++;\r
- }\r
- if (i == mid) // midpoint\r
- while(pos+len < Pico.snd.len/2) {\r
- dac_cnt -= lines;\r
- len++;\r
- }\r
- dac_cnt += Pico.snd.len;\r
- pos += len;\r
- dac_info[i] = pos;\r
- }\r
- }\r
- for (i = lines; i < sizeof(dac_info) / sizeof(dac_info[0]); i++)\r
- dac_info[i] = dac_info[0];\r
+ resampler_free(fmresampler); fmresampler = NULL;\r
}\r
\r
-\r
PICO_INTERNAL void PsndReset(void)\r
{\r
// PsndRerate calls YM2612Init, which also resets\r
timers_reset();\r
}\r
\r
+// FM polyphase FIR resampling\r
+#define FMFIR_TAPS 8\r
+\r
+// resample FM from its native 53267Hz/52781Hz with polyphase FIR filter\r
+static int ymchans;\r
+static void YM2612Update(s32 *buffer, int length, int stereo)\r
+{\r
+ ymchans = YM2612UpdateOne(buffer, length, stereo, 1);\r
+}\r
+\r
+static int YM2612UpdateFIR(s32 *buffer, int length, int stereo, int is_buf_empty)\r
+{\r
+ resampler_update(fmresampler, buffer, length, YM2612Update);\r
+ return ymchans;\r
+}\r
+\r
+// resample SMS FM from its native 49716Hz/49262Hz with polyphase FIR filter\r
+static void YM2413Update(s32 *buffer, int length, int stereo)\r
+{\r
+ while (length-- > 0) {\r
+ int16_t getdata = OPLL_calc(opll) * 3;\r
+ *buffer++ = getdata;\r
+ buffer += stereo; // only left for stereo, to be mixed to right later\r
+ }\r
+}\r
+\r
+static int YM2413UpdateFIR(s32 *buffer, int length, int stereo, int is_buf_empty)\r
+{\r
+ if (!is_buf_empty) memset(buffer, 0, (length << stereo) * sizeof(*buffer));\r
+ resampler_update(fmresampler, buffer, length, YM2413Update);\r
+ return 0;\r
+}\r
+\r
+// FIR setup, looks for a close enough rational number matching the ratio\r
+static void YMFM_setup_FIR(int inrate, int outrate, int stereo)\r
+{\r
+ int mindiff = 999;\r
+ int diff, mul, div;\r
+ int minmult = 11, maxmult = 61; // min,max interpolation factor\r
+\r
+ // compute filter ratio with largest multiplier for smallest error\r
+ for (mul = minmult; mul <= maxmult; mul++) {\r
+ div = (inrate*mul + outrate/2) / outrate;\r
+ diff = outrate*div/mul - inrate;\r
+ if (abs(diff) < abs(mindiff)) {\r
+ mindiff = diff;\r
+ Pico.snd.fm_fir_mul = mul;\r
+ Pico.snd.fm_fir_div = div;\r
+ if (abs(mindiff)*1000 <= inrate) break; // below error limit\r
+ }\r
+ }\r
+ printf("FM polyphase FIR ratio=%d/%d error=%.3f%%\n",\r
+ Pico.snd.fm_fir_mul, Pico.snd.fm_fir_div, 100.0*mindiff/inrate);\r
+\r
+ resampler_free(fmresampler);\r
+ fmresampler = resampler_new(FMFIR_TAPS, Pico.snd.fm_fir_mul, Pico.snd.fm_fir_div,\r
+ 0.85, 2, 2*inrate/50, stereo);\r
+}\r
+\r
+// wrapper for the YM2612UpdateONE macro\r
+static int YM2612UpdateONE(s32 *buffer, int length, int stereo, int is_buf_empty)\r
+{\r
+ return YM2612UpdateOne(buffer, length, stereo, is_buf_empty);\r
+}\r
+\r
+static int ymclock;\r
+static int ymrate;\r
+static int ymopts;\r
\r
// to be called after changing sound rate or chips\r
void PsndRerate(int preserve_state)\r
{\r
void *state = NULL;\r
int target_fps = Pico.m.pal ? 50 : 60;\r
-\r
- if (preserve_state) {\r
- state = malloc(0x204);\r
- if (state == NULL) return;\r
- ym2612_pack_state();\r
- memcpy(state, YM2612GetRegs(), 0x204);\r
- }\r
- YM2612Init(Pico.m.pal ? OSC_PAL/7 : OSC_NTSC/7, PicoIn.sndRate);\r
- if (preserve_state) {\r
- // feed it back it's own registers, just like after loading state\r
- memcpy(YM2612GetRegs(), state, 0x204);\r
- ym2612_unpack_state();\r
+ int target_lines = Pico.m.pal ? 313 : 262;\r
+ int sms_clock = Pico.m.pal ? OSC_PAL/15 : OSC_NTSC/15;\r
+ int ym2413_rate = (sms_clock + 36) / 72;\r
+ int ym2612_clock = Pico.m.pal ? OSC_PAL/7 : OSC_NTSC/7;\r
+ int ym2612_rate = YM2612_NATIVE_RATE();\r
+ int ym2612_init = !preserve_state;\r
+ int state_size = 4096;\r
+\r
+ // don't init YM2612 if preserve_state and no parameter changes\r
+ ym2612_init |= ymclock != ym2612_clock || ymopts != (PicoIn.opt & (POPT_DIS_FM_SSGEG|POPT_EN_FM_DAC));\r
+ ym2612_init |= ymrate != (PicoIn.opt & POPT_EN_FM_FILTER ? ym2612_rate : PicoIn.sndRate);\r
+ ymclock = ym2612_clock;\r
+ ymrate = (PicoIn.opt & POPT_EN_FM_FILTER ? ym2612_rate : PicoIn.sndRate);\r
+ ymopts = PicoIn.opt & (POPT_DIS_FM_SSGEG|POPT_EN_FM_DAC);\r
+\r
+ if (preserve_state && ym2612_init) {\r
+ state = malloc(state_size);\r
+ if (state)\r
+ state_size = YM2612PicoStateSave3(state, state_size);\r
}\r
\r
- if (preserve_state) memcpy(state, sn76496_regs, 28*4); // remember old state\r
- SN76496_init(Pico.m.pal ? OSC_PAL/15 : OSC_NTSC/15, PicoIn.sndRate);\r
- if (preserve_state) memcpy(sn76496_regs, state, 28*4); // restore old state\r
+ if (PicoIn.AHW & PAHW_SMS) {\r
+ OPLL_setRate(opll, ym2413_rate);\r
+ if (!preserve_state)\r
+ OPLL_reset(opll);\r
+ YMFM_setup_FIR(ym2413_rate, PicoIn.sndRate, 0);\r
+ PsndFMUpdate = YM2413UpdateFIR;\r
+ } else if ((PicoIn.opt & POPT_EN_FM_FILTER) && ym2612_rate != PicoIn.sndRate) {\r
+ // polyphase FIR resampler, resampling directly from native to output rate\r
+ if (ym2612_init)\r
+ YM2612Init(ym2612_clock, ym2612_rate,\r
+ ((PicoIn.opt&POPT_DIS_FM_SSGEG) ? 0 : ST_SSG) |\r
+ ((PicoIn.opt&POPT_EN_FM_DAC) ? ST_DAC : 0));\r
+ YMFM_setup_FIR(ym2612_rate, PicoIn.sndRate, PicoIn.opt & POPT_EN_STEREO);\r
+ PsndFMUpdate = YM2612UpdateFIR;\r
+ } else {\r
+ if (ym2612_init)\r
+ YM2612Init(ym2612_clock, PicoIn.sndRate,\r
+ ((PicoIn.opt&POPT_DIS_FM_SSGEG) ? 0 : ST_SSG) |\r
+ ((PicoIn.opt&POPT_EN_FM_DAC) ? ST_DAC : 0));\r
+ PsndFMUpdate = YM2612UpdateONE;\r
+ }\r
\r
- if (state)\r
+ if (state) {\r
+ YM2612PicoStateLoad3(state, state_size);\r
free(state);\r
+ }\r
+\r
+ if (preserve_state)\r
+ SN76496_set_clockrate(Pico.m.pal ? OSC_PAL/15 : OSC_NTSC/15, PicoIn.sndRate);\r
+ else\r
+ SN76496_init(Pico.m.pal ? OSC_PAL/15 : OSC_NTSC/15, PicoIn.sndRate);\r
\r
// calculate Pico.snd.len\r
Pico.snd.len = PicoIn.sndRate / target_fps;\r
Pico.snd.len_e_add = ((PicoIn.sndRate - Pico.snd.len * target_fps) << 16) / target_fps;\r
- Pico.snd.len_e_cnt = 0;\r
+ Pico.snd.len_e_cnt = 0; // Q16\r
\r
- // recalculate dac info\r
- dac_recalculate();\r
+ // samples per line (Q16)\r
+ Pico.snd.smpl_mult = 65536LL * PicoIn.sndRate / (target_fps*target_lines);\r
+ // samples per z80 clock (Q20)\r
+ Pico.snd.clkz_mult = 16 * Pico.snd.smpl_mult * 15/7 / 488.5;\r
+ // samples per 44.1 KHz sample (Q16)\r
+ Pico.snd.cdda_mult = 65536LL * 44100 / PicoIn.sndRate;\r
+ Pico.snd.cdda_div = 65536LL * PicoIn.sndRate / 44100;\r
\r
// clear all buffers\r
memset32(PsndBuffer, 0, sizeof(PsndBuffer)/4);\r
PsndClear();\r
\r
// set mixer\r
- PsndMix_32_to_16l = (PicoIn.opt & POPT_EN_STEREO) ? mix_32_to_16l_stereo : mix_32_to_16_mono;\r
+ PsndMix_32_to_16 = (PicoIn.opt & POPT_EN_STEREO) ? mix_32_to_16_stereo : mix_32_to_16_mono;\r
+ mix_reset(PicoIn.opt & POPT_EN_SNDFILTER ? PicoIn.sndFilterAlpha : 0);\r
\r
if (PicoIn.AHW & PAHW_PICO)\r
PicoReratePico();\r
Pico.snd.len_e_cnt -= 0x10000;\r
Pico.snd.len_use++;\r
}\r
-\r
- Pico.snd.dac_line = Pico.snd.psg_line = 0;\r
- Pico.m.status &= ~1;\r
- dac_info[224] = Pico.snd.len_use;\r
}\r
\r
-PICO_INTERNAL void PsndDoDAC(int line_to)\r
+PICO_INTERNAL void PsndDoDAC(int cyc_to)\r
{\r
- int pos, pos1, len;\r
+ int pos, len;\r
int dout = ym2612.dacout;\r
- int line_from = Pico.snd.dac_line;\r
\r
- if (line_to >= 313)\r
- line_to = 312;\r
+ // nothing to do if sound is off\r
+ if (!PicoIn.sndOut) return;\r
+\r
+ // number of samples to fill in buffer (Q20)\r
+ len = (cyc_to * Pico.snd.clkz_mult) - Pico.snd.dac_pos;\r
+\r
+ // update position and calculate buffer offset and length\r
+ pos = (Pico.snd.dac_pos+0x80000) >> 20;\r
+ Pico.snd.dac_pos += len;\r
+ len = ((Pico.snd.dac_pos+0x80000) >> 20) - pos;\r
\r
- pos = dac_info[line_from];\r
- pos1 = dac_info[line_to + 1];\r
- len = pos1 - pos;\r
+ // avoid loss of the 1st sample of a new block (Q rounding issues)\r
+ if (pos+len == 0)\r
+ len = 1, Pico.snd.dac_pos += 0x80000;\r
if (len <= 0)\r
return;\r
\r
- Pico.snd.dac_line = line_to + 1;\r
+ // fill buffer, applying a rather weak order 1 bessel IIR on the way\r
+ // y[n] = (x[n] + x[n-1])*(1/2) (3dB cutoff at 11025 Hz, no gain)\r
+ // 1 sample delay for correct IIR filtering over audio frame boundaries\r
+ if (PicoIn.opt & POPT_EN_STEREO) {\r
+ s16 *d = PicoIn.sndOut + pos*2;\r
+ int pan = ym2612.REGS[YM2612_CH6PAN];\r
+ int l = pan & 0x80 ? Pico.snd.dac_val : 0;\r
+ int r = pan & 0x40 ? Pico.snd.dac_val : 0;\r
+ *d++ += pan & 0x80 ? Pico.snd.dac_val2 : 0;\r
+ *d++ += pan & 0x40 ? Pico.snd.dac_val2 : 0;\r
+ while (--len) *d++ += l, *d++ += r;\r
+ } else {\r
+ s16 *d = PicoIn.sndOut + pos;\r
+ *d++ += Pico.snd.dac_val2;\r
+ while (--len) *d++ += Pico.snd.dac_val;\r
+ }\r
+ Pico.snd.dac_val2 = (Pico.snd.dac_val + dout) >> 1;\r
+ Pico.snd.dac_val = dout;\r
+}\r
\r
- if (!PicoIn.sndOut)\r
+PICO_INTERNAL void PsndDoPSG(int cyc_to)\r
+{\r
+ int pos, len;\r
+ int stereo = 0;\r
+\r
+ // nothing to do if sound is off\r
+ if (!PicoIn.sndOut) return;\r
+\r
+ // number of samples to fill in buffer (Q20)\r
+ len = (cyc_to * Pico.snd.clkz_mult) - Pico.snd.psg_pos;\r
+\r
+ // update position and calculate buffer offset and length\r
+ pos = (Pico.snd.psg_pos+0x80000) >> 20;\r
+ Pico.snd.psg_pos += len;\r
+ len = ((Pico.snd.psg_pos+0x80000) >> 20) - pos;\r
+\r
+ if (len <= 0)\r
+ return;\r
+ if (!(PicoIn.opt & POPT_EN_PSG))\r
return;\r
\r
if (PicoIn.opt & POPT_EN_STEREO) {\r
- short *d = PicoIn.sndOut + pos*2;\r
- for (; len > 0; len--, d+=2) *d += dout;\r
- } else {\r
- short *d = PicoIn.sndOut + pos;\r
- for (; len > 0; len--, d++) *d += dout;\r
+ stereo = 1;\r
+ pos <<= 1;\r
}\r
+ SN76496Update(PicoIn.sndOut + pos, len, stereo);\r
}\r
\r
-PICO_INTERNAL void PsndDoPSG(int line_to)\r
+PICO_INTERNAL void PsndDoSMSFM(int cyc_to)\r
{\r
- int line_from = Pico.snd.psg_line;\r
- int pos, pos1, len;\r
+ int pos, len;\r
int stereo = 0;\r
+ s32 *buf32 = PsndBuffer;\r
+ s16 *buf = PicoIn.sndOut;\r
\r
- if (line_to >= 313)\r
- line_to = 312;\r
+ // nothing to do if sound is off\r
+ if (!PicoIn.sndOut) return;\r
+\r
+ // number of samples to fill in buffer (Q20)\r
+ len = (cyc_to * Pico.snd.clkz_mult) - Pico.snd.ym2413_pos;\r
+\r
+ // update position and calculate buffer offset and length\r
+ pos = (Pico.snd.ym2413_pos+0x80000) >> 20;\r
+ Pico.snd.ym2413_pos += len;\r
+ len = ((Pico.snd.ym2413_pos+0x80000) >> 20) - pos;\r
\r
- pos = dac_info[line_from];\r
- pos1 = dac_info[line_to + 1];\r
- len = pos1 - pos;\r
- //elprintf(EL_STATUS, "%3d %3d %3d %3d %3d",\r
- // pos, pos1, len, line_from, line_to);\r
if (len <= 0)\r
return;\r
+ if (!(PicoIn.opt & POPT_EN_YM2413))\r
+ return;\r
\r
- Pico.snd.psg_line = line_to + 1;\r
+ if (PicoIn.opt & POPT_EN_STEREO) {\r
+ stereo = 1;\r
+ pos <<= 1;\r
+ }\r
+\r
+ if (Pico.m.hardware & PMS_HW_FMUSED) {\r
+ buf += pos;\r
+ PsndFMUpdate(buf32, len, 0, 0);\r
+ if (stereo) \r
+ while (len--) {\r
+ *buf++ += *buf32;\r
+ *buf++ += *buf32++;\r
+ }\r
+ else\r
+ while (len--) {\r
+ *buf++ += *buf32++;\r
+ }\r
+ }\r
+}\r
\r
- if (!PicoIn.sndOut || !(PicoIn.opt & POPT_EN_PSG))\r
+PICO_INTERNAL void PsndDoFM(int cyc_to)\r
+{\r
+ int pos, len;\r
+ int stereo = 0;\r
+\r
+ // nothing to do if sound is off\r
+ if (!PicoIn.sndOut) return;\r
+\r
+ // Q20, number of samples since last call\r
+ len = (cyc_to * Pico.snd.clkz_mult) - Pico.snd.fm_pos;\r
+\r
+ // update position and calculate buffer offset and length\r
+ pos = (Pico.snd.fm_pos+0x80000) >> 20;\r
+ Pico.snd.fm_pos += len;\r
+ len = ((Pico.snd.fm_pos+0x80000) >> 20) - pos;\r
+ if (len <= 0)\r
return;\r
\r
+ // fill buffer\r
if (PicoIn.opt & POPT_EN_STEREO) {\r
stereo = 1;\r
pos <<= 1;\r
}\r
- SN76496Update(PicoIn.sndOut + pos, len, stereo);\r
+ if (PicoIn.opt & POPT_EN_FM)\r
+ PsndFMUpdate(PsndBuffer + pos, len, stereo, 1);\r
+}\r
+\r
+PICO_INTERNAL void PsndDoPCM(int cyc_to)\r
+{\r
+ int pos, len;\r
+ int stereo = 0;\r
+\r
+ // nothing to do if sound is off\r
+ if (!PicoIn.sndOut) return;\r
+\r
+ // Q20, number of samples since last call\r
+ len = (cyc_to * Pico.snd.clkz_mult) - Pico.snd.pcm_pos;\r
+\r
+ // update position and calculate buffer offset and length\r
+ pos = (Pico.snd.pcm_pos+0x80000) >> 20;\r
+ Pico.snd.pcm_pos += len;\r
+ len = ((Pico.snd.pcm_pos+0x80000) >> 20) - pos;\r
+ if (len <= 0)\r
+ return;\r
+\r
+ // fill buffer\r
+ if (PicoIn.opt & POPT_EN_STEREO) {\r
+ stereo = 1;\r
+ pos <<= 1;\r
+ }\r
+ PicoPicoPCMUpdate(PicoIn.sndOut + pos, len, stereo);\r
}\r
\r
// cdda\r
-static void cdda_raw_update(int *buffer, int length)\r
+static void cdda_raw_update(s32 *buffer, int length, int stereo)\r
{\r
- int ret, cdda_bytes, mult = 1;\r
+ int ret, cdda_bytes;\r
+\r
+ // apply start offset in frame (offset to 1st lba to play)\r
+ int offs = Pico_mcd->cdda_frame_offs * Pico.snd.cdda_div >> 16;\r
+ length -= offs;\r
+ buffer += offs * (stereo ? 2 : 1);\r
+ Pico_mcd->cdda_frame_offs = 0;\r
\r
- cdda_bytes = length*4;\r
- if (PicoIn.sndRate <= 22050 + 100) mult = 2;\r
- if (PicoIn.sndRate < 22050 - 100) mult = 4;\r
- cdda_bytes *= mult;\r
+ cdda_bytes = (length * Pico.snd.cdda_mult >> 16) * 4;\r
\r
- ret = pm_read(cdda_out_buffer, cdda_bytes, Pico_mcd->cdda_stream);\r
+ // compute offset of last played sample in this frame (need for save/load)\r
+ Pico_mcd->m.cdda_lba_offset += cdda_bytes/4;\r
+ while (Pico_mcd->m.cdda_lba_offset >= 2352/4)\r
+ Pico_mcd->m.cdda_lba_offset -= 2352/4;\r
+\r
+ ret = pm_read_audio(cdda_out_buffer, cdda_bytes, Pico_mcd->cdda_stream);\r
if (ret < cdda_bytes) {\r
memset((char *)cdda_out_buffer + ret, 0, cdda_bytes - ret);\r
Pico_mcd->cdda_stream = NULL;\r
- return;\r
}\r
\r
// now mix\r
- switch (mult) {\r
- case 1: mix_16h_to_32(buffer, cdda_out_buffer, length*2); break;\r
- case 2: mix_16h_to_32_s1(buffer, cdda_out_buffer, length*2); break;\r
- case 4: mix_16h_to_32_s2(buffer, cdda_out_buffer, length*2); break;\r
- }\r
+ if (stereo) switch (Pico.snd.cdda_mult) {\r
+ case 0x10000: mix_16h_to_32(buffer, cdda_out_buffer, length*2); break;\r
+ case 0x20000: mix_16h_to_32_s1(buffer, cdda_out_buffer, length*2); break;\r
+ case 0x40000: mix_16h_to_32_s2(buffer, cdda_out_buffer, length*2); break;\r
+ default: mix_16h_to_32_resample_stereo(buffer, cdda_out_buffer, length, Pico.snd.cdda_mult);\r
+ } else\r
+ mix_16h_to_32_resample_mono(buffer, cdda_out_buffer, length, Pico.snd.cdda_mult);\r
}\r
\r
void cdda_start_play(int lba_base, int lba_offset, int lb_len)\r
return;\r
}\r
\r
- pm_seek(Pico_mcd->cdda_stream, (lba_base + lba_offset) * 2352, SEEK_SET);\r
+ // on restart after loading, consider offset of last played sample\r
+ pm_seek(Pico_mcd->cdda_stream, (lba_base + lba_offset) * 2352 +\r
+ Pico_mcd->m.cdda_lba_offset * 4, SEEK_SET);\r
if (Pico_mcd->cdda_type == CT_WAV)\r
{\r
// skip headers, assume it's 44kHz stereo uncompressed\r
{\r
int len = Pico.snd.len;\r
if (Pico.snd.len_e_add) len++;\r
+\r
+ // drop pos remainder to avoid rounding errors (not entirely correct though)\r
+ Pico.snd.dac_pos = Pico.snd.fm_pos = Pico.snd.psg_pos = Pico.snd.ym2413_pos = Pico.snd.pcm_pos = 0;\r
+ if (!PicoIn.sndOut) return;\r
+\r
if (PicoIn.opt & POPT_EN_STEREO)\r
memset32((int *) PicoIn.sndOut, 0, len); // assume PicoIn.sndOut to be aligned\r
else {\r
- short *out = PicoIn.sndOut;\r
- if ((long)out & 2) { *out++ = 0; len--; }\r
+ s16 *out = PicoIn.sndOut;\r
+ if ((uintptr_t)out & 2) { *out++ = 0; len--; }\r
memset32((int *) out, 0, len/2);\r
if (len & 1) out[len-1] = 0;\r
}\r
+ if (!(PicoIn.opt & POPT_EN_FM))\r
+ memset32(PsndBuffer, 0, PicoIn.opt & POPT_EN_STEREO ? len*2 : len);\r
}\r
\r
\r
static int PsndRender(int offset, int length)\r
{\r
- int buf32_updated = 0;\r
- int *buf32 = PsndBuffer+offset;\r
+ s32 *buf32;\r
int stereo = (PicoIn.opt & 8) >> 3;\r
+ int fmlen = ((Pico.snd.fm_pos+0x80000) >> 20);\r
+ int daclen = ((Pico.snd.dac_pos+0x80000) >> 20);\r
+ int psglen = ((Pico.snd.psg_pos+0x80000) >> 20);\r
+ int pcmlen = ((Pico.snd.pcm_pos+0x80000) >> 20);\r
\r
- offset <<= stereo;\r
+ buf32 = PsndBuffer+(offset<<stereo);\r
\r
pprof_start(sound);\r
\r
+ // Add in parts of the PSG output not yet done\r
+ if (length-psglen > 0 && PicoIn.sndOut) {\r
+ s16 *psgbuf = PicoIn.sndOut + (psglen << stereo);\r
+ Pico.snd.psg_pos += (length-psglen) << 20;\r
+ if (PicoIn.opt & POPT_EN_PSG)\r
+ SN76496Update(psgbuf, length-psglen, stereo);\r
+ }\r
+\r
if (PicoIn.AHW & PAHW_PICO) {\r
- PicoPicoPCMUpdate(PicoIn.sndOut+offset, length, stereo);\r
+ // always need to render sound for interrupts\r
+ s16 *buf16 = PicoIn.sndOut ? PicoIn.sndOut + (pcmlen<<stereo) : NULL;\r
+ PicoPicoPCMUpdate(buf16, length-pcmlen, stereo);\r
return length;\r
}\r
\r
- // Add in the stereo FM buffer\r
- if (PicoIn.opt & POPT_EN_FM) {\r
- buf32_updated = YM2612UpdateOne(buf32, length, stereo, 1);\r
- } else\r
- memset32(buf32, 0, length<<stereo);\r
+ // Fill up DAC output in case of missing samples (Q rounding errors)\r
+ if (length-daclen > 0 && PicoIn.sndOut) {\r
+ Pico.snd.dac_pos += (length-daclen) << 20;\r
+ if (PicoIn.opt & POPT_EN_STEREO) {\r
+ s16 *d = PicoIn.sndOut + daclen*2;\r
+ int pan = ym2612.REGS[YM2612_CH6PAN];\r
+ int l = pan & 0x80 ? Pico.snd.dac_val : 0;\r
+ int r = pan & 0x40 ? Pico.snd.dac_val : 0;\r
+ *d++ += pan & 0x80 ? Pico.snd.dac_val2 : 0;\r
+ *d++ += pan & 0x40 ? Pico.snd.dac_val2 : 0;\r
+ if (l|r) for (daclen++; length-daclen > 0; daclen++)\r
+ *d++ += l, *d++ += r;\r
+ } else {\r
+ s16 *d = PicoIn.sndOut + daclen;\r
+ *d++ += Pico.snd.dac_val2;\r
+ if (Pico.snd.dac_val) for (daclen++; length-daclen > 0; daclen++)\r
+ *d++ += Pico.snd.dac_val;\r
+ }\r
+ Pico.snd.dac_val2 = Pico.snd.dac_val;\r
+ }\r
\r
-//printf("active_chs: %02x\n", buf32_updated);\r
- (void)buf32_updated;\r
+ // Add in parts of the FM buffer not yet done\r
+ if (length-fmlen > 0 && PicoIn.sndOut) {\r
+ s32 *fmbuf = buf32 + ((fmlen-offset) << stereo);\r
+ Pico.snd.fm_pos += (length-fmlen) << 20;\r
+ if (PicoIn.opt & POPT_EN_FM)\r
+ PsndFMUpdate(fmbuf, length-fmlen, stereo, 1);\r
+ }\r
\r
// CD: PCM sound\r
if (PicoIn.AHW & PAHW_MCD) {\r
- pcd_pcm_update(buf32, length, stereo);\r
- //buf32_updated = 1;\r
+ pcd_pcm_update(buf32, length-offset, stereo);\r
}\r
\r
// CD: CDDA audio\r
// CD mode, cdda enabled, not data track, CDC is reading\r
if ((PicoIn.AHW & PAHW_MCD) && (PicoIn.opt & POPT_EN_MCD_CDDA)\r
&& Pico_mcd->cdda_stream != NULL\r
- && !(Pico_mcd->s68k_regs[0x36] & 1))\r
+ && (!(Pico_mcd->s68k_regs[0x36] & 1) || Pico_msd.state == 3))\r
{\r
- // note: only 44, 22 and 11 kHz supported, with forced stereo\r
if (Pico_mcd->cdda_type == CT_MP3)\r
- mp3_update(buf32, length, stereo);\r
+ mp3_update(buf32, length-offset, stereo);\r
else\r
- cdda_raw_update(buf32, length);\r
+ cdda_raw_update(buf32, length-offset, stereo);\r
}\r
\r
if ((PicoIn.AHW & PAHW_32X) && (PicoIn.opt & POPT_EN_PWM))\r
- p32x_pwm_update(buf32, length, stereo);\r
+ p32x_pwm_update(buf32, length-offset, stereo);\r
\r
// convert + limit to normal 16bit output\r
- PsndMix_32_to_16l(PicoIn.sndOut+offset, buf32, length);\r
+ if (PicoIn.sndOut)\r
+ PsndMix_32_to_16(PicoIn.sndOut+(offset<<stereo), buf32, length-offset);\r
\r
pprof_end(sound);\r
\r
return length;\r
}\r
\r
-// to be called on 224 or line_sample scanlines only\r
PICO_INTERNAL void PsndGetSamples(int y)\r
{\r
static int curr_pos = 0;\r
\r
- if (ym2612.dacen && Pico.snd.dac_line < y)\r
- PsndDoDAC(y - 1);\r
- PsndDoPSG(y - 1);\r
+ curr_pos = PsndRender(0, Pico.snd.len_use);\r
\r
- if (y == 224)\r
- {\r
- if (Pico.m.status & 2)\r
- curr_pos += PsndRender(curr_pos, Pico.snd.len-Pico.snd.len/2);\r
- else curr_pos = PsndRender(0, Pico.snd.len_use);\r
- if (Pico.m.status & 1)\r
- Pico.m.status |= 2;\r
- else Pico.m.status &= ~2;\r
- if (PicoIn.writeSound)\r
- PicoIn.writeSound(curr_pos * ((PicoIn.opt & POPT_EN_STEREO) ? 4 : 2));\r
- // clear sound buffer\r
- PsndClear();\r
- Pico.snd.dac_line = 224;\r
- dac_info[224] = 0;\r
- }\r
- else if (Pico.m.status & 3) {\r
- Pico.m.status |= 2;\r
- Pico.m.status &= ~1;\r
- curr_pos = PsndRender(0, Pico.snd.len/2);\r
- }\r
+ if (PicoIn.writeSound && PicoIn.sndOut)\r
+ PicoIn.writeSound(curr_pos * ((PicoIn.opt & POPT_EN_STEREO) ? 4 : 2));\r
+ // clear sound buffer\r
+ PsndClear();\r
}\r
\r
-PICO_INTERNAL void PsndGetSamplesMS(void)\r
+static int PsndRenderMS(int offset, int length)\r
{\r
- int length = Pico.snd.len_use;\r
+ s32 *buf32 = PsndBuffer;\r
+ int stereo = (PicoIn.opt & 8) >> 3;\r
+ int psglen = ((Pico.snd.psg_pos+0x80000) >> 20);\r
+ int ym2413len = ((Pico.snd.ym2413_pos+0x80000) >> 20);\r
\r
- PsndDoPSG(223);\r
+ if (!PicoIn.sndOut)\r
+ return length;\r
\r
- // upmix to "stereo" if needed\r
- if (PicoIn.opt & POPT_EN_STEREO) {\r
- int i, *p;\r
- for (i = length, p = (void *)PicoIn.sndOut; i > 0; i--, p++)\r
- *p |= *p << 16;\r
+ pprof_start(sound);\r
+\r
+ // Add in parts of the PSG output not yet done\r
+ if (length-psglen > 0) {\r
+ s16 *psgbuf = PicoIn.sndOut + (psglen << stereo);\r
+ Pico.snd.psg_pos += (length-psglen) << 20;\r
+ if (PicoIn.opt & POPT_EN_PSG)\r
+ SN76496Update(psgbuf, length-psglen, stereo);\r
}\r
\r
- if (PicoIn.writeSound != NULL)\r
- PicoIn.writeSound(length * ((PicoIn.opt & POPT_EN_STEREO) ? 4 : 2));\r
- PsndClear();\r
+ if (length-ym2413len > 0) {\r
+ s16 *ym2413buf = PicoIn.sndOut + (ym2413len << stereo);\r
+ Pico.snd.ym2413_pos += (length-ym2413len) << 20;\r
+ int len = (length-ym2413len);\r
+ if (Pico.m.hardware & PMS_HW_FMUSED) {\r
+ PsndFMUpdate(buf32, len, 0, 0);\r
+ if (stereo)\r
+ while (len--) {\r
+ *ym2413buf++ += *buf32;\r
+ *ym2413buf++ += *buf32++;\r
+ }\r
+ else\r
+ while (len--) {\r
+ *ym2413buf++ += *buf32++;\r
+ }\r
+ }\r
+ }\r
+\r
+ pprof_end(sound);\r
\r
- dac_info[224] = 0;\r
+ return length;\r
+}\r
+\r
+PICO_INTERNAL void PsndGetSamplesMS(int y)\r
+{\r
+ static int curr_pos = 0;\r
+\r
+ curr_pos = PsndRenderMS(0, Pico.snd.len_use);\r
+\r
+ if (PicoIn.writeSound != NULL && PicoIn.sndOut)\r
+ PicoIn.writeSound(curr_pos * ((PicoIn.opt & POPT_EN_STEREO) ? 4 : 2));\r
+ PsndClear();\r
}\r
\r
// vim:shiftwidth=2:ts=2:expandtab\r