move more globals to PicoInterface
[picodrive.git] / pico / sound / sound.c
index e799e93..fc71b74 100644 (file)
@@ -25,31 +25,20 @@ static unsigned short dac_info[312+4]; // pos in sample buffer
 // cdda output buffer\r
 short cdda_out_buffer[2*1152];\r
 \r
-// for Pico\r
-int PsndRate=0;\r
-int PsndLen=0; // number of mono samples, multiply by 2 for stereo\r
-int PsndLen_exc_add=0; // this is for non-integer sample counts per line, eg. 22050/60\r
-int PsndLen_exc_cnt=0;\r
-int PsndDacLine, PsndPsgLine;\r
-short *PsndOut=NULL; // PCM data buffer\r
-static int PsndLen_use;\r
-\r
-// timers\r
-int timer_a_next_oflow, timer_a_step; // in z80 cycles\r
-int timer_b_next_oflow, timer_b_step;\r
-\r
 // sn76496\r
 extern int *sn76496_regs;\r
 \r
 \r
 static void dac_recalculate(void)\r
 {\r
-  int i, dac_cnt, pos, len, lines = Pico.m.pal ? 313 : 262, mid = Pico.m.pal ? 68 : 93;\r
+  int lines = Pico.m.pal ? 313 : 262;\r
+  int mid = Pico.m.pal ? 68 : 93;\r
+  int i, dac_cnt, pos, len;\r
 \r
-  if (PsndLen <= lines)\r
+  if (Pico.snd.len <= lines)\r
   {\r
     // shrinking algo\r
-    dac_cnt = -PsndLen;\r
+    dac_cnt = -Pico.snd.len;\r
     len=1; pos=0;\r
     dac_info[225] = 1;\r
 \r
@@ -60,14 +49,14 @@ static void dac_recalculate(void)
         pos++;\r
         dac_cnt += lines;\r
       }\r
-      dac_cnt -= PsndLen;\r
+      dac_cnt -= Pico.snd.len;\r
       dac_info[i] = pos;\r
     }\r
   }\r
   else\r
   {\r
     // stretching\r
-    dac_cnt = PsndLen;\r
+    dac_cnt = Pico.snd.len;\r
     pos=0;\r
     for(i = 225; i != 224; i++)\r
     {\r
@@ -78,11 +67,11 @@ static void dac_recalculate(void)
         len++;\r
       }\r
       if (i == mid) // midpoint\r
-        while(pos+len < PsndLen/2) {\r
+        while(pos+len < Pico.snd.len/2) {\r
           dac_cnt -= lines;\r
           len++;\r
         }\r
-      dac_cnt += PsndLen;\r
+      dac_cnt += Pico.snd.len;\r
       pos += len;\r
       dac_info[i] = pos;\r
     }\r
@@ -112,7 +101,7 @@ void PsndRerate(int preserve_state)
     ym2612_pack_state();\r
     memcpy(state, YM2612GetRegs(), 0x204);\r
   }\r
-  YM2612Init(Pico.m.pal ? OSC_PAL/7 : OSC_NTSC/7, PsndRate);\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
@@ -120,16 +109,16 @@ void PsndRerate(int preserve_state)
   }\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, PsndRate);\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
 \r
   if (state)\r
     free(state);\r
 \r
