1  /*
2   * UADE's audio state machine emulation
3   *
4   * Copyright 1995, 1996, 1997 Bernd Schmidt
5   * Copyright 1996 Marcus Sundberg
6   * Copyright 1996 Manfred Thole
7   * Copyright 2005 Heikki Orsila
8   * Copyright 2005 Antti S. Lankila
9   */
10 
11 #include <math.h>
12 
13 #include "sysconfig.h"
14 #include "sysdeps.h"
15 
16 #include "options.h"
17 #include "memory.h"
18 #include "custom.h"
19 #include "gensound.h"
20 #include "sd-sound.h"
21 #include "events.h"
22 #include "cia.h"
23 #include "audio.h"
24 #include "amigafilter.h"
25 #include "uade.h"
26 #include "compilersupport.h"
27 
28 #include "sinctable.h"
29 
30 #include "text_scope.h"
31 
32 
33 struct audio_channel_data audio_channel[4];
34 static void (*sample_handler) (void);
35 static void (*sample_prehandler) (unsigned long best_evtime);
36 
37 /* Average time in bus cycles to output a new sample */
38 static float sample_evtime_interval;
39 static float next_sample_evtime;
40 
41 int sound_available;
42 
43 static int use_text_scope;
44 
45 static int sound_use_filter = FILTER_MODEL_A500;
46 
47 /* denormals are very small floating point numbers that force FPUs into slow
48    mode. All lowpass filters using floats are suspectible to denormals unless
49    a small offset is added to avoid very small floating point numbers. */
50 #define DENORMAL_OFFSET (1E-10)
51 
52 static unsigned long last_audio_cycles;
53 
54 static int audperhack;
55 
56 static struct filter_state {
57     float rc1, rc2, rc3, rc4, rc5;
58 } sound_filter_state[2];
59 
60 static float a500e_filter1_a0;
61 static float a500e_filter2_a0;
62 static float filter_a0; /* a500 and a1200 use the same */
63 
64 
clamp_sample(int o)65 static inline int clamp_sample(int o)
66 {
67     if (unlikely(o > 32767 || o < -32768)) {
68 	if (o > 32767) {
69 	    return 32767;
70 	} else {
71 	    return -32768;
72 	}
73     }
74     return o;
75 }
76 
77 
78 /* Amiga has two separate filtering circuits per channel, a static RC filter
79  * on A500 and the LED filter. This code emulates both.
80  *
81  * The Amiga filtering circuitry depends on Amiga model. Older Amigas seem
82  * to have a 6 dB/oct RC filter with cutoff frequency such that the -6 dB
83  * point for filter is reached at 6 kHz, while newer Amigas have no filtering.
84  *
85  * The LED filter is complicated, and we are modelling it with a pair of
86  * RC filters, the other providing a highboost. The LED starts to cut
87  * into signal somewhere around 5-6 kHz, and there's some kind of highboost
88  * in effect above 12 kHz. Better measurements are required.
89  *
90  * The current filtering should be accurate to 2 dB with the filter on,
91  * and to 1 dB with the filter off.
92 */
93 
filter(int input,struct filter_state * fs)94 static int filter(int input, struct filter_state *fs)
95 {
96     float tmp, normal_output, led_output;
97 
98     switch (sound_use_filter) {
99     case FILTER_MODEL_A500:
100 	fs->rc1 = a500e_filter1_a0 * input + (1 - a500e_filter1_a0) * fs->rc1 + DENORMAL_OFFSET;
101 	fs->rc2 = a500e_filter2_a0 * fs->rc1 + (1-a500e_filter2_a0) * fs->rc2;
102 	normal_output = fs->rc2;
103 
104 	fs->rc3 = filter_a0 * normal_output + (1 - filter_a0) * fs->rc3;
105 	fs->rc4 = filter_a0 * fs->rc3       + (1 - filter_a0) * fs->rc4;
106 	fs->rc5 = filter_a0 * fs->rc4       + (1 - filter_a0) * fs->rc5;
107 
108 	led_output = fs->rc5;
109         break;
110 
111     case FILTER_MODEL_A1200:
112         normal_output = input;
113 
114         fs->rc2 = filter_a0 * normal_output + (1 - filter_a0) * fs->rc2 + DENORMAL_OFFSET;
115         fs->rc3 = filter_a0 * fs->rc2       + (1 - filter_a0) * fs->rc3;
116         fs->rc4 = filter_a0 * fs->rc3       + (1 - filter_a0) * fs->rc4;
117 
118         led_output = fs->rc4;
119         break;
120 
121     default:
122 	fprintf(stderr, "Unknown filter mode\n");
123 	exit(-1);
124     }
125 
126     return clamp_sample(gui_ledstate ? led_output : normal_output);
127 }
128 
129 
check_sound_buffers(void)130 static void check_sound_buffers (void)
131 {
132     intptr_t bytes;
133 
134     if (uade_reboot)
135 	return;
136 
137     assert(uade_read_size > 0);
138 
139     bytes = ((intptr_t) sndbufpt) - ((intptr_t) sndbuffer);
140 
141     if (uade_audio_output) {
142 	if (bytes == uade_read_size) {
143 	    uade_check_sound_buffers(uade_read_size);
144 	    sndbufpt = sndbuffer;
145 	}
146     } else {
147 	uade_audio_skip += bytes;
148 	/* if sound core doesn't report audio output start in 3 seconds from
149 	   the reboot, begin audio output anyway */
150 	if (uade_audio_skip >= (sound_bytes_per_second * 3)) {
151 	    fprintf(stderr, "involuntary audio output start\n");
152 	    uade_audio_output = 1;
153 	}
154 	sndbufpt = sndbuffer;
155     }
156 }
157 
158 
sample_backend(int left,int right)159 static inline void sample_backend(int left, int right)
160 {
161 #if AUDIO_DEBUG
162     int nr;
163     for (nr = 0; nr < 4; nr++) {
164 	struct audio_channel_data *cdp = audio_channel + nr;
165 	if (cdp->state != 0 && cdp->datpt != 0 && (dmacon & (1 << nr)) && cdp->datpt >= cdp->datptend) {
166 	    fprintf(stderr, "Audio output overrun on channel %d: %.8x/%.8x\n", nr, cdp->datpt, cdp->datptend);
167 	}
168     }
169 #endif
170 
171     /* samples are in range -16384 (-128*64*2) and 16256 (127*64*2) */
172     left <<= 16 - 14 - 1;
173     right <<= 16 - 14 - 1;
174     /* [-32768, 32512] */
175 
176     if (sound_use_filter) {
177 	left = filter(left, &sound_filter_state[0]);
178 	right = filter(right, &sound_filter_state[1]);
179     }
180 
181     *(sndbufpt++) = left;
182     *(sndbufpt++) = right;
183 
184     check_sound_buffers();
185 }
186 
187 
sample16s_handler(void)188 static void sample16s_handler (void)
189 {
190     int datas[4];
191     int i;
192 
193     for (i = 0; i < 4; i++) {
194 	datas[i] = audio_channel[i].current_sample * audio_channel[i].vol;
195 	datas[i] &= audio_channel[i].adk_mask;
196     }
197 
198     sample_backend(datas[0] + datas[3], datas[1] + datas[2]);
199 }
200 
201 
202 /* This interpolator examines sample points when Paula switches the output
203  * voltage and computes the average of Paula's output */
sample16si_anti_handler(void)204 static void sample16si_anti_handler (void)
205 {
206     int i;
207     int datas[4];
208 
209     for (i = 0; i < 4; i += 1) {
210         datas[i] = audio_channel[i].sample_accum / audio_channel[i].sample_accum_time;
211         audio_channel[i].sample_accum = 0;
212 	audio_channel[i].sample_accum_time = 0;
213     }
214 
215     sample_backend(datas[0] + datas[3], datas[1] + datas[2]);
216 }
217 
218 
219 /* this interpolator performs BLEP mixing (bleps are shaped like integrated sinc
220  * functions) with a type of BLEP that matches the filtering configuration. */
sample16si_sinc_handler(void)221 static void sample16si_sinc_handler (void)
222 {
223     int i, n;
224     int const *winsinc;
225     int datas[4];
226 
227     if (sound_use_filter) {
228 	n = (sound_use_filter == FILTER_MODEL_A500) ? 0 : 2;
229         if (gui_ledstate)
230             n += 1;
231     } else {
232 	n = 4;
233     }
234     winsinc = winsinc_integral[n];
235 
236     for (i = 0; i < 4; i += 1) {
237         int j;
238         struct audio_channel_data *acd = &audio_channel[i];
239         /* The sum rings with harmonic components up to infinity... */
240 	int sum = acd->output_state << 17;
241         /* ...but we cancel them through mixing in BLEPs instead */
242         int offsetpos = acd->sinc_queue_head & (SINC_QUEUE_LENGTH - 1);
243         for (j = 0; j < SINC_QUEUE_LENGTH; j += 1) {
244             int age = acd->sinc_queue_time - acd->sinc_queue[offsetpos].time;
245             if (age >= SINC_QUEUE_MAX_AGE)
246                 break;
247             sum -= winsinc[age] * acd->sinc_queue[offsetpos].output;
248             offsetpos = (offsetpos + 1) & (SINC_QUEUE_LENGTH - 1);
249         }
250         datas[i] = sum >> 16;
251     }
252 
253     *(sndbufpt++) = clamp_sample(datas[0] + datas[3]);
254     *(sndbufpt++) = clamp_sample(datas[1] + datas[2]);
255 
256     check_sound_buffers();
257 }
258 
259 
anti_prehandler(unsigned long best_evtime)260 static void anti_prehandler(unsigned long best_evtime)
261 {
262     int i, j, output;
263     struct audio_channel_data *acd;
264 
265     /* Handle accumulator antialiasiation */
266     for (i = 0; i < 4; i++) {
267 	acd = &audio_channel[i];
268 	output = (acd->current_sample * acd->vol) & acd->adk_mask;
269 	acd->sample_accum += output * best_evtime;
270 	acd->sample_accum_time += best_evtime;
271     }
272 }
273 
274 
sinc_prehandler(unsigned long best_evtime)275 static void sinc_prehandler(unsigned long best_evtime)
276 {
277     int i, j, output;
278     struct audio_channel_data *acd;
279 
280     for (i = 0; i < 4; i++) {
281 	acd = &audio_channel[i];
282 	output = (acd->current_sample * acd->vol) & acd->adk_mask;
283 
284         /* if output state changes, record the state change and also
285          * write data into sinc queue for mixing in the BLEP */
286         if (acd->output_state != output) {
287             acd->sinc_queue_head = (acd->sinc_queue_head - 1) & (SINC_QUEUE_LENGTH - 1);
288             acd->sinc_queue[acd->sinc_queue_head].time = acd->sinc_queue_time;
289             acd->sinc_queue[acd->sinc_queue_head].output = output - acd->output_state;
290             acd->output_state = output;
291         }
292 
293         acd->sinc_queue_time += best_evtime;
294     }
295 }
296 
297 
audio_handler(int nr)298 static void audio_handler (int nr)
299 {
300     struct audio_channel_data *cdp = audio_channel + nr;
301 
302     switch (cdp->state) {
303      case 0:
304 	fprintf(stderr, "Bug in sound code\n");
305 	break;
306 
307      case 1:
308 	/* We come here at the first hsync after DMA was turned on. */
309 	cdp->evtime = maxhpos;
310 
311 	cdp->state = 5;
312 	INTREQ(0x8000 | (0x80 << nr));
313 	if (cdp->wlen != 1)
314 	    cdp->wlen = (cdp->wlen - 1) & 0xFFFF;
315 	cdp->nextdat = chipmem_bank.wget(cdp->pt);
316 
317 	cdp->nextdatpt = cdp->pt;
318 	cdp->nextdatptend = cdp->ptend;
319 
320 	/* BUG in UAE. Only hsync handler should increase DMA pointer
321 	   cdp->pt += 2;
322 	*/
323 	break;
324 
325      case 5:
326 	/* We come here at the second hsync after DMA was turned on. */
327 	cdp->evtime = cdp->per;
328 	cdp->dat = cdp->nextdat;
329 
330 	cdp->datpt = cdp->nextdatpt;
331 	cdp->datptend = cdp->nextdatptend;
332 
333 	cdp->current_sample = (uae_s8)(cdp->dat >> 8);
334 
335 	cdp->state = 2;
336 	{
337 	    int audav = adkcon & (1 << nr);
338 	    int audap = adkcon & (16 << nr);
339 	    int napnav = (!audav && !audap) || audav;
340 	    if (napnav)
341 		cdp->data_written = 2;
342 	}
343 	break;
344 
345      case 2:
346 	/* We come here when a 2->3 transition occurs */
347 	cdp->current_sample = (uae_s8)(cdp->dat & 0xFF);
348 	cdp->evtime = cdp->per;
349 
350 	cdp->state = 3;
351 
352 	/* Period attachment? */
353 	if (adkcon & (0x10 << nr)) {
354 	    if (cdp->intreq2 && cdp->dmaen) {
355 		INTREQ(0x8000 | (0x80 << nr));
356 	    }
357 	    cdp->intreq2 = 0;
358 
359 	    cdp->dat = cdp->nextdat;
360 
361 	    cdp->datpt = cdp->nextdatpt;
362 	    cdp->datptend = cdp->nextdatptend;
363 
364 	    if (cdp->dmaen)
365 		cdp->data_written = 2;
366 	    if (nr < 3) {
367 		if (cdp->dat == 0)
368 		    (cdp+1)->per = 65535;
369 		else
370 		    (cdp+1)->per = cdp->dat;
371 	    }
372 	}
373 	break;
374 
375      case 3:
376 	/* We come here when a 3->2 transition occurs */
377 	cdp->evtime = cdp->per;
378 
379 	if ((INTREQR() & (0x80 << nr)) && !cdp->dmaen) {
380 	    cdp->state = 0;
381 	    cdp->current_sample = 0;
382 	    break;
383 	} else {
384 	    int audav = adkcon & (1 << nr);
385 	    int audap = adkcon & (16 << nr);
386 	    int napnav = (!audav && !audap) || audav;
387 	    cdp->state = 2;
388 
389 	    if ((cdp->intreq2 && cdp->dmaen && napnav)
390 		|| (napnav && !cdp->dmaen)) {
391 	      INTREQ(0x8000 | (0x80 << nr));
392 	    }
393 	    cdp->intreq2 = 0;
394 
395 	    cdp->dat = cdp->nextdat;
396 
397 	    cdp->datpt = cdp->nextdatpt;
398 	    cdp->datptend = cdp->nextdatptend;
399 
400 	    cdp->current_sample = (uae_s8)(cdp->dat >> 8);
401 
402 	    if (cdp->dmaen && napnav)
403 		cdp->data_written = 2;
404 
405 	    /* Volume attachment? */
406 	    if (audav) {
407 		if (nr < 3) {
408 		    (cdp+1)->vol = cdp->dat;
409 		}
410 	    }
411 	}
412 	break;
413 
414      default:
415 	cdp->state = 0;
416 	break;
417     }
418 }
419 
420 
audio_reset(void)421 void audio_reset (void)
422 {
423     memset (audio_channel, 0, sizeof audio_channel);
424     audio_channel[0].per = 65535;
425     audio_channel[1].per = 65535;
426     audio_channel[2].per = 65535;
427     audio_channel[3].per = 65535;
428 
429     last_audio_cycles = 0;
430     next_sample_evtime = sample_evtime_interval;
431 
432     audperhack = 0;
433 
434     memset(sound_filter_state, 0, sizeof sound_filter_state);
435 
436     audio_set_resampler(NULL);
437 
438     use_text_scope = 0;
439 }
440 
441 
442 /* This computes the 1st order low-pass filter term b0.
443  * The a1 term is 1.0 - b0. The center frequency marks the -3 dB point. */
rc_calculate_a0(int sample_rate,int cutoff_freq)444 static float rc_calculate_a0(int sample_rate, int cutoff_freq)
445 {
446     float omega;
447 
448     /* The BLT correction formula below blows up if the cutoff is above nyquist. */
449     if (cutoff_freq >= sample_rate / 2)
450         return 1.0;
451 
452     omega = 2 * M_PI * cutoff_freq / sample_rate;
453     /* Compensate for the bilinear transformation. This allows us to specify the
454      * stop frequency more exactly, but the filter becomes less steep further
455      * from stopband. */
456     omega = tan(omega / 2) * 2;
457     return 1 / (1 + 1/omega);
458 }
459 
460 
audio_set_filter(int filter_type,int filter_force)461 void audio_set_filter(int filter_type, int filter_force)
462 {
463   /* If filter_type is zero, filtering is disabled, but if it's
464      non-zero, it contains the filter type (a500 or a1200) */
465   if (filter_type < 0 || filter_type >= FILTER_MODEL_UPPER_BOUND) {
466     fprintf(stderr, "Invalid filter number: %d\n", filter_type);
467     exit(-1);
468   }
469   sound_use_filter = filter_type;
470 
471   if (filter_force & 2) {
472     gui_ledstate_forced = filter_force & 3;
473     gui_ledstate = gui_ledstate_forced & 1;
474   } else {
475     gui_ledstate_forced = 0;
476     gui_ledstate = (~ciaapra & 2) >> 1;
477   }
478 }
479 
480 
audio_set_rate(int rate)481 void audio_set_rate(int rate)
482 {
483     sample_evtime_interval = ((float) SOUNDTICKS) / rate;
484 
485     /* Although these numbers are in Hz, these values should not be taken to
486      * be the true filter cutoff values of Amiga 500 and Amiga 1200.
487      * This is because these filters are composites. The true values are
488      * 5 kHz (or 4.5 kHz possibly on some models) for A500 fixed lowpass filter
489      * and 1.7 kHz 12 db/oct Butterworth for the LED filter.
490      */
491     a500e_filter1_a0 = rc_calculate_a0(rate, 6200);
492     a500e_filter2_a0 = rc_calculate_a0(rate, 20000);
493     filter_a0 = rc_calculate_a0(rate, 7000);
494 }
495 
496 
audio_set_resampler(char * name)497 void audio_set_resampler(char *name)
498 {
499     sample_handler = sample16si_anti_handler;
500     sample_prehandler = anti_prehandler;
501 
502     if (name == NULL || strcasecmp(name, "default") == 0)
503 	return;
504 
505     if (strcasecmp(name, "sinc") == 0) {
506 	sample_handler = sample16si_sinc_handler;
507 	sample_prehandler = sinc_prehandler;
508     } else if (strcasecmp(name, "none") == 0) {
509 	sample_handler = sample16s_handler;
510 	sample_prehandler = NULL;
511     } else {
512 	fprintf(stderr, "\nUnknown resampling method: %s. Using the default.\n", name);
513     }
514 }
515 
516 
audio_use_text_scope(void)517 void audio_use_text_scope(void)
518 {
519     use_text_scope = 1;
520 }
521 
522 
523 /* update_audio() emulates actions of audio state machine since it was last
524    time called. One can assume it is called at least once per horizontal
525    line and possibly more often. */
update_audio(void)526 void update_audio (void)
527 {
528     /* Number of cycles that has passed since last call to update_audio() */
529     unsigned long n_cycles = cycles - last_audio_cycles;
530 
531     while (n_cycles > 0) {
532 	unsigned long best_evtime = n_cycles + 1;
533 	int i;
534 	unsigned long rounded;
535 	float f;
536 
537 	for (i = 0; i < 4; i++) {
538 	    if (audio_channel[i].state != 0 && best_evtime > audio_channel[i].evtime)
539 		best_evtime = audio_channel[i].evtime;
540 	}
541 
542 	/* next_sample_evtime >= 0 so floor() behaves as expected */
543 	rounded = floorf(next_sample_evtime);
544 	if ((next_sample_evtime - rounded) >= 0.5)
545 	    rounded++;
546 
547 	if (best_evtime > rounded)
548 	    best_evtime = rounded;
549 
550 	if (best_evtime > n_cycles)
551 	    best_evtime = n_cycles;
552 
553 	/* Decrease time-to-wait counters */
554 	next_sample_evtime -= best_evtime;
555 
556 	/* sample_prehandler makes it possible to compute effects with
557 	   accuracy of one bus cycle. sample_handler is only called when
558 	   a sample is outputted. */
559 	if (sample_prehandler != NULL)
560 	    sample_prehandler(best_evtime);
561 
562 	for (i = 0; i < 4; i++)
563 	    audio_channel[i].evtime -= best_evtime;
564 
565 	n_cycles -= best_evtime;
566 
567 	/* Test if new sample needs to be outputted */
568 	if (rounded == best_evtime) {
569 	    /* Before the following addition, next_sample_evtime is in range
570 	       [-0.5, 0.5) */
571 	    next_sample_evtime += sample_evtime_interval;
572 	    (*sample_handler) ();
573 	}
574 
575 	/* Call audio state machines if needed */
576 	for (i = 0; i < 4; i++) {
577 	    if (audio_channel[i].evtime == 0 && audio_channel[i].state != 0)
578 		audio_handler(i);
579 	}
580     }
581 
582     last_audio_cycles = cycles - n_cycles;
583 }
584 
585 
AUDxDAT(int nr,uae_u16 v)586 void AUDxDAT (int nr, uae_u16 v)
587 {
588     struct audio_channel_data *cdp = audio_channel + nr;
589 
590     TEXT_SCOPE(cycles, nr, PET_DAT, v);
591 
592     update_audio ();
593 
594     cdp->dat = v;
595     cdp->datpt = 0;
596 
597     if (cdp->state == 0 && !(INTREQR() & (0x80 << nr))) {
598 	cdp->state = 2;
599 	INTREQ(0x8000 | (0x80 << nr));
600 	/* data_written = 2 ???? */
601 	cdp->evtime = cdp->per;
602     }
603 }
604 
605 
AUDxLCH(int nr,uae_u16 v)606 void AUDxLCH (int nr, uae_u16 v)
607 {
608     TEXT_SCOPE(cycles, nr, PET_LCH, v);
609 
610     update_audio ();
611 
612     audio_channel[nr].lc = (audio_channel[nr].lc & 0xffff) | ((uae_u32)v << 16);
613 }
614 
615 
AUDxLCL(int nr,uae_u16 v)616 void AUDxLCL (int nr, uae_u16 v)
617 {
618     TEXT_SCOPE(cycles, nr, PET_LCL, v);
619 
620     update_audio ();
621 
622     audio_channel[nr].lc = (audio_channel[nr].lc & ~0xffff) | (v & 0xFFFE);
623 }
624 
625 
AUDxPER(int nr,uae_u16 v)626 void AUDxPER (int nr, uae_u16 v)
627 {
628     TEXT_SCOPE(cycles, nr, PET_PER, v);
629 
630     update_audio ();
631 
632     if (v == 0)
633 	v = 65535;
634     else if (v < 16) {
635 	/* With the risk of breaking super-cool players,
636 	   we limit the value to 16 to save cpu time on not so powerful
637 	   machines. robocop customs use low values for example. */
638 	if (!audperhack) {
639 	    audperhack = 1;
640 	    uade_send_debug("Eagleplayer inserted %d into aud%dper.", v, nr);
641 	}
642 	v = 16;
643     }
644     audio_channel[nr].per = v;
645 }
646 
647 
AUDxLEN(int nr,uae_u16 v)648 void AUDxLEN (int nr, uae_u16 v)
649 {
650     TEXT_SCOPE(cycles, nr, PET_LEN, v);
651 
652     update_audio ();
653 
654     audio_channel[nr].len = v;
655 }
656 
657 
AUDxVOL(int nr,uae_u16 v)658 void AUDxVOL (int nr, uae_u16 v)
659 {
660     int v2 = v & 64 ? 63 : v & 63;
661 
662     TEXT_SCOPE(cycles, nr, PET_VOL, v);
663 
664     update_audio ();
665 
666     audio_channel[nr].vol = v2;
667 }
668