cd: switch to CD drive emu code from genplus
[picodrive.git] / pico / sound / sound.c
index f3cd221..69c1be0 100644 (file)
 #include "ym2612.h"\r
 #include "sn76496.h"\r
 #include "../pico_int.h"\r
-#include "../cd/pcm.h"\r
+#include "../cd/cue.h"\r
 #include "mix.h"\r
 \r
+#define SIMPLE_WRITE_SOUND 0\r
+\r
 void (*PsndMix_32_to_16l)(short *dest, int *src, int count) = mix_32_to_16l_stereo;\r
 \r
 // master int buffer to mix to\r
@@ -119,15 +121,6 @@ void PsndRerate(int preserve_state)
   void *state = NULL;\r
   int target_fps = Pico.m.pal ? 50 : 60;\r
 \r
-  // not all rates are supported in MCD mode due to mp3 decoder limitations\r
-  if (PicoAHW & PAHW_MCD) {\r
-    if (!(11025-100 <= PsndRate && PsndRate <= 11025+100) &&\r
-        !(22050-100 <= PsndRate && PsndRate <= 22050+100) &&\r
-        !(44100-100 <= PsndRate && PsndRate <= 44100+100))\r
-      PsndRate = 22050;\r
-    PicoOpt |= POPT_EN_STEREO; // force stereo\r
-  }\r
-\r
   if (preserve_state) {\r
     state = malloc(0x204);\r
     if (state == NULL) return;\r
@@ -139,8 +132,6 @@ void PsndRerate(int preserve_state)
     // feed it back it's own registers, just like after loading state\r
     memcpy(YM2612GetRegs(), state, 0x204);\r
     ym2612_unpack_state();\r
-    if ((PicoAHW & PAHW_MCD) && !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))\r
-      cdda_start_play();\r
   }\r
 \r
   if (preserve_state) memcpy(state, sn76496_regs, 28*4); // remember old state\r
@@ -158,9 +149,6 @@ void PsndRerate(int preserve_state)
   // recalculate dac info\r
   dac_recalculate();\r
 \r
-  if (PicoAHW & PAHW_MCD)\r
-    pcm_set_rate(PsndRate);\r
-\r
   // clear all buffers\r
   memset32(PsndBuffer, 0, sizeof(PsndBuffer)/4);\r
   memset(cdda_out_buffer, 0, sizeof(cdda_out_buffer));\r
@@ -181,6 +169,9 @@ PICO_INTERNAL void PsndDoDAC(int line_to)
   int dout = ym2612.dacout;\r
   int line_from = PsndDacLine;\r
 \r
+  if (line_to >= 312)\r
+    line_to = 311;\r
+\r
   PsndDacLine = line_to + 1;\r
 \r
   pos =dac_info[line_from]>>4;\r
@@ -195,34 +186,22 @@ PICO_INTERNAL void PsndDoDAC(int line_to)
     short *d = PsndOut + pos;\r
     for (; len > 0; len--, d++)  *d = dout;\r
   }\r
-\r
-#if 0\r
-  if (do_pcm) {\r
-    int *d = PsndBuffer;\r
-    d += (PicoOpt&8) ? pos*2 : pos;\r
-    pcm_update(d, len, 1);\r
-  }\r
-#endif\r
 }\r
 \r
 // cdda\r
-static pm_file *cdda_stream = NULL;\r
-\r
 static void cdda_raw_update(int *buffer, int length)\r
 {\r
   int ret, cdda_bytes, mult = 1;\r
-  if (cdda_stream == NULL)\r
-    return;\r
 \r
   cdda_bytes = length*4;\r
   if (PsndRate <= 22050 + 100) mult = 2;\r
   if (PsndRate <  22050 - 100) mult = 4;\r
   cdda_bytes *= mult;\r
 \r
-  ret = pm_read(cdda_out_buffer, cdda_bytes, cdda_stream);\r
+  ret = pm_read(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
-    cdda_stream = NULL;\r
+    Pico_mcd->cdda_stream = NULL;\r
     return;\r
   }\r
 \r
@@ -234,51 +213,24 @@ static void cdda_raw_update(int *buffer, int length)
   }\r
 }\r
 \r
-PICO_INTERNAL void cdda_start_play(void)\r
+void cdda_start_play(int lba_base, int lba_offset, int lb_len)\r
 {\r
-  int lba_offset, index, lba_length, i;\r
-\r
-  elprintf(EL_STATUS, "cdda play track #%i", Pico_mcd->scd.Cur_Track);\r
-\r
-  index = Pico_mcd->scd.Cur_Track - 1;\r
-\r
-  lba_offset = Pico_mcd->scd.Cur_LBA - Track_to_LBA(index + 1);\r
-  if (lba_offset < 0) lba_offset = 0;\r
-  lba_offset += Pico_mcd->TOC.Tracks[index].Offset;\r
-\r
-  // find the actual file for this track\r
-  for (i = index; i > 0; i--)\r
-    if (Pico_mcd->TOC.Tracks[i].F != NULL) break;\r
-\r
-  if (Pico_mcd->TOC.Tracks[i].F == NULL) {\r
-    elprintf(EL_STATUS|EL_ANOMALY, "no track?!");\r
-    return;\r
-  }\r
-\r
-  if (Pico_mcd->TOC.Tracks[i].ftype == TYPE_MP3)\r
+  if (Pico_mcd->cdda_type == CT_MP3)\r
   {\r
     int pos1024 = 0;\r
 \r
-    lba_length = Pico_mcd->TOC.Tracks[i].Length;\r
-    for (i++; i < Pico_mcd->TOC.Last_Track; i++) {\r
-      if (Pico_mcd->TOC.Tracks[i].F != NULL) break;\r
-      lba_length += Pico_mcd->TOC.Tracks[i].Length;\r
-    }\r
-\r
     if (lba_offset)\r
-      pos1024 = lba_offset * 1024 / lba_length;\r
+      pos1024 = lba_offset * 1024 / lb_len;\r
 \r
-    mp3_start_play(Pico_mcd->TOC.Tracks[index].F, pos1024);\r
+    mp3_start_play(Pico_mcd->cdda_stream, pos1024);\r
     return;\r
   }\r
 \r
-  cdda_stream = Pico_mcd->TOC.Tracks[i].F;\r
-  PicoCDBufferFlush(); // buffering relies on fp not being touched\r
-  pm_seek(cdda_stream, lba_offset * 2352, SEEK_SET);\r
-  if (Pico_mcd->TOC.Tracks[i].ftype == TYPE_WAV)\r
+  pm_seek(Pico_mcd->cdda_stream, (lba_base + lba_offset) * 2352, SEEK_SET);\r
+  if (Pico_mcd->cdda_type == CT_WAV)\r
   {\r
     // skip headers, assume it's 44kHz stereo uncompressed\r
-    pm_seek(cdda_stream, 44, SEEK_CUR);\r
+    pm_seek(Pico_mcd->cdda_stream, 44, SEEK_CUR);\r
   }\r
 }\r
 \r
@@ -303,9 +255,7 @@ static int PsndRender(int offset, int length)
   int  buf32_updated = 0;\r
   int *buf32 = PsndBuffer+offset;\r
   int stereo = (PicoOpt & 8) >> 3;\r
-  // emulating CD && PCM option enabled && PCM chip on && have enabled channels\r
-  int do_pcm = (PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_PCM) &&\r
-               (Pico_mcd->pcm.control & 0x80) && Pico_mcd->pcm.enabled;\r
+\r
   offset <<= stereo;\r
 \r
   pprof_start(sound);\r
@@ -340,20 +290,19 @@ static int PsndRender(int offset, int length)
   (void)buf32_updated;\r
 \r
   // CD: PCM sound\r
-  if (do_pcm) {\r
-    pcm_update(buf32, length, stereo);\r
+  if (PicoAHW & PAHW_MCD) {\r
+    pcd_pcm_update(buf32, length, stereo);\r
     //buf32_updated = 1;\r
   }\r
 \r
   // CD: CDDA audio\r
   // CD mode, cdda enabled, not data track, CDC is reading\r
-  if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_CDDA) &&\r
-               !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))\r
+  if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_CDDA)\r
+      && Pico_mcd->cdda_stream != NULL\r
+      && !(Pico_mcd->s68k_regs[0x36] & 1))\r
   {\r
     // note: only 44, 22 and 11 kHz supported, with forced stereo\r
-    int index = Pico_mcd->scd.Cur_Track - 1;\r
-\r
-    if (Pico_mcd->TOC.Tracks[index].ftype == TYPE_MP3)\r
+    if (Pico_mcd->cdda_type == CT_MP3)\r
       mp3_update(buf32, length, stereo);\r
     else\r
       cdda_raw_update(buf32, length);\r