sound code rewrite for lower sample rates (except mp3s)
[picodrive.git] / Pico / Pico.c
index 1f201f4..3cee0c2 100644 (file)
@@ -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\r
 int PicoAutoRgnOrder = 0;\r
 int emustatus = 0;\r
-void (*PicoWriteSound)(void) = 0; // called once per frame at the best time to send sound buffer (PsndOut) to hardware\r
+void (*PicoWriteSound)(int len) = 0; // called once per frame at the best time to send sound buffer (PsndOut) to hardware\r
 \r
 struct PicoSRAM SRam;\r
 int z80startCycle = 0, z80stopCycle = 0; // in 68k cycles\r
@@ -49,7 +49,8 @@ int PicoInit(void)
 // to be called once on emu exit\r
 void PicoExit(void)\r
 {\r
-  PicoExitMCD();\r
+  if (PicoMCD&1)\r
+    PicoExitMCD();\r
   z80_exit();\r
 \r
   // notaz: sram\r
@@ -314,20 +315,22 @@ static int CheckIdle(void)
 // to be called on 224 or line_sample scanlines only\r
 static __inline void getSamples(int y)\r
 {\r
+  static int curr_pos = 0;\r
+\r
   if(y == 224) {\r
     //dprintf("sta%i: %i [%i]", (emustatus & 2), emustatus, y);\r
     if(emustatus & 2)\r
-        sound_render(PsndLen/2, PsndLen-PsndLen/2);\r
-    else sound_render(0, PsndLen);\r
+         curr_pos += sound_render(curr_pos, PsndLen-PsndLen/2);\r
+    else curr_pos  = sound_render(0, PsndLen);\r
     if (emustatus&1) emustatus|=2; else emustatus&=~2;\r
-    if (PicoWriteSound) PicoWriteSound();\r
+    if (PicoWriteSound) PicoWriteSound(curr_pos);\r
     // clear sound buffer\r
-    memset(PsndOut, 0, (PicoOpt & 8) ? (PsndLen<<2) : (PsndLen<<1));\r
+    sound_clear();\r
   }\r
   else if(emustatus & 3) {\r
     emustatus|= 2;\r
     emustatus&=~1;\r
-    sound_render(0, PsndLen/2);\r
+    curr_pos = sound_render(0, PsndLen/2);\r
   }\r
 }\r
 \r
@@ -536,10 +539,10 @@ static int PicoFrameSimple(void)
 \r
   // here we render sound if ym2612 is disabled\r
   if(!(PicoOpt&1) && PsndOut) {\r
-    sound_render(0, PsndLen);\r
-    if(PicoWriteSound) PicoWriteSound();\r
+    int len = sound_render(0, PsndLen);\r
+    if(PicoWriteSound) PicoWriteSound(len);\r
     // clear sound buffer\r
-    memset(PsndOut, 0, (PicoOpt & 8) ? (PsndLen<<2) : (PsndLen<<1));\r
+    sound_clear();\r
   }\r
 \r
   // render screen\r