-  // calculate PsndLen\r
-  PsndLen=PsndRate / target_fps;\r
-  PsndLen_exc_add=((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
-  PsndLen_exc_cnt=0;\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
 \r
   // recalculate dac info\r
   dac_recalculate();\r
@@ -137,7 +126,7 @@ void PsndRerate(int preserve_state)
   // clear all buffers\r
   memset32(PsndBuffer, 0, sizeof(PsndBuffer)/4);\r
   memset(cdda_out_buffer, 0, sizeof(cdda_out_buffer));\r
-  if (PsndOut)\r
+  if (PicoIn.sndOut)\r
     PsndClear();\r
 \r
   // set mixer\r
@@ -150,24 +139,24 @@ void PsndRerate(int preserve_state)
 \r
 PICO_INTERNAL void PsndStartFrame(void)\r
 {\r
-  // compensate for float part of PsndLen\r
-  PsndLen_use = PsndLen;\r
-  PsndLen_exc_cnt += PsndLen_exc_add;\r
-  if (PsndLen_exc_cnt >= 0x10000) {\r
-    PsndLen_exc_cnt -= 0x10000;\r
-    PsndLen_use++;\r
+  // compensate for float part of Pico.snd.len\r
+  Pico.snd.len_use = Pico.snd.len;\r
+  Pico.snd.len_e_cnt += Pico.snd.len_e_add;\r
+  if (Pico.snd.len_e_cnt >= 0x10000) {\r
+    Pico.snd.len_e_cnt -= 0x10000;\r
+    Pico.snd.len_use++;\r
   }\r
 \r
-  PsndDacLine = PsndPsgLine = 0;\r
+  Pico.snd.dac_line = Pico.snd.psg_line = 0;\r
   Pico.m.status &= ~1;\r
-  dac_info[224] = PsndLen_use;\r
+  dac_info[224] = Pico.snd.len_use;\r
 }\r
 \r
 PICO_INTERNAL void PsndDoDAC(int line_to)\r
 {\r
   int pos, pos1, len;\r
   int dout = ym2612.dacout;\r
-  int line_from = PsndDacLine;\r
+  int line_from = Pico.snd.dac_line;\r
 \r
   if (line_to >= 313)\r
     line_to = 312;\r
@@ -178,23 +167,23 @@ PICO_INTERNAL void PsndDoDAC(int line_to)
   if (len <= 0)\r
     return;\r
 \r
-  PsndDacLine = line_to + 1;\r
+  Pico.snd.dac_line = line_to + 1;\r
 \r
-  if (!PsndOut)\r
+  if (!PicoIn.sndOut)\r
     return;\r
 \r
   if (PicoIn.opt & POPT_EN_STEREO) {\r
-    short *d = PsndOut + pos*2;\r
+    short *d = PicoIn.sndOut + pos*2;\r
     for (; len > 0; len--, d+=2) *d += dout;\r
   } else {\r
-    short *d = PsndOut + pos;\r
+    short *d = PicoIn.sndOut + pos;\r
     for (; len > 0; len--, d++)  *d += dout;\r
   }\r
 }\r
 \r
 PICO_INTERNAL void PsndDoPSG(int line_to)\r
 {\r
-  int line_from = PsndPsgLine;\r
+  int line_from = Pico.snd.psg_line;\r
   int pos, pos1, len;\r
   int stereo = 0;\r
 \r
@@ -209,16 +198,16 @@ PICO_INTERNAL void PsndDoPSG(int line_to)
   if (len <= 0)\r
     return;\r
 \r
-  PsndPsgLine = line_to + 1;\r
+  Pico.snd.psg_line = line_to + 1;\r
 \r
-  if (!PsndOut || !(PicoIn.opt & POPT_EN_PSG))\r
+  if (!PicoIn.sndOut || !(PicoIn.opt & POPT_EN_PSG))\r
     return;\r
 \r
   if (PicoIn.opt & POPT_EN_STEREO) {\r
     stereo = 1;\r
     pos <<= 1;\r
   }\r
-  SN76496Update(PsndOut + pos, len, stereo);\r
+  SN76496Update(PicoIn.sndOut + pos, len, stereo);\r
 }\r
 \r
 // cdda\r
@@ -227,8 +216,8 @@ static void cdda_raw_update(int *buffer, int length)
   int ret, cdda_bytes, mult = 1;\r
 \r
   cdda_bytes = length*4;\r
-  if (PsndRate <= 22050 + 100) mult = 2;\r
-  if (PsndRate <  22050 - 100) mult = 4;\r
+  if (PicoIn.sndRate <= 22050 + 100) mult = 2;\r
+  if (PicoIn.sndRate <  22050 - 100) mult = 4;\r
   cdda_bytes *= mult;\r
 \r
   ret = pm_read(cdda_out_buffer, cdda_bytes, Pico_mcd->cdda_stream);\r
@@ -270,12 +259,12 @@ void cdda_start_play(int lba_base, int lba_offset, int lb_len)
 \r
 PICO_INTERNAL void PsndClear(void)\r
 {\r
-  int len = PsndLen;\r
-  if (PsndLen_exc_add) len++;\r
+  int len = Pico.snd.len;\r
+  if (Pico.snd.len_e_add) len++;\r
   if (PicoIn.opt & POPT_EN_STEREO)\r
-    memset32((int *) PsndOut, 0, len); // assume PsndOut to be aligned\r
+    memset32((int *) PicoIn.sndOut, 0, len); // assume PicoIn.sndOut to be aligned\r
   else {\r
-    short *out = PsndOut;\r
+    short *out = PicoIn.sndOut;\r
     if ((long)out & 2) { *out++ = 0; len--; }\r
     memset32((int *) out, 0, len/2);\r
     if (len & 1) out[len-1] = 0;\r
@@ -294,7 +283,7 @@ static int PsndRender(int offset, int length)
   pprof_start(sound);\r
 \r
   if (PicoIn.AHW & PAHW_PICO) {\r
-    PicoPicoPCMUpdate(PsndOut+offset, length, stereo);\r
+    PicoPicoPCMUpdate(PicoIn.sndOut+offset, length, stereo);\r
     return length;\r
   }\r
 \r
@@ -330,7 +319,7 @@ static int PsndRender(int offset, int length)
     p32x_pwm_update(buf32, length, stereo);\r
 \r
   // convert + limit to normal 16bit output\r
-  PsndMix_32_to_16l(PsndOut+offset, buf32, length);\r
+  PsndMix_32_to_16l(PicoIn.sndOut+offset, buf32, length);\r
 \r
   pprof_end(sound);\r
 \r
@@ -342,47 +331,47 @@ PICO_INTERNAL void PsndGetSamples(int y)
 {\r
   static int curr_pos = 0;\r
 \r
-  if (ym2612.dacen && PsndDacLine < y)\r
+  if (ym2612.dacen && Pico.snd.dac_line < y)\r
     PsndDoDAC(y - 1);\r
   PsndDoPSG(y - 1);\r
 \r
   if (y == 224)\r
   {\r
     if (Pico.m.status & 2)\r
-         curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2);\r
-    else curr_pos  = PsndRender(0, PsndLen_use);\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 (PicoWriteSound)\r
-      PicoWriteSound(curr_pos * ((PicoIn.opt & POPT_EN_STEREO) ? 4 : 2));\r
+    if (PicoIn.writeSound)\r
+      PicoIn.writeSound(curr_pos * ((PicoIn.opt & POPT_EN_STEREO) ? 4 : 2));\r
     // clear sound buffer\r
     PsndClear();\r
-    PsndDacLine = 224;\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, PsndLen/2);\r
+    curr_pos = PsndRender(0, Pico.snd.len/2);\r
   }\r
 }\r
 \r
 PICO_INTERNAL void PsndGetSamplesMS(void)\r
 {\r
-  int length = PsndLen_use;\r
+  int length = Pico.snd.len_use;\r
 \r
   PsndDoPSG(223);\r
 \r
   // upmix to "stereo" if needed\r
   if (PicoIn.opt & POPT_EN_STEREO) {\r
     int i, *p;\r
-    for (i = length, p = (void *)PsndOut; i > 0; i--, p++)\r
+    for (i = length, p = (void *)PicoIn.sndOut; i > 0; i--, p++)\r
       *p |= *p << 16;\r
   }\r
 \r
-  if (PicoWriteSound != NULL)\r
-    PicoWriteSound(length * ((PicoIn.opt & POPT_EN_STEREO) ? 4 : 2));\r
+  if (PicoIn.writeSound != NULL)\r
+    PicoIn.writeSound(length * ((PicoIn.opt & POPT_EN_STEREO) ? 4 : 2));\r
   PsndClear();\r
 \r
   dac_info[224] = 0;\r