added copyright line to top of source files next to license information
[cyclone68000.git] / Pico / Psnd.cpp
1 \r
2 // This file is part of the PicoDrive Megadrive Emulator\r
3 \r
4 // Copyright (c) 2011 FinalDave (emudave (at) gmail.com)\r
5 \r
6 // This code is licensed under the GNU General Public License version 2.0 and the MAME License.\r
7 // You can choose the license that has the most advantages for you.\r
8 \r
9 // SVN repository can be found at http://code.google.com/p/cyclone68000/\r
10 \r
11 #include "PicoInt.h"\r
12 \r
13 #ifdef MSOUND\r
14 extern "C"\r
15 {\r
16 int YM2612UpdateReq(int) { return 0; }\r
17 void *errorlog=NULL;\r
18 }\r
19 #endif\r
20 \r
21 int PsndRate=0,PsndLen=0;\r
22 short *PsndOut=NULL;\r
23 \r
24 // An operator is a single sine wave generator\r
25 struct Operator\r
26 {\r
27   unsigned short angle; // 0-0xffff\r
28   unsigned short freq; // Converted frequency\r
29   unsigned char key;\r
30   unsigned char state; // 1=attack, 2=decay, 3=sustain, 4=release\r
31   int vol;\r
32   int delta; // Change in volume per sample\r
33   int limit; // Next volume limit\r
34 };\r
35 \r
36 struct Channel\r
37 {\r
38   struct Operator op[4]; // 4 operators for the channel\r
39   unsigned short note; // Written to 0xa4 and 0xa0\r
40 };\r
41 \r
42 static struct Channel Chan[8];\r
43 \r
44 unsigned char PicoSreg[0x200];\r
45 \r
46 static int WriteReg(int side,int a,int d)\r
47 {\r
48   struct Channel *pc=NULL;\r
49 \r
50   PicoSreg[(side<<8)|a]=(unsigned char)d;\r
51 \r
52   if (a==0x28)\r
53   {\r
54     pc=Chan+(d&7);\r
55     // Key On/Key Off\r
56     if (d&0xf0) pc->op[0].state=1; // Attack\r
57     else        pc->op[0].state=4; // Release\r
58 \r
59     return 0;\r
60   }\r
61 \r
62   // Find channel:\r
63   pc=Chan+(a&3); if (side) pc+=4;\r
64 \r
65   if ((a&0xf0)==0xa0)\r
66   {\r
67     int n=0,f=0,mult=2455;\r
68 \r
69     if (PsndRate>0) mult=44100*2455/PsndRate;\r
70 \r
71     if (a&4) pc->note =(unsigned short)(d<<8);\r
72     else     pc->note|=d;\r
73 \r
74     // Convert to an actual frequency:\r
75     n=pc->note; f=(n&0x7ff)<<((n>>11)&7);\r
76 \r
77     pc->op[0].freq=(unsigned short)((f*mult)>>16);\r
78     return 0;\r
79   }\r
80 \r
81   return 0;\r
82 }\r
83 \r
84 int PsndReset()\r
85 {\r
86   int i=0;\r
87   memset(&Chan,0,sizeof(Chan));\r
88   memset(PicoSreg,0,sizeof(PicoSreg));\r
89 \r
90 // Change Sine wave into a square wave\r
91   for (i=0x00; i<0x080; i++) Sine[i]= 0x2000;\r
92   for (i=0x80; i<0x100; i++) Sine[i]=-0x2000;\r
93 \r
94   return 0;\r
95 }\r
96 \r
97 int PsndFm(int a,int d)\r
98 {\r
99   int side=0;\r
100 \r
101 #ifdef MSOUND\r
102   YM2612Write(0,a&3,(unsigned char)d);\r
103 #endif\r
104 \r
105   a&=3; side=a>>1; // Which side: channels 0-2 or 3-5\r
106 \r
107   if (a&1) WriteReg(side,Pico.s.fmsel[side],d); // Write register\r
108   else     Pico.s.fmsel[side]=(unsigned char)d; // Select register\r
109 \r
110   return 0;\r
111 }\r
112 \r
113 static void BlankSound(short *dest,int len)\r
114 {\r
115   short *end=NULL;\r
116 \r
117   end=dest+(len<<1);\r
118 \r
119   // Init sound to silence:\r
120   do { *dest=0; dest++; } while (dest<end);\r
121 }\r
122 \r
123 // Add to output and clip:\r
124 static inline void AddClip(short *targ,int d)\r
125 {\r
126   int total=*targ+d;\r
127   if ((total+0x8000)&0xffff0000)\r
128   {\r
129     if (total>0) total=0x7fff; else total=-0x8000;\r
130   }\r
131   *targ=(short)total;\r
132 }\r
133 \r
134 static void OpNewState(int c)\r
135 {\r
136   struct Operator *op=Chan[c].op;\r
137   int off=0;\r
138 \r
139   off=((c&4)<<6)|(c&3);\r
140 \r
141   switch (op->state)\r
142   {\r
143     case 1:\r
144     {\r
145       // Attack:\r
146       int ar=PicoSreg[0x50|off];\r
147       ar&=0x1f; if (ar) ar+=0x10;\r
148       op->delta=ar<<7; op->limit=0x1000000; break;\r
149     }\r
150     case 2:\r
151     {\r
152       // Decay:\r
153       int d1r=PicoSreg[0x60|off];\r
154       d1r&=0x1f; if (d1r) d1r+=0x10;\r
155       op->delta=-(d1r<<5); op->limit=0;\r
156     }\r
157     break;\r
158     case 3:\r
159     {\r
160       // Sustain:\r
161       int d2r=0,rr=0;\r
162       \r
163       d2r=PicoSreg[0x70|off];\r
164       d2r&=0x1f; if (d2r) d2r+=0x10;\r
165       rr =PicoSreg[0x80|off];\r
166       rr>>=4;\r
167 \r
168       op->delta=-(d2r<<5); op->limit=rr<<20;\r
169     }\r
170     break;\r
171     case 4:\r
172       // Release:\r
173       int rr=PicoSreg[0x80|off];\r
174       rr&=0x0f; rr<<=1; rr+=0x10;\r
175       op->delta=-(rr<<5); op->limit=0;\r
176     break;\r
177   }\r
178 }\r
179 \r
180 // Merely adding this code in seems to bugger up the N-Gage???\r
181 static void UpdateOp(int c)\r
182 {\r
183   struct Operator *op=Chan[c].op;\r
184 \r
185   op->angle=(unsigned short)(op->angle+op->freq);\r
186   op->vol+=op->delta;\r
187 \r
188   switch (op->state)\r
189   {\r
190     case 1:\r
191       if (op->vol>=op->limit) { op->vol=op->limit; op->state++; OpNewState(c); }\r
192     break;\r
193     case 2: case 3: // Decay/Sustain\r
194       if (op->vol< op->limit) { op->vol=op->limit; op->state++; OpNewState(c); }\r
195     break;\r
196     case 4:\r
197       if (op->vol< op->limit) { op->vol=op->limit; }\r
198     break;\r
199   }\r
200 }\r
201 \r
202 static void AddChannel(int c,short *dest,int len)\r
203 {\r
204   struct Channel *pc=Chan+c;\r
205   struct Operator *op=pc->op;\r
206   short *end=NULL;\r
207 \r
208   // Work out volume delta for this operator:\r
209   OpNewState(c);\r
210 \r
211   end=dest+len;\r
212   do\r
213   {\r
214     int d=0;\r
215     d=Sine[(op->angle>>8)&0xff]>>2;\r
216 \r
217     d*=(op->vol>>8); d>>=16;\r
218 \r
219     // Add to output:\r
220     AddClip(dest,d);\r
221     UpdateOp(c);\r
222 \r
223     dest++;\r
224   }\r
225   while (dest<end);\r
226 }\r
227 \r
228 int PsndRender()\r
229 {\r
230   int i=0;\r
231 \r
232   if (PsndOut==NULL) return 1; // Not rendering sound\r
233 \r
234 #ifdef MSOUND\r
235   if (PicoOpt&1)\r
236   {\r
237     short *wave[2]={NULL,NULL};\r
238     wave[0]=PsndOut;\r
239     wave[1]=PsndOut+PsndLen;\r
240     YM2612UpdateOne(0,wave,PsndLen);\r
241   }\r
242 #endif\r
243 \r
244   if ((PicoOpt&1)==0)\r
245   {\r
246     BlankSound(PsndOut,PsndLen);\r
247     for (i=0;i<7;i++)\r
248     {\r
249       if (i!=3) AddChannel(i,PsndOut,PsndLen);\r
250     }\r
251   }\r
252 \r
253   return 0;\r
254 }\r