Speaker sound buffer to generate perfect square waves

This commit is contained in:
tudnai 2020-05-10 10:54:34 -07:00
parent 86a1c3a755
commit 6894d1576f
5 changed files with 52 additions and 27 deletions

View File

@ -427,8 +427,8 @@
32A9F7472467B5DF004902A1 /* audio */ = {
isa = PBXGroup;
children = (
32A9F7492467B60B004902A1 /* speaker.c */,
32A9F7482467B60B004902A1 /* speaker.h */,
32A9F7492467B60B004902A1 /* speaker.c */,
);
path = audio;
sourceTree = "<group>";

View File

@ -713,10 +713,6 @@ void softReset() {
void m6502_Run() {
// clear speaker buffer, so we can fill it up by new data
memset(spkr_samples, 127, spkr_buf_size);
// init time
//#ifdef CLK_WAIT
// unsigned long long elpased = (unsigned long long)-1LL;
@ -778,7 +774,11 @@ void m6502_Run() {
}
spkr_play();
// clear speaker buffer, so we can fill it up by new data
memset(spkr_samples, spkr_level, spkr_buf_size);
}
void read_rom( const char * bundlePath, const char * filename, uint8_t * rom, const uint16_t addr ) {

View File

@ -48,6 +48,10 @@ ALCcontext *ctx = NULL;
ALuint spkr_buf = 0;
ALuint spkr_src = 0;
// we start with the max, because otherwise the speaker clicks
uint8_t spkr_level = SPKR_LEVEL_MAX;
const int spkr_seconds = 1;
const unsigned spkr_sample_rate = 44100;
const unsigned spkr_buf_size = spkr_seconds * spkr_sample_rate;
@ -64,7 +68,7 @@ void spkr_init() {
alcMakeContextCurrent(ctx);
// Fill buffer with zeros
memset( spkr_samples, 127, spkr_buf_size );
memset( spkr_samples, spkr_level, spkr_buf_size );
}
@ -112,4 +116,6 @@ void spkr_play() {
alSourcei(spkr_src, AL_LOOPING, 0);
alSourcePlay(spkr_src);
memset(spkr_samples, spkr_level, spkr_buf_size);
}

View File

@ -11,6 +11,8 @@
#include <stdio.h>
#define SPKR_LEVEL_MIN 0
#define SPKR_LEVEL_MAX 127
extern void spkr_init(void);
extern void spkr_exit(void);
@ -19,5 +21,8 @@ extern void spkr_play(void);
extern const unsigned spkr_buf_size;
extern char spkr_samples [];
extern uint8_t spkr_level;
#endif /* speaker_h */

View File

@ -48,7 +48,6 @@ uint8_t * const RDHIMEM = Apple2_64K_MEM; // Pointer to the Shadow Memory
uint8_t * const WRHIMEM = Apple2_Dummy_RAM; // Pointer to the Shadow Memory Map so we can use this from Swift
#define DEF_RAM_PAGE(mem,pg) \
(mem) + ((pg) << 8)
@ -401,6 +400,35 @@ void auxMemorySelect() {
}
INLINE void spkr_switch() {
// TODO: This is very slow!
// printf("io_KBDSTRB\n");
// push a click into the speaker buffer
// (we will play the entire buffer at the end of the frame)
int sample_idx = clkfrm / 22;
if ( spkr_level > SPKR_LEVEL_MIN ) {
spkr_level = SPKR_LEVEL_MIN;
}
else {
spkr_level = SPKR_LEVEL_MAX;
}
//spkr_samples[sample_idx] = spkr_level;
memset(spkr_samples + sample_idx, spkr_level, spkr_buf_size - sample_idx);
//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);
@ -419,28 +447,10 @@ INLINE uint8_t ioRead( uint16_t addr ) {
Apple2_64K_RAM[io_KBD] &= 0x7F;
return Apple2_64K_RAM[io_KBDSTRB];
case (uint8_t)io_SPKR: {
// TODO: This is very slow!
// printf("io_KBDSTRB\n");
// push a click into the speaker buffer
// (we will play the entire buffer at the end of the frame)
int sample_idx = clkfrm / 22;
spkr_samples[sample_idx] = 0;
//ViewController_spk_up_play();
case (uint8_t)io_SPKR:
spkr_switch();
return Apple2_64K_RAM[io_SPKR];
// case io_VID_CLR80VID:
// videoMode.col80 = 0;
// break;
//
// case io_VID_SET80VID:
// videoMode.col80 = 1;
// break;
//
}
case (uint8_t)io_VID_RDTEXT:
return videoMode.text << 7;
@ -699,6 +709,10 @@ INLINE void ioWrite( uint16_t addr, uint8_t val ) {
Apple2_64K_RAM[io_KBD] &= 0x7F;
break;
case io_SPKR:
spkr_switch();
break;
case io_RDMAINRAM:
// printf("io_RDMAINRAM\n");
MEMcfg.RD_AUX_MEM = 0;