1 /*
2 * UAE - The Un*x Amiga Emulator
3 *
4 * Paula audio emulation
5 *
6 * Copyright 1995, 1996, 1997 Bernd Schmidt
7 * Copyright 1996 Marcus Sundberg
8 * Copyright 1996 Manfred Thole
9 * Copyright 2006 Toni Wilen
10 *
11 * new filter algorithm and anti&sinc interpolators by Antti S. Lankila
12 *
13 */
14
15 #include "sysconfig.h"
16 #include "sysdeps.h"
17
18 #include "options.h"
19 #include "uae/memory.h"
20 #include "custom.h"
21 #include "newcpu.h"
22 #include "autoconf.h"
23 #include "gensound.h"
24 #include "audio.h"
25 #include "sounddep/sound.h"
26 #include "events.h"
27 #include "savestate.h"
28 #include "driveclick.h"
29 #include "zfile.h"
30 #include "uae.h"
31 #include "gui.h"
32 #include "xwin.h"
33 #include "debug.h"
34 #include "sndboard.h"
35 #ifdef AVIOUTPUT
36 #include "avioutput.h"
37 #endif
38 #ifdef AHI
39 #include "traps.h"
40 #include "ahidsound.h"
41 #include "ahidsound_new.h"
42 #endif
43 #include "threaddep/thread.h"
44
45 #include <math.h>
46
47 #define DEBUG_AUDIO 0
48 #define DEBUG_AUDIO_HACK 0
49 #define DEBUG_CHANNEL_MASK 15
50 #define TEST_AUDIO 0
51
52 #define PERIOD_MIN 4
53 #define PERIOD_MIN_NONCE 60
54
55 int audio_channel_mask = 15;
56
isaudio(void)57 STATIC_INLINE bool isaudio (void)
58 {
59 return currprefs.produce_sound != 0;
60 }
61
62 #if DEBUG_AUDIO > 0 || DEBUG_AUDIO_HACK > 0
debugchannel(int ch)63 static bool debugchannel (int ch)
64 {
65 return ((1 << ch) & DEBUG_CHANNEL_MASK) != 0;
66 }
67 #endif
68
usehacks(void)69 STATIC_INLINE bool usehacks(void)
70 {
71 return currprefs.cpu_model >= 68020 || currprefs.m68k_speed != 0 || (currprefs.cs_hacks & 4);
72 }
73
74 #define SINC_QUEUE_MAX_AGE 2048
75 /* Queue length 256 implies minimum emulated period of 8. This should be
76 * sufficient for all imaginable purposes. This must be power of two. */
77 #define SINC_QUEUE_LENGTH 256
78
79 #include "sinctable.cpp"
80
81 typedef struct {
82 int time, output;
83 } sinc_queue_t;
84
85 struct audio_channel_data {
86 unsigned int adk_mask;
87 unsigned int evtime;
88 bool dmaenstore;
89 bool intreq2;
90 bool dr;
91 bool dsr;
92 bool pbufldl;
93 int drhpos;
94 bool dat_written;
95 uaecptr lc, pt;
96 int current_sample, last_sample;
97 int state;
98 int per;
99 int vol;
100 int len, wlen;
101 uae_u16 dat, dat2;
102 int sample_accum, sample_accum_time;
103 int sinc_output_state;
104 sinc_queue_t sinc_queue[SINC_QUEUE_LENGTH];
105 int sinc_queue_time;
106 int sinc_queue_head;
107 #if TEST_AUDIO > 0
108 bool hisample, losample;
109 bool have_dat;
110 int per_original;
111 #endif
112 /* too fast cpu fixes */
113 uaecptr ptx;
114 bool ptx_written;
115 bool ptx_tofetch;
116 int dmaofftime_active;
117 };
118
119 static int audio_channel_count = AUDIO_CHANNELS_PAULA;
120 static bool audio_extra_channels[2];
121
122 static int samplecnt;
123 #if SOUNDSTUFF > 0
124 static int extrasamples, outputsample, doublesample;
125 #endif
126
127 int sampleripper_enabled;
128 struct ripped_sample
129 {
130 struct ripped_sample *next;
131 uae_u8 *sample;
132 int len, per, changed;
133 };
134
135 static struct ripped_sample *ripped_samples;
136
write_wavheader(struct zfile * wavfile,uae_u32 size,uae_u32 freq)137 void write_wavheader (struct zfile *wavfile, uae_u32 size, uae_u32 freq)
138 {
139 uae_u16 tw;
140 uae_u32 tl;
141 int bits = 8, channels = 1;
142
143 zfile_fseek (wavfile, 0, SEEK_SET);
144 zfile_fwrite ("RIFF", 1, 4, wavfile);
145 tl = 0;
146 if (size)
147 tl = size - 8;
148 zfile_fwrite (&tl, 1, 4, wavfile);
149 zfile_fwrite ("WAVEfmt ", 1, 8, wavfile);
150 tl = 16;
151 zfile_fwrite (&tl, 1, 4, wavfile);
152 tw = 1;
153 zfile_fwrite (&tw, 1, 2, wavfile);
154 tw = channels;
155 zfile_fwrite (&tw, 1, 2, wavfile);
156 tl = freq;
157 zfile_fwrite (&tl, 1, 4, wavfile);
158 tl = freq * channels * bits / 8;
159 zfile_fwrite (&tl, 1, 4, wavfile);
160 tw = channels * bits / 8;
161 zfile_fwrite (&tw, 1, 2, wavfile);
162 tw = bits;
163 zfile_fwrite (&tw, 1, 2, wavfile);
164 zfile_fwrite ("data", 1, 4, wavfile);
165 tl = 0;
166 if (size)
167 tl = size - 44;
168 zfile_fwrite (&tl, 1, 4, wavfile);
169 }
170
convertsample(uae_u8 * sample,int len)171 static void convertsample (uae_u8 *sample, int len)
172 {
173 int i;
174 for (i = 0; i < len; i++)
175 sample[i] += 0x80;
176 }
177
namesplit(TCHAR * s)178 static void namesplit (TCHAR *s)
179 {
180 int l;
181
182 l = _tcslen (s) - 1;
183 while (l >= 0) {
184 if (s[l] == '.')
185 s[l] = 0;
186 if (s[l] == '\\' || s[l] == '/' || s[l] == ':' || s[l] == '?') {
187 l++;
188 break;
189 }
190 l--;
191 }
192 if (l > 0)
193 memmove (s, s + l, (_tcslen (s + l) + 1) * sizeof (TCHAR));
194 }
195
audio_sampleripper(int mode)196 void audio_sampleripper (int mode)
197 {
198 struct ripped_sample *rs = ripped_samples;
199 int cnt = 1;
200 TCHAR path[MAX_DPATH], name[MAX_DPATH], filename[MAX_DPATH];
201 TCHAR underline[] = _T("_");
202 TCHAR extension[4];
203 struct zfile *wavfile;
204
205 if (mode < 0) {
206 while (rs) {
207 struct ripped_sample *next = rs->next;
208 xfree(rs);
209 rs = next;
210 }
211 ripped_samples = NULL;
212 return;
213 }
214
215 while (rs) {
216 if (rs->changed) {
217 rs->changed = 0;
218 fetch_ripperpath (path, sizeof (path) / sizeof (TCHAR));
219 name[0] = 0;
220 if (currprefs.floppyslots[0].dfxtype >= 0)
221 _tcscpy (name, currprefs.floppyslots[0].df);
222 else if (currprefs.cdslots[0].inuse)
223 _tcscpy (name, currprefs.cdslots[0].name);
224 if (!name[0])
225 underline[0] = 0;
226 namesplit (name);
227 _tcscpy (extension, _T("wav"));
228 _stprintf (filename, _T("%s%s%s%03d.%s"), path, name, underline, cnt, extension);
229 wavfile = zfile_fopen (filename, _T("wb"), 0);
230 if (wavfile) {
231 int freq = rs->per > 0 ? (currprefs.ntscmode ? 3579545 : 3546895 / rs->per) : 8000;
232 write_wavheader (wavfile, 0, 0);
233 convertsample (rs->sample, rs->len);
234 zfile_fwrite (rs->sample, rs->len, 1, wavfile);
235 convertsample (rs->sample, rs->len);
236 write_wavheader (wavfile, zfile_ftell(wavfile), freq);
237 zfile_fclose (wavfile);
238 write_log (_T("SAMPLERIPPER: %d: %dHz %d bytes\n"), cnt, freq, rs->len);
239 } else {
240 write_log (_T("SAMPLERIPPER: failed to open '%s'\n"), filename);
241 }
242 }
243 cnt++;
244 rs = rs->next;
245 }
246 }
247
do_samplerip(struct audio_channel_data * adp)248 static void do_samplerip (struct audio_channel_data *adp)
249 {
250 struct ripped_sample *rs = ripped_samples, *prev;
251 int len = adp->wlen * 2;
252 uae_u8 *smp = chipmem_xlate_indirect (adp->pt);
253 int cnt = 0, i;
254
255 if (!smp || !chipmem_check_indirect (adp->pt, len))
256 return;
257 for (i = 0; i < len; i++) {
258 if (smp[i] != 0)
259 break;
260 }
261 if (i == len || len <= 2)
262 return;
263 prev = NULL;
264 while(rs) {
265 if (rs->sample) {
266 if (len == rs->len && !memcmp (rs->sample, smp, len))
267 break;
268 /* replace old identical but shorter sample */
269 if (len > rs->len && !memcmp (rs->sample, smp, rs->len)) {
270 xfree (rs->sample);
271 rs->sample = xmalloc (uae_u8, len);
272 memcpy (rs->sample, smp, len);
273 write_log (_T("SAMPLERIPPER: replaced sample %d (%d -> %d)\n"), cnt, rs->len, len);
274 rs->len = len;
275 rs->per = adp->per / CYCLE_UNIT;
276 rs->changed = 1;
277 audio_sampleripper (0);
278 return;
279 }
280 }
281 prev = rs;
282 rs = rs->next;
283 cnt++;
284 }
285 if (rs || cnt > 100)
286 return;
287 rs = xmalloc (struct ripped_sample ,1);
288 if (prev)
289 prev->next = rs;
290 else
291 ripped_samples = rs;
292 rs->len = len;
293 rs->per = adp->per / CYCLE_UNIT;
294 rs->sample = xmalloc (uae_u8, len);
295 memcpy (rs->sample, smp, len);
296 rs->next = NULL;
297 rs->changed = 1;
298 write_log (_T("SAMPLERIPPER: sample added (%06X, %d bytes), total %d samples\n"), adp->pt, len, ++cnt);
299 audio_sampleripper (0);
300 }
301
302 static struct audio_channel_data audio_channel[AUDIO_CHANNELS_MAX];
303 int sound_available = 0;
304 void (*sample_handler) (void);
305 static void(*sample_prehandler) (unsigned long best_evtime);
306 static void(*extra_sample_prehandler) (unsigned long best_evtime);
307
308 float sample_evtime;
309 float scaled_sample_evtime;
310
311 int sound_cd_volume[2];
312 int sound_paula_volume[2];
313
314 static unsigned long last_cycles;
315 static float next_sample_evtime;
316
317 typedef uae_s8 sample8_t;
318 #define DO_CHANNEL_1(v, c) do { (v) *= audio_channel[c].vol; } while (0)
319 #define SBASEVAL16(logn) ((logn) == 1 ? SOUND16_BASE_VAL >> 1 : SOUND16_BASE_VAL)
320
FINISH_DATA(int data,int bits,int ch)321 STATIC_INLINE int FINISH_DATA (int data, int bits, int ch)
322 {
323 if (bits == 16) {
324 return data;
325 } else if (bits - 16 > 0) {
326 data >>= bits - 16;
327 } else {
328 int shift = 16 - bits;
329 data <<= shift;
330 }
331 data = data * sound_paula_volume[ch] / 32768;
332 return data;
333 }
334
335 static uae_u32 right_word_saved[SOUND_MAX_DELAY_BUFFER];
336 static uae_u32 left_word_saved[SOUND_MAX_DELAY_BUFFER];
337 static uae_u32 right2_word_saved[SOUND_MAX_DELAY_BUFFER];
338 static uae_u32 left2_word_saved[SOUND_MAX_DELAY_BUFFER];
339 static int saved_ptr, saved_ptr2;
340
341 static int mixed_on, mixed_stereo_size, mixed_mul1, mixed_mul2;
342 static int led_filter_forced, sound_use_filter, sound_use_filter_sinc, led_filter_on;
343
344 /* denormals are very small floating point numbers that force FPUs into slow
345 mode. All lowpass filters using floats are suspectible to denormals unless
346 a small offset is added to avoid very small floating point numbers. */
347 #define DENORMAL_OFFSET (1E-10)
348
349 static struct filter_state {
350 float rc1, rc2, rc3, rc4, rc5;
351 } sound_filter_state[AUDIO_CHANNELS_PAULA];
352
353 static float a500e_filter1_a0;
354 static float a500e_filter2_a0;
355 static float filter_a0; /* a500 and a1200 use the same */
356
357 enum {
358 FILTER_NONE = 0,
359 FILTER_MODEL_A500,
360 FILTER_MODEL_A1200
361 };
362
363 /* Amiga has two separate filtering circuits per channel, a static RC filter
364 * on A500 and the LED filter. This code emulates both.
365 *
366 * The Amiga filtering circuitry depends on Amiga model. Older Amigas seem
367 * to have a 6 dB/oct RC filter with cutoff frequency such that the -6 dB
368 * point for filter is reached at 6 kHz, while newer Amigas have no filtering.
369 *
370 * The LED filter is complicated, and we are modelling it with a pair of
371 * RC filters, the other providing a highboost. The LED starts to cut
372 * into signal somewhere around 5-6 kHz, and there's some kind of highboost
373 * in effect above 12 kHz. Better measurements are required.
374 *
375 * The current filtering should be accurate to 2 dB with the filter on,
376 * and to 1 dB with the filter off.
377 */
378
filter(int input,struct filter_state * fs)379 static int filter (int input, struct filter_state *fs)
380 {
381 int o;
382 float normal_output, led_output;
383
384 input = (uae_s16)input;
385 switch (sound_use_filter) {
386
387 case FILTER_MODEL_A500:
388 fs->rc1 = a500e_filter1_a0 * input + (1 - a500e_filter1_a0) * fs->rc1 + DENORMAL_OFFSET;
389 fs->rc2 = a500e_filter2_a0 * fs->rc1 + (1-a500e_filter2_a0) * fs->rc2;
390 normal_output = fs->rc2;
391
392 fs->rc3 = filter_a0 * normal_output + (1 - filter_a0) * fs->rc3;
393 fs->rc4 = filter_a0 * fs->rc3 + (1 - filter_a0) * fs->rc4;
394 fs->rc5 = filter_a0 * fs->rc4 + (1 - filter_a0) * fs->rc5;
395
396 led_output = fs->rc5;
397 break;
398
399 case FILTER_MODEL_A1200:
400 normal_output = input;
401
402 fs->rc2 = filter_a0 * normal_output + (1 - filter_a0) * fs->rc2 + DENORMAL_OFFSET;
403 fs->rc3 = filter_a0 * fs->rc2 + (1 - filter_a0) * fs->rc3;
404 fs->rc4 = filter_a0 * fs->rc3 + (1 - filter_a0) * fs->rc4;
405
406 led_output = fs->rc4;
407 break;
408
409 case FILTER_NONE:
410 default:
411 return input;
412
413 }
414
415 if (led_filter_on)
416 o = led_output;
417 else
418 o = normal_output;
419
420 if (o > 32767)
421 o = 32767;
422 else if (o < -32768)
423 o = -32768;
424
425 return o;
426 }
427
428 /* Always put the right word before the left word. */
429
put_sound_word_right(uae_u32 w)430 static void put_sound_word_right (uae_u32 w)
431 {
432 if (mixed_on) {
433 right_word_saved[saved_ptr] = w;
434 return;
435 }
436 PUT_SOUND_WORD(w);
437 }
438
put_sound_word_left(uae_u32 w)439 static void put_sound_word_left (uae_u32 w)
440 {
441 if (mixed_on) {
442 uae_u32 rold, lold, rnew, lnew, tmp;
443
444 left_word_saved[saved_ptr] = w;
445 lnew = w - SOUND16_BASE_VAL;
446 rnew = right_word_saved[saved_ptr] - SOUND16_BASE_VAL;
447
448 saved_ptr = (saved_ptr + 1) & mixed_stereo_size;
449
450 lold = left_word_saved[saved_ptr] - SOUND16_BASE_VAL;
451 tmp = (rnew * mixed_mul2 + lold * mixed_mul1) / MIXED_STEREO_SCALE;
452 tmp += SOUND16_BASE_VAL;
453
454 rold = right_word_saved[saved_ptr] - SOUND16_BASE_VAL;
455 w = (lnew * mixed_mul2 + rold * mixed_mul1) / MIXED_STEREO_SCALE;
456
457 PUT_SOUND_WORD(tmp);
458 PUT_SOUND_WORD(w);
459 } else {
460 PUT_SOUND_WORD(w);
461 }
462 }
463
put_sound_word_right2(uae_u32 w)464 static void put_sound_word_right2 (uae_u32 w)
465 {
466 if (mixed_on) {
467 right2_word_saved[saved_ptr2] = w;
468 return;
469 }
470 PUT_SOUND_WORD(w);
471 }
472
put_sound_word_left2(uae_u32 w)473 static void put_sound_word_left2 (uae_u32 w)
474 {
475 if (mixed_on) {
476 uae_u32 rold, lold, rnew, lnew, tmp;
477
478 left2_word_saved[saved_ptr2] = w;
479 lnew = w - SOUND16_BASE_VAL;
480 rnew = right2_word_saved[saved_ptr2] - SOUND16_BASE_VAL;
481
482 saved_ptr2 = (saved_ptr2 + 1) & mixed_stereo_size;
483
484 lold = left2_word_saved[saved_ptr2] - SOUND16_BASE_VAL;
485 tmp = (rnew * mixed_mul2 + lold * mixed_mul1) / MIXED_STEREO_SCALE;
486 tmp += SOUND16_BASE_VAL;
487
488 rold = right2_word_saved[saved_ptr2] - SOUND16_BASE_VAL;
489 w = (lnew * mixed_mul2 + rold * mixed_mul1) / MIXED_STEREO_SCALE;
490
491 PUT_SOUND_WORD(tmp);
492 PUT_SOUND_WORD(w);
493 } else {
494 PUT_SOUND_WORD(w);
495 }
496 }
497
anti_prehandler(unsigned long best_evtime)498 static void anti_prehandler (unsigned long best_evtime)
499 {
500 int i, output;
501 struct audio_channel_data *acd;
502
503 /* Handle accumulator antialiasiation */
504 for (i = 0; i < audio_channel_count; i++) {
505 acd = &audio_channel[i];
506 output = (acd->current_sample * acd->vol) & acd->adk_mask;
507 acd->sample_accum += output * best_evtime;
508 acd->sample_accum_time += best_evtime;
509 }
510 }
511
samplexx_anti_handler(int * datasp,int ch_start,int ch_num)512 static void samplexx_anti_handler (int *datasp, int ch_start, int ch_num)
513 {
514 int i, j;
515 for (i = ch_start, j = 0; j < ch_num; i++, j++) {
516 datasp[j] = audio_channel[i].sample_accum_time ? (audio_channel[i].sample_accum / audio_channel[i].sample_accum_time) : 0;
517 audio_channel[i].sample_accum = 0;
518 audio_channel[i].sample_accum_time = 0;
519 }
520 }
521
sinc_prehandler_paula(unsigned long best_evtime)522 static void sinc_prehandler_paula (unsigned long best_evtime)
523 {
524 int i, output;
525 struct audio_channel_data *acd;
526
527 for (i = 0; i < AUDIO_CHANNELS_PAULA; i++) {
528 acd = &audio_channel[i];
529 int vol = acd->vol;
530 output = (acd->current_sample * vol) & acd->adk_mask;
531
532 /* if output state changes, record the state change and also
533 * write data into sinc queue for mixing in the BLEP */
534 if (acd->sinc_output_state != output) {
535 acd->sinc_queue_head = (acd->sinc_queue_head - 1) & (SINC_QUEUE_LENGTH - 1);
536 acd->sinc_queue[acd->sinc_queue_head].time = acd->sinc_queue_time;
537 acd->sinc_queue[acd->sinc_queue_head].output = output - acd->sinc_output_state;
538 acd->sinc_output_state = output;
539 }
540
541 acd->sinc_queue_time += best_evtime;
542 }
543 }
544
545 /* this interpolator performs BLEP mixing (bleps are shaped like integrated sinc
546 * functions) with a type of BLEP that matches the filtering configuration. */
samplexx_sinc_handler(int * datasp,int ch_start,int ch_num)547 static void samplexx_sinc_handler (int *datasp, int ch_start, int ch_num)
548 {
549 int n, i, k;
550 int const *winsinc;
551
552 if (sound_use_filter_sinc && ch_start == 0) {
553 n = (sound_use_filter_sinc == FILTER_MODEL_A500) ? 0 : 2;
554 if (led_filter_on)
555 n += 1;
556 } else {
557 n = 4;
558 }
559 winsinc = winsinc_integral[n];
560
561
562 for (i = ch_start, k = 0; k < ch_num; i++, k++) {
563 int j, v;
564 struct audio_channel_data *acd = &audio_channel[i];
565 /* The sum rings with harmonic components up to infinity... */
566 int sum = acd->sinc_output_state << 17;
567 /* ...but we cancel them through mixing in BLEPs instead */
568 int offsetpos = acd->sinc_queue_head & (SINC_QUEUE_LENGTH - 1);
569 for (j = 0; j < SINC_QUEUE_LENGTH; j += 1) {
570 int age = acd->sinc_queue_time - acd->sinc_queue[offsetpos].time;
571 if (age >= SINC_QUEUE_MAX_AGE || age < 0)
572 break;
573 sum -= winsinc[age] * acd->sinc_queue[offsetpos].output;
574 offsetpos = (offsetpos + 1) & (SINC_QUEUE_LENGTH - 1);
575 }
576 v = sum >> 15;
577 if (v > 32767)
578 v = 32767;
579 else if (v < -32768)
580 v = -32768;
581 datasp[k] = v;
582 }
583 }
584
do_filter(int * data,int num)585 static void do_filter(int *data, int num)
586 {
587 if (currprefs.sound_filter)
588 *data = filter(*data, &sound_filter_state[num]);
589 }
590
get_extra_channels(int * data1,int * data2,int sample1,int sample2)591 static void get_extra_channels(int *data1, int *data2, int sample1, int sample2)
592 {
593 int d1 = *data1 + sample1;
594 int d2 = *data2 + sample2;
595 if (d1 < -32768)
596 d1 = -32768;
597 if (d1 > 32767)
598 d1 = 32767;
599 if (d2 < -32768)
600 d2 = -32768;
601 if (d2 > 32767)
602 d2 = 32767;
603 int needswap = currprefs.sound_stereo_swap_paula ^ currprefs.sound_stereo_swap_ahi;
604 if (needswap) {
605 *data1 = d2;
606 *data2 = d1;
607 } else {
608 *data1 = d1;
609 *data2 = d2;
610 }
611 }
get_extra_channels_sample(int * data1,int * data2,int mode)612 static void get_extra_channels_sample(int *data1, int *data2, int mode)
613 {
614 if (audio_extra_channels[0]) {
615 int datas[2];
616 samplexx_anti_handler(datas, AUDIO_CHANNEL_SNDBOARD_LEFT, 2);
617 get_extra_channels(data1, data2, datas[0], datas[1]);
618 }
619 if (audio_extra_channels[1]) {
620 int datas[2];
621 samplexx_anti_handler(datas, AUDIO_CHANNEL_CDA_LEFT, 2);
622 get_extra_channels(data1, data2, datas[0], datas[1]);
623 }
624 }
get_extra_channels_sample_mono(int * data1,int mode)625 static void get_extra_channels_sample_mono(int *data1, int mode)
626 {
627 if (audio_extra_channels[0]) {
628 int datas[1];
629 samplexx_anti_handler(datas, AUDIO_CHANNEL_SNDBOARD_LEFT, 1);
630 int d1 = *data1 + datas[0];
631 if (d1 < -32768)
632 d1 = -32768;
633 if (d1 > 32767)
634 d1 = 32767;
635 *data1 = d1;
636 }
637 if (audio_extra_channels[1]) {
638 int datas[1];
639 samplexx_anti_handler(datas, AUDIO_CHANNEL_CDA_LEFT, 1);
640 int d1 = *data1 + datas[0];
641 if (d1 < -32768)
642 d1 = -32768;
643 if (d1 > 32767)
644 d1 = 32767;
645 *data1 = d1;
646 }
647 }
648
sample16i_sinc_handler(void)649 static void sample16i_sinc_handler (void)
650 {
651 int datas[AUDIO_CHANNELS_PAULA], data1;
652
653 samplexx_sinc_handler (datas, 0, AUDIO_CHANNELS_PAULA);
654 data1 = datas[0] + datas[3] + datas[1] + datas[2];
655 data1 = FINISH_DATA (data1, 18, 0);
656
657 do_filter(&data1, 0);
658
659 get_extra_channels_sample_mono(&data1, 2);
660
661 set_sound_buffers ();
662 PUT_SOUND_WORD_MONO (data1);
663 check_sound_buffers ();
664 }
665
sample16_handler(void)666 void sample16_handler (void)
667 {
668 int data0 = audio_channel[0].current_sample;
669 int data1 = audio_channel[1].current_sample;
670 int data2 = audio_channel[2].current_sample;
671 int data3 = audio_channel[3].current_sample;
672 int data;
673
674 DO_CHANNEL_1 (data0, 0);
675 DO_CHANNEL_1 (data1, 1);
676 DO_CHANNEL_1 (data2, 2);
677 DO_CHANNEL_1 (data3, 3);
678 data0 &= audio_channel[0].adk_mask;
679 data1 &= audio_channel[1].adk_mask;
680 data2 &= audio_channel[2].adk_mask;
681 data3 &= audio_channel[3].adk_mask;
682 data0 += data1;
683 data0 += data2;
684 data0 += data3;
685 data = SBASEVAL16(2) + data0;
686 data = FINISH_DATA (data, 16, 0);
687
688 do_filter(&data, 0);
689
690 get_extra_channels_sample_mono(&data, 0);
691
692 set_sound_buffers ();
693 PUT_SOUND_WORD_MONO (data);
694 check_sound_buffers ();
695 }
696
697 /* This interpolator examines sample points when Paula switches the output
698 * voltage and computes the average of Paula's output */
sample16i_anti_handler(void)699 static void sample16i_anti_handler (void)
700 {
701 int datas[AUDIO_CHANNELS_PAULA], data1;
702
703 samplexx_anti_handler (datas, 0, AUDIO_CHANNELS_PAULA);
704 data1 = datas[0] + datas[3] + datas[1] + datas[2];
705 data1 = FINISH_DATA (data1, 16, 0);
706
707 do_filter(&data1, 0);
708
709 get_extra_channels_sample_mono(&data1, 1);
710
711 set_sound_buffers ();
712 PUT_SOUND_WORD_MONO (data1);
713 check_sound_buffers ();
714 }
715
sample16i_rh_handler(void)716 static void sample16i_rh_handler (void)
717 {
718 unsigned long delta, ratio;
719
720 int data0 = audio_channel[0].current_sample;
721 int data1 = audio_channel[1].current_sample;
722 int data2 = audio_channel[2].current_sample;
723 int data3 = audio_channel[3].current_sample;
724 int data0p = audio_channel[0].last_sample;
725 int data1p = audio_channel[1].last_sample;
726 int data2p = audio_channel[2].last_sample;
727 int data3p = audio_channel[3].last_sample;
728 int data;
729
730 DO_CHANNEL_1 (data0, 0);
731 DO_CHANNEL_1 (data1, 1);
732 DO_CHANNEL_1 (data2, 2);
733 DO_CHANNEL_1 (data3, 3);
734 DO_CHANNEL_1 (data0p, 0);
735 DO_CHANNEL_1 (data1p, 1);
736 DO_CHANNEL_1 (data2p, 2);
737 DO_CHANNEL_1 (data3p, 3);
738
739 data0 &= audio_channel[0].adk_mask;
740 data0p &= audio_channel[0].adk_mask;
741 data1 &= audio_channel[1].adk_mask;
742 data1p &= audio_channel[1].adk_mask;
743 data2 &= audio_channel[2].adk_mask;
744 data2p &= audio_channel[2].adk_mask;
745 data3 &= audio_channel[3].adk_mask;
746 data3p &= audio_channel[3].adk_mask;
747
748 /* linear interpolation and summing up... */
749 delta = audio_channel[0].per;
750 ratio = ((audio_channel[0].evtime % delta) << 8) / delta;
751 data0 = (data0 * (256 - ratio) + data0p * ratio) >> 8;
752 delta = audio_channel[1].per;
753 ratio = ((audio_channel[1].evtime % delta) << 8) / delta;
754 data0 += (data1 * (256 - ratio) + data1p * ratio) >> 8;
755 delta = audio_channel[2].per;
756 ratio = ((audio_channel[2].evtime % delta) << 8) / delta;
757 data0 += (data2 * (256 - ratio) + data2p * ratio) >> 8;
758 delta = audio_channel[3].per;
759 ratio = ((audio_channel[3].evtime % delta) << 8) / delta;
760 data0 += (data3 * (256 - ratio) + data3p * ratio) >> 8;
761 data = SBASEVAL16(2) + data0;
762 data = FINISH_DATA (data, 16, 0);
763
764 do_filter(&data, 0);
765
766 get_extra_channels_sample_mono(&data, 0);
767
768 set_sound_buffers ();
769 PUT_SOUND_WORD_MONO (data);
770 check_sound_buffers ();
771 }
772
sample16i_crux_handler(void)773 static void sample16i_crux_handler (void)
774 {
775 int data0 = audio_channel[0].current_sample;
776 int data1 = audio_channel[1].current_sample;
777 int data2 = audio_channel[2].current_sample;
778 int data3 = audio_channel[3].current_sample;
779 int data0p = audio_channel[0].last_sample;
780 int data1p = audio_channel[1].last_sample;
781 int data2p = audio_channel[2].last_sample;
782 int data3p = audio_channel[3].last_sample;
783 int data;
784
785 DO_CHANNEL_1 (data0, 0);
786 DO_CHANNEL_1 (data1, 1);
787 DO_CHANNEL_1 (data2, 2);
788 DO_CHANNEL_1 (data3, 3);
789 DO_CHANNEL_1 (data0p, 0);
790 DO_CHANNEL_1 (data1p, 1);
791 DO_CHANNEL_1 (data2p, 2);
792 DO_CHANNEL_1 (data3p, 3);
793
794 data0 &= audio_channel[0].adk_mask;
795 data0p &= audio_channel[0].adk_mask;
796 data1 &= audio_channel[1].adk_mask;
797 data1p &= audio_channel[1].adk_mask;
798 data2 &= audio_channel[2].adk_mask;
799 data2p &= audio_channel[2].adk_mask;
800 data3 &= audio_channel[3].adk_mask;
801 data3p &= audio_channel[3].adk_mask;
802
803 {
804 struct audio_channel_data *cdp;
805 unsigned long ratio, ratio1;
806 #define INTERVAL (scaled_sample_evtime * 3)
807 cdp = audio_channel + 0;
808 ratio1 = cdp->per - cdp->evtime;
809 ratio = (ratio1 << 12) / INTERVAL;
810 if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL)
811 ratio = 4096;
812 data0 = (data0 * ratio + data0p * (4096 - ratio)) >> 12;
813
814 cdp = audio_channel + 1;
815 ratio1 = cdp->per - cdp->evtime;
816 ratio = (ratio1 << 12) / INTERVAL;
817 if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL)
818 ratio = 4096;
819 data1 = (data1 * ratio + data1p * (4096 - ratio)) >> 12;
820
821 cdp = audio_channel + 2;
822 ratio1 = cdp->per - cdp->evtime;
823 ratio = (ratio1 << 12) / INTERVAL;
824 if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL)
825 ratio = 4096;
826 data2 = (data2 * ratio + data2p * (4096 - ratio)) >> 12;
827
828 cdp = audio_channel + 3;
829 ratio1 = cdp->per - cdp->evtime;
830 ratio = (ratio1 << 12) / INTERVAL;
831 if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL)
832 ratio = 4096;
833 data3 = (data3 * ratio + data3p * (4096 - ratio)) >> 12;
834 }
835 data1 += data2;
836 data0 += data3;
837 data0 += data1;
838 data = SBASEVAL16(2) + data0;
839 data = FINISH_DATA (data, 16, 0);
840
841 do_filter(&data, 0);
842
843 get_extra_channels_sample_mono(&data, 0);
844
845 set_sound_buffers ();
846 PUT_SOUND_WORD_MONO (data);
847 check_sound_buffers ();
848 }
849
850 #ifdef HAVE_STEREO_SUPPORT
851
make6ch(uae_s32 d0,uae_s32 d1,uae_s32 d2,uae_s32 d3)852 STATIC_INLINE void make6ch (uae_s32 d0, uae_s32 d1, uae_s32 d2, uae_s32 d3)
853 {
854 uae_s32 sum = d0 + d1 + d2 + d3;
855 sum /= 8;
856 PUT_SOUND_WORD (sum);
857 PUT_SOUND_WORD (sum);
858 }
859
sample16ss_handler(void)860 void sample16ss_handler (void)
861 {
862 int data0 = audio_channel[0].current_sample;
863 int data1 = audio_channel[1].current_sample;
864 int data2 = audio_channel[2].current_sample;
865 int data3 = audio_channel[3].current_sample;
866 DO_CHANNEL_1 (data0, 0);
867 DO_CHANNEL_1 (data1, 1);
868 DO_CHANNEL_1 (data2, 2);
869 DO_CHANNEL_1 (data3, 3);
870
871 data0 &= audio_channel[0].adk_mask;
872 data1 &= audio_channel[1].adk_mask;
873 data2 &= audio_channel[2].adk_mask;
874 data3 &= audio_channel[3].adk_mask;
875
876 data0 = FINISH_DATA (data0, 14, 0);
877 data1 = FINISH_DATA (data1, 14, 0);
878 data2 = FINISH_DATA (data2, 14, 1);
879 data3 = FINISH_DATA (data3, 14, 1);
880
881 do_filter(&data0, 0);
882 do_filter(&data1, 1);
883 do_filter(&data2, 3);
884 do_filter(&data3, 2);
885
886 get_extra_channels_sample(&data0, &data1, 0);
887 get_extra_channels_sample(&data2, &data3, 0);
888
889 set_sound_buffers ();
890 put_sound_word_right(data0);
891 put_sound_word_left (data1);
892 if (currprefs.sound_stereo == SND_6CH)
893 make6ch (data0, data1, data2, data3);
894 put_sound_word_right2(data3);
895 put_sound_word_left2 (data2);
896 check_sound_buffers ();
897 }
898
899 /* This interpolator examines sample points when Paula switches the output
900 * voltage and computes the average of Paula's output */
901
sample16ss_anti_handler(void)902 static void sample16ss_anti_handler (void)
903 {
904 int data0, data1, data2, data3;
905 int datas[AUDIO_CHANNELS_PAULA];
906
907 samplexx_anti_handler (datas, 0, AUDIO_CHANNELS_PAULA);
908 data0 = FINISH_DATA (datas[0], 14, 0);
909 data1 = FINISH_DATA (datas[1], 14, 0);
910 data2 = FINISH_DATA (datas[2], 14, 1);
911 data3 = FINISH_DATA (datas[3], 14, 1);
912
913 do_filter(&data0, 0);
914 do_filter(&data1, 1);
915 do_filter(&data2, 3);
916 do_filter(&data3, 2);
917
918 get_extra_channels_sample(&data0, &data1, 1);
919 get_extra_channels_sample(&data3, &data2, 1);
920
921 set_sound_buffers ();
922 put_sound_word_right(data0);
923 put_sound_word_left (data1);
924 if (currprefs.sound_stereo == SND_6CH)
925 make6ch (data0, data1, data2, data3);
926 put_sound_word_right2(data3);
927 put_sound_word_left2 (data2);
928 check_sound_buffers ();
929 }
930
sample16si_anti_handler(void)931 static void sample16si_anti_handler (void)
932 {
933 int datas[AUDIO_CHANNELS_PAULA], data1, data2;
934
935 samplexx_anti_handler (datas, 0, AUDIO_CHANNELS_PAULA);
936 data1 = datas[0] + datas[3];
937 data2 = datas[1] + datas[2];
938 data1 = FINISH_DATA (data1, 15, 0);
939 data2 = FINISH_DATA (data2, 15, 1);
940
941 do_filter(&data1, 0);
942 do_filter(&data2, 1);
943
944 get_extra_channels_sample(&data1, &data2, 1);
945
946 set_sound_buffers ();
947 put_sound_word_right(data1);
948 put_sound_word_left (data2);
949 check_sound_buffers ();
950 }
951
sample16ss_sinc_handler(void)952 static void sample16ss_sinc_handler (void)
953 {
954 int data0, data1, data2, data3;
955 int datas[AUDIO_CHANNELS_PAULA];
956
957 samplexx_sinc_handler (datas, 0, AUDIO_CHANNELS_PAULA);
958 data0 = FINISH_DATA (datas[0], 16, 0);
959 data1 = FINISH_DATA (datas[1], 16, 0);
960 data2 = FINISH_DATA (datas[2], 16, 1);
961 data3 = FINISH_DATA (datas[3], 16, 1);
962
963 do_filter(&data0, 0);
964 do_filter(&data1, 1);
965 do_filter(&data2, 3);
966 do_filter(&data3, 2);
967
968 get_extra_channels_sample(&data0, &data1, 2);
969 get_extra_channels_sample(&data3, &data2, 2);
970
971 set_sound_buffers ();
972 put_sound_word_right(data0);
973 put_sound_word_left (data1);
974 if (currprefs.sound_stereo == SND_6CH)
975 make6ch (data0, data1, data2, data3);
976 put_sound_word_right2(data3);
977 put_sound_word_left2 (data2);
978 check_sound_buffers ();
979 }
980
sample16si_sinc_handler(void)981 static void sample16si_sinc_handler (void)
982 {
983 int datas[AUDIO_CHANNELS_PAULA], data1, data2;
984
985 samplexx_sinc_handler (datas, 0, AUDIO_CHANNELS_PAULA);
986 data1 = datas[0] + datas[3];
987 data2 = datas[1] + datas[2];
988 data1 = FINISH_DATA (data1, 17, 0);
989 data2 = FINISH_DATA (data2, 17, 1);
990
991 do_filter(&data1, 0);
992 do_filter(&data2, 1);
993
994 get_extra_channels_sample(&data1, &data2, 2);
995
996 set_sound_buffers ();
997 put_sound_word_right(data1);
998 put_sound_word_left(data2);
999 check_sound_buffers ();
1000 }
1001
sample16s_handler(void)1002 void sample16s_handler (void)
1003 {
1004 int data0 = audio_channel[0].current_sample;
1005 int data1 = audio_channel[1].current_sample;
1006 int data2 = audio_channel[2].current_sample;
1007 int data3 = audio_channel[3].current_sample;
1008 DO_CHANNEL_1 (data0, 0);
1009 DO_CHANNEL_1 (data1, 1);
1010 DO_CHANNEL_1 (data2, 2);
1011 DO_CHANNEL_1 (data3, 3);
1012
1013 data0 &= audio_channel[0].adk_mask;
1014 data1 &= audio_channel[1].adk_mask;
1015 data2 &= audio_channel[2].adk_mask;
1016 data3 &= audio_channel[3].adk_mask;
1017
1018 data0 += data3;
1019 data1 += data2;
1020 data2 = SBASEVAL16(1) + data0;
1021 data2 = FINISH_DATA (data2, 15, 0);
1022 data3 = SBASEVAL16(1) + data1;
1023 data3 = FINISH_DATA (data3, 15, 1);
1024
1025 do_filter(&data2, 0);
1026 do_filter(&data3, 1);
1027
1028 get_extra_channels_sample(&data2, &data3, 0);
1029
1030 set_sound_buffers ();
1031 put_sound_word_right(data2);
1032 put_sound_word_left(data3);
1033 check_sound_buffers ();
1034 }
1035
sample16si_crux_handler(void)1036 static void sample16si_crux_handler (void)
1037 {
1038 int data0 = audio_channel[0].current_sample;
1039 int data1 = audio_channel[1].current_sample;
1040 int data2 = audio_channel[2].current_sample;
1041 int data3 = audio_channel[3].current_sample;
1042 int data0p = audio_channel[0].last_sample;
1043 int data1p = audio_channel[1].last_sample;
1044 int data2p = audio_channel[2].last_sample;
1045 int data3p = audio_channel[3].last_sample;
1046
1047 DO_CHANNEL_1 (data0, 0);
1048 DO_CHANNEL_1 (data1, 1);
1049 DO_CHANNEL_1 (data2, 2);
1050 DO_CHANNEL_1 (data3, 3);
1051 DO_CHANNEL_1 (data0p, 0);
1052 DO_CHANNEL_1 (data1p, 1);
1053 DO_CHANNEL_1 (data2p, 2);
1054 DO_CHANNEL_1 (data3p, 3);
1055
1056 data0 &= audio_channel[0].adk_mask;
1057 data0p &= audio_channel[0].adk_mask;
1058 data1 &= audio_channel[1].adk_mask;
1059 data1p &= audio_channel[1].adk_mask;
1060 data2 &= audio_channel[2].adk_mask;
1061 data2p &= audio_channel[2].adk_mask;
1062 data3 &= audio_channel[3].adk_mask;
1063 data3p &= audio_channel[3].adk_mask;
1064
1065 {
1066 struct audio_channel_data *cdp;
1067 unsigned long ratio, ratio1;
1068 #define INTERVAL (scaled_sample_evtime * 3)
1069 cdp = audio_channel + 0;
1070 ratio1 = cdp->per - cdp->evtime;
1071 ratio = (ratio1 << 12) / INTERVAL;
1072 if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL)
1073 ratio = 4096;
1074 data0 = (data0 * ratio + data0p * (4096 - ratio)) >> 12;
1075
1076 cdp = audio_channel + 1;
1077 ratio1 = cdp->per - cdp->evtime;
1078 ratio = (ratio1 << 12) / INTERVAL;
1079 if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL)
1080 ratio = 4096;
1081 data1 = (data1 * ratio + data1p * (4096 - ratio)) >> 12;
1082
1083 cdp = audio_channel + 2;
1084 ratio1 = cdp->per - cdp->evtime;
1085 ratio = (ratio1 << 12) / INTERVAL;
1086 if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL)
1087 ratio = 4096;
1088 data2 = (data2 * ratio + data2p * (4096 - ratio)) >> 12;
1089
1090 cdp = audio_channel + 3;
1091 ratio1 = cdp->per - cdp->evtime;
1092 ratio = (ratio1 << 12) / INTERVAL;
1093 if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL)
1094 ratio = 4096;
1095 data3 = (data3 * ratio + data3p * (4096 - ratio)) >> 12;
1096 }
1097 data1 += data2;
1098 data0 += data3;
1099 data2 = SBASEVAL16(1) + data0;
1100 data2 = FINISH_DATA (data2, 15, 0);
1101 data3 = SBASEVAL16(1) + data1;
1102 data3 = FINISH_DATA (data3, 15, 1);
1103
1104 do_filter(&data2, 0);
1105 do_filter(&data3, 1);
1106
1107 get_extra_channels_sample(&data2, &data3, 0);
1108
1109 set_sound_buffers ();
1110 put_sound_word_right(data2);
1111 put_sound_word_left (data3);
1112 check_sound_buffers ();
1113 }
1114
sample16si_rh_handler(void)1115 static void sample16si_rh_handler (void)
1116 {
1117 unsigned long delta, ratio;
1118
1119 int data0 = audio_channel[0].current_sample;
1120 int data1 = audio_channel[1].current_sample;
1121 int data2 = audio_channel[2].current_sample;
1122 int data3 = audio_channel[3].current_sample;
1123 int data0p = audio_channel[0].last_sample;
1124 int data1p = audio_channel[1].last_sample;
1125 int data2p = audio_channel[2].last_sample;
1126 int data3p = audio_channel[3].last_sample;
1127
1128 DO_CHANNEL_1 (data0, 0);
1129 DO_CHANNEL_1 (data1, 1);
1130 DO_CHANNEL_1 (data2, 2);
1131 DO_CHANNEL_1 (data3, 3);
1132 DO_CHANNEL_1 (data0p, 0);
1133 DO_CHANNEL_1 (data1p, 1);
1134 DO_CHANNEL_1 (data2p, 2);
1135 DO_CHANNEL_1 (data3p, 3);
1136
1137 data0 &= audio_channel[0].adk_mask;
1138 data0p &= audio_channel[0].adk_mask;
1139 data1 &= audio_channel[1].adk_mask;
1140 data1p &= audio_channel[1].adk_mask;
1141 data2 &= audio_channel[2].adk_mask;
1142 data2p &= audio_channel[2].adk_mask;
1143 data3 &= audio_channel[3].adk_mask;
1144 data3p &= audio_channel[3].adk_mask;
1145
1146 /* linear interpolation and summing up... */
1147 delta = audio_channel[0].per;
1148 ratio = ((audio_channel[0].evtime % delta) << 8) / delta;
1149 data0 = (data0 * (256 - ratio) + data0p * ratio) >> 8;
1150 delta = audio_channel[1].per;
1151 ratio = ((audio_channel[1].evtime % delta) << 8) / delta;
1152 data1 = (data1 * (256 - ratio) + data1p * ratio) >> 8;
1153 delta = audio_channel[2].per;
1154 ratio = ((audio_channel[2].evtime % delta) << 8) / delta;
1155 data1 += (data2 * (256 - ratio) + data2p * ratio) >> 8;
1156 delta = audio_channel[3].per;
1157 ratio = ((audio_channel[3].evtime % delta) << 8) / delta;
1158 data0 += (data3 * (256 - ratio) + data3p * ratio) >> 8;
1159 data2 = SBASEVAL16(1) + data0;
1160 data2 = FINISH_DATA (data2, 15, 0);
1161 data3 = SBASEVAL16(1) + data1;
1162 data3 = FINISH_DATA (data3, 15, 1);
1163
1164 do_filter(&data2, 0);
1165 do_filter(&data3, 1);
1166
1167 get_extra_channels_sample(&data2, &data3, 0);
1168
1169 set_sound_buffers ();
1170 put_sound_word_right(data2);
1171 put_sound_word_left (data3);
1172 check_sound_buffers ();
1173 }
1174
1175 #else
sample16s_handler(void)1176 void sample16s_handler (void)
1177 {
1178 sample16_handler ();
1179 }
sample16si_crux_handler(void)1180 static void sample16si_crux_handler (void)
1181 {
1182 sample16i_crux_handler ();
1183 }
sample16si_rh_handler(void)1184 static void sample16si_rh_handler (void)
1185 {
1186 sample16i_rh_handler ();
1187 }
1188 #endif
1189
1190 static int audio_work_to_do;
1191
zerostate(int nr)1192 static void zerostate (int nr)
1193 {
1194 struct audio_channel_data *cdp = audio_channel + nr;
1195 #if DEBUG_AUDIO > 0
1196 if (debugchannel (nr))
1197 write_log (_T("%d: ZEROSTATE\n"), nr);
1198 #endif
1199 cdp->state = 0;
1200 cdp->evtime = MAX_EV;
1201 cdp->intreq2 = 0;
1202 cdp->dmaenstore = false;
1203 cdp->dmaofftime_active = 0;
1204 #if TEST_AUDIO > 0
1205 cdp->have_dat = false;
1206 #endif
1207
1208 }
1209
schedule_audio(void)1210 static void schedule_audio (void)
1211 {
1212 unsigned long best = MAX_EV;
1213 int i;
1214
1215 eventtab[ev_audio].active = 0;
1216 eventtab[ev_audio].oldcycles = get_cycles ();
1217 for (i = 0; i < audio_channel_count; i++) {
1218 struct audio_channel_data *cdp = audio_channel + i;
1219 if (cdp->evtime != MAX_EV) {
1220 if (best > cdp->evtime) {
1221 best = cdp->evtime;
1222 eventtab[ev_audio].active = 1;
1223 }
1224 }
1225 }
1226 eventtab[ev_audio].evtime = get_cycles () + best;
1227 }
1228
audio_event_reset(void)1229 static void audio_event_reset (void)
1230 {
1231 int i;
1232
1233 last_cycles = get_cycles ();
1234 next_sample_evtime = scaled_sample_evtime;
1235 if (!isrestore ()) {
1236 for (i = 0; i < AUDIO_CHANNELS_PAULA; i++)
1237 zerostate (i);
1238 }
1239 schedule_audio ();
1240 events_schedule ();
1241 samplecnt = 0;
1242 extrasamples = 0;
1243 outputsample = 1;
1244 doublesample = 0;
1245 }
1246
audio_deactivate(void)1247 void audio_deactivate (void)
1248 {
1249 gui_data.sndbuf_status = 3;
1250 gui_data.sndbuf = 0;
1251 audio_work_to_do = 0;
1252 pause_sound_buffer ();
1253 clear_sound_buffers ();
1254 audio_event_reset ();
1255 }
1256
audio_activate(void)1257 int audio_activate (void)
1258 {
1259 int ret = 0;
1260
1261 if (!audio_work_to_do) {
1262 restart_sound_buffer ();
1263 ret = 1;
1264 audio_event_reset ();
1265 }
1266 audio_work_to_do = 4 * maxvpos_nom * 50;
1267 return ret;
1268 }
1269
is_audio_active(void)1270 STATIC_INLINE int is_audio_active (void)
1271 {
1272 return audio_work_to_do;
1273 }
1274
audio_dmal(void)1275 uae_u16 audio_dmal (void)
1276 {
1277 uae_u16 dmal = 0;
1278 for (int nr = 0; nr < AUDIO_CHANNELS_PAULA; nr++) {
1279 struct audio_channel_data *cdp = audio_channel + nr;
1280 if (cdp->dr)
1281 dmal |= 1 << (nr * 2);
1282 if (cdp->dsr)
1283 dmal |= 1 << (nr * 2 + 1);
1284 cdp->dr = cdp->dsr = false;
1285 }
1286 return dmal;
1287 }
1288
isirq(int nr)1289 static int isirq (int nr)
1290 {
1291 return INTREQR () & (0x80 << nr);
1292 }
1293
setirq(int nr,int which)1294 static void setirq (int nr, int which)
1295 {
1296 #if DEBUG_AUDIO > 0
1297 struct audio_channel_data *cdp = audio_channel + nr;
1298 if (debugchannel (nr) && cdp->wlen > 1)
1299 write_log (_T("SETIRQ%d (%d,%d) PC=%08X\n"), nr, which, isirq (nr) ? 1 : 0, M68K_GETPC);
1300 #endif
1301 INTREQ_0 (0x8000 | (0x80 << nr));
1302 }
1303
newsample(int nr,sample8_t sample)1304 static void newsample (int nr, sample8_t sample)
1305 {
1306 struct audio_channel_data *cdp = audio_channel + nr;
1307 #if DEBUG_AUDIO > 0
1308 if (!debugchannel (nr))
1309 sample = 0;
1310 #endif
1311 #if DEBUG_AUDIO > 2
1312 if (debugchannel (nr))
1313 write_log (_T("SAMPLE%d: %02x\n"), nr, sample & 0xff);
1314 #endif
1315 if (!(audio_channel_mask & (1 << nr)))
1316 sample = 0;
1317 cdp->last_sample = cdp->current_sample;
1318 cdp->current_sample = sample;
1319 }
1320
setdr(int nr)1321 STATIC_INLINE void setdr (int nr)
1322 {
1323 struct audio_channel_data *cdp = audio_channel + nr;
1324 #if TEST_AUDIO > 0
1325 if (debugchannel (nr) && cdp->dr)
1326 write_log (_T("%d: DR already active (STATE=%d)\n"), nr, cdp->state);
1327 #endif
1328 cdp->drhpos = current_hpos ();
1329 cdp->dr = true;
1330 if (cdp->wlen == 1) {
1331 cdp->dsr = true;
1332 #if DEBUG_AUDIO > 0
1333 if (debugchannel (nr) && cdp->wlen > 1)
1334 write_log (_T("DSR%d PT=%08X PC=%08X\n"), nr, cdp->pt, M68K_GETPC);
1335 #endif
1336 }
1337 }
1338
loaddat(int nr,bool modper)1339 static void loaddat (int nr, bool modper)
1340 {
1341 struct audio_channel_data *cdp = audio_channel + nr;
1342 int audav = adkcon & (0x01 << nr);
1343 int audap = adkcon & (0x10 << nr);
1344 if (audav || (modper && audap)) {
1345 if (nr >= 3)
1346 return;
1347 if (modper && audap) {
1348 if (cdp->dat == 0)
1349 #ifdef FSUAE
1350 // FIXME: changed PERIOD_MAX from ULONG_MAX to UINT_MAX
1351 // since cdp[1].per is int, not long. But should per
1352 // be an unsigned int??
1353 #endif
1354 cdp[1].per = 65536 * CYCLE_UNIT;
1355 else if (cdp->dat > PERIOD_MIN)
1356 cdp[1].per = cdp->dat * CYCLE_UNIT;
1357 else
1358 cdp[1].per = PERIOD_MIN * CYCLE_UNIT;
1359 } else if (audav) {
1360 cdp[1].vol = cdp->dat;
1361 cdp[1].vol &= 127;
1362 if (cdp[1].vol > 64)
1363 cdp[1].vol = 64;
1364 }
1365 } else {
1366 #if TEST_AUDIO > 0
1367 if (debugchannel (nr)) {
1368 if (cdp->hisample || cdp->losample)
1369 write_log (_T("%d: high or low sample not used\n"), nr);
1370 if (!cdp->have_dat)
1371 write_log (_T("%d: dat not updated. STATE=%d 1=%04x 2=%04x\n"), nr, cdp->state, cdp->dat, cdp->dat2);
1372 }
1373 cdp->hisample = cdp->losample = true;
1374 cdp->have_dat = false;
1375 #endif
1376 #if DEBUG_AUDIO > 2
1377 if (debugchannel (nr))
1378 write_log (_T("LOAD%dDAT: New:%04x, Old:%04x\n"), nr, cdp->dat, cdp->dat2);
1379 #endif
1380 cdp->dat2 = cdp->dat;
1381 }
1382 }
loaddat(int nr)1383 static void loaddat (int nr)
1384 {
1385 loaddat (nr, false);
1386 }
1387
loadper(int nr)1388 STATIC_INLINE void loadper (int nr)
1389 {
1390 struct audio_channel_data *cdp = audio_channel + nr;
1391
1392 cdp->evtime = cdp->per;
1393 if (cdp->evtime < CYCLE_UNIT)
1394 write_log (_T("LOADPER%d bug %d\n"), nr, cdp->evtime);
1395 }
1396
1397
audio_state_channel2(int nr,bool perfin)1398 static void audio_state_channel2 (int nr, bool perfin)
1399 {
1400 struct audio_channel_data *cdp = audio_channel + nr;
1401 bool chan_ena = (dmacon & DMA_MASTER) && (dmacon & (1 << nr));
1402 bool old_dma = cdp->dmaenstore;
1403 int audav = adkcon & (0x01 << nr);
1404 int audap = adkcon & (0x10 << nr);
1405 int napnav = (!audav && !audap) || audav;
1406 int hpos = current_hpos ();
1407
1408 cdp->dmaenstore = chan_ena;
1409
1410 if (currprefs.produce_sound == 0) {
1411 zerostate (nr);
1412 return;
1413 }
1414 audio_activate ();
1415
1416 if ((cdp->state == 2 || cdp->state == 3) && usehacks()) {
1417 if (!chan_ena && old_dma) {
1418 // DMA switched off, state=2/3 and "too fast CPU": set flag
1419 cdp->dmaofftime_active = true;
1420 }
1421 if (cdp->dmaofftime_active && !old_dma && chan_ena) {
1422 // We are still in state=2/3 and program is going to re-enable
1423 // DMA. Force state to zero to prevent CPU timed DMA wait
1424 // routines in common tracker players to lose notes.
1425 #if DEBUG_AUDIO_HACK > 0
1426 if (debugchannel (nr))
1427 write_log (_T("%d: INSTADMAOFF\n"), nr, M68K_GETPC);
1428 #endif
1429 newsample (nr, (cdp->dat2 >> 0) & 0xff);
1430 // if (napnav)
1431 // setirq (nr, 91);
1432 zerostate (nr);
1433 }
1434 }
1435
1436 #if DEBUG_AUDIO > 0
1437 if (debugchannel (nr) && old_dma != chan_ena) {
1438 write_log (_T("%d:DMA=%d IRQ=%d PC=%08x\n"), nr, chan_ena, isirq (nr) ? 1 : 0, M68K_GETPC);
1439 }
1440 #endif
1441 switch (cdp->state)
1442 {
1443 case 0:
1444 if (chan_ena) {
1445 cdp->evtime = MAX_EV;
1446 cdp->state = 1;
1447 cdp->dr = true;
1448 cdp->drhpos = hpos;
1449 cdp->wlen = cdp->len;
1450 cdp->ptx_written = false;
1451 /* Some programs first start short empty sample and then later switch to
1452 * real sample, we must not enable the hack in this case
1453 */
1454 if (cdp->wlen > 2)
1455 cdp->ptx_tofetch = true;
1456 cdp->dsr = true;
1457 #if TEST_AUDIO > 0
1458 cdp->have_dat = false;
1459 #endif
1460 #if DEBUG_AUDIO > 0
1461 if (debugchannel (nr)) {
1462 write_log (_T("%d:0>1: LEN=%d PC=%08x\n"), nr, cdp->wlen, M68K_GETPC);
1463 }
1464 #endif
1465 } else if (cdp->dat_written && !isirq (nr)) {
1466 cdp->state = 2;
1467 setirq (nr, 0);
1468 loaddat (nr);
1469 if (usehacks() && cdp->per < 10 * CYCLE_UNIT) {
1470 // make sure audio.device AUDxDAT startup returns to idle state before DMA is enabled
1471 newsample (nr, (cdp->dat2 >> 0) & 0xff);
1472 zerostate (nr);
1473 } else {
1474 cdp->pbufldl = true;
1475 audio_state_channel2 (nr, false);
1476 }
1477 } else {
1478 zerostate (nr);
1479 }
1480 break;
1481 case 1:
1482 cdp->evtime = MAX_EV;
1483 if (!chan_ena) {
1484 zerostate (nr);
1485 return;
1486 }
1487 if (!cdp->dat_written)
1488 return;
1489 #if TEST_AUDIO > 0
1490 if (debugchannel (nr) && !cdp->have_dat)
1491 write_log (_T("%d: state 1 but no have_dat\n"), nr);
1492 cdp->have_dat = false;
1493 cdp->losample = cdp->hisample = false;
1494 #endif
1495 setirq (nr, 10);
1496 setdr (nr);
1497 if (cdp->wlen != 1)
1498 cdp->wlen = (cdp->wlen - 1) & 0xffff;
1499 cdp->state = 5;
1500 if (sampleripper_enabled)
1501 do_samplerip (cdp);
1502 break;
1503 case 5:
1504 cdp->evtime = MAX_EV;
1505 if (!chan_ena) {
1506 zerostate (nr);
1507 return;
1508 }
1509 if (!cdp->dat_written)
1510 return;
1511 #if DEBUG_AUDIO > 0
1512 if (debugchannel (nr))
1513 write_log (_T("%d:>5: LEN=%d PT=%08X PC=%08X\n"), nr, cdp->wlen, cdp->pt, M68K_GETPC);
1514 #endif
1515 if (cdp->ptx_written) {
1516 cdp->ptx_written = 0;
1517 cdp->lc = cdp->ptx;
1518 }
1519 loaddat (nr);
1520 if (napnav)
1521 setdr (nr);
1522 cdp->state = 2;
1523 loadper (nr);
1524 cdp->pbufldl = true;
1525 cdp->intreq2 = 0;
1526 audio_state_channel2 (nr, false);
1527 break;
1528 case 2:
1529 if (cdp->pbufldl) {
1530 #if TEST_AUDIO > 0
1531 if (debugchannel (nr) && cdp->hisample == false)
1532 write_log (_T("%d: high sample used twice\n"), nr);
1533 cdp->hisample = false;
1534 #endif
1535 newsample (nr, (cdp->dat2 >> 8) & 0xff);
1536 loadper (nr);
1537 cdp->pbufldl = false;
1538 }
1539 if (!perfin)
1540 return;
1541 if (audap)
1542 loaddat (nr, true);
1543 if (chan_ena) {
1544 if (audap)
1545 setdr (nr);
1546 if (cdp->intreq2 && audap)
1547 setirq (nr, 21);
1548 } else {
1549 if (audap)
1550 setirq (nr, 22);
1551 }
1552 cdp->pbufldl = true;
1553 cdp->state = 3;
1554 audio_state_channel2 (nr, false);
1555 break;
1556 case 3:
1557 if (cdp->pbufldl) {
1558 #if TEST_AUDIO > 0
1559 if (debugchannel (nr) && cdp->losample == false)
1560 write_log (_T("%d: low sample used twice\n"), nr);
1561 cdp->losample = false;
1562 #endif
1563 newsample (nr, (cdp->dat2 >> 0) & 0xff);
1564 loadper (nr);
1565 cdp->pbufldl = false;
1566 }
1567 if (!perfin)
1568 return;
1569 if (chan_ena) {
1570 loaddat (nr);
1571 if (cdp->intreq2 && napnav)
1572 setirq (nr, 31);
1573 if (napnav)
1574 setdr (nr);
1575 } else {
1576 if (isirq (nr)) {
1577 #if DEBUG_AUDIO > 0
1578 if (debugchannel (nr))
1579 write_log (_T("%d: IDLE\n"), nr);
1580 #endif
1581 zerostate (nr);
1582 return;
1583 }
1584 loaddat (nr);
1585 if (napnav)
1586 setirq (nr, 32);
1587 }
1588 cdp->intreq2 = 0;
1589 cdp->pbufldl = true;
1590 cdp->state = 2;
1591 audio_state_channel2 (nr, false);
1592 break;
1593 }
1594 }
1595
1596 static void audio_state_cda(void);
1597
audio_state_channel(int nr,bool perfin)1598 static void audio_state_channel (int nr, bool perfin)
1599 {
1600 struct audio_channel_data *cdp = audio_channel + nr;
1601 if (nr < AUDIO_CHANNELS_PAULA) {
1602 audio_state_channel2 (nr, perfin);
1603 cdp->dat_written = false;
1604 } else if (nr == AUDIO_CHANNEL_SNDBOARD_LEFT || nr == AUDIO_CHANNEL_SNDBOARD_RIGHT) {
1605 audio_state_sndboard(nr - AUDIO_CHANNEL_SNDBOARD_LEFT);
1606 } else if (nr == AUDIO_CHANNEL_CDA_LEFT) {
1607 audio_state_cda();
1608 }
1609 }
1610
audio_state_machine(void)1611 void audio_state_machine (void)
1612 {
1613 update_audio ();
1614 for (int nr = 0; nr < AUDIO_CHANNELS_PAULA; nr++) {
1615 struct audio_channel_data *cdp = audio_channel + nr;
1616 audio_state_channel2 (nr, false);
1617 cdp->dat_written = false;
1618 }
1619 schedule_audio ();
1620 events_schedule ();
1621 }
1622
audio_reset(void)1623 void audio_reset (void)
1624 {
1625 int i;
1626 struct audio_channel_data *cdp;
1627
1628 #ifdef AHI
1629 ahi_close_sound ();
1630 free_ahi_v2 ();
1631 #endif
1632 reset_sound ();
1633 memset (sound_filter_state, 0, sizeof sound_filter_state);
1634 if (!isrestore ()) {
1635 for (i = 0; i < AUDIO_CHANNELS_MAX; i++) {
1636 cdp = &audio_channel[i];
1637 memset (cdp, 0, sizeof *audio_channel);
1638 cdp->per = PERIOD_MAX - 1;
1639 cdp->vol = 0;
1640 cdp->evtime = MAX_EV;
1641 }
1642 }
1643
1644 last_cycles = get_cycles ();
1645 next_sample_evtime = scaled_sample_evtime;
1646 schedule_audio ();
1647 events_schedule ();
1648 }
1649
sound_prefs_changed(void)1650 static int sound_prefs_changed (void)
1651 {
1652 if (!config_changed)
1653 return 0;
1654 if (changed_prefs.produce_sound != currprefs.produce_sound
1655 || changed_prefs.win32_soundcard != currprefs.win32_soundcard
1656 || changed_prefs.sound_stereo != currprefs.sound_stereo
1657 || changed_prefs.sound_maxbsiz != currprefs.sound_maxbsiz
1658 || changed_prefs.sound_freq != currprefs.sound_freq
1659 || changed_prefs.sound_auto != currprefs.sound_auto)
1660 return 1;
1661
1662 if (changed_prefs.sound_stereo_separation != currprefs.sound_stereo_separation
1663 || changed_prefs.sound_mixed_stereo_delay != currprefs.sound_mixed_stereo_delay
1664 || changed_prefs.sound_interpol != currprefs.sound_interpol
1665 || changed_prefs.sound_volume_paula != currprefs.sound_volume_paula
1666 || changed_prefs.sound_volume_cd != currprefs.sound_volume_cd
1667 || changed_prefs.sound_volume_master != currprefs.sound_volume_master
1668 || changed_prefs.sound_volume_board != currprefs.sound_volume_board
1669 || changed_prefs.sound_stereo_swap_paula != currprefs.sound_stereo_swap_paula
1670 || changed_prefs.sound_stereo_swap_ahi != currprefs.sound_stereo_swap_ahi
1671 || changed_prefs.sound_cdaudio != currprefs.sound_cdaudio
1672 || changed_prefs.sound_filter != currprefs.sound_filter
1673 || changed_prefs.sound_filter_type != currprefs.sound_filter_type)
1674 return -1;
1675 return 0;
1676 }
1677
1678 /* This computes the 1st order low-pass filter term b0.
1679 * The a1 term is 1.0 - b0. The center frequency marks the -3 dB point. */
1680 #ifndef M_PI
1681 #define M_PI 3.14159265358979323846
1682 #endif
rc_calculate_a0(int sample_rate,int cutoff_freq)1683 static float rc_calculate_a0 (int sample_rate, int cutoff_freq)
1684 {
1685 float omega;
1686 /* The BLT correction formula below blows up if the cutoff is above nyquist. */
1687 if (cutoff_freq >= sample_rate / 2)
1688 return 1.0;
1689
1690 omega = 2 * M_PI * cutoff_freq / sample_rate;
1691 /* Compensate for the bilinear transformation. This allows us to specify the
1692 * stop frequency more exactly, but the filter becomes less steep further
1693 * from stopband. */
1694 omega = tan (omega / 2) * 2;
1695 return 1 / (1 + 1 / omega);
1696 }
1697
check_prefs_changed_audio(void)1698 void check_prefs_changed_audio (void)
1699 {
1700 int ch;
1701
1702 if (sound_available) {
1703 ch = sound_prefs_changed ();
1704 if (ch > 0) {
1705 #ifdef AVIOUTPUT
1706 AVIOutput_Restart ();
1707 #endif
1708 clear_sound_buffers ();
1709 }
1710 if (ch) {
1711 set_audio ();
1712 audio_activate ();
1713 }
1714 }
1715 #ifdef DRIVESOUND
1716 driveclick_check_prefs ();
1717 #endif
1718 }
1719
set_extra_prehandler(void)1720 static void set_extra_prehandler(void)
1721 {
1722 if (audio_channel_count > AUDIO_CHANNELS_PAULA && sample_prehandler != anti_prehandler) {
1723 extra_sample_prehandler = anti_prehandler;
1724 } else {
1725 extra_sample_prehandler = NULL;
1726 }
1727 }
1728
1729
set_audio(void)1730 void set_audio (void)
1731 {
1732 int old_mixed_size = mixed_stereo_size;
1733 int sep, delay;
1734 int ch;
1735
1736 ch = sound_prefs_changed ();
1737 if (ch >= 0)
1738 close_sound ();
1739
1740 currprefs.produce_sound = changed_prefs.produce_sound;
1741 currprefs.win32_soundcard = changed_prefs.win32_soundcard;
1742 currprefs.sound_stereo = changed_prefs.sound_stereo;
1743 currprefs.sound_auto = changed_prefs.sound_auto;
1744 currprefs.sound_freq = changed_prefs.sound_freq;
1745 currprefs.sound_maxbsiz = changed_prefs.sound_maxbsiz;
1746
1747 currprefs.sound_stereo_separation = changed_prefs.sound_stereo_separation;
1748 currprefs.sound_mixed_stereo_delay = changed_prefs.sound_mixed_stereo_delay;
1749 currprefs.sound_interpol = changed_prefs.sound_interpol;
1750 currprefs.sound_filter = changed_prefs.sound_filter;
1751 currprefs.sound_filter_type = changed_prefs.sound_filter_type;
1752 currprefs.sound_volume_paula = changed_prefs.sound_volume_paula;
1753 currprefs.sound_volume_master = changed_prefs.sound_volume_master;
1754 currprefs.sound_volume_board = changed_prefs.sound_volume_board;
1755 currprefs.sound_volume_cd = changed_prefs.sound_volume_cd;
1756 currprefs.sound_cdaudio = changed_prefs.sound_cdaudio;
1757 currprefs.sound_stereo_swap_paula = changed_prefs.sound_stereo_swap_paula;
1758 currprefs.sound_stereo_swap_ahi = changed_prefs.sound_stereo_swap_ahi;
1759
1760 sound_cd_volume[0] = sound_cd_volume[1] = (100 - (currprefs.sound_volume_cd < 0 ? 0 : currprefs.sound_volume_cd)) * 32768 / 100;
1761 sound_paula_volume[0] = sound_paula_volume[1] = (100 - currprefs.sound_volume_paula) * 32768 / 100;
1762 sndboard_ext_volume();
1763
1764 if (ch >= 0) {
1765 if (currprefs.produce_sound >= 2) {
1766 if (!init_audio ()) {
1767 if (! sound_available) {
1768 write_log (_T("Sound is not supported.\n"));
1769 } else {
1770 write_log (_T("Sorry, can't initialize sound.\n"));
1771 currprefs.produce_sound = 1;
1772 /* So we don't do this every frame */
1773 changed_prefs.produce_sound = 1;
1774 }
1775 }
1776 }
1777 next_sample_evtime = scaled_sample_evtime;
1778 last_cycles = get_cycles ();
1779 compute_vsynctime ();
1780 } else {
1781 sound_volume (0);
1782 }
1783
1784 sep = (currprefs.sound_stereo_separation = changed_prefs.sound_stereo_separation) * 3 / 2;
1785 if (sep >= 15)
1786 sep = 16;
1787 delay = currprefs.sound_mixed_stereo_delay = changed_prefs.sound_mixed_stereo_delay;
1788 mixed_mul1 = MIXED_STEREO_SCALE / 2 - sep;
1789 mixed_mul2 = MIXED_STEREO_SCALE / 2 + sep;
1790 mixed_stereo_size = delay > 0 ? (1 << delay) - 1 : 0;
1791 mixed_on = sep < MIXED_STEREO_MAX || mixed_stereo_size > 0;
1792 if (mixed_on && old_mixed_size != mixed_stereo_size) {
1793 saved_ptr = 0;
1794 memset (right_word_saved, 0, sizeof right_word_saved);
1795 }
1796
1797 led_filter_forced = -1; // always off
1798 sound_use_filter = sound_use_filter_sinc = 0;
1799 if (currprefs.sound_filter) {
1800 if (currprefs.sound_filter == FILTER_SOUND_ON)
1801 led_filter_forced = 1;
1802 if (currprefs.sound_filter == FILTER_SOUND_EMUL)
1803 led_filter_forced = 0;
1804 if (currprefs.sound_filter_type == FILTER_SOUND_TYPE_A500)
1805 sound_use_filter = FILTER_MODEL_A500;
1806 else if (currprefs.sound_filter_type == FILTER_SOUND_TYPE_A1200)
1807 sound_use_filter = FILTER_MODEL_A1200;
1808 }
1809 a500e_filter1_a0 = rc_calculate_a0 (currprefs.sound_freq, 6200);
1810 a500e_filter2_a0 = rc_calculate_a0 (currprefs.sound_freq, 20000);
1811 filter_a0 = rc_calculate_a0 (currprefs.sound_freq, 7000);
1812 led_filter_audio ();
1813
1814 /* Select the right interpolation method. */
1815 if (sample_handler == sample16_handler
1816 || sample_handler == sample16i_crux_handler
1817 || sample_handler == sample16i_rh_handler
1818 || sample_handler == sample16i_sinc_handler
1819 || sample_handler == sample16i_anti_handler)
1820 {
1821 sample_handler = (currprefs.sound_interpol == 0 ? sample16_handler
1822 : currprefs.sound_interpol == 3 ? sample16i_rh_handler
1823 : currprefs.sound_interpol == 4 ? sample16i_crux_handler
1824 : currprefs.sound_interpol == 2 ? sample16i_sinc_handler
1825 : sample16i_anti_handler);
1826 } else if (sample_handler == sample16s_handler
1827 || sample_handler == sample16si_crux_handler
1828 || sample_handler == sample16si_rh_handler
1829 || sample_handler == sample16si_sinc_handler
1830 || sample_handler == sample16si_anti_handler)
1831 {
1832 sample_handler = (currprefs.sound_interpol == 0 ? sample16s_handler
1833 : currprefs.sound_interpol == 3 ? sample16si_rh_handler
1834 : currprefs.sound_interpol == 4 ? sample16si_crux_handler
1835 : currprefs.sound_interpol == 2 ? sample16si_sinc_handler
1836 : sample16si_anti_handler);
1837 } else if (sample_handler == sample16ss_handler
1838 || sample_handler == sample16ss_sinc_handler
1839 || sample_handler == sample16ss_anti_handler)
1840 {
1841 sample_handler = (currprefs.sound_interpol == 0 ? sample16ss_handler
1842 : currprefs.sound_interpol == 3 ? sample16ss_handler
1843 : currprefs.sound_interpol == 4 ? sample16ss_handler
1844 : currprefs.sound_interpol == 2 ? sample16ss_sinc_handler
1845 : sample16ss_anti_handler);
1846 }
1847 sample_prehandler = NULL;
1848 if (sample_handler == sample16si_sinc_handler || sample_handler == sample16i_sinc_handler || sample_handler == sample16ss_sinc_handler) {
1849 sample_prehandler = sinc_prehandler_paula;
1850 sound_use_filter_sinc = sound_use_filter;
1851 sound_use_filter = 0;
1852 } else if (sample_handler == sample16si_anti_handler || sample_handler == sample16i_anti_handler || sample_handler == sample16ss_anti_handler) {
1853 sample_prehandler = anti_prehandler;
1854 }
1855 set_extra_prehandler();
1856
1857 if (currprefs.produce_sound == 0) {
1858 eventtab[ev_audio].active = 0;
1859 events_schedule ();
1860 } else {
1861 audio_activate ();
1862 schedule_audio ();
1863 events_schedule ();
1864 }
1865 set_config_changed ();
1866 }
1867
update_audio(void)1868 void update_audio (void)
1869 {
1870 unsigned long int n_cycles = 0;
1871 #if SOUNDSTUFF > 1
1872 static int samplecounter;
1873 #endif
1874
1875 if (!isaudio ())
1876 goto end;
1877 if (isrestore ())
1878 goto end;
1879 if (!is_audio_active ())
1880 goto end;
1881
1882 n_cycles = get_cycles () - last_cycles;
1883 while (n_cycles > 0) {
1884 unsigned long int best_evtime = n_cycles + 1;
1885 unsigned long rounded;
1886 int i;
1887
1888 for (i = 0; i < audio_channel_count; i++) {
1889 if (audio_channel[i].evtime != MAX_EV && best_evtime > audio_channel[i].evtime)
1890 best_evtime = audio_channel[i].evtime;
1891 }
1892
1893 /* next_sample_evtime >= 0 so floor() behaves as expected */
1894 rounded = floorf (next_sample_evtime);
1895 if ((next_sample_evtime - rounded) >= 0.5)
1896 rounded++;
1897
1898 if (currprefs.produce_sound > 1 && best_evtime > rounded)
1899 best_evtime = rounded;
1900
1901 if (best_evtime > n_cycles)
1902 best_evtime = n_cycles;
1903
1904 /* Decrease time-to-wait counters */
1905 next_sample_evtime -= best_evtime;
1906
1907 if (currprefs.produce_sound > 1) {
1908 if (sample_prehandler)
1909 sample_prehandler (best_evtime / CYCLE_UNIT);
1910 if (extra_sample_prehandler)
1911 extra_sample_prehandler(best_evtime / CYCLE_UNIT);
1912 }
1913
1914 for (i = 0; i < audio_channel_count; i++) {
1915 if (audio_channel[i].evtime != MAX_EV)
1916 audio_channel[i].evtime -= best_evtime;
1917 }
1918
1919 n_cycles -= best_evtime;
1920
1921 if (currprefs.produce_sound > 1) {
1922 /* Test if new sample needs to be outputted */
1923 if (rounded == best_evtime) {
1924 /* Before the following addition, next_sample_evtime is in range [-0.5, 0.5) */
1925 next_sample_evtime += scaled_sample_evtime;
1926 #if SOUNDSTUFF > 1
1927 next_sample_evtime -= extrasamples * 15;
1928 doublesample = 0;
1929 if (--samplecounter <= 0) {
1930 samplecounter = currprefs.sound_freq / 1000;
1931 if (extrasamples > 0) {
1932 outputsample = 1;
1933 doublesample = 1;
1934 extrasamples--;
1935 } else if (extrasamples < 0) {
1936 outputsample = 0;
1937 doublesample = 0;
1938 extrasamples++;
1939 }
1940 }
1941 #endif
1942 (*sample_handler) ();
1943 #if SOUNDSTUFF > 1
1944 if (outputsample == 0)
1945 outputsample = -1;
1946 else if (outputsample < 0)
1947 outputsample = 1;
1948 #endif
1949
1950 }
1951 }
1952
1953 for (i = 0; i < audio_channel_count; i++) {
1954 if (audio_channel[i].evtime == 0) {
1955 audio_state_channel (i, true);
1956 if (audio_channel[i].evtime == 0) {
1957 write_log (_T("evtime==0 sound bug channel %d\n"), i);
1958 audio_channel[i].evtime = MAX_EV;
1959 }
1960 }
1961 }
1962 }
1963 end:
1964 last_cycles = get_cycles () - n_cycles;
1965 }
1966
audio_evhandler(void)1967 void audio_evhandler (void)
1968 {
1969 update_audio ();
1970 schedule_audio ();
1971 }
1972
audio_hsync(void)1973 void audio_hsync (void)
1974 {
1975 if (!currprefs.produce_sound)
1976 return;
1977 if (!isaudio ())
1978 return;
1979 if (audio_work_to_do > 0 && currprefs.sound_auto && !audio_extra_channels[0] && !audio_extra_channels[1]
1980 #ifdef AVIOUTPUT
1981 && !avioutput_enabled
1982 #endif
1983 ) {
1984 audio_work_to_do--;
1985 if (audio_work_to_do == 0)
1986 audio_deactivate ();
1987 }
1988 update_audio ();
1989 }
1990
AUDxDAT(int nr,uae_u16 v,uaecptr addr)1991 void AUDxDAT (int nr, uae_u16 v, uaecptr addr)
1992 {
1993 struct audio_channel_data *cdp = audio_channel + nr;
1994 int chan_ena = (dmacon & DMA_MASTER) && (dmacon & (1 << nr));
1995
1996 #if DEBUG_AUDIO > 0
1997 if (debugchannel (nr) && (DEBUG_AUDIO > 1 || (!chan_ena || addr == 0xffffffff || (cdp->state != 2 && cdp->state != 3)))) {
1998 write_log (_T("AUD%dDAT: %04X ADDR=%08X LEN=%d/%d %d,%d,%d %06X\n"), nr,
1999 v, addr, cdp->wlen, cdp->len, cdp->state, chan_ena, isirq (nr) ? 1 : 0, M68K_GETPC);
2000 }
2001 #endif
2002 cdp->dat = v;
2003 cdp->dat_written = true;
2004 #if TEST_AUDIO > 0
2005 if (debugchannel (nr) && cdp->have_dat)
2006 write_log (_T("%d: audxdat 1=%04x 2=%04x but old dat not yet used\n"), nr, cdp->dat, cdp->dat2);
2007 cdp->have_dat = true;
2008 #endif
2009 if (cdp->state == 2 || cdp->state == 3) {
2010 if (chan_ena) {
2011 if (cdp->wlen == 1) {
2012 cdp->wlen = cdp->len;
2013 cdp->intreq2 = true;
2014 if (sampleripper_enabled)
2015 do_samplerip (cdp);
2016 #if DEBUG_AUDIO > 0
2017 if (debugchannel (nr) && cdp->wlen > 1)
2018 write_log (_T("AUD%d looped, IRQ=%d, LC=%08X LEN=%d\n"), nr, isirq (nr) ? 1 : 0, cdp->pt, cdp->wlen);
2019 #endif
2020 } else {
2021 cdp->wlen = (cdp->wlen - 1) & 0xffff;
2022 }
2023 }
2024 } else {
2025 audio_activate ();
2026 update_audio ();
2027 audio_state_channel (nr, false);
2028 schedule_audio ();
2029 events_schedule ();
2030 }
2031 cdp->dat_written = false;
2032 }
AUDxDAT(int nr,uae_u16 v)2033 void AUDxDAT (int nr, uae_u16 v)
2034 {
2035 AUDxDAT (nr, v, 0xffffffff);
2036 }
2037
audio_getpt(int nr,bool reset)2038 uaecptr audio_getpt (int nr, bool reset)
2039 {
2040 struct audio_channel_data *cdp = audio_channel + nr;
2041 uaecptr p = cdp->pt;
2042 cdp->pt += 2;
2043 if (reset)
2044 cdp->pt = cdp->lc;
2045 cdp->ptx_tofetch = false;
2046 return p;
2047 }
2048
AUDxLCH(int nr,uae_u16 v)2049 void AUDxLCH (int nr, uae_u16 v)
2050 {
2051 struct audio_channel_data *cdp = audio_channel + nr;
2052 audio_activate ();
2053 update_audio ();
2054
2055 // someone wants to update PT but DSR has not yet been processed.
2056 // too fast CPU and some tracker players: enable DMA, CPU delay, update AUDxPT with loop position
2057 if (usehacks() && ((cdp->ptx_tofetch && cdp->state == 1) || cdp->ptx_written)) {
2058 cdp->ptx = cdp->lc;
2059 cdp->ptx_written = true;
2060 #if DEBUG_AUDIO_HACK > 0
2061 if (debugchannel (nr))
2062 write_log (_T("AUD%dLCH HACK: %04X %08X (%d) (%d %d %08x)\n"), nr, v, M68K_GETPC, cdp->state, cdp->dsr, cdp->ptx_written, cdp->ptx);
2063 #endif
2064 } else {
2065 cdp->lc = (cdp->lc & 0xffff) | ((uae_u32)v << 16);
2066 #if DEBUG_AUDIO > 0
2067 if (debugchannel (nr))
2068 write_log (_T("AUD%dLCH: %04X %08X (%d) (%d %d %08x)\n"), nr, v, M68K_GETPC, cdp->state, cdp->dsr, cdp->ptx_written, cdp->ptx);
2069 #endif
2070 }
2071 }
2072
AUDxLCL(int nr,uae_u16 v)2073 void AUDxLCL (int nr, uae_u16 v)
2074 {
2075 struct audio_channel_data *cdp = audio_channel + nr;
2076 audio_activate ();
2077 update_audio ();
2078 if (usehacks() && ((cdp->ptx_tofetch && cdp->state == 1) || cdp->ptx_written)) {
2079 cdp->ptx = cdp->lc;
2080 cdp->ptx_written = true;
2081 #if DEBUG_AUDIO_HACK > 0
2082 if (debugchannel (nr))
2083 write_log (_T("AUD%dLCL HACK: %04X %08X (%d) (%d %d %08x)\n"), nr, v, M68K_GETPC, cdp->state, cdp->dsr, cdp->ptx_written, cdp->ptx);
2084 #endif
2085 } else {
2086 cdp->lc = (cdp->lc & ~0xffff) | (v & 0xFFFE);
2087 #if DEBUG_AUDIO > 0
2088 if (debugchannel (nr))
2089 write_log (_T("AUD%dLCL: %04X %08X (%d) (%d %d %08x)\n"), nr, v, M68K_GETPC, cdp->state, cdp->dsr, cdp->ptx_written, cdp->ptx);
2090 #endif
2091 }
2092 }
2093
AUDxPER(int nr,uae_u16 v)2094 void AUDxPER (int nr, uae_u16 v)
2095 {
2096 struct audio_channel_data *cdp = audio_channel + nr;
2097 unsigned long per;
2098
2099 audio_activate ();
2100 update_audio ();
2101
2102 per = v * CYCLE_UNIT;
2103 if (per == 0)
2104 per = PERIOD_MAX - 1;
2105
2106 if (per < PERIOD_MIN * CYCLE_UNIT) {
2107 /* smaller values would cause extremely high cpu usage */
2108 per = PERIOD_MIN * CYCLE_UNIT;
2109 }
2110 if (per < PERIOD_MIN_NONCE * CYCLE_UNIT && !currprefs.cpu_cycle_exact && cdp->dmaenstore) {
2111 /* DMAL emulation and low period can cause very very high cpu usage on slow performance PCs
2112 * Only do this hack if audio DMA is active.
2113 */
2114 per = PERIOD_MIN_NONCE * CYCLE_UNIT;
2115 }
2116
2117 if (cdp->per == PERIOD_MAX - 1 && per != PERIOD_MAX - 1) {
2118 cdp->evtime = CYCLE_UNIT;
2119 if (isaudio ()) {
2120 schedule_audio ();
2121 events_schedule ();
2122 }
2123 }
2124 #if TEST_AUDIO > 0
2125 cdp->per_original = v;
2126 #endif
2127 cdp->per = per;
2128 #if DEBUG_AUDIO > 0
2129 if (debugchannel (nr))
2130 write_log (_T("AUD%dPER: %d %08X\n"), nr, v, M68K_GETPC);
2131 #endif
2132 }
2133
AUDxLEN(int nr,uae_u16 v)2134 void AUDxLEN (int nr, uae_u16 v)
2135 {
2136 struct audio_channel_data *cdp = audio_channel + nr;
2137 audio_activate ();
2138 update_audio ();
2139 cdp->len = v;
2140 #if DEBUG_AUDIO > 0
2141 if (debugchannel (nr))
2142 write_log (_T("AUD%dLEN: %d %08X\n"), nr, v, M68K_GETPC);
2143 #endif
2144 }
2145
AUDxVOL(int nr,uae_u16 v)2146 void AUDxVOL (int nr, uae_u16 v)
2147 {
2148 struct audio_channel_data *cdp = audio_channel + nr;
2149
2150 // 7 bit register in Paula.
2151 v &= 127;
2152 if (v > 64)
2153 v = 64;
2154 audio_activate ();
2155 update_audio ();
2156 cdp->vol = v;
2157 #if DEBUG_AUDIO > 0
2158 if (debugchannel (nr))
2159 write_log (_T("AUD%dVOL: %d %08X\n"), nr, v, M68K_GETPC);
2160 #endif
2161 }
2162
audio_update_adkmasks(void)2163 void audio_update_adkmasks (void)
2164 {
2165 static int prevcon = -1;
2166 unsigned long t = adkcon | (adkcon >> 4);
2167
2168 audio_channel[0].adk_mask = (((t >> 0) & 1) - 1);
2169 audio_channel[1].adk_mask = (((t >> 1) & 1) - 1);
2170 audio_channel[2].adk_mask = (((t >> 2) & 1) - 1);
2171 audio_channel[3].adk_mask = (((t >> 3) & 1) - 1);
2172 if ((prevcon & 0xff) != (adkcon & 0xff)) {
2173 audio_activate ();
2174 #if DEBUG_AUDIO > 0
2175 write_log (_T("ADKCON=%02x %08X\n"), adkcon & 0xff, M68K_GETPC);
2176 #endif
2177 prevcon = adkcon;
2178 }
2179 }
2180
init_audio(void)2181 int init_audio (void)
2182 {
2183 return init_sound ();
2184 }
2185
led_filter_audio(void)2186 void led_filter_audio (void)
2187 {
2188 led_filter_on = 0;
2189 if (led_filter_forced > 0 || (gui_data.powerled && led_filter_forced >= 0))
2190 led_filter_on = 1;
2191 }
2192
audio_vsync(void)2193 void audio_vsync (void)
2194 {
2195 #if 0
2196 #if SOUNDSTUFF > 0
2197 int max, min;
2198 int vsync = isvsync ();
2199 static int lastdir;
2200
2201 if (1 || !vsync) {
2202 extrasamples = 0;
2203 return;
2204 }
2205
2206 min = -10 * 10;
2207 max = vsync ? 10 * 10 : 20 * 10;
2208 extrasamples = 0;
2209 if (gui_data.sndbuf < min) { // +1
2210 extrasamples = (min - gui_data.sndbuf) / 10;
2211 lastdir = 1;
2212 } else if (gui_data.sndbuf > max) { // -1
2213 extrasamples = (max - gui_data.sndbuf) / 10;
2214 } else if (gui_data.sndbuf > 1 * 50 && lastdir < 0) {
2215 extrasamples--;
2216 } else if (gui_data.sndbuf < -1 * 50 && lastdir > 0) {
2217 extrasamples++;
2218 } else {
2219 lastdir = 0;
2220 }
2221
2222 if (extrasamples > 99)
2223 extrasamples = 99;
2224 if (extrasamples < -99)
2225 extrasamples = -99;
2226 #endif
2227 #endif
2228 }
2229
2230 #ifdef SAVESTATE
2231
restore_audio_finish(void)2232 void restore_audio_finish (void)
2233 {
2234 last_cycles = get_cycles ();
2235 schedule_audio ();
2236 events_schedule ();
2237 }
2238
restore_audio(int nr,uae_u8 * src)2239 uae_u8 *restore_audio (int nr, uae_u8 *src)
2240 {
2241 struct audio_channel_data *acd = audio_channel + nr;
2242
2243 zerostate (nr);
2244 acd->state = restore_u8 ();
2245 acd->vol = restore_u8 ();
2246 acd->intreq2 = restore_u8 () ? true : false;
2247 uae_u8 flags = restore_u8 ();
2248 acd->dr = acd->dsr = false;
2249 if (flags & 1)
2250 acd->dr = true;
2251 if (flags & 2)
2252 acd->dsr = true;
2253 acd->len = restore_u16 ();
2254 acd->wlen = restore_u16 ();
2255 uae_u16 p = restore_u16 ();
2256 acd->per = p ? p * CYCLE_UNIT : PERIOD_MAX;
2257 acd->dat = acd->dat2 = restore_u16 ();
2258 acd->lc = restore_u32 ();
2259 acd->pt = restore_u32 ();
2260 acd->evtime = restore_u32 ();
2261 if (flags & 0x80)
2262 acd->drhpos = restore_u8 ();
2263 else
2264 acd->drhpos = 1;
2265 acd->dmaenstore = (dmacon & DMA_MASTER) && (dmacon & (1 << nr));
2266 return src;
2267 }
2268
2269 #endif /* SAVESTATE */
2270
2271 #if defined SAVESTATE || defined DEBUGGER
2272
save_audio(int nr,int * len,uae_u8 * dstptr)2273 uae_u8 *save_audio (int nr, int *len, uae_u8 *dstptr)
2274 {
2275 struct audio_channel_data *acd = audio_channel + nr;
2276 uae_u8 *dst, *dstbak;
2277
2278 if (dstptr)
2279 dstbak = dst = dstptr;
2280 else
2281 dstbak = dst = xmalloc (uae_u8, 100);
2282 save_u8 (acd->state);
2283 save_u8 (acd->vol);
2284 save_u8 (acd->intreq2);
2285 save_u8 ((acd->dr ? 1 : 0) | (acd->dsr ? 2 : 0) | 0x80);
2286 save_u16 (acd->len);
2287 save_u16 (acd->wlen);
2288 save_u16 (acd->per == PERIOD_MAX ? 0 : acd->per / CYCLE_UNIT);
2289 save_u16 (acd->dat);
2290 save_u32 (acd->lc);
2291 save_u32 (acd->pt);
2292 save_u32 (acd->evtime);
2293 save_u8 (acd->drhpos);
2294 *len = dst - dstbak;
2295 return dstbak;
2296 }
2297
2298 #endif /* defined SAVESTATE || defined DEBUGGER */
2299
audio_set_extra_channels(void)2300 static void audio_set_extra_channels(void)
2301 {
2302 audio_channel_count = audio_extra_channels[1] ? AUDIO_CHANNELS_PAULA + 4 : (audio_extra_channels[0] ? AUDIO_CHANNELS_PAULA + 2 : AUDIO_CHANNELS_PAULA);
2303 set_extra_prehandler();
2304 }
2305
audio_enable_sndboard(bool enable)2306 void audio_enable_sndboard(bool enable)
2307 {
2308 struct audio_channel_data *acd = audio_channel + AUDIO_CHANNEL_SNDBOARD_LEFT;
2309 if (!enable) {
2310 audio_extra_channels[0] = false;
2311 acd[1].evtime = acd->evtime = MAX_EV;
2312 } else {
2313 audio_extra_channels[0] = true;
2314 acd[1].evtime = acd->evtime = CYCLE_UNIT;
2315 acd[1].adk_mask = acd->adk_mask = 0xffffffff;
2316 acd[1].vol = acd->vol = 1;
2317 audio_activate();
2318 }
2319 audio_set_extra_channels();
2320 }
2321
audio_enable_cda(bool enable)2322 static void audio_enable_cda(bool enable)
2323 {
2324 struct audio_channel_data *acd = audio_channel + AUDIO_CHANNEL_CDA_LEFT;
2325 if (!enable) {
2326 audio_extra_channels[1] = false;
2327 acd[1].evtime = acd->evtime = MAX_EV;
2328 } else {
2329 audio_extra_channels[1] = true;
2330 acd[1].evtime = acd->evtime = CYCLE_UNIT;
2331 acd[1].adk_mask = acd->adk_mask = 0xffffffff;
2332 acd[1].vol = acd->vol = 1;
2333 audio_activate();
2334 }
2335 audio_set_extra_channels();
2336 }
2337
audio_state_sndboard_state(int ch,int sample,unsigned int evt)2338 void audio_state_sndboard_state(int ch, int sample, unsigned int evt)
2339 {
2340 struct audio_channel_data *acd = audio_channel + AUDIO_CHANNEL_SNDBOARD_LEFT + ch;
2341 acd->last_sample = acd->current_sample;
2342 acd->current_sample = sample;
2343 acd->evtime = evt;
2344 }
2345
2346 static uae_s16 *cda_bufptr;
2347 static int cda_length, cda_userdata;
2348 static unsigned int cda_evt;
2349 static uae_s16 dummy_buffer[4] = { 0 };
2350 static CDA_CALLBACK cda_next_cd_audio_buffer_callback;
2351 static int cda_volume[2];
2352
update_cda_sound(double clk)2353 void update_cda_sound(double clk)
2354 {
2355 cda_evt = clk * CYCLE_UNIT / 44100;
2356 }
2357
audio_cda_volume(int left,int right)2358 void audio_cda_volume(int left, int right)
2359 {
2360 for (int j = 0; j < 2; j++) {
2361 cda_volume[j] = j == 0 ? left : right;
2362 cda_volume[j] = sound_cd_volume[j] * cda_volume[j] / 32768;
2363 if (cda_volume[j])
2364 cda_volume[j]++;
2365 if (cda_volume[j] >= 32768)
2366 cda_volume[j] = 32768;
2367 }
2368 }
2369
audio_cda_new_buffer(uae_s16 * buffer,int length,int userdata,CDA_CALLBACK next_cd_audio_buffer_callback)2370 void audio_cda_new_buffer(uae_s16 *buffer, int length, int userdata, CDA_CALLBACK next_cd_audio_buffer_callback)
2371 {
2372 if (!buffer) {
2373 cda_bufptr = dummy_buffer;
2374 cda_length = 0;
2375 } else {
2376 cda_bufptr = buffer;
2377 cda_length = length;
2378 cda_userdata = userdata;
2379 if (!audio_extra_channels[1])
2380 audio_enable_cda(true);
2381 }
2382 cda_next_cd_audio_buffer_callback = next_cd_audio_buffer_callback;
2383 audio_activate();
2384 }
2385
audio_state_cda(void)2386 static void audio_state_cda(void)
2387 {
2388 if (cda_bufptr >= dummy_buffer && cda_bufptr <= dummy_buffer + 4) {
2389 audio_enable_cda(false);
2390 return;
2391 }
2392 struct audio_channel_data *acd = audio_channel + AUDIO_CHANNEL_CDA_LEFT;
2393 acd->last_sample = acd->current_sample;
2394 acd->current_sample = cda_bufptr[0] * cda_volume[0] / 32768;
2395 acd->evtime = cda_evt;
2396 acd++;
2397 acd->last_sample = acd->current_sample;
2398 acd->current_sample = cda_bufptr[1] * cda_volume[1] / 32768;
2399 acd->evtime = cda_evt;
2400 cda_bufptr += 2;
2401 cda_length--;
2402 if (cda_length <= 0 && cda_next_cd_audio_buffer_callback) {
2403 cda_next_cd_audio_buffer_callback(cda_userdata);
2404 }
2405 }
2406