-
-
Notifications
You must be signed in to change notification settings - Fork 306
/
snd_sb.c
5689 lines (5035 loc) · 174 KB
/
snd_sb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 86Box A hypervisor and IBM PC system emulator that specializes in
* running old operating systems and software designed for IBM
* PC systems and compatibles from 1981 through fairly recent
* system designs based on the PCI bus.
*
* This file is part of the 86Box distribution.
*
* Sound Blaster emulation.
*
*
*
* Authors: Sarah Walker, <https://pcem-emulator.co.uk/>
* Miran Grca, <mgrca8@gmail.com>
* TheCollector1995, <mariogplayer@gmail.com>
*
* Copyright 2008-2020 Sarah Walker.
* Copyright 2016-2020 Miran Grca.
*/
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#define HAVE_STDARG_H
#include <86box/86box.h>
#include <86box/device.h>
#include <86box/filters.h>
#include <86box/gameport.h>
#include <86box/hdc.h>
#include <86box/isapnp.h>
#include <86box/hdc_ide.h>
#include <86box/io.h>
#include <86box/mca.h>
#include <86box/mem.h>
#include <86box/midi.h>
#include <86box/pic.h>
#include <86box/rom.h>
#include <86box/sound.h>
#include <86box/timer.h>
#include <86box/snd_sb.h>
#include <86box/plat_unused.h>
#define PNP_ROM_SB_16_PNP "roms/sound/creative/CTL0024A.BIN"
#define PNP_ROM_SB_VIBRA16XV "roms/sound/creative/CT4170 PnP.BIN"
#define PNP_ROM_SB_VIBRA16C "roms/sound/creative/CT4180 PnP.BIN"
#define PNP_ROM_SB_32_PNP "roms/sound/creative/CT3600 PnP.BIN"
#define PNP_ROM_SB_AWE32_PNP "roms/sound/creative/CT3980 PnP.BIN"
#define PNP_ROM_SB_AWE64_VALUE "roms/sound/creative/CT4520 PnP.BIN"
#define PNP_ROM_SB_AWE64 "roms/sound/creative/CTL009DA.BIN"
#define PNP_ROM_SB_AWE64_GOLD "roms/sound/creative/CT4540 PnP.BIN"
/* TODO: Find real ESS PnP ROM dumps. */
#define PNP_ROM_ESS0100 "roms/sound/ess/ESS0100.BIN"
#define PNP_ROM_ESS0102 "roms/sound/ess/ESS0102.BIN"
#define PNP_ROM_ESS0968 "roms/sound/ess/ESS0968.BIN"
/* 0 to 7 -> -14dB to 0dB i 2dB steps. 8 to 15 -> 0 to +14dB in 2dB steps.
Note that for positive dB values, this is not amplitude, it is amplitude - 1. */
static const double sb_bass_treble_4bits[] = {
0.199526231, 0.25, 0.316227766, 0.398107170, 0.5, 0.63095734, 0.794328234, 1,
0, 0.25892541, 0.584893192, 1, 1.511886431, 2.16227766, 3, 4.011872336
};
/* Attenuation tables for the mixer. Max volume = 32767 in order to give 6dB of
* headroom and avoid integer overflow */
// clang-format off
static const double sb_att_2dbstep_5bits[] = {
25.0, 32.0, 41.0, 51.0, 65.0, 82.0, 103.0, 130.0, 164.0, 206.0,
260.0, 327.0, 412.0, 519.0, 653.0, 822.0, 1036.0, 1304.0, 1641.0, 2067.0,
2602.0, 3276.0, 4125.0, 5192.0, 6537.0, 8230.0, 10362.0, 13044.0, 16422.0, 20674.0,
26027.0, 32767.0
};
static const double sb_att_4dbstep_3bits[] = {
164.0, 2067.0, 3276.0, 5193.0, 8230.0, 13045.0, 20675.0, 32767.0
};
static const double sb_att_7dbstep_2bits[] = {
164.0, 6537.0, 14637.0, 32767.0
};
/* Attenuation table for ESS 4-bit microphone volume.
* The last step is a jump to -48 dB. */
static const double sb_att_1p4dbstep_4bits[] = {
164.0, 3431.0, 4031.0, 4736.0, 5565.0, 6537.0, 7681.0, 9025.0,
10603.0, 12458.0, 14637.0, 17196.0, 20204.0, 23738.0, 27889.0, 32767.0
};
/* Attenuation table for ESS 4-bit mixer volume.
* The last step is a jump to -48 dB. */
static const double sb_att_2dbstep_4bits[] = {
164.0, 1304.0, 1641.0, 2067.0, 2602.0, 3276.0, 4125.0, 5192.0,
6537.0, 8230.0, 10362.0, 13044.0, 16422.0, 20674.0, 26027.0, 32767.0
};
/* Attenuation table for ESS 3-bit PC speaker volume. */
static const double sb_att_3dbstep_3bits[] = {
0.0, 4125.0, 5826.0, 8230.0, 11626.0, 16422.0, 23197.0, 32767.0
};
// clang-format on
static const uint16_t sb_mcv_addr[8] = { 0x200, 0x210, 0x220, 0x230, 0x240, 0x250, 0x260, 0x270 };
static const int sb_pro_mcv_irqs[4] = { 7, 5, 3, 3 };
#ifdef ENABLE_SB_LOG
int sb_do_log = ENABLE_SB_LOG;
static void
sb_log(const char *fmt, ...)
{
va_list ap;
if (sb_do_log) {
va_start(ap, fmt);
pclog_ex(fmt, ap);
va_end(ap);
}
}
#else
# define sb_log(fmt, ...)
#endif
/* SB 1, 1.5, MCV, and 2 do not have a mixer, so signal is hardwired. */
static void
sb_get_buffer_sb2(int32_t *buffer, int len, void *priv)
{
sb_t *sb = (sb_t *) priv;
const sb_ct1335_mixer_t *mixer = &sb->mixer_sb2;
double out_mono;
sb_dsp_update(&sb->dsp);
if (sb->cms_enabled)
cms_update(&sb->cms);
for (int c = 0; c < len * 2; c += 2) {
double out_l = 0.0;
double out_r = 0.0;
if (sb->cms_enabled) {
out_l += sb->cms.buffer[c];
out_r += sb->cms.buffer[c + 1];
}
if (sb->cms_enabled && sb->mixer_enabled) {
out_l *= mixer->fm;
out_r *= mixer->fm;
}
/* TODO: Recording: I assume it has direct mic and line in like SB2.
It is unclear from the docs if it has a filter, but it probably does. */
/* TODO: Recording: Mic and line In with AGC. */
if (sb->mixer_enabled)
out_mono = (sb_iir(0, 0, (double) sb->dsp.buffer[c]) * mixer->voice) / 3.9;
else
out_mono = (((sb_iir(0, 0, (double) sb->dsp.buffer[c]) / 1.3) * 65536.0) / 3.0) / 65536.0;
out_l += out_mono;
out_r += out_mono;
if (sb->mixer_enabled) {
out_l *= mixer->master;
out_r *= mixer->master;
}
buffer[c] += (int32_t) out_l;
buffer[c + 1] += (int32_t) out_r;
}
sb->dsp.pos = 0;
if (sb->cms_enabled)
sb->cms.pos = 0;
}
static void
sb_get_music_buffer_sb2(int32_t *buffer, int len, void *priv)
{
const sb_t *sb = (const sb_t *) priv;
const sb_ct1335_mixer_t *mixer = &sb->mixer_sb2;
const int32_t *opl_buf = NULL;
opl_buf = sb->opl.update(sb->opl.priv);
for (int c = 0; c < len * 2; c += 2) {
double out_l = 0.0;
double out_r = 0.0;
const double out_mono = ((double) opl_buf[c]) * 0.7171630859375;
out_l += out_mono;
out_r += out_mono;
if (sb->mixer_enabled) {
out_l *= mixer->fm;
out_r *= mixer->fm;
}
if (sb->mixer_enabled) {
out_l *= mixer->master;
out_r *= mixer->master;
}
buffer[c] += (int32_t) out_l;
buffer[c + 1] += (int32_t) out_r;
}
sb->opl.reset_buffer(sb->opl.priv);
}
static void
sb2_filter_cd_audio(UNUSED(int channel), double *buffer, void *priv)
{
const sb_t *sb = (sb_t *) priv;
const sb_ct1335_mixer_t *mixer = &sb->mixer_sb2;
double c;
if (sb->mixer_enabled) {
c = ((sb_iir(2, 0, *buffer) / 1.3) * mixer->cd) / 3.0;
*buffer = c * mixer->master;
} else {
c = (((sb_iir(2, 0, (*buffer)) / 1.3) * 65536) / 3.0) / 65536.0;
*buffer = c;
}
}
void
sb_get_buffer_sbpro(int32_t *buffer, const int len, void *priv)
{
sb_t *sb = (sb_t *) priv;
const sb_ct1345_mixer_t *mixer = &sb->mixer_sbpro;
sb_dsp_update(&sb->dsp);
for (int c = 0; c < len * 2; c += 2) {
double out_l = 0.0;
double out_r = 0.0;
/* TODO: Implement the stereo switch on the mixer instead of on the dsp? */
if (mixer->output_filter) {
out_l += (sb_iir(0, 0, (double) sb->dsp.buffer[c]) * mixer->voice_l) / 3.9;
out_r += (sb_iir(0, 1, (double) sb->dsp.buffer[c + 1]) * mixer->voice_r) / 3.9;
} else {
out_l += (sb->dsp.buffer[c] * mixer->voice_l) / 3.0;
out_r += (sb->dsp.buffer[c + 1] * mixer->voice_r) / 3.0;
}
/* TODO: recording CD, Mic with AGC or line in. Note: mic volume does not affect recording. */
out_l *= mixer->master_l;
out_r *= mixer->master_r;
buffer[c] += (int32_t) out_l;
buffer[c + 1] += (int32_t) out_r;
}
sb->dsp.pos = 0;
}
void
sb_get_music_buffer_sbpro(int32_t *buffer, int len, void *priv)
{
sb_t *sb = (sb_t *) priv;
const sb_ct1345_mixer_t *mixer = &sb->mixer_sbpro;
double out_l = 0.0;
double out_r = 0.0;
const int32_t *opl_buf = NULL;
const int32_t *opl2_buf = NULL;
if (!sb->opl_enabled)
return;
if (sb->dsp.sb_type == SBPRO) {
opl_buf = sb->opl.update(sb->opl.priv);
opl2_buf = sb->opl2.update(sb->opl2.priv);
} else
opl_buf = sb->opl.update(sb->opl.priv);
sb_dsp_update(&sb->dsp);
for (int c = 0; c < len * 2; c += 2) {
out_l = 0.0;
out_r = 0.0;
if (sb->dsp.sb_type == SBPRO) {
/* Two chips for LEFT and RIGHT channels.
Each chip stores data into the LEFT channel only (no sample alternating.) */
out_l = (((double) opl_buf[c]) * mixer->fm_l) * 0.7171630859375;
if (opl2_buf != NULL)
out_r = (((double) opl2_buf[c]) * mixer->fm_r) * 0.7171630859375;
} else {
out_l = (((double) opl_buf[c]) * mixer->fm_l) * 0.7171630859375;
out_r = (((double) opl_buf[c + 1]) * mixer->fm_r) * 0.7171630859375;
if (sb->opl_mix && sb->opl_mixer)
sb->opl_mix(sb->opl_mixer, &out_l, &out_r);
}
/* TODO: recording CD, Mic with AGC or line in. Note: mic volume does not affect recording. */
out_l *= mixer->master_l;
out_r *= mixer->master_r;
buffer[c] += (int32_t) out_l;
buffer[c + 1] += (int32_t) out_r;
}
sb->opl.reset_buffer(sb->opl.priv);
if (sb->dsp.sb_type == SBPRO)
sb->opl2.reset_buffer(sb->opl2.priv);
}
void
sbpro_filter_cd_audio(int channel, double *buffer, void *priv)
{
const sb_t *sb = (sb_t *) priv;
const sb_ct1345_mixer_t *mixer = &sb->mixer_sbpro;
const double cd = channel ? mixer->cd_r : mixer->cd_l;
const double master = channel ? mixer->master_r : mixer->master_l;
double c = ((*buffer * cd) / 3.0) * master;
*buffer = c;
}
static void
sb_get_buffer_sb16_awe32(int32_t *buffer, int len, void *priv)
{
sb_t *sb = (sb_t *) priv;
const sb_ct1745_mixer_t *mixer = &sb->mixer_sb16;
double bass_treble;
sb_dsp_update(&sb->dsp);
for (int c = 0; c < len * 2; c += 2) {
double out_l = 0.0;
double out_r = 0.0;
if (mixer->output_filter) {
/* We divide by 3 to get the volume down to normal. */
out_l += (low_fir_sb16(0, 0, (double) sb->dsp.buffer[c]) * mixer->voice_l) / 3.0;
out_r += (low_fir_sb16(0, 1, (double) sb->dsp.buffer[c + 1]) * mixer->voice_r) / 3.0;
} else {
out_l += (((double) sb->dsp.buffer[c]) * mixer->voice_l) / 3.0;
out_r += (((double) sb->dsp.buffer[c + 1]) * mixer->voice_r) / 3.0;
}
out_l *= mixer->master_l;
out_r *= mixer->master_r;
/* This is not exactly how one does bass/treble controls, but the end result is like it.
A better implementation would reduce the CPU usage. */
if (mixer->bass_l != 8) {
bass_treble = sb_bass_treble_4bits[mixer->bass_l];
if (mixer->bass_l > 8)
out_l += (low_iir(0, 0, out_l) * bass_treble);
else
out_l = (out_l *bass_treble + low_cut_iir(0, 0, out_l) * (1.0 - bass_treble));
}
if (mixer->bass_r != 8) {
bass_treble = sb_bass_treble_4bits[mixer->bass_r];
if (mixer->bass_r > 8)
out_r += (low_iir(0, 1, out_r) * bass_treble);
else
out_r = (out_r *bass_treble + low_cut_iir(0, 1, out_r) * (1.0 - bass_treble));
}
if (mixer->treble_l != 8) {
bass_treble = sb_bass_treble_4bits[mixer->treble_l];
if (mixer->treble_l > 8)
out_l += (high_iir(0, 0, out_l) * bass_treble);
else
out_l = (out_l *bass_treble + high_cut_iir(0, 0, out_l) * (1.0 - bass_treble));
}
if (mixer->treble_r != 8) {
bass_treble = sb_bass_treble_4bits[mixer->treble_r];
if (mixer->treble_r > 8)
out_r += (high_iir(0, 1, out_r) * bass_treble);
else
out_r = (out_l *bass_treble + high_cut_iir(0, 1, out_r) * (1.0 - bass_treble));
}
buffer[c] += (int32_t) (out_l * mixer->output_gain_L);
buffer[c + 1] += (int32_t) (out_r * mixer->output_gain_R);
}
sb->dsp.pos = 0;
}
static void
sb_get_music_buffer_sb16_awe32(int32_t *buffer, const int len, void *priv)
{
sb_t *sb = (sb_t *) priv;
const sb_ct1745_mixer_t *mixer = &sb->mixer_sb16;
const int dsp_rec_pos = sb->dsp.record_pos_write;
double bass_treble;
const int32_t *opl_buf = NULL;
if (sb->opl_enabled)
opl_buf = sb->opl.update(sb->opl.priv);
for (int c = 0; c < len * 2; c += 2) {
double out_l = 0.0;
double out_r = 0.0;
if (sb->opl_enabled) {
out_l = ((double) opl_buf[c]) * mixer->fm_l * 0.7171630859375;
out_r = ((double) opl_buf[c + 1]) * mixer->fm_r * 0.7171630859375;
}
/* TODO: Multi-recording mic with agc/+20db, CD, and line in with channel inversion */
int32_t in_l = (mixer->input_selector_left & INPUT_MIDI_L) ?
((int32_t) out_l) : 0 + (mixer->input_selector_left & INPUT_MIDI_R) ? ((int32_t) out_r) : 0;
int32_t in_r = (mixer->input_selector_right & INPUT_MIDI_L) ?
((int32_t) out_l) : 0 + (mixer->input_selector_right & INPUT_MIDI_R) ? ((int32_t) out_r) : 0;
out_l *= mixer->master_l;
out_r *= mixer->master_r;
/* This is not exactly how one does bass/treble controls, but the end result is like it.
A better implementation would reduce the CPU usage. */
if (mixer->bass_l != 8) {
bass_treble = sb_bass_treble_4bits[mixer->bass_l];
if (mixer->bass_l > 8)
out_l += (low_iir(1, 0, out_l) * bass_treble);
else
out_l = (out_l *bass_treble + low_cut_iir(1, 0, out_l) * (1.0 - bass_treble));
}
if (mixer->bass_r != 8) {
bass_treble = sb_bass_treble_4bits[mixer->bass_r];
if (mixer->bass_r > 8)
out_r += (low_iir(1, 1, out_r) * bass_treble);
else
out_r = (out_r *bass_treble + low_cut_iir(1, 1, out_r) * (1.0 - bass_treble));
}
if (mixer->treble_l != 8) {
bass_treble = sb_bass_treble_4bits[mixer->treble_l];
if (mixer->treble_l > 8)
out_l += (high_iir(1, 0, out_l) * bass_treble);
else
out_l = (out_l *bass_treble + high_cut_iir(1, 0, out_l) * (1.0 - bass_treble));
}
if (mixer->treble_r != 8) {
bass_treble = sb_bass_treble_4bits[mixer->treble_r];
if (mixer->treble_r > 8)
out_r += (high_iir(1, 1, out_r) * bass_treble);
else
out_r = (out_l *bass_treble + high_cut_iir(1, 1, out_r) * (1.0 - bass_treble));
}
if (sb->dsp.sb_enable_i) {
const int c_record = dsp_rec_pos + ((c * sb->dsp.sb_freq) / MUSIC_FREQ);
in_l <<= mixer->input_gain_L;
in_r <<= mixer->input_gain_R;
/* Clip signal */
if (in_l < -32768)
in_l = -32768;
else if (in_l > 32767)
in_l = 32767;
if (in_r < -32768)
in_r = -32768;
else if (in_r > 32767)
in_r = 32767;
sb->dsp.record_buffer[c_record & 0xffff] = (int16_t) in_l;
sb->dsp.record_buffer[(c_record + 1) & 0xffff] = (int16_t) in_r;
}
buffer[c] += (int32_t) (out_l * mixer->output_gain_L);
buffer[c + 1] += (int32_t) (out_r * mixer->output_gain_R);
}
sb->dsp.record_pos_write += ((len * sb->dsp.sb_freq) / 24000);
sb->dsp.record_pos_write &= 0xffff;
if (sb->opl_enabled)
sb->opl.reset_buffer(sb->opl.priv);
}
static void
sb_get_wavetable_buffer_sb16_awe32(int32_t *buffer, const int len, void *priv)
{
sb_t *sb = (sb_t *) priv;
const sb_ct1745_mixer_t *mixer = &sb->mixer_sb16;
double bass_treble;
emu8k_update(&sb->emu8k);
for (int c = 0; c < len * 2; c += 2) {
double out_l = 0.0;
double out_r = 0.0;
out_l += (((double) sb->emu8k.buffer[c]) * mixer->fm_l);
out_r += (((double) sb->emu8k.buffer[c + 1]) * mixer->fm_r);
out_l *= mixer->master_l;
out_r *= mixer->master_r;
/* This is not exactly how one does bass/treble controls, but the end result is like it.
A better implementation would reduce the CPU usage. */
if (mixer->bass_l != 8) {
bass_treble = sb_bass_treble_4bits[mixer->bass_l];
if (mixer->bass_l > 8)
out_l += (low_iir(4, 0, out_l) * bass_treble);
else
out_l = (out_l *bass_treble + low_cut_iir(4, 0, out_l) * (1.0 - bass_treble));
}
if (mixer->bass_r != 8) {
bass_treble = sb_bass_treble_4bits[mixer->bass_r];
if (mixer->bass_r > 8)
out_r += (low_iir(4, 1, out_r) * bass_treble);
else
out_r = (out_r *bass_treble + low_cut_iir(4, 1, out_r) * (1.0 - bass_treble));
}
if (mixer->treble_l != 8) {
bass_treble = sb_bass_treble_4bits[mixer->treble_l];
if (mixer->treble_l > 8)
out_l += (high_iir(4, 0, out_l) * bass_treble);
else
out_l = (out_l *bass_treble + high_cut_iir(4, 0, out_l) * (1.0 - bass_treble));
}
if (mixer->treble_r != 8) {
bass_treble = sb_bass_treble_4bits[mixer->treble_r];
if (mixer->treble_r > 8)
out_r += (high_iir(4, 1, out_r) * bass_treble);
else
out_r = (out_l *bass_treble + high_cut_iir(4, 1, out_r) * (1.0 - bass_treble));
}
buffer[c] += (int32_t) (out_l * mixer->output_gain_L);
buffer[c + 1] += (int32_t) (out_r * mixer->output_gain_R);
}
sb->emu8k.pos = 0;
}
void
sb16_awe32_filter_cd_audio(int channel, double *buffer, void *priv)
{
const sb_t *sb = (sb_t *) priv;
const sb_ct1745_mixer_t *mixer = &sb->mixer_sb16;
const double cd = channel ? mixer->cd_r : mixer->cd_l /* / 3.0 */;
const double master = channel ? mixer->master_r : mixer->master_l;
const int32_t bass = channel ? mixer->bass_r : mixer->bass_l;
const int32_t treble = channel ? mixer->treble_r : mixer->treble_l;
const double output_gain = (channel ? mixer->output_gain_R : mixer->output_gain_L);
double bass_treble;
double c = (((*buffer) * cd) / 3.0) * master;
/* This is not exactly how one does bass/treble controls, but the end result is like it.
A better implementation would reduce the CPU usage. */
if (bass != 8) {
bass_treble = sb_bass_treble_4bits[bass];
if (bass > 8)
c += (low_iir(2, channel, c) * bass_treble);
else
c = (c * bass_treble + low_cut_iir(2, channel, c) * (1.0 - bass_treble));
}
if (treble != 8) {
bass_treble = sb_bass_treble_4bits[treble];
if (treble > 8)
c += (high_iir(2, channel, c) * bass_treble);
else
c = (c * bass_treble + high_cut_iir(2, channel, c) * (1.0 - bass_treble));
}
*buffer = c * output_gain;
}
void
sb16_awe32_filter_pc_speaker(int channel, double *buffer, void *priv)
{
const sb_t *sb = (sb_t *) priv;
const sb_ct1745_mixer_t *mixer = &sb->mixer_sb16;
const double spk = mixer->speaker;
const double master = channel ? mixer->master_r : mixer->master_l;
const int32_t bass = channel ? mixer->bass_r : mixer->bass_l;
const int32_t treble = channel ? mixer->treble_r : mixer->treble_l;
const double output_gain = (channel ? mixer->output_gain_R : mixer->output_gain_L);
double bass_treble;
double c;
if (mixer->output_filter)
c = (low_fir_sb16(3, channel, *buffer) * spk) / 3.0;
else
c = ((*buffer) * spk) / 3.0;
c *= master;
/* This is not exactly how one does bass/treble controls, but the end result is like it.
A better implementation would reduce the CPU usage. */
if (bass != 8) {
bass_treble = sb_bass_treble_4bits[bass];
if (bass > 8)
c += (low_iir(3, channel, c) * bass_treble);
else
c = (c * bass_treble + low_cut_iir(3, channel, c) * (1.0 - bass_treble));
}
if (treble != 8) {
bass_treble = sb_bass_treble_4bits[treble];
if (treble > 8)
c += (high_iir(3, channel, c) * bass_treble);
else
c = (c * bass_treble + high_cut_iir(3, channel, c) * (1.0 - bass_treble));
}
*buffer = c * output_gain;
}
void
sb_get_buffer_ess(int32_t *buffer, int len, void *priv)
{
sb_t *ess = (sb_t *) priv;
const ess_mixer_t *mixer = &ess->mixer_ess;
sb_dsp_update(&ess->dsp);
for (int c = 0; c < len * 2; c += 2) {
double out_l = 0.0;
double out_r = 0.0;
/* TODO: Implement the stereo switch on the mixer instead of on the dsp? */
if (mixer->output_filter) {
out_l += (low_fir_sb16(0, 0, (double) ess->dsp.buffer[c]) * mixer->voice_l) / 3.0;
out_r += (low_fir_sb16(0, 1, (double) ess->dsp.buffer[c + 1]) * mixer->voice_r) / 3.0;
} else {
out_l += (ess->dsp.buffer[c] * mixer->voice_l) / 3.0;
out_r += (ess->dsp.buffer[c + 1] * mixer->voice_r) / 3.0;
}
/* TODO: recording from the mixer. */
out_l *= mixer->master_l;
out_r *= mixer->master_r;
buffer[c] += (int32_t) out_l;
buffer[c + 1] += (int32_t) out_r;
}
ess->dsp.pos = 0;
}
void
sb_get_music_buffer_ess(int32_t *buffer, int len, void *priv)
{
sb_t *ess = (sb_t *) priv;
const ess_mixer_t *mixer = &ess->mixer_ess;
double out_l = 0.0;
double out_r = 0.0;
const int32_t *opl_buf = NULL;
opl_buf = ess->opl.update(ess->opl.priv);
for (int c = 0; c < len * 2; c += 2) {
out_l = 0.0;
out_r = 0.0;
out_l = (((double) opl_buf[c]) * mixer->fm_l) * 0.7171630859375;
out_r = (((double) opl_buf[c + 1]) * mixer->fm_r) * 0.7171630859375;
if (ess->opl_mix && ess->opl_mixer)
ess->opl_mix(ess->opl_mixer, &out_l, &out_r);
/* TODO: recording from the mixer. */
out_l *= mixer->master_l;
out_r *= mixer->master_r;
buffer[c] += (int32_t) out_l;
buffer[c + 1] += (int32_t) out_r;
}
ess->opl.reset_buffer(ess->opl.priv);
}
void
ess_filter_cd_audio(int channel, double *buffer, void *priv)
{
const sb_t *ess = (sb_t *) priv;
const ess_mixer_t *mixer = &ess->mixer_ess;
double c;
double cd = channel ? mixer->cd_r : mixer->cd_l;
double master = channel ? mixer->master_r : mixer->master_l;
/* TODO: recording from the mixer. */
c = (*buffer * cd) / 3.0;
*buffer = c * master;
}
void
ess_filter_pc_speaker(int channel, double *buffer, void *priv)
{
const sb_t *ess = (sb_t *) priv;
const ess_mixer_t *mixer = &ess->mixer_ess;
double c;
double spk = mixer->speaker;
double master = channel ? mixer->master_r : mixer->master_l;
if (mixer->output_filter)
c = (low_fir_sb16(3, channel, *buffer) * spk) / 3.0;
else
c = ((*buffer) * spk) / 3.0;
c *= master;
*buffer = c;
}
void
sb_ct1335_mixer_write(uint16_t addr, uint8_t val, void *priv)
{
sb_t *sb = (sb_t *) priv;
sb_ct1335_mixer_t *mixer = &sb->mixer_sb2;
if (!(addr & 1)) {
mixer->index = val;
mixer->regs[0x01] = val;
} else {
if (mixer->index == 0) {
/* Reset */
mixer->regs[0x02] = mixer->regs[0x06] = 0x08;
mixer->regs[0x08] = 0x00;
/* Changed default from -46dB to 0dB*/
mixer->regs[0x0a] = 0x06;
} else {
mixer->regs[mixer->index] = val;
switch (mixer->index) {
case 0x00:
case 0x02:
case 0x06:
case 0x08:
case 0x0a:
break;
default:
sb_log("sb_ct1335: Unknown register WRITE: %02X\t%02X\n", mixer->index, mixer->regs[mixer->index]);
break;
}
}
mixer->master = sb_att_4dbstep_3bits[(mixer->regs[0x02] >> 1) & 0x7] / 32768.0;
mixer->fm = sb_att_4dbstep_3bits[(mixer->regs[0x06] >> 1) & 0x7] / 32768.0;
mixer->cd = sb_att_4dbstep_3bits[(mixer->regs[0x08] >> 1) & 0x7] / 32768.0;
mixer->voice = sb_att_7dbstep_2bits[(mixer->regs[0x0a] >> 1) & 0x3] / 32768.0;
}
}
uint8_t
sb_ct1335_mixer_read(uint16_t addr, void *priv)
{
const sb_t *sb = (sb_t *) priv;
const sb_ct1335_mixer_t *mixer = &sb->mixer_sb2;
if (!(addr & 1))
return mixer->index;
switch (mixer->index) {
case 0x00:
case 0x02:
case 0x06:
case 0x08:
case 0x0A:
return mixer->regs[mixer->index];
default:
sb_log("sb_ct1335: Unknown register READ: %02X\t%02X\n", mixer->index, mixer->regs[mixer->index]);
break;
}
return 0xff;
}
void
sb_ct1335_mixer_reset(sb_t *sb)
{
sb_ct1335_mixer_write(0x254, 0, sb);
sb_ct1335_mixer_write(0x255, 0, sb);
}
void
sb_ct1345_mixer_write(uint16_t addr, uint8_t val, void *priv)
{
sb_t *sb = (sb_t *) priv;
sb_ct1345_mixer_t *mixer = (sb == NULL) ? NULL : &sb->mixer_sbpro;
if (mixer == NULL)
return;
if (!(addr & 1)) {
mixer->index = val;
mixer->regs[0x01] = val;
} else {
if (mixer->index == 0) {
/* Reset */
mixer->regs[0x0a] = mixer->regs[0x0c] = 0x00;
mixer->regs[0x0e] = 0x00;
/* Changed default from -11dB to 0dB */
mixer->regs[0x04] = mixer->regs[0x22] = 0xee;
mixer->regs[0x26] = mixer->regs[0x28] = 0xee;
mixer->regs[0x2e] = 0x00;
sb_dsp_set_stereo(&sb->dsp, mixer->regs[0x0e] & 2);
} else {
mixer->regs[mixer->index] = val;
switch (mixer->index) {
/* Compatibility: chain registers 0x02 and 0x22 as well as 0x06 and 0x26 */
case 0x02:
case 0x06:
case 0x08:
mixer->regs[mixer->index + 0x20] = ((val & 0xe) << 4) | (val & 0xe);
break;
case 0x22:
case 0x26:
case 0x28:
mixer->regs[mixer->index - 0x20] = (val & 0xe);
break;
/* More compatibility:
SoundBlaster Pro selects register 020h for 030h, 022h for 032h,
026h for 036h, and 028h for 038h. */
case 0x30:
case 0x32:
case 0x36:
case 0x38:
mixer->regs[mixer->index - 0x10] = (val & 0xee);
break;
case 0x00:
case 0x04:
case 0x0a:
case 0x0c:
case 0x0e:
case 0x2e:
break;
default:
sb_log("sb_ct1345: Unknown register WRITE: %02X\t%02X\n", mixer->index, mixer->regs[mixer->index]);
break;
}
}
mixer->voice_l = sb_att_4dbstep_3bits[(mixer->regs[0x04] >> 5) & 0x7] / 32768.0;
mixer->voice_r = sb_att_4dbstep_3bits[(mixer->regs[0x04] >> 1) & 0x7] / 32768.0;
mixer->master_l = sb_att_4dbstep_3bits[(mixer->regs[0x22] >> 5) & 0x7] / 32768.0;
mixer->master_r = sb_att_4dbstep_3bits[(mixer->regs[0x22] >> 1) & 0x7] / 32768.0;
mixer->fm_l = sb_att_4dbstep_3bits[(mixer->regs[0x26] >> 5) & 0x7] / 32768.0;
mixer->fm_r = sb_att_4dbstep_3bits[(mixer->regs[0x26] >> 1) & 0x7] / 32768.0;
mixer->cd_l = sb_att_4dbstep_3bits[(mixer->regs[0x28] >> 5) & 0x7] / 32768.0;
mixer->cd_r = sb_att_4dbstep_3bits[(mixer->regs[0x28] >> 1) & 0x7] / 32768.0;
mixer->line_l = sb_att_4dbstep_3bits[(mixer->regs[0x2e] >> 5) & 0x7] / 32768.0;
mixer->line_r = sb_att_4dbstep_3bits[(mixer->regs[0x2e] >> 1) & 0x7] / 32768.0;
mixer->mic = sb_att_7dbstep_2bits[(mixer->regs[0x0a] >> 1) & 0x3] / 32768.0;
mixer->output_filter = !(mixer->regs[0xe] & 0x20);
mixer->input_filter = !(mixer->regs[0xc] & 0x20);
mixer->in_filter_freq = ((mixer->regs[0xc] & 0x8) == 0) ? 3200 : 8800;
mixer->stereo = mixer->regs[0xe] & 2;
if (mixer->index == 0xe)
sb_dsp_set_stereo(&sb->dsp, val & 2);
switch (mixer->regs[0xc] & 6) {
case 2:
mixer->input_selector = INPUT_CD_L | INPUT_CD_R;
break;
case 6:
mixer->input_selector = INPUT_LINE_L | INPUT_LINE_R;
break;
default:
mixer->input_selector = INPUT_MIC;
break;
}
/* TODO: pcspeaker volume? Or is it not worth? */
}
}
uint8_t
sb_ct1345_mixer_read(uint16_t addr, void *priv)
{
const sb_t *sb = (sb_t *) priv;
const sb_ct1345_mixer_t *mixer = &sb->mixer_sbpro;
if (!(addr & 1))
return mixer->index;
switch (mixer->index) {
case 0x00:
case 0x04:
case 0x0a:
case 0x0c:
case 0x0e:
case 0x22:
case 0x26:
case 0x28:
case 0x2e:
case 0x02:
case 0x06:
case 0x30:
case 0x32:
case 0x36:
case 0x38:
return mixer->regs[mixer->index];
default:
sb_log("sb_ct1345: Unknown register READ: %02X\t%02X\n", mixer->index, mixer->regs[mixer->index]);
break;
}
return 0xff;
}
void
sb_ct1345_mixer_reset(sb_t *sb)
{
sb_ct1345_mixer_write(4, 0, sb);
sb_ct1345_mixer_write(5, 0, sb);
}
void
sb_ct1745_mixer_write(uint16_t addr, uint8_t val, void *priv)
{
sb_t *sb = (sb_t *) priv;
sb_ct1745_mixer_t *mixer = (sb == NULL) ? NULL : &sb->mixer_sb16;
if (mixer == NULL)
return;
if (!(addr & 1))
mixer->index = val;
else {
/* DESCRIPTION:
Contains previously selected register value. Mixer Data Register value.
NOTES:
SoundBlaster 16 sets bit 7 if previous mixer index invalid.
Status bytes initially 080h on startup for all but level bytes (SB16). */
sb_log("CT1745: [W] %02X = %02X\n", mixer->index, val);
if (mixer->index == 0) {
/* Reset: Changed defaults from -14dB to 0dB */
mixer->regs[0x30] = mixer->regs[0x31] = 0xf8;
mixer->regs[0x32] = mixer->regs[0x33] = 0xf8;
mixer->regs[0x34] = mixer->regs[0x35] = 0xf8;
mixer->regs[0x36] = mixer->regs[0x37] = 0xf8;
mixer->regs[0x38] = mixer->regs[0x39] = 0x00;
mixer->regs[0x3a] = 0x00;
/* Speaker control - it appears to be in steps of 64. */
mixer->regs[0x3b] = 0x80;
mixer->regs[0x3c] = (OUTPUT_MIC | OUTPUT_CD_R | OUTPUT_CD_L | OUTPUT_LINE_R | OUTPUT_LINE_L);
mixer->regs[0x3d] = (INPUT_MIC | INPUT_CD_L | INPUT_LINE_L | INPUT_MIDI_L);
mixer->regs[0x3e] = (INPUT_MIC | INPUT_CD_R | INPUT_LINE_R | INPUT_MIDI_R);
mixer->regs[0x3f] = mixer->regs[0x40] = 0x00;
mixer->regs[0x41] = mixer->regs[0x42] = 0x00;
mixer->regs[0x44] = mixer->regs[0x45] = 0x80;
mixer->regs[0x46] = mixer->regs[0x47] = 0x80;
/* 0x43 = Mic AGC (Automatic Gain Control?) according to Linux's sb.h.
NSC LM4560 datasheet: Bit 0: 1 = Enable, 0 = Disable;
Another source says this: Bit 0: 0 = AGC on (default), 1 = Fixed gain of 20 dB. */
mixer->regs[0x43] = 0x00;
mixer->regs[0x49] = mixer->regs[0x4a] = 0x80;
mixer->regs[0x83] = 0xff;
sb->dsp.sb_irqm8 = 0;
sb->dsp.sb_irqm16 = 0;
sb->dsp.sb_irqm401 = 0;
mixer->regs[0xfd] = 0x10;
mixer->regs[0xfe] = 0x06;
mixer->regs[0xff] = sb->dsp.sb_16_dma_supported ? 0x05 : 0x03;