From bde4c4d94190e536fc61e73b2c1603f7c39ddccc Mon Sep 17 00:00:00 2001 From: tudnai Date: Sun, 17 May 2020 19:32:45 -0700 Subject: [PATCH] - spkr_switch renamed to spkr_toggle and moved to speaker.c - Also some minor tweaks and cosmetics --- src/cpu/6502.c | 1 - src/cpu/6502.h | 1 + src/dev/audio/speaker.c | 94 ++++++++++++++++++++++++++++++----------- src/dev/audio/speaker.h | 8 ++-- src/dev/mem/mmio.h | 45 +------------------- 5 files changed, 78 insertions(+), 71 deletions(-) diff --git a/src/cpu/6502.c b/src/cpu/6502.c index 0d40619..d059a58 100644 --- a/src/cpu/6502.c +++ b/src/cpu/6502.c @@ -849,7 +849,6 @@ void m6502_ColdReset( const char * bundlePath, const char * romFileName ) { mhz = (double)MHz_6502 / M; spkr_init(); - spkr_playStart(); unsigned long long saved_frm_set = clk_6502_per_frm_set; clk_6502_per_frm = diff --git a/src/cpu/6502.h b/src/cpu/6502.h index b8c9963..1ebe077 100644 --- a/src/cpu/6502.h +++ b/src/cpu/6502.h @@ -14,6 +14,7 @@ #include "../../src/dev/audio/speaker.h" // So we can access to speaker stuff from Swift +extern const unsigned long long default_MHz_6502; extern unsigned long long MHz_6502; extern unsigned long long clk_6502_per_frm; extern unsigned long long clk_6502_per_frm_set; diff --git a/src/dev/audio/speaker.c b/src/dev/audio/speaker.c index 722ac14..b4dfd7b 100644 --- a/src/dev/audio/speaker.c +++ b/src/dev/audio/speaker.c @@ -55,20 +55,8 @@ ALCcontext *ctx = NULL; ALuint spkr_buf = 0; ALuint spkr_src = 0; -// we start with the max, because otherwise the speaker clicks -int spkr_level = SPKR_LEVEL_MAX; - -const int spkr_fps = fps; -const int spkr_seconds = 1; -const unsigned spkr_sample_rate = 44100; -unsigned spkr_extra_buf = 13; // TODO: Should it be a dynamic value calculated by how many bytes we overshot by the edge curve generator? -const unsigned spkr_buf_size = spkr_seconds * spkr_sample_rate / spkr_fps; -char spkr_samples [ spkr_buf_size * spkr_fps * 2]; // 1s of sound -unsigned spkr_sample_idx = 0; - -const unsigned spkr_play_timeout = 10; -unsigned spkr_play_time = 0; +int spkr_level = SPKR_LEVEL_ZERO; #define BUFFER_COUNT 10 @@ -77,10 +65,25 @@ unsigned spkr_play_time = 0; ALuint spkr_buffers[BUFFER_COUNT]; +const int spkr_fps = fps; +const int spkr_seconds = 1; +const unsigned spkr_sample_rate = 44100; +unsigned spkr_extra_buf = 13; // TODO: Should it be a dynamic value calculated by how many bytes we overshot by the edge curve generator? +const unsigned spkr_buf_size = spkr_seconds * spkr_sample_rate / spkr_fps; +char spkr_samples [ spkr_buf_size * spkr_fps * BUFFER_COUNT]; +unsigned spkr_sample_idx = 0; + +const unsigned spkr_play_timeout = 10; +unsigned spkr_play_time = 0; + + // initialize OpenAL void spkr_init() { const char *defname = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER); printf( "Default device: %s\n", defname ); + + // restart OpenAL when restarting the virtual machine + spkr_exit(); dev = alcOpenDevice(defname); ctx = alcCreateContext(dev, NULL); @@ -116,22 +119,65 @@ void spkr_init() { // Dealloc OpenAL void spkr_exit() { - ALCdevice *dev = NULL; - ALCcontext *ctx = NULL; - ctx = alcGetCurrentContext(); - dev = alcGetContextsDevice(ctx); - - alcMakeContextCurrent(NULL); - alcDestroyContext(ctx); - alcCloseDevice(dev); - - al_check_error(); + if ( spkr_src ) { + ALCdevice *dev = NULL; + ALCcontext *ctx = NULL; + ctx = alcGetCurrentContext(); + dev = alcGetContextsDevice(ctx); + + alcMakeContextCurrent(NULL); + alcDestroyContext(ctx); + alcCloseDevice(dev); + + al_check_error(); + + spkr_src = 0; + } } -void spkr_playStart() { + +void spkr_toggle() { + // TODO: This is very slow! + // printf("io_KBDSTRB\n"); + + spkr_play_time = spkr_play_timeout; + + // push a click into the speaker buffer + // (we will play the entire buffer at the end of the frame) + spkr_sample_idx = clkfrm / (default_MHz_6502 / spkr_sample_rate); + + if ( spkr_level > SPKR_LEVEL_MIN ) { + // down edge + while( (spkr_level -= (spkr_level - SPKR_LEVEL_MIN) / 2 ) > SPKR_LEVEL_MIN + 1 ) { + spkr_samples[ spkr_sample_idx++ ] = spkr_level; + } + spkr_level = SPKR_LEVEL_MIN; + } + else { + // up edge + while( (spkr_level += (SPKR_LEVEL_MAX - spkr_level) / 2 ) < SPKR_LEVEL_MAX - 1 ) { + spkr_samples[ spkr_sample_idx++ ] = spkr_level; + } + spkr_level = SPKR_LEVEL_MAX; + } + //spkr_samples[sample_idx] = spkr_level; + memset(spkr_samples + spkr_sample_idx, spkr_level, spkr_buf_size); + + //ViewController_spk_up_play(); + + + // case io_VID_CLR80VID: + // videoMode.col80 = 0; + // break; + // + // case io_VID_SET80VID: + // videoMode.col80 = 1; + // break; + // } + ALint freeBuffers = BUFFER_COUNT; //ALuint alBuffers[BUFFER_COUNT]; diff --git a/src/dev/audio/speaker.h b/src/dev/audio/speaker.h index 9a066f3..8bffb90 100644 --- a/src/dev/audio/speaker.h +++ b/src/dev/audio/speaker.h @@ -15,9 +15,6 @@ #define SPKR_LEVEL_MAX 255 #define SPKR_LEVEL_ZERO ( SPKR_LEVEL_MIN + SPKR_LEVEL_MAX / 2 ) -extern void spkr_init(void); -extern void spkr_exit(void); -extern void spkr_update(void); extern const unsigned spkr_sample_rate; extern const unsigned spkr_buf_size; @@ -32,5 +29,10 @@ extern const unsigned spkr_play_timeout; extern unsigned spkr_play_time; +extern void spkr_init(void); +extern void spkr_exit(void); +extern void spkr_update(void); +extern void spkr_toggle(void); + #endif /* speaker_h */ diff --git a/src/dev/mem/mmio.h b/src/dev/mem/mmio.h index 77f7096..2b8b7c3 100644 --- a/src/dev/mem/mmio.h +++ b/src/dev/mem/mmio.h @@ -400,47 +400,6 @@ void auxMemorySelect() { } -INLINE void spkr_switch() { - // TODO: This is very slow! - // printf("io_KBDSTRB\n"); - - spkr_play_time = spkr_play_timeout; - - // push a click into the speaker buffer - // (we will play the entire buffer at the end of the frame) - spkr_sample_idx = clkfrm / (default_MHz_6502 / spkr_sample_rate); - - if ( spkr_level > SPKR_LEVEL_MIN ) { - // down edge - while( (spkr_level -= (spkr_level - SPKR_LEVEL_MIN) / 2 ) > SPKR_LEVEL_MIN + 1 ) { - spkr_samples[ spkr_sample_idx++ ] = spkr_level; - } - spkr_level = SPKR_LEVEL_MIN; - } - else { - // up edge - while( (spkr_level += (SPKR_LEVEL_MAX - spkr_level) / 2 ) < SPKR_LEVEL_MAX - 1 ) { - spkr_samples[ spkr_sample_idx++ ] = spkr_level; - } - spkr_level = SPKR_LEVEL_MAX; - } - //spkr_samples[sample_idx] = spkr_level; - memset(spkr_samples + spkr_sample_idx, spkr_level, spkr_buf_size); - - //ViewController_spk_up_play(); - - - // case io_VID_CLR80VID: - // videoMode.col80 = 0; - // break; - // - // case io_VID_SET80VID: - // videoMode.col80 = 1; - // break; - // -} - - INLINE uint8_t ioRead( uint16_t addr ) { // if (outdev) fprintf(outdev, "ioRead:%04X\n", addr); // printf("ioRead:%04X (PC:%04X)\n", addr, m6502.PC); @@ -459,7 +418,7 @@ INLINE uint8_t ioRead( uint16_t addr ) { return Apple2_64K_RAM[io_KBDSTRB]; case (uint8_t)io_SPKR: - spkr_switch(); + spkr_toggle(); return Apple2_64K_RAM[io_SPKR]; case (uint8_t)io_VID_RDTEXT: @@ -721,7 +680,7 @@ INLINE void ioWrite( uint16_t addr, uint8_t val ) { break; case io_SPKR: - spkr_switch(); + spkr_toggle(); break; case io_RDMAINRAM: