3 DATATYPE
*buf
= (DATATYPE
*)buffer
;
4 struct SN76496
*R
= &sn
[chip
];
7 /* If the volume is 0, increase the counter */
10 if (R
->Volume
[i
] == 0)
12 /* note that I do count += length, NOT count = length + 1. You might think */
13 /* it's the same since the volume is 0, but doing the latter could cause */
14 /* interferencies when the program is rapidly modulating the volume. */
15 if (R
->Count
[i
] <= length
*STEP
) R
->Count
[i
] += length
*STEP
;
26 /* vol[] keeps track of how long each square wave stays */
27 /* in the 1 position during the sample period. */
28 vol
[0] = vol
[1] = vol
[2] = vol
[3] = 0;
32 if (R
->Output
[i
]) vol
[i
] += R
->Count
[i
];
34 /* Period[i] is the half period of the square wave. Here, in each */
35 /* loop I add Period[i] twice, so that at the end of the loop the */
36 /* square wave is in the same status (0 or 1) it was at the start. */
37 /* vol[i] is also incremented by Period[i], since the wave has been 1 */
38 /* exactly half of the time, regardless of the initial position. */
39 /* If we exit the loop in the middle, Output[i] has to be inverted */
40 /* and vol[i] incremented only if the exit status of the square */
42 while (R
->Count
[i
] <= 0)
44 R
->Count
[i
] += R
->Period
[i
];
48 if (R
->Output
[i
]) vol
[i
] += R
->Period
[i
];
51 R
->Count
[i
] += R
->Period
[i
];
52 vol
[i
] += R
->Period
[i
];
54 if (R
->Output
[i
]) vol
[i
] -= R
->Count
[i
];
63 if (R
->Count
[3] < left
) nextevent
= R
->Count
[3];
64 else nextevent
= left
;
66 if (R
->Output
[3]) vol
[3] += R
->Count
[3];
67 R
->Count
[3] -= nextevent
;
70 if (R
->RNG
& 1) R
->RNG
^= R
->NoiseFB
;
72 R
->Output
[3] = R
->RNG
& 1;
73 R
->Count
[3] += R
->Period
[3];
74 if (R
->Output
[3]) vol
[3] += R
->Period
[3];
76 if (R
->Output
[3]) vol
[3] -= R
->Count
[3];
81 out
= vol
[0] * R
->Volume
[0] + vol
[1] * R
->Volume
[1] +
82 vol
[2] * R
->Volume
[2] + vol
[3] * R
->Volume
[3];
84 if (out
> MAX_OUTPUT
* STEP
) out
= MAX_OUTPUT
* STEP
;
86 *(buf
++) = ((DATACONV(out
)*3)>>3); // Dave: a bit quieter