7 int YM2612UpdateReq(int) { return 0; }
\r
12 int PsndRate=0,PsndLen=0;
\r
13 short *PsndOut=NULL;
\r
15 // An operator is a single sine wave generator
\r
18 unsigned short angle; // 0-0xffff
\r
19 unsigned short freq; // Converted frequency
\r
21 unsigned char state; // 1=attack, 2=decay, 3=sustain, 4=release
\r
23 int delta; // Change in volume per sample
\r
24 int limit; // Next volume limit
\r
29 struct Operator op[4]; // 4 operators for the channel
\r
30 unsigned short note; // Written to 0xa4 and 0xa0
\r
33 static struct Channel Chan[8];
\r
35 unsigned char PicoSreg[0x200];
\r
37 static int WriteReg(int side,int a,int d)
\r
39 struct Channel *pc=NULL;
\r
41 PicoSreg[(side<<8)|a]=(unsigned char)d;
\r
47 if (d&0xf0) pc->op[0].state=1; // Attack
\r
48 else pc->op[0].state=4; // Release
\r
54 pc=Chan+(a&3); if (side) pc+=4;
\r
58 int n=0,f=0,mult=2455;
\r
60 if (PsndRate>0) mult=44100*2455/PsndRate;
\r
62 if (a&4) pc->note =(unsigned short)(d<<8);
\r
65 // Convert to an actual frequency:
\r
66 n=pc->note; f=(n&0x7ff)<<((n>>11)&7);
\r
68 pc->op[0].freq=(unsigned short)((f*mult)>>16);
\r
78 memset(&Chan,0,sizeof(Chan));
\r
79 memset(PicoSreg,0,sizeof(PicoSreg));
\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
88 int PsndFm(int a,int d)
\r
93 YM2612Write(0,a&3,(unsigned char)d);
\r
96 a&=3; side=a>>1; // Which side: channels 0-2 or 3-5
\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
104 static void BlankSound(short *dest,int len)
\r
110 // Init sound to silence:
\r
111 do { *dest=0; dest++; } while (dest<end);
\r
114 // Add to output and clip:
\r
115 static inline void AddClip(short *targ,int d)
\r
118 if ((total+0x8000)&0xffff0000)
\r
120 if (total>0) total=0x7fff; else total=-0x8000;
\r
122 *targ=(short)total;
\r
125 static void OpNewState(int c)
\r
127 struct Operator *op=Chan[c].op;
\r
130 off=((c&4)<<6)|(c&3);
\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
144 int d1r=PicoSreg[0x60|off];
\r
145 d1r&=0x1f; if (d1r) d1r+=0x10;
\r
146 op->delta=-(d1r<<5); op->limit=0;
\r
154 d2r=PicoSreg[0x70|off];
\r
155 d2r&=0x1f; if (d2r) d2r+=0x10;
\r
156 rr =PicoSreg[0x80|off];
\r
159 op->delta=-(d2r<<5); op->limit=rr<<20;
\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
171 // Merely adding this code in seems to bugger up the N-Gage???
\r
172 static void UpdateOp(int c)
\r
174 struct Operator *op=Chan[c].op;
\r
176 op->angle=(unsigned short)(op->angle+op->freq);
\r
177 op->vol+=op->delta;
\r
182 if (op->vol>=op->limit) { op->vol=op->limit; op->state++; OpNewState(c); }
\r
184 case 2: case 3: // Decay/Sustain
\r
185 if (op->vol< op->limit) { op->vol=op->limit; op->state++; OpNewState(c); }
\r
188 if (op->vol< op->limit) { op->vol=op->limit; }
\r
193 static void AddChannel(int c,short *dest,int len)
\r
195 struct Channel *pc=Chan+c;
\r
196 struct Operator *op=pc->op;
\r
199 // Work out volume delta for this operator:
\r
206 d=Sine[(op->angle>>8)&0xff]>>2;
\r
208 d*=(op->vol>>8); d>>=16;
\r
223 if (PsndOut==NULL) return 1; // Not rendering sound
\r
228 short *wave[2]={NULL,NULL};
\r
230 wave[1]=PsndOut+PsndLen;
\r
231 YM2612UpdateOne(0,wave,PsndLen);
\r
235 if ((PicoOpt&1)==0)
\r
237 BlankSound(PsndOut,PsndLen);
\r
240 if (i!=3) AddChannel(i,PsndOut,PsndLen);
\r