-
Notifications
You must be signed in to change notification settings - Fork 90
/
audio.cpp
2992 lines (2687 loc) · 78.1 KB
/
audio.cpp
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
/*
* UAE - The Un*x Amiga Emulator
*
* Paula audio emulation
*
* Copyright 1995, 1996, 1997 Bernd Schmidt
* Copyright 1996 Marcus Sundberg
* Copyright 1996 Manfred Thole
* Copyright 2006 Toni Wilen
*
* new filter algorithm and anti&sinc interpolators by Antti S. Lankila
*
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "memory.h"
#include "custom.h"
#include "newcpu.h"
#include "autoconf.h"
#include "gensound.h"
#include "audio.h"
#include "sounddep/sound.h"
#include "events.h"
#include "savestate.h"
#ifdef DRIVESOUND
#include "driveclick.h"
#endif
#include "zfile.h"
#include "uae.h"
#include "gui.h"
#include "xwin.h"
#include "debug.h"
#include "sndboard.h"
#ifdef AVIOUTPUT
#include "avioutput.h"
#endif
#ifdef AHI
#include "traps.h"
#include "ahidsound.h"
#include "ahidsound_new.h"
#endif
#include "threaddep/thread.h"
#include <math.h>
#define DEBUG_AUDIO 0
#define DEBUG_AUDIO2 0
#define DEBUG_AUDIO_HACK 0
#define DEBUG_CHANNEL_MASK 15
#define TEST_AUDIO 0
#define TEST_MISSED_DMA 0
#define TEST_MANUAL_AUDIO 0
#define PERIOD_MIN 1
#define PERIOD_MIN_NONCE 60
#define PERIOD_MIN_LOOP 16
#define PERIOD_MIN_LOOP_COUNT 64
#define PERIOD_LOW 124
int audio_channel_mask = 15;
volatile bool cd_audio_mode_changed;
STATIC_INLINE bool isaudio (void)
{
return currprefs.produce_sound != 0;
}
#if DEBUG_AUDIO > 0 || DEBUG_AUDIO_HACK > 0 || DEBUG_AUDIO2 > 0
static bool debugchannel (int ch)
{
return ((1 << ch) & DEBUG_CHANNEL_MASK) != 0;
}
#endif
STATIC_INLINE bool usehacks(void)
{
return !(currprefs.cs_hacks & 8) && (currprefs.cpu_model >= 68020 || currprefs.m68k_speed != 0 || (currprefs.cs_hacks & 4));
}
#define SINC_QUEUE_MAX_AGE 2048
/* Queue length 256 implies minimum emulated period of 8. This should be
* sufficient for all imaginable purposes. This must be power of two. */
#define SINC_QUEUE_LENGTH 256
#include "sinctable.cpp"
typedef struct {
int time, output;
} sinc_queue_t;
struct audio_channel_data2
{
int current_sample, last_sample;
uae_u8 new_sample;
int sample_accum, sample_accum_time;
int sinc_output_state;
sinc_queue_t sinc_queue[SINC_QUEUE_LENGTH];
int sinc_queue_time;
int sinc_queue_head;
int audvol;
int mixvol;
unsigned int adk_mask;
};
struct audio_stream_data
{
bool active;
unsigned int evtime;
struct audio_channel_data2 data[AUDIO_CHANNEL_MAX_STREAM_CH];
SOUND_STREAM_CALLBACK cb;
void *cb_data;
};
#define FIR_WIDTH 512
#define VOLCNT_BUFFER_SIZE 4096
union sIntFlt { uae_u32 U32; float F32; };
static float firmem[2 * FIR_WIDTH + 1];
struct audio_channel_data
{
uae_u32 evtime;
bool dmaenstore;
bool intreq2;
int irqcheck;
bool dr;
bool dsr;
bool pbufldl;
int drhpos;
bool dat_written;
#if TEST_MISSED_DMA
bool dat_loaded;
#endif
#if TEST_MANUAL_AUDIO
bool mdat_loaded;
#endif
uaecptr lc, pt;
int state;
int per;
int len, wlen;
int volcnt;
uae_u16 dat, dat2;
struct audio_channel_data2 data;
#if TEST_AUDIO > 0
bool hisample, losample;
bool have_dat;
int per_original;
#endif
/* too fast cpu fixes */
uaecptr ptx;
bool ptx_written;
bool ptx_tofetch;
int dmaofftime_active;
int dmaofftime_cpu_cnt;
uaecptr dmaofftime_pc;
int minperloop;
int volcntbufcnt;
float volcntbuf[VOLCNT_BUFFER_SIZE];
};
static int audio_extra_streams[AUDIO_CHANNEL_STREAMS];
static int audio_total_extra_streams;
static int samplecnt;
#if SOUNDSTUFF > 0
static int extrasamples, outputsample, doublesample;
#endif
int sampleripper_enabled;
struct ripped_sample
{
struct ripped_sample *next;
uae_u8 *sample;
int len, per, changed;
};
static struct ripped_sample *ripped_samples;
void write_wavheader (struct zfile *wavfile, size_t size, uae_u32 freq)
{
uae_u16 tw;
size_t tl;
int bits = 8, channels = 1;
zfile_fseek (wavfile, 0, SEEK_SET);
zfile_fwrite ("RIFF", 1, 4, wavfile);
tl = 0;
if (size)
tl = size - 8;
zfile_fwrite (&tl, 1, 4, wavfile);
zfile_fwrite ("WAVEfmt ", 1, 8, wavfile);
tl = 16;
zfile_fwrite (&tl, 1, 4, wavfile);
tw = 1;
zfile_fwrite (&tw, 1, 2, wavfile);
tw = channels;
zfile_fwrite (&tw, 1, 2, wavfile);
tl = freq;
zfile_fwrite (&tl, 1, 4, wavfile);
tl = freq * channels * bits / 8;
zfile_fwrite (&tl, 1, 4, wavfile);
tw = channels * bits / 8;
zfile_fwrite (&tw, 1, 2, wavfile);
tw = bits;
zfile_fwrite (&tw, 1, 2, wavfile);
zfile_fwrite ("data", 1, 4, wavfile);
tl = 0;
if (size)
tl = size - 44;
zfile_fwrite (&tl, 1, 4, wavfile);
}
static void convertsample (uae_u8 *sample, int len)
{
int i;
for (i = 0; i < len; i++)
sample[i] += 0x80;
}
static void namesplit (TCHAR *s)
{
int l;
l = uaetcslen(s) - 1;
while (l >= 0) {
if (s[l] == '.')
s[l] = 0;
if (s[l] == '\\' || s[l] == '/' || s[l] == ':' || s[l] == '?') {
l++;
break;
}
l--;
}
if (l > 0)
memmove (s, s + l, (_tcslen (s + l) + 1) * sizeof (TCHAR));
}
void audio_sampleripper (int mode)
{
struct ripped_sample *rs = ripped_samples;
int cnt = 1;
TCHAR path[MAX_DPATH], name[MAX_DPATH], filename[MAX_DPATH];
TCHAR underline[] = _T("_");
TCHAR extension[4];
struct zfile *wavfile;
if (mode < 0) {
while (rs) {
struct ripped_sample *next = rs->next;
xfree(rs);
rs = next;
}
ripped_samples = NULL;
return;
}
while (rs) {
if (rs->changed) {
int type = -1;
rs->changed = 0;
fetch_ripperpath (path, sizeof (path) / sizeof (TCHAR));
name[0] = 0;
if (currprefs.floppyslots[0].dfxtype >= 0) {
_tcscpy(name, currprefs.floppyslots[0].df);
type = PATH_FLOPPY;
} else if (currprefs.cdslots[0].inuse) {
_tcscpy(name, currprefs.cdslots[0].name);
type = PATH_CD;
}
if (!name[0])
underline[0] = 0;
if (type >= 0)
cfgfile_resolve_path_load(name, sizeof(name) / sizeof(TCHAR), type);
namesplit (name);
_tcscpy (extension, _T("wav"));
_stprintf (filename, _T("%s%s%s%03d.%s"), path, name, underline, cnt, extension);
wavfile = zfile_fopen (filename, _T("wb"), 0);
if (wavfile) {
int freq = rs->per > 0 ? (currprefs.ntscmode ? 3579545 : 3546895 / rs->per) : 8000;
write_wavheader (wavfile, 0, 0);
convertsample (rs->sample, rs->len);
zfile_fwrite (rs->sample, rs->len, 1, wavfile);
convertsample (rs->sample, rs->len);
write_wavheader (wavfile, zfile_ftell32(wavfile), freq);
zfile_fclose (wavfile);
write_log (_T("SAMPLERIPPER: %d: %dHz %d bytes\n"), cnt, freq, rs->len);
} else {
write_log (_T("SAMPLERIPPER: failed to open '%s'\n"), filename);
}
}
cnt++;
rs = rs->next;
}
}
static void do_samplerip (struct audio_channel_data *adp)
{
struct ripped_sample *rs = ripped_samples, *prev;
int len = adp->wlen * 2;
uae_u8 *smp = chipmem_xlate_indirect (adp->pt);
int cnt = 0, i;
if (!smp || !chipmem_check_indirect (adp->pt, len))
return;
for (i = 0; i < len; i++) {
if (smp[i] != 0)
break;
}
if (i == len || len <= 2)
return;
prev = NULL;
while(rs) {
if (rs->sample) {
if (len == rs->len && !memcmp (rs->sample, smp, len))
break;
/* replace old identical but shorter sample */
if (len > rs->len && !memcmp (rs->sample, smp, rs->len)) {
xfree (rs->sample);
rs->sample = xmalloc (uae_u8, len);
memcpy (rs->sample, smp, len);
write_log (_T("SAMPLERIPPER: replaced sample %d (%d -> %d)\n"), cnt, rs->len, len);
rs->len = len;
rs->per = adp->per / CYCLE_UNIT;
rs->changed = 1;
audio_sampleripper (0);
return;
}
}
prev = rs;
rs = rs->next;
cnt++;
}
if (rs || cnt > 100)
return;
rs = xmalloc (struct ripped_sample ,1);
if (prev)
prev->next = rs;
else
ripped_samples = rs;
rs->len = len;
rs->per = adp->per / CYCLE_UNIT;
rs->sample = xmalloc (uae_u8, len);
memcpy (rs->sample, smp, len);
rs->next = NULL;
rs->changed = 1;
write_log (_T("SAMPLERIPPER: sample added (%06X, %d bytes), total %d samples\n"), adp->pt, len, ++cnt);
audio_sampleripper (0);
}
static struct audio_channel_data audio_channel[AUDIO_CHANNELS_PAULA];
static struct audio_stream_data audio_stream[AUDIO_CHANNEL_STREAMS];
static struct audio_channel_data2 *audio_data[AUDIO_CHANNELS_PAULA + AUDIO_CHANNEL_STREAMS * AUDIO_CHANNEL_MAX_STREAM_CH];
int sound_available = 0;
void (*sample_handler) (void);
static void(*sample_prehandler) (unsigned long best_evtime);
static void(*extra_sample_prehandler) (unsigned long best_evtime);
float sample_evtime;
float scaled_sample_evtime;
int sound_cd_volume[2];
int sound_paula_volume[2];
static evt_t last_cycles;
static float next_sample_evtime;
static int previous_volcnt_update;
typedef uae_s8 sample8_t;
#define DO_CHANNEL_1(v, c) do { (v) *= audio_channel[c].data.mixvol; } while (0)
#define SBASEVAL16(logn) ((logn) == 1 ? SOUND16_BASE_VAL >> 1 : SOUND16_BASE_VAL)
STATIC_INLINE int FINISH_DATA (int data, int bits, int ch)
{
if (bits < 16) {
int shift = 16 - bits;
data <<= shift;
} else {
int shift = bits - 16;
data >>= shift;
}
data = data * sound_paula_volume[ch] / 32768;
return data;
}
static uae_u32 right_word_saved[SOUND_MAX_DELAY_BUFFER];
static uae_u32 left_word_saved[SOUND_MAX_DELAY_BUFFER];
static uae_u32 right2_word_saved[SOUND_MAX_DELAY_BUFFER];
static uae_u32 left2_word_saved[SOUND_MAX_DELAY_BUFFER];
static int saved_ptr, saved_ptr2;
static int mixed_on, mixed_stereo_size, mixed_mul1, mixed_mul2;
static int led_filter_forced, sound_use_filter, sound_use_filter_sinc, led_filter_on;
#define PAULARATE 3740000
static float Sinc(float x)
{
return x ? sinf(x) / x : 1;
}
static float Hamming(float x)
{
float pi = 4 * atanf(1);
float v;
if (x > -1 && x < 1)
v = cosf(x * pi / 2);
else
v = 0;
return v * v;
}
static void makefir(void)
{
float pi = 4 * atanf(1);
float *FIRTable = firmem + FIR_WIDTH;
float yscale = float(currprefs.sound_freq) / float(PAULARATE);
float xscale = pi * yscale;
for (int i = -FIR_WIDTH; i <= FIR_WIDTH; i++)
FIRTable[i] = yscale * Sinc(float(i) * xscale) * Hamming(float(i) / float(FIR_WIDTH - 1));
}
/* denormals are very small floating point numbers that force FPUs into slow
mode. All lowpass filters using floats are suspectible to denormals unless
a small offset is added to avoid very small floating point numbers. */
#define DENORMAL_OFFSET (1E-10)
static struct filter_state {
float rc1, rc2, rc3, rc4, rc5;
} sound_filter_state[AUDIO_CHANNELS_PAULA];
static float a500e_filter1_a0;
static float a500e_filter2_a0;
static float filter_a0; /* a500 and a1200 use the same */
enum {
FILTER_NONE = 0,
FILTER_MODEL_A500,
FILTER_MODEL_A1200,
FILTER_MODEL_A500_FIXEDONLY
};
/* Amiga has two separate filtering circuits per channel, a static RC filter
* on A500 and the LED filter. This code emulates both.
*
* The Amiga filtering circuitry depends on Amiga model. Older Amigas seem
* to have a 6 dB/oct RC filter with cutoff frequency such that the -6 dB
* point for filter is reached at 6 kHz, while newer Amigas have no filtering.
*
* The LED filter is complicated, and we are modelling it with a pair of
* RC filters, the other providing a highboost. The LED starts to cut
* into signal somewhere around 5-6 kHz, and there's some kind of highboost
* in effect above 12 kHz. Better measurements are required.
*
* The current filtering should be accurate to 2 dB with the filter on,
* and to 1 dB with the filter off.
*/
static int filter (int input, struct filter_state *fs)
{
int o;
float normal_output, led_output;
input = (uae_s16)input;
switch (sound_use_filter) {
case FILTER_MODEL_A500:
fs->rc1 = (float)(a500e_filter1_a0 * input + (1.0f - a500e_filter1_a0) * fs->rc1 + DENORMAL_OFFSET);
fs->rc2 = a500e_filter2_a0 * fs->rc1 + (1.0f - a500e_filter2_a0) * fs->rc2;
normal_output = fs->rc2;
fs->rc3 = filter_a0 * normal_output + (1 - filter_a0) * fs->rc3;
fs->rc4 = filter_a0 * fs->rc3 + (1 - filter_a0) * fs->rc4;
fs->rc5 = filter_a0 * fs->rc4 + (1 - filter_a0) * fs->rc5;
led_output = fs->rc5;
break;
case FILTER_MODEL_A500_FIXEDONLY:
fs->rc1 = (float)(a500e_filter1_a0 * input + (1.0f - a500e_filter1_a0) * fs->rc1 + DENORMAL_OFFSET);
fs->rc2 = a500e_filter2_a0 * fs->rc1 + (1.0f - a500e_filter2_a0) * fs->rc2;
normal_output = fs->rc2;
led_output = fs->rc2;
break;
case FILTER_MODEL_A1200:
normal_output = (float)input;
fs->rc2 = (float)(filter_a0 * normal_output + (1 - filter_a0) * fs->rc2 + DENORMAL_OFFSET);
fs->rc3 = filter_a0 * fs->rc2 + (1 - filter_a0) * fs->rc3;
fs->rc4 = filter_a0 * fs->rc3 + (1 - filter_a0) * fs->rc4;
led_output = fs->rc4;
break;
case FILTER_NONE:
default:
return input;
}
if (led_filter_on)
o = (int)led_output;
else
o = (int)normal_output;
if (o > 32767)
o = 32767;
else if (o < -32768)
o = -32768;
return o;
}
/* Always put the right word before the left word. */
static void put_sound_word_right (uae_u32 w)
{
if (mixed_on) {
right_word_saved[saved_ptr] = w;
return;
}
PUT_SOUND_WORD(w);
}
static void put_sound_word_left (uae_u32 w)
{
if (mixed_on) {
uae_u32 rold, lold, rnew, lnew, tmp;
left_word_saved[saved_ptr] = w;
lnew = w - SOUND16_BASE_VAL;
rnew = right_word_saved[saved_ptr] - SOUND16_BASE_VAL;
saved_ptr = (saved_ptr + 1) & mixed_stereo_size;
lold = left_word_saved[saved_ptr] - SOUND16_BASE_VAL;
tmp = (rnew * mixed_mul2 + lold * mixed_mul1) / MIXED_STEREO_SCALE;
tmp += SOUND16_BASE_VAL;
rold = right_word_saved[saved_ptr] - SOUND16_BASE_VAL;
w = (lnew * mixed_mul2 + rold * mixed_mul1) / MIXED_STEREO_SCALE;
PUT_SOUND_WORD(tmp);
PUT_SOUND_WORD(w);
} else {
PUT_SOUND_WORD(w);
}
}
static void put_sound_word_right2 (uae_u32 w)
{
if (mixed_on) {
right2_word_saved[saved_ptr2] = w;
return;
}
PUT_SOUND_WORD(w);
}
static void put_sound_word_left2 (uae_u32 w)
{
if (mixed_on) {
uae_u32 rold, lold, rnew, lnew, tmp;
left2_word_saved[saved_ptr2] = w;
lnew = w - SOUND16_BASE_VAL;
rnew = right2_word_saved[saved_ptr2] - SOUND16_BASE_VAL;
saved_ptr2 = (saved_ptr2 + 1) & mixed_stereo_size;
lold = left2_word_saved[saved_ptr2] - SOUND16_BASE_VAL;
tmp = (rnew * mixed_mul2 + lold * mixed_mul1) / MIXED_STEREO_SCALE;
tmp += SOUND16_BASE_VAL;
rold = right2_word_saved[saved_ptr2] - SOUND16_BASE_VAL;
w = (lnew * mixed_mul2 + rold * mixed_mul1) / MIXED_STEREO_SCALE;
PUT_SOUND_WORD(tmp);
PUT_SOUND_WORD(w);
} else {
PUT_SOUND_WORD(w);
}
}
static void anti_prehandler (unsigned long best_evtime)
{
int i, output;
struct audio_channel_data2 *acd;
/* Handle accumulator antialiasiation */
for (i = 0; audio_data[i]; i++) {
acd = audio_data[i];
output = (acd->current_sample * acd->mixvol) & acd->adk_mask;
acd->sample_accum += output * best_evtime;
acd->sample_accum_time += best_evtime;
}
}
static void samplexx_anti_handler (int *datasp, int ch_start, int ch_num)
{
int i, j;
for (i = ch_start, j = 0; j < ch_num; i++, j++) {
struct audio_channel_data2 *acd = audio_data[i];
datasp[j] = acd->sample_accum_time ? (acd->sample_accum / acd->sample_accum_time) : 0;
acd->sample_accum = 0;
acd->sample_accum_time = 0;
}
}
static void sinc_prehandler_paula (unsigned long best_evtime)
{
int i, output;
struct audio_channel_data2 *acd;
for (i = 0; i < AUDIO_CHANNELS_PAULA; i++) {
acd = audio_data[i];
int vol = acd->mixvol;
output = (acd->current_sample * vol) & acd->adk_mask;
/* if output state changes, record the state change and also
* write data into sinc queue for mixing in the BLEP */
if (acd->sinc_output_state != output) {
acd->sinc_queue_head = (acd->sinc_queue_head - 1) & (SINC_QUEUE_LENGTH - 1);
acd->sinc_queue[acd->sinc_queue_head].time = acd->sinc_queue_time;
acd->sinc_queue[acd->sinc_queue_head].output = output - acd->sinc_output_state;
acd->sinc_output_state = output;
}
acd->sinc_queue_time += best_evtime;
}
}
/* this interpolator performs BLEP mixing (bleps are shaped like integrated sinc
* functions) with a type of BLEP that matches the filtering configuration. */
static void samplexx_sinc_handler (int *datasp, int ch_start, int ch_num)
{
int n, i, k;
int const *winsinc;
if (sound_use_filter_sinc && ch_start == 0) {
n = (sound_use_filter_sinc == FILTER_MODEL_A500 || sound_use_filter_sinc == FILTER_MODEL_A500_FIXEDONLY) ? 0 : 2;
if (led_filter_on)
n += 1;
} else {
n = 4;
}
winsinc = winsinc_integral[n];
for (i = ch_start, k = 0; k < ch_num; i++, k++) {
int j, v;
struct audio_channel_data2 *acd = audio_data[i];
/* The sum rings with harmonic components up to infinity... */
int sum = acd->sinc_output_state << 17;
/* ...but we cancel them through mixing in BLEPs instead */
int offsetpos = acd->sinc_queue_head & (SINC_QUEUE_LENGTH - 1);
for (j = 0; j < SINC_QUEUE_LENGTH; j += 1) {
int age = acd->sinc_queue_time - acd->sinc_queue[offsetpos].time;
if (age >= SINC_QUEUE_MAX_AGE || age < 0)
break;
sum -= winsinc[age] * acd->sinc_queue[offsetpos].output;
offsetpos = (offsetpos + 1) & (SINC_QUEUE_LENGTH - 1);
}
v = sum >> 15;
if (v > 32767)
v = 32767;
else if (v < -32768)
v = -32768;
datasp[k] = v;
}
}
static void do_filter(int *data, int num)
{
if (currprefs.sound_filter)
*data = filter(*data, &sound_filter_state[num]);
}
static void get_extra_channels(int *data1, int *data2, int sample1, int sample2)
{
int d1 = *data1 + sample1;
int d2 = (data2 ? *data2 : 0) + sample2;
if (d1 < -32768)
d1 = -32768;
if (d1 > 32767)
d1 = 32767;
if (d2 < -32768)
d2 = -32768;
if (d2 > 32767)
d2 = 32767;
int needswap = currprefs.sound_stereo_swap_paula ^ currprefs.sound_stereo_swap_ahi;
if (needswap) {
*data1 = d2;
if (data2)
*data2 = d1;
} else {
*data1 = d1;
if (data2)
*data2 = d2;
}
}
static void do_extra_channels(int idx, int ch, int *data1, int *data2, int *data3, int *data4, int *data5, int *data6)
{
idx += AUDIO_CHANNELS_PAULA;
if (ch == 2) {
int datas[2];
samplexx_anti_handler(datas, idx, 2);
get_extra_channels(data1, data2, datas[0], datas[1]);
} else if (ch == 1) {
int datas[1];
samplexx_anti_handler(datas, idx, 1);
int d1 = *data1 + datas[0];
if (d1 < -32768)
d1 = -32768;
if (d1 > 32767)
d1 = 32767;
*data1 = d1;
if (data2)
*data2 = d1;
} else if (ch > 2) {
int datas[AUDIO_CHANNEL_MAX_STREAM_CH];
samplexx_anti_handler(datas, idx, 6);
get_extra_channels(data1, data2, datas[0], datas[1]);
if (data3 && data4)
get_extra_channels(data3, data4, datas[2], datas[3]);
if (data5 && data6)
get_extra_channels(data5, data6, datas[4], datas[5]);
}
}
static void get_extra_channels_sample2(int *data1, int *data2, int mode)
{
if (!audio_total_extra_streams)
return;
int idx = 0;
for (int i = 0; i < AUDIO_CHANNEL_STREAMS; i++) {
int ch = audio_extra_streams[i];
if (ch) {
do_extra_channels(idx, ch, data1, data2, NULL, NULL, NULL, NULL);
idx += ch;
}
}
}
static void get_extra_channels_sample6(int *data1, int *data2, int *data3, int *data4, int *data5, int *data6, int mode)
{
if (!audio_total_extra_streams)
return;
int idx = 0;
for (int i = 0; i < AUDIO_CHANNEL_STREAMS; i++) {
int ch = audio_extra_streams[i];
if (ch) {
do_extra_channels(idx, ch, data1, data2, data3, data4, data5, data6);
idx += ch;
}
}
}
static void set_sound_buffers(void)
{
#if SOUNDSTUFF > 1
paula_sndbufpt_prev = paula_sndbufpt_start;
paula_sndbufpt_start = paula_sndbufpt;
#endif
}
static void clear_sound_buffers(void)
{
memset(paula_sndbuffer, 0, paula_sndbufsize);
paula_sndbufpt = paula_sndbuffer;
}
static void check_sound_buffers(void)
{
#if SOUNDSTUFF > 1
int len;
#endif
if (active_sound_stereo == SND_4CH_CLONEDSTEREO) {
((uae_u16 *)paula_sndbufpt)[0] = ((uae_u16 *)paula_sndbufpt)[-2];
((uae_u16 *)paula_sndbufpt)[1] = ((uae_u16 *)paula_sndbufpt)[-1];
paula_sndbufpt = (uae_u16 *)(((uae_u8 *)paula_sndbufpt) + 2 * 2);
} else if (active_sound_stereo == SND_6CH_CLONEDSTEREO) {
uae_s16 *p = ((uae_s16 *)paula_sndbufpt);
uae_s32 sum;
p[2] = p[-2];
p[3] = p[-1];
sum = (uae_s32)(p[-2]) + (uae_s32)(p[-1]) + (uae_s32)(p[2]) + (uae_s32)(p[3]);
p[0] = sum / 8;
p[1] = sum / 8;
paula_sndbufpt = (uae_u16 *)(((uae_u8 *)paula_sndbufpt) + 4 * 2);
} else if (active_sound_stereo == SND_8CH_CLONEDSTEREO) {
uae_s16 *p = ((uae_s16 *)paula_sndbufpt);
uae_s32 sum;
p[2] = p[-2];
p[3] = p[-1];
p[4] = p[-2];
p[5] = p[-1];
sum = (uae_s32)(p[-2]) + (uae_s32)(p[-1]) + (uae_s32)(p[2]) + (uae_s32)(p[3]);
p[0] = sum / 8;
p[1] = sum / 8;
paula_sndbufpt = (uae_u16 *)(((uae_u8 *)paula_sndbufpt) + 6 * 2);
}
#if SOUNDSTUFF > 1
if (outputsample == 0)
return;
len = paula_sndbufpt - paula_sndbufpt_start;
if (outputsample < 0) {
int i;
uae_s16 *p1 = (uae_s16 *)paula_sndbufpt_prev;
uae_s16 *p2 = (uae_s16 *)paula_sndbufpt_start;
for (i = 0; i < len; i++) {
*p1 = (*p1 + *p2) / 2;
}
paula_sndbufpt = paula_sndbufpt_start;
}
#endif
if ((uae_u8 *)paula_sndbufpt - (uae_u8 *)paula_sndbuffer >= paula_sndbufsize) {
finish_sound_buffer();
}
#if SOUNDSTUFF > 1
while (doublesample-- > 0) {
memcpy(paula_sndbufpt, paula_sndbufpt_start, len * 2);
paula_sndbufpt += len;
if ((uae_u8 *)paula_sndbufpt - (uae_u8 *)paula_sndbuffer >= paula_sndbufsize) {
finish_sound_buffer();
paula_sndbufpt = paula_sndbuffer;
}
}
#endif
}
static void sample16i_sinc_handler (void)
{
int datas[AUDIO_CHANNELS_PAULA], data1;
samplexx_sinc_handler (datas, 0, AUDIO_CHANNELS_PAULA);
data1 = datas[0] + datas[3] + datas[1] + datas[2];
data1 = FINISH_DATA (data1, 18, 0);
do_filter(&data1, 0);
get_extra_channels_sample2(&data1, NULL, 2);
set_sound_buffers ();
PUT_SOUND_WORD_MONO (data1);
check_sound_buffers ();
}
void sample16_handler (void)
{
int data0 = audio_channel[0].data.current_sample;
int data1 = audio_channel[1].data.current_sample;
int data2 = audio_channel[2].data.current_sample;
int data3 = audio_channel[3].data.current_sample;
int data;
DO_CHANNEL_1 (data0, 0);
DO_CHANNEL_1 (data1, 1);
DO_CHANNEL_1 (data2, 2);
DO_CHANNEL_1 (data3, 3);
data0 &= audio_channel[0].data.adk_mask;
data1 &= audio_channel[1].data.adk_mask;
data2 &= audio_channel[2].data.adk_mask;
data3 &= audio_channel[3].data.adk_mask;
data0 += data1;
data0 += data2;
data0 += data3;
data = SBASEVAL16(2) + data0;
data = FINISH_DATA (data, 16, 0);
do_filter(&data, 0);
get_extra_channels_sample2(&data, NULL, 0);
set_sound_buffers ();
PUT_SOUND_WORD_MONO (data);
check_sound_buffers ();
}
/* This interpolator examines sample points when Paula switches the output
* voltage and computes the average of Paula's output */
static void sample16i_anti_handler (void)
{
int datas[AUDIO_CHANNELS_PAULA], data1;
samplexx_anti_handler (datas, 0, AUDIO_CHANNELS_PAULA);
data1 = datas[0] + datas[3] + datas[1] + datas[2];
data1 = FINISH_DATA (data1, 16, 0);
do_filter(&data1, 0);
get_extra_channels_sample2(&data1, NULL, 1);
set_sound_buffers ();
PUT_SOUND_WORD_MONO (data1);
check_sound_buffers ();
}
static void sample16i_rh_handler (void)
{
unsigned long delta, ratio;
int data0 = audio_channel[0].data.current_sample;
int data1 = audio_channel[1].data.current_sample;
int data2 = audio_channel[2].data.current_sample;
int data3 = audio_channel[3].data.current_sample;
int data0p = audio_channel[0].data.last_sample;
int data1p = audio_channel[1].data.last_sample;
int data2p = audio_channel[2].data.last_sample;
int data3p = audio_channel[3].data.last_sample;
int data;
DO_CHANNEL_1 (data0, 0);
DO_CHANNEL_1 (data1, 1);
DO_CHANNEL_1 (data2, 2);
DO_CHANNEL_1 (data3, 3);
DO_CHANNEL_1 (data0p, 0);
DO_CHANNEL_1 (data1p, 1);
DO_CHANNEL_1 (data2p, 2);
DO_CHANNEL_1 (data3p, 3);
data0 &= audio_channel[0].data.adk_mask;
data0p &= audio_channel[0].data.adk_mask;
data1 &= audio_channel[1].data.adk_mask;
data1p &= audio_channel[1].data.adk_mask;
data2 &= audio_channel[2].data.adk_mask;
data2p &= audio_channel[2].data.adk_mask;
data3 &= audio_channel[3].data.adk_mask;
data3p &= audio_channel[3].data.adk_mask;
/* linear interpolation and summing up... */
delta = audio_channel[0].per;
ratio = ((audio_channel[0].evtime % delta) << 8) / delta;
data0 = (data0 * (256 - ratio) + data0p * ratio) >> 8;
delta = audio_channel[1].per;
ratio = ((audio_channel[1].evtime % delta) << 8) / delta;
data0 += (data1 * (256 - ratio) + data1p * ratio) >> 8;
delta = audio_channel[2].per;
ratio = ((audio_channel[2].evtime % delta) << 8) / delta;
data0 += (data2 * (256 - ratio) + data2p * ratio) >> 8;
delta = audio_channel[3].per;
ratio = ((audio_channel[3].evtime % delta) << 8) / delta;
data0 += (data3 * (256 - ratio) + data3p * ratio) >> 8;
data = SBASEVAL16(2) + data0;
data = FINISH_DATA (data, 16, 0);
do_filter(&data, 0);
get_extra_channels_sample2(&data, NULL, 0);
set_sound_buffers ();
PUT_SOUND_WORD_MONO (data);
check_sound_buffers ();
}
static void sample16i_crux_handler (void)
{
int data0 = audio_channel[0].data.current_sample;
int data1 = audio_channel[1].data.current_sample;
int data2 = audio_channel[2].data.current_sample;
int data3 = audio_channel[3].data.current_sample;
int data0p = audio_channel[0].data.last_sample;
int data1p = audio_channel[1].data.last_sample;
int data2p = audio_channel[2].data.last_sample;
int data3p = audio_channel[3].data.last_sample;
int data;
DO_CHANNEL_1 (data0, 0);
DO_CHANNEL_1 (data1, 1);
DO_CHANNEL_1 (data2, 2);
DO_CHANNEL_1 (data3, 3);
DO_CHANNEL_1 (data0p, 0);
DO_CHANNEL_1 (data1p, 1);
DO_CHANNEL_1 (data2p, 2);
DO_CHANNEL_1 (data3p, 3);
data0 &= audio_channel[0].data.adk_mask;
data0p &= audio_channel[0].data.adk_mask;
data1 &= audio_channel[1].data.adk_mask;
data1p &= audio_channel[1].data.adk_mask;
data2 &= audio_channel[2].data.adk_mask;
data2p &= audio_channel[2].data.adk_mask;
data3 &= audio_channel[3].data.adk_mask;
data3p &= audio_channel[3].data.adk_mask;
{
struct audio_channel_data *cdp;
unsigned long ratio, ratio1;
#define INTERVAL ((int)(scaled_sample_evtime * 3))
cdp = audio_channel + 0;
ratio1 = cdp->per - cdp->evtime;
ratio = (ratio1 << 12) / INTERVAL;
if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL)
ratio = 4096;
data0 = (data0 * ratio + data0p * (4096 - ratio)) >> 12;
cdp = audio_channel + 1;
ratio1 = cdp->per - cdp->evtime;
ratio = (ratio1 << 12) / INTERVAL;
if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL)