- spkr_switch renamed to spkr_toggle and moved to speaker.c

- Also some minor tweaks and cosmetics
This commit is contained in:
tudnai 2020-05-17 19:32:45 -07:00
parent ae5d8211fb
commit bde4c4d941
5 changed files with 78 additions and 71 deletions

View File

@ -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 =

View File

@ -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;

View File

@ -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];

View File

@ -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 */

View File

@ -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: