Added 0.030 of PicoDrive and moved license files into root
[cyclone68000.git] / Pico / Psnd.cpp
diff --git a/Pico/Psnd.cpp b/Pico/Psnd.cpp
new file mode 100644 (file)
index 0000000..235f7da
--- /dev/null
@@ -0,0 +1,245 @@
+\r
+#include "PicoInt.h"\r
+\r
+#ifdef MSOUND\r
+extern "C"\r
+{\r
+int YM2612UpdateReq(int) { return 0; }\r
+void *errorlog=NULL;\r
+}\r
+#endif\r
+\r
+int PsndRate=0,PsndLen=0;\r
+short *PsndOut=NULL;\r
+\r
+// An operator is a single sine wave generator\r
+struct Operator\r
+{\r
+  unsigned short angle; // 0-0xffff\r
+  unsigned short freq; // Converted frequency\r
+  unsigned char key;\r
+  unsigned char state; // 1=attack, 2=decay, 3=sustain, 4=release\r
+  int vol;\r
+  int delta; // Change in volume per sample\r
+  int limit; // Next volume limit\r
+};\r
+\r
+struct Channel\r
+{\r
+  struct Operator op[4]; // 4 operators for the channel\r
+  unsigned short note; // Written to 0xa4 and 0xa0\r
+};\r
+\r
+static struct Channel Chan[8];\r
+\r
+unsigned char PicoSreg[0x200];\r
+\r
+static int WriteReg(int side,int a,int d)\r
+{\r
+  struct Channel *pc=NULL;\r
+\r
+  PicoSreg[(side<<8)|a]=(unsigned char)d;\r
+\r
+  if (a==0x28)\r
+  {\r
+    pc=Chan+(d&7);\r
+    // Key On/Key Off\r
+    if (d&0xf0) pc->op[0].state=1; // Attack\r
+    else        pc->op[0].state=4; // Release\r
+\r
+    return 0;\r
+  }\r
+\r
+  // Find channel:\r
+  pc=Chan+(a&3); if (side) pc+=4;\r
+\r
+  if ((a&0xf0)==0xa0)\r
+  {\r
+    int n=0,f=0,mult=2455;\r
+\r
+    if (PsndRate>0) mult=44100*2455/PsndRate;\r
+\r
+    if (a&4) pc->note =(unsigned short)(d<<8);\r
+    else     pc->note|=d;\r
+\r
+    // Convert to an actual frequency:\r
+    n=pc->note; f=(n&0x7ff)<<((n>>11)&7);\r
+\r
+    pc->op[0].freq=(unsigned short)((f*mult)>>16);\r
+    return 0;\r
+  }\r
+\r
+  return 0;\r
+}\r
+\r
+int PsndReset()\r
+{\r
+  int i=0;\r
+  memset(&Chan,0,sizeof(Chan));\r
+  memset(PicoSreg,0,sizeof(PicoSreg));\r
+\r
+// Change Sine wave into a square wave\r
+  for (i=0x00; i<0x080; i++) Sine[i]= 0x2000;\r
+  for (i=0x80; i<0x100; i++) Sine[i]=-0x2000;\r
+\r
+  return 0;\r
+}\r
+\r
+int PsndFm(int a,int d)\r
+{\r
+  int side=0;\r
+\r
+#ifdef MSOUND\r
+  YM2612Write(0,a&3,(unsigned char)d);\r
+#endif\r
+\r
+  a&=3; side=a>>1; // Which side: channels 0-2 or 3-5\r
+\r
+  if (a&1) WriteReg(side,Pico.s.fmsel[side],d); // Write register\r
+  else     Pico.s.fmsel[side]=(unsigned char)d; // Select register\r
+\r
+  return 0;\r
+}\r
+\r
+static void BlankSound(short *dest,int len)\r
+{\r
+  short *end=NULL;\r
+\r
+  end=dest+(len<<1);\r
+\r
+  // Init sound to silence:\r
+  do { *dest=0; dest++; } while (dest<end);\r
+}\r
+\r
+// Add to output and clip:\r
+static inline void AddClip(short *targ,int d)\r
+{\r
+  int total=*targ+d;\r
+  if ((total+0x8000)&0xffff0000)\r
+  {\r
+    if (total>0) total=0x7fff; else total=-0x8000;\r
+  }\r
+  *targ=(short)total;\r
+}\r
+\r
+static void OpNewState(int c)\r
+{\r
+  struct Operator *op=Chan[c].op;\r
+  int off=0;\r
+\r
+  off=((c&4)<<6)|(c&3);\r
+\r
+  switch (op->state)\r
+  {\r
+    case 1:\r
+    {\r
+      // Attack:\r
+      int ar=PicoSreg[0x50|off];\r
+      ar&=0x1f; if (ar) ar+=0x10;\r
+      op->delta=ar<<7; op->limit=0x1000000; break;\r
+    }\r
+    case 2:\r
+    {\r
+      // Decay:\r
+      int d1r=PicoSreg[0x60|off];\r
+      d1r&=0x1f; if (d1r) d1r+=0x10;\r
+      op->delta=-(d1r<<5); op->limit=0;\r
+    }\r
+    break;\r
+    case 3:\r
+    {\r
+      // Sustain:\r
+      int d2r=0,rr=0;\r
+      \r
+      d2r=PicoSreg[0x70|off];\r
+      d2r&=0x1f; if (d2r) d2r+=0x10;\r
+      rr =PicoSreg[0x80|off];\r
+      rr>>=4;\r
+\r
+      op->delta=-(d2r<<5); op->limit=rr<<20;\r
+    }\r
+    break;\r
+    case 4:\r
+      // Release:\r
+      int rr=PicoSreg[0x80|off];\r
+      rr&=0x0f; rr<<=1; rr+=0x10;\r
+      op->delta=-(rr<<5); op->limit=0;\r
+    break;\r
+  }\r
+}\r
+\r
+// Merely adding this code in seems to bugger up the N-Gage???\r
+static void UpdateOp(int c)\r
+{\r
+  struct Operator *op=Chan[c].op;\r
+\r
+  op->angle=(unsigned short)(op->angle+op->freq);\r
+  op->vol+=op->delta;\r
+\r
+  switch (op->state)\r
+  {\r
+    case 1:\r
+      if (op->vol>=op->limit) { op->vol=op->limit; op->state++; OpNewState(c); }\r
+    break;\r
+    case 2: case 3: // Decay/Sustain\r
+      if (op->vol< op->limit) { op->vol=op->limit; op->state++; OpNewState(c); }\r
+    break;\r
+    case 4:\r
+      if (op->vol< op->limit) { op->vol=op->limit; }\r
+    break;\r
+  }\r
+}\r
+\r
+static void AddChannel(int c,short *dest,int len)\r
+{\r
+  struct Channel *pc=Chan+c;\r
+  struct Operator *op=pc->op;\r
+  short *end=NULL;\r
+\r
+  // Work out volume delta for this operator:\r
+  OpNewState(c);\r
+\r
+  end=dest+len;\r
+  do\r
+  {\r
+    int d=0;\r
+    d=Sine[(op->angle>>8)&0xff]>>2;\r
+\r
+    d*=(op->vol>>8); d>>=16;\r
+\r
+    // Add to output:\r
+    AddClip(dest,d);\r
+    UpdateOp(c);\r
+\r
+    dest++;\r
+  }\r
+  while (dest<end);\r
+}\r
+\r
+int PsndRender()\r
+{\r
+  int i=0;\r
+\r
+  if (PsndOut==NULL) return 1; // Not rendering sound\r
+\r
+#ifdef MSOUND\r
+  if (PicoOpt&1)\r
+  {\r
+    short *wave[2]={NULL,NULL};\r
+    wave[0]=PsndOut;\r
+    wave[1]=PsndOut+PsndLen;\r
+    YM2612UpdateOne(0,wave,PsndLen);\r
+  }\r
+#endif\r
+\r
+  if ((PicoOpt&1)==0)\r
+  {\r
+    BlankSound(PsndOut,PsndLen);\r
+    for (i=0;i<7;i++)\r
+    {\r
+      if (i!=3) AddChannel(i,PsndOut,PsndLen);\r
+    }\r
+  }\r
+\r
+  return 0;\r
+}\r