--- uae/src/audio.c 2018/04/24 16:48:32 1.1.1.6 +++ uae/src/audio.c 2018/04/24 17:12:19 1.1.1.14 @@ -15,32 +15,22 @@ #include "options.h" #include "memory.h" #include "custom.h" +#include "newcpu.h" +#include "autoconf.h" #include "gensound.h" #include "sounddep/sound.h" #include "events.h" #include "audio.h" - -#undef BENCHMARK_AUDIO - -#ifdef BENCHMARK_AUDIO - -#define BEGIN_BENCH frame_time_t audbench = read_processor_time (); -#define END_BENCH sh_time += read_processor_time () - audbench; sh_count++; -static frame_time_t sh_time = 0; -unsigned long sh_count = 0; - -#else - -#define BEGIN_BENCH -#define END_BENCH - -#endif +#include "savestate.h" struct audio_channel_data audio_channel[4]; int sound_available = 0; int sound_table[64][256]; void (*sample_handler) (void); -unsigned long int sample_evtime; + +unsigned long sample_evtime, scaled_sample_evtime; +int scaled_sample_evtime_ok; + static unsigned long last_cycles, next_sample_evtime; void init_sound_table16 (void) @@ -49,7 +39,7 @@ void init_sound_table16 (void) for (i = 0; i < 256; i++) for (j = 0; j < 64; j++) - sound_table[j][i] = j * (uae_s8)i * (currprefs.stereo ? 2 : 1); + sound_table[j][i] = j * (uae_s8)i * (currprefs.sound_stereo ? 2 : 1); } void init_sound_table8 (void) @@ -58,7 +48,7 @@ void init_sound_table8 (void) for (i = 0; i < 256; i++) for (j = 0; j < 64; j++) - sound_table[j][i] = (j * (uae_s8)i * (currprefs.stereo ? 2 : 1)) / 256; + sound_table[j][i] = (j * (uae_s8)i * (currprefs.sound_stereo ? 2 : 1)) / 256; } #define MULTIPLICATION_PROFITABLE @@ -68,7 +58,7 @@ typedef uae_s8 sample8_t; #define DO_CHANNEL_1(v, c) do { (v) *= audio_channel[c].vol; } while (0) #define SBASEVAL8(logn) ((logn) == 1 ? SOUND8_BASE_VAL << 7 : SOUND8_BASE_VAL << 8) #define SBASEVAL16(logn) ((logn) == 1 ? SOUND16_BASE_VAL >> 1 : SOUND16_BASE_VAL) -#define FINISH_DATA(b,logn) do { if (14 - (b) + (logn) > 0) data >>= 14 - (b) + (logn); else data <<= (b) - 14 - (logn); } while (0); +#define FINISH_DATA(data,b,logn) do { if (14 - (b) + (logn) > 0) (data) >>= 14 - (b) + (logn); else (data) <<= (b) - 14 - (logn); } while (0); #else typedef uae_u8 sample8_t; #define DO_CHANNEL_1(v, c) do { (v) = audio_channel[c].voltbl[(v)]; } while (0) @@ -77,12 +67,47 @@ typedef uae_u8 sample8_t; #define FINISH_DATA(b,logn) #endif +/* Always put the right word before the left word. */ +#define DELAY_BUFFER 32 +static uae_u32 right_word_saved[DELAY_BUFFER]; +static uae_u32 left_word_saved[DELAY_BUFFER]; +static int saved_ptr; + +STATIC_INLINE void put_sound_word_right (uae_u32 w) +{ + if (currprefs.mixed_stereo) { + right_word_saved[saved_ptr] = w; + return; + } + + PUT_SOUND_WORD_RIGHT (w); +} + +STATIC_INLINE void put_sound_word_left (uae_u32 w) +{ + if (currprefs.mixed_stereo) { + uae_u32 rold, lold, rnew, lnew, tmp; + + left_word_saved[saved_ptr] = w; + lnew = w - SOUND16_BASE_VAL; + rnew = right_word_saved[saved_ptr] - SOUND16_BASE_VAL; + + saved_ptr = (saved_ptr + 1) & (DELAY_BUFFER - 1); + lold = left_word_saved[saved_ptr] - SOUND16_BASE_VAL; + tmp = (rnew * 5 + lold * 3) >> 3; + tmp += SOUND16_BASE_VAL; + PUT_SOUND_WORD_RIGHT (tmp); + + rold = right_word_saved[saved_ptr] - SOUND16_BASE_VAL; + w = (lnew * 5 + rold * 3) >> 3; + } + PUT_SOUND_WORD_LEFT (w); +} + #define DO_CHANNEL(v, c) do { (v) &= audio_channel[c].adk_mask; data += v; } while (0); void sample16_handler (void) { - BEGIN_BENCH - uae_u32 data0 = audio_channel[0].current_sample; uae_u32 data1 = audio_channel[1].current_sample; uae_u32 data2 = audio_channel[2].current_sample; @@ -100,11 +125,9 @@ void sample16_handler (void) data0 += data3; { uae_u32 data = SBASEVAL16(2) + data0; - FINISH_DATA(16, 2); + FINISH_DATA (data, 16, 2); PUT_SOUND_WORD (data); } - END_BENCH - check_sound_buffers (); } @@ -112,8 +135,6 @@ void sample16i_rh_handler (void) { unsigned long delta, ratio; - BEGIN_BENCH - uae_u32 data0 = audio_channel[0].current_sample; uae_u32 data1 = audio_channel[1].current_sample; uae_u32 data2 = audio_channel[2].current_sample; @@ -156,20 +177,15 @@ void sample16i_rh_handler (void) { uae_u32 data = SBASEVAL16(2) + data0; - FINISH_DATA(16, 2); + FINISH_DATA (data, 16, 2); PUT_SOUND_WORD (data); } - END_BENCH check_sound_buffers (); } void sample16i_crux_handler (void) { - unsigned long delta, ratio; - - BEGIN_BENCH - uae_u32 data0 = audio_channel[0].current_sample; uae_u32 data1 = audio_channel[1].current_sample; uae_u32 data2 = audio_channel[2].current_sample; @@ -198,33 +214,33 @@ void sample16i_crux_handler (void) { struct audio_channel_data *cdp; - int ratio, ratio1; -#define INTERVAL (sample_evtime * 3) + unsigned long ratio, ratio1; +#define INTERVAL (scaled_sample_evtime * 3) cdp = audio_channel + 0; ratio1 = cdp->per - cdp->evtime; ratio = (ratio1 << 12) / INTERVAL; - if (cdp->evtime < sample_evtime || ratio1 >= INTERVAL) + if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL) ratio = 4096; data0 = (data0 * ratio + data0p * (4096 - ratio)) >> 12; cdp = audio_channel + 1; ratio1 = cdp->per - cdp->evtime; ratio = (ratio1 << 12) / INTERVAL; - if (cdp->evtime < sample_evtime || ratio1 >= INTERVAL) + if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL) ratio = 4096; data1 = (data1 * ratio + data1p * (4096 - ratio)) >> 12; cdp = audio_channel + 2; ratio1 = cdp->per - cdp->evtime; ratio = (ratio1 << 12) / INTERVAL; - if (cdp->evtime < sample_evtime || ratio1 >= INTERVAL) + if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL) ratio = 4096; data2 = (data2 * ratio + data2p * (4096 - ratio)) >> 12; cdp = audio_channel + 3; ratio1 = cdp->per - cdp->evtime; ratio = (ratio1 << 12) / INTERVAL; - if (cdp->evtime < sample_evtime || ratio1 >= INTERVAL) + if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL) ratio = 4096; data3 = (data3 * ratio + data3p * (4096 - ratio)) >> 12; } @@ -233,18 +249,14 @@ void sample16i_crux_handler (void) data0 += data1; { uae_u32 data = SBASEVAL16(2) + data0; - FINISH_DATA(16, 2); + FINISH_DATA (data, 16, 2); PUT_SOUND_WORD (data); } - END_BENCH - check_sound_buffers (); } void sample8_handler (void) { - BEGIN_BENCH - uae_u32 data0 = audio_channel[0].current_sample; uae_u32 data1 = audio_channel[1].current_sample; uae_u32 data2 = audio_channel[2].current_sample; @@ -262,10 +274,9 @@ void sample8_handler (void) data0 += data3; { uae_u32 data = SBASEVAL8(2) + data0; - FINISH_DATA(8, 2); + FINISH_DATA (data, 8, 2); PUT_SOUND_BYTE (data); } - END_BENCH check_sound_buffers (); } @@ -273,8 +284,6 @@ void sample8_handler (void) #ifdef HAVE_STEREO_SUPPORT void sample16s_handler (void) { - BEGIN_BENCH - uae_u32 data0 = audio_channel[0].current_sample; uae_u32 data1 = audio_channel[1].current_sample; uae_u32 data2 = audio_channel[2].current_sample; @@ -292,28 +301,22 @@ void sample16s_handler (void) data0 += data3; { uae_u32 data = SBASEVAL16(1) + data0; - FINISH_DATA (16, 1); - PUT_SOUND_WORD_RIGHT (data); + FINISH_DATA (data, 16, 1); + put_sound_word_right (data); } data1 += data2; { - uae_u32 data = SBASEVAL16(1) + data1; - FINISH_DATA (16, 1); - PUT_SOUND_WORD_LEFT (data); + uae_u32 data = SBASEVAL16(1) + data1; + FINISH_DATA (data, 16, 1); + put_sound_word_left (data); } - - END_BENCH - + check_sound_buffers (); } void sample16si_crux_handler (void) { - unsigned long delta, ratio; - - BEGIN_BENCH - uae_u32 data0 = audio_channel[0].current_sample; uae_u32 data1 = audio_channel[1].current_sample; uae_u32 data2 = audio_channel[2].current_sample; @@ -343,33 +346,33 @@ void sample16si_crux_handler (void) { struct audio_channel_data *cdp; - int ratio, ratio1; -#define INTERVAL (sample_evtime * 3) + unsigned long ratio, ratio1; +#define INTERVAL (scaled_sample_evtime * 3) cdp = audio_channel + 0; ratio1 = cdp->per - cdp->evtime; ratio = (ratio1 << 12) / INTERVAL; - if (cdp->evtime < sample_evtime || ratio1 >= INTERVAL) + if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL) ratio = 4096; data0 = (data0 * ratio + data0p * (4096 - ratio)) >> 12; cdp = audio_channel + 1; ratio1 = cdp->per - cdp->evtime; ratio = (ratio1 << 12) / INTERVAL; - if (cdp->evtime < sample_evtime || ratio1 >= INTERVAL) + if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL) ratio = 4096; data1 = (data1 * ratio + data1p * (4096 - ratio)) >> 12; cdp = audio_channel + 2; ratio1 = cdp->per - cdp->evtime; ratio = (ratio1 << 12) / INTERVAL; - if (cdp->evtime < sample_evtime || ratio1 >= INTERVAL) + if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL) ratio = 4096; data2 = (data2 * ratio + data2p * (4096 - ratio)) >> 12; cdp = audio_channel + 3; ratio1 = cdp->per - cdp->evtime; ratio = (ratio1 << 12) / INTERVAL; - if (cdp->evtime < sample_evtime || ratio1 >= INTERVAL) + if (cdp->evtime < scaled_sample_evtime || ratio1 >= INTERVAL) ratio = 4096; data3 = (data3 * ratio + data3p * (4096 - ratio)) >> 12; } @@ -377,18 +380,15 @@ void sample16si_crux_handler (void) data0 += data3; { uae_u32 data = SBASEVAL16 (1) + data0; - FINISH_DATA (16, 1); - PUT_SOUND_WORD_RIGHT (data); + FINISH_DATA (data, 16, 1); + put_sound_word_right (data); } { uae_u32 data = SBASEVAL16 (1) + data1; - FINISH_DATA (16, 1); - PUT_SOUND_WORD_LEFT (data); + FINISH_DATA (data, 16, 1); + put_sound_word_left (data); } - - END_BENCH - check_sound_buffers (); } @@ -396,8 +396,6 @@ void sample16si_rh_handler (void) { unsigned long delta, ratio; - BEGIN_BENCH - uae_u32 data0 = audio_channel[0].current_sample; uae_u32 data1 = audio_channel[1].current_sample; uae_u32 data2 = audio_channel[2].current_sample; @@ -440,25 +438,20 @@ void sample16si_rh_handler (void) data0 += (data3 * (256 - ratio) + data3p * ratio) >> 8; { uae_u32 data = SBASEVAL16 (1) + data0; - FINISH_DATA (16, 1); - PUT_SOUND_WORD_RIGHT (data); + FINISH_DATA (data, 16, 1); + put_sound_word_right (data); } { uae_u32 data = SBASEVAL16 (1) + data1; - FINISH_DATA (16, 1); - PUT_SOUND_WORD_LEFT (data); + FINISH_DATA (data, 16, 1); + put_sound_word_left (data); } - - END_BENCH - check_sound_buffers (); } void sample8s_handler (void) { - BEGIN_BENCH - uae_u32 data0 = audio_channel[0].current_sample; uae_u32 data1 = audio_channel[1].current_sample; uae_u32 data2 = audio_channel[2].current_sample; @@ -476,18 +469,16 @@ void sample8s_handler (void) data0 += data3; { uae_u32 data = SBASEVAL8(1) + data0; - FINISH_DATA (8, 1); + FINISH_DATA (data, 8, 1); PUT_SOUND_BYTE_RIGHT (data); } data1 += data2; { uae_u32 data = SBASEVAL8(1) + data1; - FINISH_DATA (8, 1); + FINISH_DATA (data, 8, 1); PUT_SOUND_BYTE_LEFT (data); } - END_BENCH - check_sound_buffers (); } #else @@ -559,24 +550,44 @@ void sample_ulaw_handler (void) check_sound_buffers (); } +void schedule_audio (void) +{ + unsigned long best = ~0ul; + int i; + + eventtab[ev_audio].active = 0; + eventtab[ev_audio].oldcycles = get_cycles (); + for (i = 0; i < 6; i++) { + struct audio_channel_data *cdp = audio_channel + i; + + if (cdp->state != 0) { + if (best > cdp->evtime) { + best = cdp->evtime; + eventtab[ev_audio].active = 1; + } + } + } + eventtab[ev_audio].evtime = get_cycles () + best; +} + static void audio_handler (int nr) { struct audio_channel_data *cdp = audio_channel + nr; switch (cdp->state) { case 0: - fprintf(stderr, "Bug in sound code\n"); + write_log ("Bug in sound code\n"); break; case 1: /* We come here at the first hsync after DMA was turned on. */ - cdp->evtime = maxhpos; + cdp->evtime = maxhpos * CYCLE_UNIT; cdp->state = 5; INTREQ(0x8000 | (0x80 << nr)); if (cdp->wlen != 1) - cdp->wlen--; - cdp->nextdat = chipmem_bank.wget(cdp->pt); + cdp->wlen = (cdp->wlen - 1) & 0xFFFF; + cdp->nextdat = chipmem_wget (cdp->pt); cdp->pt += 2; break; @@ -584,7 +595,7 @@ static void audio_handler (int nr) case 5: /* We come here at the second hsync after DMA was turned on. */ if (currprefs.produce_sound == 0) - cdp->per = 65535; + cdp->per = PERIOD_MAX; cdp->evtime = cdp->per; cdp->dat = cdp->nextdat; @@ -604,7 +615,7 @@ static void audio_handler (int nr) case 2: /* We come here when a 2->3 transition occurs */ if (currprefs.produce_sound == 0) - cdp->per = 65535; + cdp->per = PERIOD_MAX; cdp->last_sample = cdp->current_sample; cdp->current_sample = (sample8_t)(cdp->dat & 0xFF); @@ -615,7 +626,7 @@ static void audio_handler (int nr) /* Period attachment? */ if (adkcon & (0x10 << nr)) { if (cdp->intreq2 && cdp->dmaen) - INTREQ(0x8000 | (0x80 << nr)); + INTREQ (0x8000 | (0x80 << nr)); cdp->intreq2 = 0; cdp->dat = cdp->nextdat; @@ -623,12 +634,11 @@ static void audio_handler (int nr) cdp->data_written = 2; if (nr < 3) { if (cdp->dat == 0) - (cdp+1)->per = 65535; - - else if (cdp->dat < maxhpos/2 && currprefs.produce_sound < 3) - (cdp+1)->per = maxhpos/2; + (cdp+1)->per = PERIOD_MAX; + else if (cdp->dat < maxhpos * CYCLE_UNIT / 2 && currprefs.produce_sound < 3) + (cdp+1)->per = maxhpos * CYCLE_UNIT / 2; else - (cdp+1)->per = cdp->dat; + (cdp+1)->per = cdp->dat * CYCLE_UNIT; } } break; @@ -636,7 +646,7 @@ static void audio_handler (int nr) case 3: /* We come here when a 3->2 transition occurs */ if (currprefs.produce_sound == 0) - cdp->per = 65535; + cdp->per = PERIOD_MAX; cdp->evtime = cdp->per; @@ -698,26 +708,61 @@ void aud3_handler (void) audio_handler (3); } +void audio_channel_enable_dma (struct audio_channel_data *cdp) +{ + if (cdp->state == 0) { + cdp->state = 1; + cdp->pt = cdp->lc; + cdp->wper = cdp->per; + cdp->wlen = cdp->len; + cdp->data_written = 2; + cdp->evtime = eventtab[ev_hsync].evtime - get_cycles (); + } +} + +void audio_channel_disable_dma (struct audio_channel_data *cdp) +{ + if (cdp->state == 1 || cdp->state == 5) { + cdp->state = 0; + cdp->last_sample = 0; + cdp->current_sample = 0; + } +} + void audio_reset (void) { - memset (audio_channel, 0, sizeof audio_channel); - audio_channel[0].per = 65535; - audio_channel[1].per = 65535; - audio_channel[2].per = 65535; - audio_channel[3].per = 65535; - audio_channel[0].voltbl = sound_table[0]; - audio_channel[1].voltbl = sound_table[0]; - audio_channel[2].voltbl = sound_table[0]; - audio_channel[3].voltbl = sound_table[0]; + int i; + if (savestate_state != STATE_RESTORE) { + memset (audio_channel, 0, 4 * sizeof *audio_channel); + audio_channel[0].per = PERIOD_MAX; + audio_channel[1].per = PERIOD_MAX; + audio_channel[2].per = PERIOD_MAX; + audio_channel[3].per = PERIOD_MAX; + audio_channel[0].voltbl = sound_table[0]; + audio_channel[1].voltbl = sound_table[0]; + audio_channel[2].voltbl = sound_table[0]; + audio_channel[3].voltbl = sound_table[0]; + } else + for (i = 0; i < 4; i++) + audio_channel[i].dmaen = (dmacon & 0x200) && (dmacon & (1 << i)); + +#ifndef MULTIPLICATION_PROFITABLE + for (i = 0; i < 4; i++) + audio_channel[nr].voltbl = sound_table[audio_channel[nr].vol]; +#endif last_cycles = 0; - next_sample_evtime = sample_evtime; + next_sample_evtime = scaled_sample_evtime; + + schedule_audio (); } STATIC_INLINE int sound_prefs_changed (void) { return (changed_prefs.produce_sound != currprefs.produce_sound - || changed_prefs.stereo != currprefs.stereo + || changed_prefs.sound_stereo != currprefs.sound_stereo + || changed_prefs.mixed_stereo != currprefs.mixed_stereo + || changed_prefs.sound_maxbsiz != currprefs.sound_maxbsiz || changed_prefs.sound_freq != currprefs.sound_freq || changed_prefs.sound_bits != currprefs.sound_bits); } @@ -728,24 +773,26 @@ void check_prefs_changed_audio (void) close_sound (); currprefs.produce_sound = changed_prefs.produce_sound; - currprefs.stereo = changed_prefs.stereo; + currprefs.sound_stereo = changed_prefs.sound_stereo; + currprefs.mixed_stereo = changed_prefs.mixed_stereo; currprefs.sound_bits = changed_prefs.sound_bits; currprefs.sound_freq = changed_prefs.sound_freq; - + currprefs.sound_maxbsiz = changed_prefs.sound_maxbsiz; if (currprefs.produce_sound >= 2) { - if (init_sound ()) { - last_cycles = cycles - 1; - next_sample_evtime = sample_evtime; + if (init_audio ()) { + last_cycles = get_cycles () - 1; + next_sample_evtime = scaled_sample_evtime; } else if (! sound_available) { - fprintf (stderr, "Sound is not supported.\n"); + write_log ("Sound is not supported.\n"); } else { - fprintf (stderr, "Sorry, can't initialize sound.\n"); + write_log ("Sorry, can't initialize sound.\n"); currprefs.produce_sound = 0; /* So we don't do this every frame */ changed_prefs.produce_sound = 0; } } + compute_vsynctime (); } /* Select the right interpolation method. */ if (sample_handler == sample16_handler @@ -760,28 +807,35 @@ void check_prefs_changed_audio (void) sample_handler = (currprefs.sound_interpol == 0 ? sample16s_handler : currprefs.sound_interpol == 1 ? sample16si_rh_handler : sample16si_crux_handler); + if (currprefs.produce_sound == 0) { + eventtab[ev_audio].active = 0; + events_schedule (); + } } void update_audio (void) { unsigned long int n_cycles; - if (currprefs.produce_sound < 2) + if (currprefs.produce_sound == 0 || savestate_state == STATE_RESTORE) return; - n_cycles = cycles - last_cycles; + n_cycles = get_cycles () - last_cycles; for (;;) { - int best = -1; unsigned long int best_evtime = n_cycles + 1; if (audio_channel[0].state != 0 && best_evtime > audio_channel[0].evtime) - best = 0, best_evtime = audio_channel[0].evtime; + best_evtime = audio_channel[0].evtime; if (audio_channel[1].state != 0 && best_evtime > audio_channel[1].evtime) - best = 1, best_evtime = audio_channel[1].evtime; + best_evtime = audio_channel[1].evtime; if (audio_channel[2].state != 0 && best_evtime > audio_channel[2].evtime) - best = 2, best_evtime = audio_channel[2].evtime; + best_evtime = audio_channel[2].evtime; if (audio_channel[3].state != 0 && best_evtime > audio_channel[3].evtime) - best = 3, best_evtime = audio_channel[3].evtime; - if (best_evtime > next_sample_evtime) + best_evtime = audio_channel[3].evtime; + if (audio_channel[4].state != 0 && best_evtime > audio_channel[4].evtime) + best_evtime = audio_channel[4].evtime; + if (audio_channel[5].state != 0 && best_evtime > audio_channel[5].evtime) + best_evtime = audio_channel[5].evtime; + if (currprefs.produce_sound > 1 && best_evtime > next_sample_evtime) best_evtime = next_sample_evtime; if (best_evtime > n_cycles) @@ -792,9 +846,11 @@ void update_audio (void) audio_channel[1].evtime -= best_evtime; audio_channel[2].evtime -= best_evtime; audio_channel[3].evtime -= best_evtime; + audio_channel[4].evtime -= best_evtime; + audio_channel[5].evtime -= best_evtime; n_cycles -= best_evtime; if (next_sample_evtime == 0 && currprefs.produce_sound > 1) { - next_sample_evtime = sample_evtime; + next_sample_evtime = scaled_sample_evtime; (*sample_handler) (); } if (audio_channel[0].evtime == 0 && audio_channel[0].state != 0) @@ -806,13 +862,25 @@ void update_audio (void) if (audio_channel[3].evtime == 0 && audio_channel[3].state != 0) audio_handler (3); } - last_cycles = cycles - n_cycles; + last_cycles = get_cycles () - n_cycles; +} + +void audio_evhandler (void) +{ + if (currprefs.produce_sound == 0) + abort (); + + update_audio (); + schedule_audio (); } void AUDxDAT (int nr, uae_u16 v) { struct audio_channel_data *cdp = audio_channel + nr; + if (currprefs.produce_sound == 0) + return; + update_audio (); cdp->dat = v; @@ -821,6 +889,8 @@ void AUDxDAT (int nr, uae_u16 v) INTREQ(0x8000 | (0x80 << nr)); /* data_written = 2 ???? */ cdp->evtime = cdp->per; + schedule_audio (); + events_schedule (); } } @@ -840,21 +910,30 @@ void AUDxLCL (int nr, uae_u16 v) void AUDxPER (int nr, uae_u16 v) { + unsigned long per = v * CYCLE_UNIT; update_audio (); - if (v == 0) - v = 65535; + if (per == 0) + per = PERIOD_MAX; - if (v < maxhpos/2 && currprefs.produce_sound < 3) - v = maxhpos/2; + if (per < maxhpos * CYCLE_UNIT / 2 && currprefs.produce_sound < 3) + per = maxhpos * CYCLE_UNIT / 2; - audio_channel[nr].per = v; + if (audio_channel[nr].per == PERIOD_MAX + && per != PERIOD_MAX) + { + audio_channel[nr].evtime = CYCLE_UNIT; + if (currprefs.produce_sound > 0) { + schedule_audio (); + events_schedule (); + } + } + audio_channel[nr].per = per; } void AUDxLEN (int nr, uae_u16 v) { update_audio (); - audio_channel[nr].len = v; } @@ -870,9 +949,66 @@ void AUDxVOL (int nr, uae_u16 v) #endif } -void dump_audio_bench (void) +int init_audio (void) { -#ifdef BENCHMARK_AUDIO - printf ("Average cycles per sample handler: %f\n", ((double)sh_time / sh_count)); -#endif + int retval; + /* Some backward compatibility hacks until every port initializes + scaled_sample_evtime... */ + scaled_sample_evtime_ok = 0; + retval = init_sound (); + if (! scaled_sample_evtime_ok) + scaled_sample_evtime = sample_evtime * CYCLE_UNIT; + return retval; +} + +/* audio save/restore code FIXME: not working correctly */ +/* help needed */ + +uae_u8 *restore_audio (uae_u8 *src, int i) +{ + struct audio_channel_data *acd; + uae_u16 p; + + acd = audio_channel + i; + acd->state = restore_u8 (); + acd->vol = restore_u8 (); + acd->intreq2 = restore_u8 (); + acd->data_written = restore_u8 (); + acd->len = restore_u16 (); + acd->wlen = restore_u16 (); + p = restore_u16 (); + acd->per = p ? p * CYCLE_UNIT : PERIOD_MAX; + p = restore_u16 (); + acd->wper = p ? p * CYCLE_UNIT : PERIOD_MAX; + acd->lc = restore_u32 (); + acd->pt = restore_u32 (); + acd->evtime = restore_u32 (); + + return src; +} + + +uae_u8 *save_audio (int *len, int i) +{ + struct audio_channel_data *acd; + uae_u8 *dst = malloc (100); + uae_u8 *dstbak = dst; + uae_u16 p; + + acd = audio_channel + i; + save_u8 ((uae_u8)acd->state); + save_u8 (acd->vol); + save_u8 (acd->intreq2); + save_u8 (acd->data_written); + save_u16 (acd->len); + save_u16 (acd->wlen); + p = acd->per == PERIOD_MAX ? 0 : acd->per / CYCLE_UNIT; + save_u16 (p); + p = acd->per == PERIOD_MAX ? 0 : acd->wper / CYCLE_UNIT; + save_u16 (p); + save_u32 (acd->lc); + save_u32 (acd->pt); + save_u32 (acd->evtime); + *len = dst - dstbak; + return dstbak; }