Debian Codegraph @

src/engine/platform/sound/pokey/mzpokeysnd.c

raw
1 /*
2 * mzpokeysnd.c - POKEY sound chip emulation, v1.6
3 *
4 * Copyright (C) 2002 Michael Borisov
5 * Copyright (C) 2002-2014 Atari800 development team (see DOC/CREDITS)
6 *
7 * This file is part of the Atari800 emulator project which emulates
8 * the Atari 400, 800, 800XL, 130XE, and 5200 8-bit computers.
9 *
10 * Atari800 is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * Atari800 is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Atari800; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 // additional modifications for Furnace by tildearrow
26
27 #define _USE_MATH_DEFINES
28 #include <stdlib.h>
29 #include <math.h>
30
31 #include "pokey.h"
32 #include "mzpokeysnd.h"
33
34 #define CONSOLE_VOL 8
35 #ifdef NONLINEAR_MIXING
36 static const double pokeymix[61+CONSOLE_VOL] = { /* Nonlinear POKEY mixing array */
37 0.000000, 5.169146, 10.157015, 15.166247,
38 20.073793, 24.927443, 29.728237, 34.495266,
39 39.181262, 43.839780, 48.429508, 52.932530,
40 57.327319, 61.586304, 65.673220, 69.547672,
41 73.207846, 76.594474, 79.739231, 82.631161,
42 85.300361, 87.750638, 90.020656, 92.108334,
43 94.051256, 95.848478, 97.521287, 99.080719,
44 100.540674, 101.902750, 103.185339, 104.375596,
45 105.491149, 106.523735, 107.473511, 108.361458,
46 109.185669, 109.962251, 110.685574, 111.367150,
47 112.008476, 112.612760, 113.185603, 113.722735,
48 114.227904, 114.712206, 115.171007, 115.605730,
49 116.024396, 116.416097, 116.803169, 117.155108,
50 117.532921, 117.835494, 118.196180, 118.502785,
51 118.825177, 119.138170, 119.421378, 119.734493,
52 /* need to add CONSOLE_VOL extra copies of the last val */
53 120.000000,120.0,120.0,120.0,120.0,120.0,120.0,120.0,120.0};
54 #endif
55
56 /* Poly tables */
57 static int poly4tbl[15];
58 static int poly5tbl[31];
59 static unsigned char poly17tbl[131071];
60 static int poly9tbl[511];
61
62 /* Forward declarations for ResetPokeyState */
63
64 int readout0_normal(PokeyState* ps);
65 void event0_pure(PokeyState* ps, int p5v, int p4v, int p917v);
66
67 int readout1_normal(PokeyState* ps);
68 void event1_pure(PokeyState* ps, int p5v, int p4v, int p917v);
69
70 int readout2_normal(PokeyState* ps);
71 void event2_pure(PokeyState* ps, int p5v, int p4v, int p917v);
72
73 int readout3_normal(PokeyState* ps);
74 void event3_pure(PokeyState* ps, int p5v, int p4v, int p917v);
75
76 void ResetPokeyState(PokeyState* ps)
77 {
78 /* Poly positions */
79 ps->poly4pos = 0;
80 ps->poly5pos = 0;
81 ps->poly9pos = 0;
82 ps->poly17pos = 0;
83
84 /* Global Pokey controls */
85 ps->mdivk = 28;
86
87 ps->selpoly9 = 0;
88 ps->c0_hf = 0;
89 ps->c1_f0 = 0;
90 ps->c2_hf = 0;
91 ps->c3_f2 = 0;
92
93 /* SKCTL for two-tone mode */
94 ps->skctl = 0;
95
96 ps->outvol_all = 0;
97 ps->forcero = 0;
98
99 /* Channel 0 state */
100 ps->readout_0 = readout0_normal;
101 ps->event_0 = event0_pure;
102
103 ps->c0divpos = 1000;
104 ps->c0divstart = 1000;
105 ps->c0divstart_p = 1000;
106 ps->c0diva = 255;
107
108 ps->c0t1 = 0;
109 ps->c0t2 = 0;
110 ps->c0t3 = 0;
111
112 ps->c0sw1 = 0;
113 ps->c0sw2 = 0;
114 ps->c0sw3 = 0;
115 ps->c0sw4 = 0;
116 ps->c0vo = 1;
117
118 #ifndef NONLINEAR_MIXING
119 ps->c0stop = 1;
120 #endif
121
122 ps->vol0 = 0;
123
124 ps->outvol_0 = 0;
125
126
127 /* Channel 1 state */
128 ps->readout_1 = readout1_normal;
129 ps->event_1 = event1_pure;
130
131 ps->c1divpos = 1000;
132 ps->c1divstart = 1000;
133 ps->c1diva = 255;
134
135 ps->c1t1 = 0;
136 ps->c1t2 = 0;
137 ps->c1t3 = 0;
138
139 ps->c1sw1 = 0;
140 ps->c1sw2 = 0;
141 ps->c1sw3 = 0;
142 ps->c1sw4 = 0;
143 ps->c1vo = 1;
144
145 #ifndef NONLINEAR_MIXING
146 ps->c1stop = 1;
147 #endif
148
149 ps->vol1 = 0;
150
151 ps->outvol_1 = 0;
152
153 /* Channel 2 state */
154 ps->readout_2 = readout2_normal;
155 ps->event_2 = event2_pure;
156
157 ps->c2divpos = 1000;
158 ps->c2divstart = 1000;
159 ps->c2divstart_p = 1000;
160 ps->c2diva = 255;
161
162 ps->c2t1 = 0;
163 ps->c2t2 = 0;
164
165 ps->c2sw1 = 0;
166 ps->c2sw2 = 0;
167 ps->c2sw3 = 0;
168
169 ps->c2vo = 0;
170
171 #ifndef NONLINEAR_MIXING
172 ps->c2stop = 1;
173 #endif
174
175 ps->vol2 = 0;
176
177 ps->outvol_2 = 0;
178
179 /* Channel 3 state */
180 ps->readout_3 = readout3_normal;
181 ps->event_3 = event3_pure;
182
183 ps->c3divpos = 1000;
184 ps->c3divstart = 1000;
185 ps->c3diva = 255;
186
187 ps->c3t1 = 0;
188 ps->c3t2 = 0;
189
190 ps->c3sw1 = 0;
191 ps->c3sw2 = 0;
192 ps->c3sw3 = 0;
193
194 ps->c3vo = 0;
195
196 #ifndef NONLINEAR_MIXING
197 ps->c3stop = 1;
198 #endif
199
200 ps->vol3 = 0;
201
202 ps->outvol_3 = 0;
203 }
204
205 static void build_poly4(void)
206 {
207 unsigned char c;
208 unsigned char i;
209 unsigned char poly4=1;
210
211 for(i=0; i<15; i++)
212 {
213 poly4tbl[i] = ~poly4;
214 c = ((poly4>>2)&1) ^ ((poly4>>3)&1);
215 poly4 = ((poly4<<1)&15) + c;
216 }
217 }
218
219 static void build_poly5(void)
220 {
221 unsigned char c;
222 unsigned char i;
223 unsigned char poly5 = 1;
224
225 for(i = 0; i < 31; i++) {
226 poly5tbl[i] = ~poly5; /* Inversion! Attention! */
227 c = ((poly5 >> 2) ^ (poly5 >> 4)) & 1;
228 poly5 = ((poly5 << 1) & 31) + c;
229 }
230 }
231
232 static void build_poly17(void)
233 {
234 unsigned int c;
235 unsigned int i;
236 unsigned int poly17 = 1;
237
238 for(i = 0; i < 131071; i++) {
239 poly17tbl[i] = (unsigned char) poly17;
240 c = ((poly17 >> 11) ^ (poly17 >> 16)) & 1;
241 poly17 = ((poly17 << 1) & 131071) + c;
242 }
243 }
244
245 static void build_poly9(void)
246 {
247 unsigned int c;
248 unsigned int i;
249 unsigned int poly9 = 1;
250
251 for(i = 0; i < 511; i++) {
252 poly9tbl[i] = (unsigned char) poly9;
253 c = ((poly9 >> 3) ^ (poly9 >> 8)) & 1;
254 poly9 = ((poly9 << 1) & 511) + c;
255 }
256 }
257
258 void advance_polies(PokeyState* ps, int tacts)
259 {
260 ps->poly4pos = (tacts + ps->poly4pos) % 15;
261 ps->poly5pos = (tacts + ps->poly5pos) % 31;
262 ps->poly17pos = (tacts + ps->poly17pos) % 131071;
263 ps->poly9pos = (tacts + ps->poly9pos) % 511;
264 }
265
266 /***********************************
267
268 READ OUTPUT 0
269
270 ************************************/
271
272 int readout0_vo(PokeyState* ps)
273 {
274 return ps->vol0;
275 }
276
277 int readout0_hipass(PokeyState* ps)
278 {
279 if(ps->c0t2 ^ ps->c0t3)
280 return ps->vol0;
281 else return 0;
282 }
283
284 int readout0_normal(PokeyState* ps)
285 {
286 if(ps->c0t2)
287 return ps->vol0;
288 else return 0;
289 }
290
291 /***********************************
292
293 READ OUTPUT 1
294
295 ************************************/
296
297 int readout1_vo(PokeyState* ps)
298 {
299 return ps->vol1;
300 }
301
302 int readout1_hipass(PokeyState* ps)
303 {
304 if(ps->c1t2 ^ ps->c1t3)
305 return ps->vol1;
306 else return 0;
307 }
308
309 int readout1_normal(PokeyState* ps)
310 {
311 if(ps->c1t2)
312 return ps->vol1;
313 else return 0;
314 }
315
316 /***********************************
317
318 READ OUTPUT 2
319
320 ************************************/
321
322 int readout2_vo(PokeyState* ps)
323 {
324 return ps->vol2;
325 }
326
327 int readout2_normal(PokeyState* ps)
328 {
329 if(ps->c2t2)
330 return ps->vol2;
331 else return 0;
332 }
333
334 /***********************************
335
336 READ OUTPUT 3
337
338 ************************************/
339
340 int readout3_vo(PokeyState* ps)
341 {
342 return ps->vol3;
343 }
344
345 int readout3_normal(PokeyState* ps)
346 {
347 if(ps->c3t2)
348 return ps->vol3;
349 else return 0;
350 }
351
352
353 /***********************************
354
355 EVENT CHANNEL 0
356
357 ************************************/
358
359 void event0_pure(PokeyState* ps, int p5v, int p4v, int p917v)
360 {
361 ps->c0t2 = !ps->c0t2;
362 ps->c0t1 = p5v;
363 }
364
365 void event0_p5(PokeyState* ps, int p5v, int p4v, int p917v)
366 {
367 if(ps->c0t1)
368 ps->c0t2 = !ps->c0t2;
369 ps->c0t1 = p5v;
370 }
371
372 void event0_p4(PokeyState* ps, int p5v, int p4v, int p917v)
373 {
374 ps->c0t2 = p4v;
375 ps->c0t1 = p5v;
376 }
377
378 void event0_p917(PokeyState* ps, int p5v, int p4v, int p917v)
379 {
380 ps->c0t2 = p917v;
381 ps->c0t1 = p5v;
382 }
383
384 void event0_p4_p5(PokeyState* ps, int p5v, int p4v, int p917v)
385 {
386 if(ps->c0t1)
387 ps->c0t2 = p4v;
388 ps->c0t1 = p5v;
389 }
390
391 void event0_p917_p5(PokeyState* ps, int p5v, int p4v, int p917v)
392 {
393 if(ps->c0t1)
394 ps->c0t2 = p917v;
395 ps->c0t1 = p5v;
396 }
397
398 /***********************************
399
400 EVENT CHANNEL 1
401
402 ************************************/
403
404 void event1_pure(PokeyState* ps, int p5v, int p4v, int p917v)
405 {
406 ps->c1t2 = !ps->c1t2;
407 ps->c1t1 = p5v;
408 }
409
410 void event1_p5(PokeyState* ps, int p5v, int p4v, int p917v)
411 {
412 if(ps->c1t1)
413 ps->c1t2 = !ps->c1t2;
414 ps->c1t1 = p5v;
415 }
416
417 void event1_p4(PokeyState* ps, int p5v, int p4v, int p917v)
418 {
419 ps->c1t2 = p4v;
420 ps->c1t1 = p5v;
421 }
422
423 void event1_p917(PokeyState* ps, int p5v, int p4v, int p917v)
424 {
425 ps->c1t2 = p917v;
426 ps->c1t1 = p5v;
427 }
428
429 void event1_p4_p5(PokeyState* ps, int p5v, int p4v, int p917v)
430 {
431 if(ps->c1t1)
432 ps->c1t2 = p4v;
433 ps->c1t1 = p5v;
434 }
435
436 void event1_p917_p5(PokeyState* ps, int p5v, int p4v, int p917v)
437 {
438 if(ps->c1t1)
439 ps->c1t2 = p917v;
440 ps->c1t1 = p5v;
441 }
442
443 /***********************************
444
445 EVENT CHANNEL 2
446
447 ************************************/
448
449 void event2_pure(PokeyState* ps, int p5v, int p4v, int p917v)
450 {
451 ps->c2t2 = !ps->c2t2;
452 ps->c2t1 = p5v;
453 /* high-pass clock for channel 0 */
454 ps->c0t3 = ps->c0t2;
455 }
456
457 void event2_p5(PokeyState* ps, int p5v, int p4v, int p917v)
458 {
459 if(ps->c2t1)
460 ps->c2t2 = !ps->c2t2;
461 ps->c2t1 = p5v;
462 /* high-pass clock for channel 0 */
463 ps->c0t3 = ps->c0t2;
464 }
465
466 void event2_p4(PokeyState* ps, int p5v, int p4v, int p917v)
467 {
468 ps->c2t2 = p4v;
469 ps->c2t1 = p5v;
470 /* high-pass clock for channel 0 */
471 ps->c0t3 = ps->c0t2;
472 }
473
474 void event2_p917(PokeyState* ps, int p5v, int p4v, int p917v)
475 {
476 ps->c2t2 = p917v;
477 ps->c2t1 = p5v;
478 /* high-pass clock for channel 0 */
479 ps->c0t3 = ps->c0t2;
480 }
481
482 void event2_p4_p5(PokeyState* ps, int p5v, int p4v, int p917v)
483 {
484 if(ps->c2t1)
485 ps->c2t2 = p4v;
486 ps->c2t1 = p5v;
487 /* high-pass clock for channel 0 */
488 ps->c0t3 = ps->c0t2;
489 }
490
491 void event2_p917_p5(PokeyState* ps, int p5v, int p4v, int p917v)
492 {
493 if(ps->c2t1)
494 ps->c2t2 = p917v;
495 ps->c2t1 = p5v;
496 /* high-pass clock for channel 0 */
497 ps->c0t3 = ps->c0t2;
498 }
499
500 /***********************************
501
502 EVENT CHANNEL 3
503
504 ************************************/
505
506 void event3_pure(PokeyState* ps, int p5v, int p4v, int p917v)
507 {
508 ps->c3t2 = !ps->c3t2;
509 ps->c3t1 = p5v;
510 /* high-pass clock for channel 1 */
511 ps->c1t3 = ps->c1t2;
512 }
513
514 void event3_p5(PokeyState* ps, int p5v, int p4v, int p917v)
515 {
516 if(ps->c3t1)
517 ps->c3t2 = !ps->c3t2;
518 ps->c3t1 = p5v;
519 /* high-pass clock for channel 1 */
520 ps->c1t3 = ps->c1t2;
521 }
522
523 void event3_p4(PokeyState* ps, int p5v, int p4v, int p917v)
524 {
525 ps->c3t2 = p4v;
526 ps->c3t1 = p5v;
527 /* high-pass clock for channel 1 */
528 ps->c1t3 = ps->c1t2;
529 }
530
531 void event3_p917(PokeyState* ps, int p5v, int p4v, int p917v)
532 {
533 ps->c3t2 = p917v;
534 ps->c3t1 = p5v;
535 /* high-pass clock for channel 1 */
536 ps->c1t3 = ps->c1t2;
537 }
538
539 void event3_p4_p5(PokeyState* ps, int p5v, int p4v, int p917v)
540 {
541 if(ps->c3t1)
542 ps->c3t2 = p4v;
543 ps->c3t1 = p5v;
544 /* high-pass clock for channel 1 */
545 ps->c1t3 = ps->c1t2;
546 }
547
548 void event3_p917_p5(PokeyState* ps, int p5v, int p4v, int p917v)
549 {
550 if(ps->c3t1)
551 ps->c3t2 = p917v;
552 ps->c3t1 = p5v;
553 /* high-pass clock for channel 1 */
554 ps->c1t3 = ps->c1t2;
555 }
556
557 void advance_ticks(PokeyState* ps, int ticks)
558 {
559 int ta,tbe, tbe0, tbe1, tbe2, tbe3;
560 int p5v,p4v,p917v;
561
562 qev_t outvol_new;
563 int need0=0;
564 int need1=0;
565 int need2=0;
566 int need3=0;
567
568 int need=0;
569
570 if (ticks <= 0) return;
571 if(ps->forcero)
572 {
573 ps->forcero = 0;
574 #ifdef NONLINEAR_MIXING
575 outvol_new = pokeymix[ps->outvol_0 + ps->outvol_1 + ps->outvol_2 + ps->outvol_3];
576 #else
577 outvol_new = ps->outvol_0 + ps->outvol_1 + ps->outvol_2 + ps->outvol_3;
578 #endif /* NONLINEAR_MIXING */
579 if(outvol_new != ps->outvol_all)
580 {
581 ps->outvol_all = outvol_new;
582 }
583 }
584
585 while(ticks>0)
586 {
587 tbe0 = ps->c0divpos;
588 tbe1 = ps->c1divpos;
589 tbe2 = ps->c2divpos;
590 tbe3 = ps->c3divpos;
591
592 tbe = ticks+1;
593
594 #ifdef NONLINEAR_MIXING
595 if(tbe0 < tbe)
596 tbe = tbe0;
597 if(tbe1 < tbe)
598 tbe = tbe1;
599 if(tbe2 < tbe)
600 tbe = tbe2;
601 if(tbe3 < tbe)
602 tbe = tbe3;
603 #else
604 if(!ps->c0stop && tbe0 < tbe)
605 tbe = tbe0;
606 if(!ps->c1stop && tbe1 < tbe)
607 tbe = tbe1;
608 if(!ps->c2stop && tbe2 < tbe)
609 tbe = tbe2;
610 if(!ps->c3stop && tbe3 < tbe)
611 tbe = tbe3;
612 #endif
613
614 if(tbe>ticks)
615 ta = ticks;
616 else
617 {
618 ta = tbe;
619 need = 1;
620 }
621
622 ticks -= ta;
623
624 #ifdef NONLINEAR_MIXING
625 ps->c0divpos -= ta;
626 ps->c1divpos -= ta;
627 ps->c2divpos -= ta;
628 ps->c3divpos -= ta;
629 #else
630 if(!ps->c0stop) ps->c0divpos -= ta;
631 if(!ps->c1stop) ps->c1divpos -= ta;
632 if(!ps->c2stop) ps->c2divpos -= ta;
633 if(!ps->c3stop) ps->c3divpos -= ta;
634 #endif
635
636 advance_polies(ps,ta);
637
638 if(need)
639 {
640 p5v = poly5tbl[ps->poly5pos] & 1;
641 p4v = poly4tbl[ps->poly4pos] & 1;
642 if(ps->selpoly9)
643 p917v = poly9tbl[ps->poly9pos] & 1;
644 else
645 p917v = poly17tbl[ps->poly17pos] & 1;
646
647 #ifdef NONLINEAR_MIXING
648 if(ta == tbe0)
649 #else
650 if(!ps->c0stop && ta == tbe0)
651 #endif
652 {
653 ps->event_0(ps,p5v,p4v,p917v);
654 ps->c0divpos = ps->c0divstart;
655 need0 = 1;
656 }
657 #ifdef NONLINEAR_MIXING
658 if(ta == tbe1)
659 #else
660 if(!ps->c1stop && ta == tbe1)
661 #endif
662 {
663 ps->event_1(ps,p5v,p4v,p917v);
664 ps->c1divpos = ps->c1divstart;
665 if(ps->c1_f0)
666 ps->c0divpos = ps->c0divstart_p;
667 need1 = 1;
668 /*two-tone filter*/
669 /*use if send break is on and two-tone mode is on*/
670 /*reset channel 1 if channel 2 changed*/
671 if((ps->skctl & 0x88) == 0x88) {
672 ps->c0divpos = ps->c0divstart;
673 /* it doesn't change the output state */
674 /*need0 = 1;*/
675 }
676 }
677 #ifdef NONLINEAR_MIXING
678 if(ta == tbe2)
679 #else
680 if(!ps->c2stop && ta == tbe2)
681 #endif
682 {
683 ps->event_2(ps,p5v,p4v,p917v);
684 ps->c2divpos = ps->c2divstart;
685 need2 = 1;
686 if(ps->c0sw4)
687 need0 = 1;
688 }
689 #ifdef NONLINEAR_MIXING
690 if(ta == tbe3)
691 #else
692 if(!ps->c3stop && ta == tbe3)
693 #endif
694 {
695 ps->event_3(ps,p5v,p4v,p917v);
696 ps->c3divpos = ps->c3divstart;
697 if(ps->c3_f2)
698 ps->c2divpos = ps->c2divstart_p;
699 need3 = 1;
700 if(ps->c1sw4)
701 need1 = 1;
702 }
703
704 if(need0)
705 {
706 #ifdef NONLINEAR_MIXING
707 ps->outvol_0 = ps->readout_0(ps);
708 #else
709 ps->outvol_0 = 2*ps->readout_0(ps);
710 #endif
711 }
712 if(need1)
713 {
714 #ifdef NONLINEAR_MIXING
715 ps->outvol_1 = ps->readout_1(ps);
716 #else
717 ps->outvol_1 = 2*ps->readout_1(ps);
718 #endif
719 }
720 if(need2)
721 {
722 #ifdef NONLINEAR_MIXING
723 ps->outvol_2 = ps->readout_2(ps);
724 #else
725 ps->outvol_2 = 2*ps->readout_2(ps);
726 #endif
727 }
728 if(need3)
729 {
730 #ifdef NONLINEAR_MIXING
731 ps->outvol_3 = ps->readout_3(ps);
732 #else
733 ps->outvol_3 = 2*ps->readout_3(ps);
734 #endif
735 }
736
737 #ifdef NONLINEAR_MIXING
738 outvol_new = pokeymix[ps->outvol_0 + ps->outvol_1 + ps->outvol_2 + ps->outvol_3];
739 #else
740 outvol_new = ps->outvol_0 + ps->outvol_1 + ps->outvol_2 + ps->outvol_3;
741 #endif /* NONLINEAR_MIXING */
742 if(outvol_new != ps->outvol_all)
743 {
744 ps->outvol_all = outvol_new;
745 }
746 }
747 }
748 }
749
750 double generate_sample(PokeyState* ps)
751 {
752 advance_ticks(ps, 1);
753 return ps->outvol_all;
754 }
755
756 /*****************************************************************************/
757 /* Module: MZPOKEYSND_Init() */
758 /* Purpose: to handle the power-up initialization functions */
759 /* these functions should only be executed on a cold-restart */
760 /* */
761 /* Authors: Michael Borisov, Krzystof Nikiel */
762 /* */
763 /* Inputs: freq17 - the value for the '1.79MHz' Pokey audio clock */
764 /* playback_freq - the playback frequency in samples per second */
765 /* */
766 /* Outputs: Adjusts local globals - no return value */
767 /* */
768 /*****************************************************************************/
769
770 int MZPOKEYSND_Init(PokeyState* ps)
771 {
772 build_poly4();
773 build_poly5();
774 build_poly9();
775 build_poly17();
776 ResetPokeyState(ps);
777 return 0; /* OK */
778 }
779
780
781 void Update_readout_0(PokeyState* ps)
782 {
783 if(ps->c0vo)
784 ps->readout_0 = readout0_vo;
785 else if(ps->c0sw4)
786 ps->readout_0 = readout0_hipass;
787 else
788 ps->readout_0 = readout0_normal;
789 }
790
791 void Update_readout_1(PokeyState* ps)
792 {
793 if(ps->c1vo)
794 ps->readout_1 = readout1_vo;
795 else if(ps->c1sw4)
796 ps->readout_1 = readout1_hipass;
797 else
798 ps->readout_1 = readout1_normal;
799 }
800
801 void Update_readout_2(PokeyState* ps)
802 {
803 if(ps->c2vo)
804 ps->readout_2 = readout2_vo;
805 else
806 ps->readout_2 = readout2_normal;
807 }
808
809 void Update_readout_3(PokeyState* ps)
810 {
811 if(ps->c3vo)
812 ps->readout_3 = readout3_vo;
813 else
814 ps->readout_3 = readout3_normal;
815 }
816
817 void Update_event0(PokeyState* ps)
818 {
819 if(ps->c0sw3)
820 {
821 if(ps->c0sw2)
822 ps->event_0 = event0_pure;
823 else
824 {
825 if(ps->c0sw1)
826 ps->event_0 = event0_p4;
827 else
828 ps->event_0 = event0_p917;
829 }
830 }
831 else
832 {
833 if(ps->c0sw2)
834 ps->event_0 = event0_p5;
835 else
836 {
837 if(ps->c0sw1)
838 ps->event_0 = event0_p4_p5;
839 else
840 ps->event_0 = event0_p917_p5;
841 }
842 }
843 }
844
845 void Update_event1(PokeyState* ps)
846 {
847 if(ps->c1sw3)
848 {
849 if(ps->c1sw2)
850 ps->event_1 = event1_pure;
851 else
852 {
853 if(ps->c1sw1)
854 ps->event_1 = event1_p4;
855 else
856 ps->event_1 = event1_p917;
857 }
858 }
859 else
860 {
861 if(ps->c1sw2)
862 ps->event_1 = event1_p5;
863 else
864 {
865 if(ps->c1sw1)
866 ps->event_1 = event1_p4_p5;
867 else
868 ps->event_1 = event1_p917_p5;
869 }
870 }
871 }
872
873 void Update_event2(PokeyState* ps)
874 {
875 if(ps->c2sw3)
876 {
877 if(ps->c2sw2)
878 ps->event_2 = event2_pure;
879 else
880 {
881 if(ps->c2sw1)
882 ps->event_2 = event2_p4;
883 else
884 ps->event_2 = event2_p917;
885 }
886 }
887 else
888 {
889 if(ps->c2sw2)
890 ps->event_2 = event2_p5;
891 else
892 {
893 if(ps->c2sw1)
894 ps->event_2 = event2_p4_p5;
895 else
896 ps->event_2 = event2_p917_p5;
897 }
898 }
899 }
900
901 void Update_event3(PokeyState* ps)
902 {
903 if(ps->c3sw3)
904 {
905 if(ps->c3sw2)
906 ps->event_3 = event3_pure;
907 else
908 {
909 if(ps->c3sw1)
910 ps->event_3 = event3_p4;
911 else
912 ps->event_3 = event3_p917;
913 }
914 }
915 else
916 {
917 if(ps->c3sw2)
918 ps->event_3 = event3_p5;
919 else
920 {
921 if(ps->c3sw1)
922 ps->event_3 = event3_p4_p5;
923 else
924 ps->event_3 = event3_p917_p5;
925 }
926 }
927 }
928
929 void Update_c0divstart(PokeyState* ps)
930 {
931 if(ps->c1_f0)
932 {
933 if(ps->c0_hf)
934 {
935 ps->c0divstart = 256;
936 ps->c0divstart_p = ps->c0diva + 7;
937 }
938 else
939 {
940 ps->c0divstart = 256 * ps->mdivk;
941 ps->c0divstart_p = (ps->c0diva+1)*ps->mdivk;
942 }
943 }
944 else
945 {
946 if(ps->c0_hf)
947 ps->c0divstart = ps->c0diva + 4;
948 else
949 ps->c0divstart = (ps->c0diva+1) * ps->mdivk;
950 }
951 }
952
953 void Update_c1divstart(PokeyState* ps)
954 {
955 if(ps->c1_f0)
956 {
957 if(ps->c0_hf)
958 ps->c1divstart = ps->c0diva + 256*ps->c1diva + 7;
959 else
960 ps->c1divstart = (ps->c0diva + 256*ps->c1diva + 1) * ps->mdivk;
961 }
962 else
963 ps->c1divstart = (ps->c1diva + 1) * ps->mdivk;
964 }
965
966 void Update_c2divstart(PokeyState* ps)
967 {
968 if(ps->c3_f2)
969 {
970 if(ps->c2_hf)
971 {
972 ps->c2divstart = 256;
973 ps->c2divstart_p = ps->c2diva + 7;
974 }
975 else
976 {
977 ps->c2divstart = 256 * ps->mdivk;
978 ps->c2divstart_p = (ps->c2diva+1)*ps->mdivk;
979 }
980 }
981 else
982 {
983 if(ps->c2_hf)
984 ps->c2divstart = ps->c2diva + 4;
985 else
986 ps->c2divstart = (ps->c2diva+1) * ps->mdivk;
987 }
988 }
989
990 void Update_c3divstart(PokeyState* ps)
991 {
992 if(ps->c3_f2)
993 {
994 if(ps->c2_hf)
995 ps->c3divstart = ps->c2diva + 256*ps->c3diva + 7;
996 else
997 ps->c3divstart = (ps->c2diva + 256*ps->c3diva + 1) * ps->mdivk;
998 }
999 else
1000 ps->c3divstart = (ps->c3diva + 1) * ps->mdivk;
1001 }
1002
1003 void Update_audctl(PokeyState* ps, unsigned char val)
1004 {
1005 int nc0_hf,nc2_hf,nc1_f0,nc3_f2,nc0sw4,nc1sw4,new_divk;
1006 int recalc0=0;
1007 int recalc1=0;
1008 int recalc2=0;
1009 int recalc3=0;
1010
1011 unsigned int cnt0 = 0;
1012 unsigned int cnt1 = 0;
1013 unsigned int cnt2 = 0;
1014 unsigned int cnt3 = 0;
1015
1016 nc0_hf = (val & 0x40) != 0;
1017 nc2_hf = (val & 0x20) != 0;
1018 nc1_f0 = (val & 0x10) != 0;
1019 nc3_f2 = (val & 0x08) != 0;
1020 nc0sw4 = (val & 0x04) != 0;
1021 nc1sw4 = (val & 0x02) != 0;
1022 if(val & 0x01)
1023 new_divk = 114;
1024 else
1025 new_divk = 28;
1026
1027 if(new_divk != ps->mdivk)
1028 {
1029 recalc0 = recalc1 = recalc2 = recalc3 = 1;
1030 }
1031 if(nc1_f0 != ps->c1_f0)
1032 {
1033 recalc0 = recalc1 = 1;
1034 }
1035 if(nc3_f2 != ps->c3_f2)
1036 {
1037 recalc2 = recalc3 = 1;
1038 }
1039 if(nc0_hf != ps->c0_hf)
1040 {
1041 recalc0 = 1;
1042 if(nc1_f0)
1043 recalc1 = 1;
1044 }
1045 if(nc2_hf != ps->c2_hf)
1046 {
1047 recalc2 = 1;
1048 if(nc3_f2)
1049 recalc3 = 1;
1050 }
1051
1052 if(recalc0)
1053 {
1054 if(ps->c0_hf)
1055 cnt0 = ps->c0divpos;
1056 else
1057 cnt0 = ps->c0divpos/ps->mdivk;
1058 }
1059 if(recalc1)
1060 {
1061 if(ps->c1_f0)
1062 {
1063 if(ps->c0_hf)
1064 cnt1 = ps->c1divpos/256;
1065 else
1066 cnt1 = ps->c1divpos/256/ps->mdivk;
1067 }
1068 else
1069 {
1070 cnt1 = ps->c1divpos/ps->mdivk;
1071 }
1072 }
1073 if(recalc2)
1074 {
1075 if(ps->c2_hf)
1076 cnt2 = ps->c2divpos;
1077 else
1078 cnt2 = ps->c2divpos/ps->mdivk;
1079 }
1080 if(recalc3)
1081 {
1082 if(ps->c3_f2)
1083 {
1084 if(ps->c2_hf)
1085 cnt3 = ps->c3divpos/256;
1086 else
1087 cnt3 = ps->c3divpos/256/ps->mdivk;
1088 }
1089 }
1090
1091 if(recalc0)
1092 {
1093 if(nc0_hf)
1094 ps->c0divpos = cnt0;
1095 else
1096 ps->c0divpos = cnt0*new_divk;
1097 }
1098 if(recalc1)
1099 {
1100 if(nc1_f0)
1101 {
1102 if(nc0_hf)
1103 ps->c1divpos = cnt1*256+cnt0;
1104 else
1105 ps->c1divpos = (cnt1*256+cnt0)*new_divk;
1106 }
1107 else
1108 {
1109 ps->c1divpos = cnt1*new_divk;
1110 }
1111 }
1112
1113 if(recalc2)
1114 {
1115 if(nc2_hf)
1116 ps->c2divpos = cnt2;
1117 else
1118 ps->c2divpos = cnt2*new_divk;
1119 }
1120 if(recalc3)
1121 {
1122 if(nc3_f2)
1123 {
1124 if(nc2_hf)
1125 ps->c3divpos = cnt3*256+cnt2;
1126 else
1127 ps->c3divpos = (cnt3*256+cnt2)*new_divk;
1128 }
1129 }
1130
1131 ps->c0_hf = nc0_hf;
1132 ps->c2_hf = nc2_hf;
1133 ps->c1_f0 = nc1_f0;
1134 ps->c3_f2 = nc3_f2;
1135 ps->c0sw4 = nc0sw4;
1136 ps->c1sw4 = nc1sw4;
1137 ps->mdivk = new_divk;
1138 }
1139
1140 /* SKCTL for two-tone mode */
1141 void Update_skctl(PokeyState* ps, unsigned char val)
1142 {
1143 ps->skctl = val;
1144 }
1145
1146 /* if using nonlinear mixing, don't stop ultrasounds */
1147 #ifdef NONLINEAR_MIXING
1148 void Update_c0stop(PokeyState* ps)
1149 {
1150 ps->outvol_0 = ps->readout_0(ps);
1151 }
1152 void Update_c1stop(PokeyState* ps)
1153 {
1154 ps->outvol_1 = ps->readout_1(ps);
1155 }
1156 void Update_c2stop(PokeyState* ps)
1157 {
1158 ps->outvol_2 = ps->readout_2(ps);
1159 }
1160 void Update_c3stop(PokeyState* ps)
1161 {
1162 ps->outvol_3 = ps->readout_3(ps);
1163 }
1164 #else
1165 void Update_c0stop(PokeyState* ps)
1166 {
1167 int lim = 1;
1168
1169 int hfa = 0;
1170 ps->c0stop = 0;
1171
1172 if(ps->c0vo || ps->vol0 == 0)
1173 ps->c0stop = 1;
1174 else if(!ps->c0sw4 && ps->c0sw3 && ps->c0sw2) /* If channel 0 is a pure tone... */
1175 {
1176 if(ps->c1_f0)
1177 {
1178 if(ps->c1divstart <= lim)
1179 {
1180 ps->c0stop = 1;
1181 hfa = 1;
1182 }
1183 }
1184 else
1185 {
1186 if(ps->c0divstart <= lim)
1187 {
1188 ps->c0stop = 1;
1189 hfa = 1;
1190 }
1191 }
1192 }
1193 else if(!ps->c0sw4 && ps->c0sw3 && !ps->c0sw2 && ps->c0sw1) /* if channel 0 is poly4... */
1194 {
1195 /* period for poly4 signal is 15 cycles */
1196 if(ps->c1_f0)
1197 {
1198 if(ps->c1divstart <= lim*2/15) /* all poly4 signal is above Nyquist */
1199 {
1200 ps->c0stop = 1;
1201 hfa = 1;
1202 }
1203 }
1204 else
1205 {
1206 if(ps->c0divstart <= lim*2/15)
1207 {
1208 ps->c0stop = 1;
1209 hfa = 1;
1210 }
1211 }
1212 }
1213
1214 ps->outvol_0 = 2*ps->readout_0(ps);
1215 if(hfa)
1216 ps->outvol_0 = ps->vol0;
1217 }
1218
1219 void Update_c1stop(PokeyState* ps)
1220 {
1221 int lim = 1;
1222
1223 int hfa = 0;
1224 ps->c1stop = 0;
1225
1226 if(!ps->c1_f0 && (ps->c1vo || ps->vol1 == 0))
1227 ps->c1stop = 1;
1228 else if(!ps->c1sw4 && ps->c1sw3 && ps->c1sw2 && ps->c1divstart <= lim) /* If channel 1 is a pure tone */
1229 {
1230 ps->c1stop = 1;
1231 hfa = 1;
1232 }
1233 else if(!ps->c1sw4 && ps->c1sw3 && !ps->c1sw2 && ps->c1sw1 && ps->c1divstart <= lim*2/15) /* all poly4 signal is above Nyquist */
1234 {
1235 ps->c1stop = 1;
1236 hfa = 1;
1237 }
1238
1239 ps->outvol_1 = 2*ps->readout_1(ps);
1240 if(hfa)
1241 ps->outvol_1 = ps->vol1;
1242 }
1243
1244 void Update_c2stop(PokeyState* ps)
1245 {
1246 int lim = 1;
1247
1248 int hfa = 0;
1249 ps->c2stop = 0;
1250
1251 if(!ps->c0sw4 && (ps->c2vo || ps->vol2 == 0))
1252 ps->c2stop = 1;
1253 /* If channel 2 is a pure tone and no filter for c0... */
1254 else if(ps->c2sw3 && ps->c2sw2 && !ps->c0sw4)
1255 {
1256 if(ps->c3_f2)
1257 {
1258 if(ps->c3divstart <= lim)
1259 {
1260 ps->c2stop = 1;
1261 hfa = 1;
1262 }
1263 }
1264 else
1265 {
1266 if(ps->c2divstart <= lim)
1267 {
1268 ps->c2stop = 1;
1269 hfa = 1;
1270 }
1271 }
1272 }
1273 else if(ps->c2sw3 && !ps->c2sw2 && ps->c2sw1 && !ps->c0sw4) /* if channel 2 is poly4 and no filter for c0... */
1274 {
1275 /* period for poly4 signal is 15 cycles */
1276 if(ps->c3_f2)
1277 {
1278 if(ps->c3divstart <= lim*2/15) /* all poly4 signal is above Nyquist */
1279 {
1280 ps->c2stop = 1;
1281 hfa = 1;
1282 }
1283 }
1284 else
1285 {
1286 if(ps->c2divstart <= lim*2/15)
1287 {
1288 ps->c2stop = 1;
1289 hfa = 1;
1290 }
1291 }
1292 }
1293
1294 ps->outvol_2 = 2*ps->readout_2(ps);
1295 if(hfa)
1296 ps->outvol_2 = ps->vol2;
1297 }
1298
1299 void Update_c3stop(PokeyState* ps)
1300 {
1301 int lim = 1;
1302 int hfa = 0;
1303 ps->c3stop = 0;
1304
1305 if(!ps->c1sw4 && !ps->c3_f2 && (ps->c3vo || ps->vol3 == 0))
1306 ps->c3stop = 1;
1307 /* If channel 3 is a pure tone */
1308 else if(ps->c3sw3 && ps->c3sw2 && !ps->c1sw4 && ps->c3divstart <= lim)
1309 {
1310 ps->c3stop = 1;
1311 hfa = 1;
1312 }
1313 else if(ps->c3sw3 && !ps->c3sw2 && ps->c3sw1 && !ps->c1sw4 && ps->c3divstart <= lim*2/15) /* all poly4 signal is above Nyquist */
1314 {
1315 ps->c3stop = 1;
1316 hfa = 1;
1317 }
1318
1319 ps->outvol_3 = 2*ps->readout_3(ps);
1320 if(hfa)
1321 ps->outvol_3 = ps->vol3;
1322 }
1323 #endif /*NONLINEAR_MIXING*/
1324
1325 /*****************************************************************************/
1326 /* Function: Update_pokey_sound_mz() */
1327 /* */
1328 /* Inputs: addr - the address of the parameter to be changed */
1329 /* val - the new value to be placed in the specified address */
1330 /* chip - chip # for stereo */
1331 /* gain - specified as an 8-bit fixed point number - use 1 for no */
1332 /* amplification (output is multiplied by gain) */
1333 /* */
1334 /* Outputs: Adjusts local globals - no return value */
1335 /* */
1336 /*****************************************************************************/
1337 void Update_pokey_sound_mz(PokeyState* ps, unsigned short addr, unsigned char val, unsigned char gain)
1338 {
1339 switch(addr & 0x0f)
1340 {
1341 case POKEY_OFFSET_AUDF1:
1342 ps->c0diva = val;
1343 Update_c0divstart(ps);
1344 if(ps->c1_f0)
1345 {
1346 Update_c1divstart(ps);
1347 Update_c1stop(ps);
1348 }
1349 Update_c0stop(ps);
1350 ps->forcero = 1;
1351 break;
1352 case POKEY_OFFSET_AUDC1:
1353 ps->c0sw1 = (val & 0x40) != 0;
1354 ps->c0sw2 = (val & 0x20) != 0;
1355 ps->c0sw3 = (val & 0x80) != 0;
1356 ps->vol0 = (val & 0xF);
1357 ps->c0vo = (val & 0x10) != 0;
1358 Update_readout_0(ps);
1359 Update_event0(ps);
1360 Update_c0stop(ps);
1361 ps->forcero = 1;
1362 break;
1363 case POKEY_OFFSET_AUDF2:
1364 ps->c1diva = val;
1365 Update_c1divstart(ps);
1366 if(ps->c1_f0)
1367 {
1368 Update_c0divstart(ps);
1369 Update_c0stop(ps);
1370 }
1371 Update_c1stop(ps);
1372 ps->forcero = 1;
1373 break;
1374 case POKEY_OFFSET_AUDC2:
1375 ps->c1sw1 = (val & 0x40) != 0;
1376 ps->c1sw2 = (val & 0x20) != 0;
1377 ps->c1sw3 = (val & 0x80) != 0;
1378 ps->vol1 = (val & 0xF);
1379 ps->c1vo = (val & 0x10) != 0;
1380 Update_readout_1(ps);
1381 Update_event1(ps);
1382 Update_c1stop(ps);
1383 ps->forcero = 1;
1384 break;
1385 case POKEY_OFFSET_AUDF3:
1386 ps->c2diva = val;
1387 Update_c2divstart(ps);
1388 if(ps->c3_f2)
1389 {
1390 Update_c3divstart(ps);
1391 Update_c3stop(ps);
1392 }
1393 Update_c2stop(ps);
1394 ps->forcero = 1;
1395 break;
1396 case POKEY_OFFSET_AUDC3:
1397 ps->c2sw1 = (val & 0x40) != 0;
1398 ps->c2sw2 = (val & 0x20) != 0;
1399 ps->c2sw3 = (val & 0x80) != 0;
1400 ps->vol2 = (val & 0xF);
1401 ps->c2vo = (val & 0x10) != 0;
1402 Update_readout_2(ps);
1403 Update_event2(ps);
1404 Update_c2stop(ps);
1405 ps->forcero = 1;
1406 break;
1407 case POKEY_OFFSET_AUDF4:
1408 ps->c3diva = val;
1409 Update_c3divstart(ps);
1410 if(ps->c3_f2)
1411 {
1412 Update_c2divstart(ps);
1413 Update_c2stop(ps);
1414 }
1415 Update_c3stop(ps);
1416 ps->forcero = 1;
1417 break;
1418 case POKEY_OFFSET_AUDC4:
1419 ps->c3sw1 = (val & 0x40) != 0;
1420 ps->c3sw2 = (val & 0x20) != 0;
1421 ps->c3sw3 = (val & 0x80) != 0;
1422 ps->vol3 = val & 0xF;
1423 ps->c3vo = (val & 0x10) != 0;
1424 Update_readout_3(ps);
1425 Update_event3(ps);
1426 Update_c3stop(ps);
1427 ps->forcero = 1;
1428 break;
1429 case POKEY_OFFSET_AUDCTL:
1430 ps->selpoly9 = (val & 0x80) != 0;
1431 Update_audctl(ps,val);
1432 Update_readout_0(ps);
1433 Update_readout_1(ps);
1434 Update_readout_2(ps);
1435 Update_readout_3(ps);
1436 Update_c0divstart(ps);
1437 Update_c1divstart(ps);
1438 Update_c2divstart(ps);
1439 Update_c3divstart(ps);
1440 Update_c0stop(ps);
1441 Update_c1stop(ps);
1442 Update_c2stop(ps);
1443 Update_c3stop(ps);
1444 ps->forcero = 1;
1445 break;
1446 case POKEY_OFFSET_STIMER:
1447 if(ps->c1_f0)
1448 ps->c0divpos = ps->c0divstart_p;
1449 else
1450 ps->c0divpos = ps->c0divstart;
1451 ps->c1divpos = ps->c1divstart;
1452 if(ps->c3_f2)
1453 ps->c2divpos = ps->c2divstart_p;
1454 else
1455 ps->c2divpos = ps->c2divstart;
1456
1457 ps->c3divpos = ps->c3divstart;
1458 /*Documentation is wrong about which voices are on after STIMER*/
1459 /*It is 3&4 which are on, tested on a real atari*/
1460 ps->c0t2 = 0;
1461 ps->c1t2 = 0;
1462 ps->c2t2 = 1;
1463 ps->c3t2 = 1;
1464 break;
1465 case POKEY_OFFSET_SKCTL:
1466 Update_skctl(ps,val);
1467 break;
1468 }
1469 }
1470
1471
1472 /**************************************************************
1473
1474 Master gain and DC offset calculation
1475 by Michael Borisov
1476
1477 In order to use the available 8-bit or 16-bit dynamic range
1478 to full extent, reducing the influence of quantization
1479 noise while simultaneously avoiding overflows, gain
1480 and DC offset should be set to appropriate value.
1481
1482 All Pokey-generated sounds have maximal amplitude of 15.
1483 When all four channels sound simultaneously and in the
1484 same phase, amplidudes would add up to 60.
1485
1486 If Pokey is generating a 'pure tone', it always has a DC
1487 offset of half its amplitude. For other signals (produced
1488 by poly generators) DC offset varies, but it is always near
1489 to half amplitude and never exceeds this value.
1490
1491 In case that pure tone base frequency is outside of audible
1492 range (ultrasound frequency for high sample rates and above-
1493 Nyquist frequency for low sample rates), to speed up the engine,
1494 the generator is stopped while having only DC offset on the
1495 output (half of corresponding AUDV value). In order that this
1496 DC offset can be always represented as integer, AUDV values
1497 are multiplied by 2 when the generator works.
1498
1499 Therefore maximum integer value before resampling filters
1500 would be 60*2 = 120 while having maximum DC offset of 60.
1501 Resampling does not change the DC offset, therefore we may
1502 subtract it from the signal either before or after resampling.
1503 In mzpokeysnd, DC offset is subtracted after resampling, however
1504 for better understanding in further measurements I assume
1505 subtracting DC before. So, input range for the resampler
1506 becomes [-60 .. 60].
1507
1508 Resampling filter removes some harmonics from the signal as if
1509 the rectangular wave was Fourier transformed forth-and-back,
1510 while zeroing all harmonics above cutoff frequency. In case
1511 of high-frequency pure tone (above samplerate/8), only first
1512 harmonic of the Fourier transofm will remain. As it
1513 is known, Fourier-transform of the rectangular function of
1514 amplitude 1 has first oscillation component of amplitude 4/M_PI.
1515 Therefore, maximum sample values for filtered rectangular
1516 signal may exceed the amplitude of rectangular signal
1517 by up to 4/M_PI times.
1518
1519 Since our range before resampler is -60 .. 60, taking into
1520 account mentioned effect with band limiting, range of values
1521 on the resampler output appears to be in the following bounds:
1522 [-60*4/M_PI .. 60*4/M_PI]
1523
1524 In order to map this into signed 8-bit range [-128 .. 127], we
1525 should multiply the resampler output by 127/60/4*M_PI.
1526
1527 As it is common for sound hardware to have 8-bit sound unsigned,
1528 additional DC offset of 128 must be added.
1529
1530 For 16-bit case the output range is [-32768 .. 32767], and
1531 we should multiply the resampler output by 32767/60/4*M_PI
1532
1533 To make some room for numerical errors, filter ripples and
1534 quantization noise, so that they do not cause overflows in
1535 quantization, dynamic range is reduced in mzpokeysnd by
1536 multiplying the output amplitude with 0.95, reserving 5%
1537 of the total range for such effects, which is about 0.51db.
1538
1539 Mentioned gain and DC values were tested with 17kHz audio
1540 playing synchronously on 4 channels, which showed to be
1541 utilizing 95% of the sample values range.
1542
1543 Since any other gain value will be not optimal, I removed
1544 user gain setting and hard-coded the gain into mzpokeysnd
1545
1546 ---
1547
1548 A note from Piotr Fusik:
1549 I've added support for the key click sound generated by GTIA. Its
1550 volume seems to be pretty much like 8 on single POKEY's channel.
1551 So, the volumes now can sum up to 136 (4 channels * 15 * 2
1552 + 8 * 2 for GTIA), not 120.
1553
1554 A note from Mark Grebe:
1555 I've added back in the console and sio sounds from the old
1556 pokey version. So, now the volumes can sum up to 152
1557 (4 channesl * 15 * 2 + 8 * 4 for old sound), not 120 or 136.
1558
1559 ******************************************************************/
1560
1561
1562 /******************************************************************
1563
1564 Quantization effects and dithering
1565 by Michael Borisov
1566
1567 Quantization error in the signal has an expectation value of half
1568 the LSB, when the rounding is performed properly. Sometimes they
1569 express quantization error as a random function with even
1570 distribution over the range [-0.5 to 0.5]. Spectrum of this function
1571 is flat, because it's a white noise.
1572
1573 Power of a discrete signal (including noise) is calculated as
1574 mean square of its samples. For the mentioned above noise
1575 this is approximately 0.33. Therefore, in decibels for 8-bit case,
1576 our noise will have power of 10*log10(0.33/256/256) = -53dB
1577
1578 Because noise is white, this power of -53dB will be evenly
1579 distributed over the whole signal band upto Nyquist frequency.
1580 The larger the band is (higher sampling frequency), less
1581 is the quantisation noise floor. For 8000Hz noise floor is
1582 10*log10(0.33/256/256/4000) = -89dB/Hz, and for 44100Hz noise
1583 floor is 10*log10(0.33/256/256/22050) = -96.4dB/Hz.
1584 This shows that higher sampling rates are better in sense of
1585 quantization noise. Moreover, as large part of quantization noise
1586 in case of 44100Hz will fall into ultrasound and hi-frequency
1587 area 10-20kHz where human ear is less sensitive, this will
1588 show up as great improvement in quantization noise performance
1589 compared to 8000Hz.
1590
1591 I was plotting spectral analysis for sounds produced by mzpokeysnd
1592 to check these measures. And it showed up that in 8-bit case
1593 there is no expected flat noise floor of -89db/Hz for 8000Hz,
1594 but some distortion spectral peaks had higher amplitude than
1595 the aliasing peaks in 16-bit case. This was a proof to another
1596 statement which says that quantization noise tends to become
1597 correlated with the signal. Correlation is especially strong
1598 for simple signals generated by Pokey. Correlation means that
1599 the noise power of -53db is no longer evenly distributed
1600 across the whole frequency range, but concentrates in larger
1601 peaks at locations which depend on the Pokey signal.
1602
1603 To decorrelate quantization distortion and make it again
1604 white noise, which would improve the sound spectrum, since
1605 the maximum distortion peaks will have less amplitude,
1606 dithering is used. Another white noise is added to the signal
1607 before quantization. Since this added noise is not correlated
1608 with the signal, it shows itself as a flat noise floor.
1609 Quantization noise now tries to correlate with the dithering
1610 noise, but this does not lead to appearance of sharp
1611 spectral peaks any more :)
1612
1613 Another thing is that for listening, white noise is better than
1614 distortion. This is because human hearing has some 'noise
1615 reduction' system which makes it easier to percept sounds
1616 on the white noise background.
1617
1618 From the other point of view, if a signal has high and low
1619 spectral peaks, it is desirable that there is no distortion
1620 component with peaks of amplitude comparable to those of
1621 the true signal. Otherwise, perception of background low-
1622 amplitude signals will be disrupted. That's why they say
1623 that dithering extends dynamic range.
1624
1625 Dithering does not eliminate correlation of quantization noise
1626 completely. Degree of reduction of this effect depends on
1627 the dithering noise power. The higher is dithering noise,
1628 the more quantization noise is decorrelated. But this also
1629 leads to increase of noise percepted by the listener. So, an
1630 optimum value should be selected. My experiments show that
1631 unbiased rand() noise of amplitude 0.25 LSB is doing well.
1632
1633 Test spectral pictures for 8-bit sound, 8kHz sampling rate,
1634 dithered, show a noise floor of approx. -87dB/Hz.
1635
1636 ******************************************************************/
1637
1638 #define MAX_SAMPLE 152
1639
1640 void mzpokeysnd_process_16(PokeyState* ps, void* sndbuffer, int sndn)
1641 {
1642 int i;
1643 int nsam = sndn;
1644 short *buffer = (short *) sndbuffer;
1645
1646 /* if there are two pokeys, then the signal is stereo
1647 we assume even sndn */
1648 while(nsam >= (int) 1)
1649 {
1650 buffer[0] = (short)floor(generate_sample(ps)
1651 * (65535.0 / 2 / MAX_SAMPLE / 4 * M_PI * 0.95));
1652 buffer += 1;
1653 nsam -= 1;
1654 }
1655 }