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