From 780afbc6a8ab36d40369a16f3a462307ca388117 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Thu, 24 Jul 2025 18:41:09 +0200 Subject: [PATCH] =?UTF-8?q?-=20[FIX]=20Arreglat=20el=20audio=20del=2048K?= =?UTF-8?q?=20amb=20lo=20apr=C3=A9s=20del=20emu=20de=20gameboy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ay-3-8912.cpp | 3 ++- main.cpp | 10 ++++---- zx_ula.cpp | 69 +++++++++------------------------------------------ 3 files changed, 19 insertions(+), 63 deletions(-) diff --git a/ay-3-8912.cpp b/ay-3-8912.cpp index 7aeb00f..3eaaf57 100644 --- a/ay-3-8912.cpp +++ b/ay-3-8912.cpp @@ -94,10 +94,11 @@ namespace audio break; } case 11: - case 12: + case 12: { uint16_t freq = registers[11] | (registers[12] << 8); envelope_freq = clock / freq; break; + } case 13: if (registers[13]&ENVELOPE_ATTACK) { envelope_volume = 0; diff --git a/main.cpp b/main.cpp index 8db8892..f48f271 100644 --- a/main.cpp +++ b/main.cpp @@ -77,13 +77,13 @@ namespace actions int main(int argc, char *argv[]) { - //const uint32_t clock = 3500000; - const uint32_t clock = 3546900; + const uint32_t clock = 3500000; + //const uint32_t clock = 3546900; const uint32_t update_freq = clock / 10; //new zx_48mem(); //new zx_128mem(); - mem::init(ZX_128K); + mem::init(ZX_48K); z80dis::loadSymbols(); z80::setClock(clock); @@ -92,8 +92,8 @@ int main(int argc, char *argv[]) SDL_Init(SDL_INIT_EVERYTHING); z80debug::init(); - //zxscreen::init(SCREEN_MODE_48K); - zxscreen::init(SCREEN_MODE_128K); + zxscreen::init(SCREEN_MODE_48K); + //zxscreen::init(SCREEN_MODE_128K); //z80viewer *v = new zx_128bankviewer(); //v->show(); diff --git a/zx_ula.cpp b/zx_ula.cpp index 9409520..8ee7747 100644 --- a/zx_ula.cpp +++ b/zx_ula.cpp @@ -2,8 +2,6 @@ #include "z80.h" #include -#define AUDIO_BUFFER_SIZE 2048 - namespace zx_ula { #define KEY_SHIFT 0 @@ -234,52 +232,21 @@ namespace zx_ula void set_ear(const uint8_t val) { ear = val; } + #define SAMPLING_FREQ 44100 + #define AUDIO_BUFFER_SIZE 2048 - + float cycles_per_sample; SDL_AudioDeviceID sdlAudioDevice; uint8_t sound_buffer[AUDIO_BUFFER_SIZE]; uint16_t sound_pos=0; - uint16_t sound_start=0; uint16_t t_sound=0; - uint32_t samples_generated=0; - uint32_t samples_time =0; - uint32_t samples_t=0; - - void audioCallback(void * userdata, uint8_t * stream, int len) - { - //const uint16_t top = sound_pos < len ? sound_pos : len; - //if (toptop) - { - memcpy(sound_buffer, &sound_buffer[top], sound_pos-top); - sound_pos=sound_pos-top; - last_1=last_1-top; - } - else - { - sound_pos=last_1=0; - } - } - else - { - sound_pos=last_1=0; - }*/ - } void sound_init() { - SDL_AudioSpec audioSpec{11025, AUDIO_U8, 1, 0, AUDIO_BUFFER_SIZE>>2, 0, 0, &audioCallback, NULL}; + SDL_AudioSpec audioSpec{SAMPLING_FREQ, AUDIO_U8, 1, 0, AUDIO_BUFFER_SIZE>>2, 0, 0, NULL, NULL}; sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0); + cycles_per_sample = z80::getClock() / SAMPLING_FREQ; sound_enable(); - samples_time=SDL_GetTicks(); } void sound_enable() @@ -295,27 +262,15 @@ namespace zx_ula void sound_update(const uint8_t dt) { t_sound += dt; - samples_t += dt; - if (t_sound>=317) { - t_sound-=317; - samples_generated++; - /*if (samples_t >=z80::getClock()) { - //printf("%i\n", samples_generated); - samples_generated=0; - samples_t = 0; - }*/ - /*if (SDL_GetTicks()>=samples_time+1000) { - printf("%i\n", samples_generated); - samples_generated=0; - samples_time = SDL_GetTicks(); - }*/ - //sound_pos = (sound_pos+1) & 0x3ff; - //sound_buffer[sound_pos] = ear*128; - - //if (sound_pos>=AUDIO_BUFFER_SIZE) sound_pos = last_1 = 0; + if (t_sound>=cycles_per_sample) { + t_sound-=cycles_per_sample; sound_buffer[(sound_pos++)&(AUDIO_BUFFER_SIZE-1)] = ear*128; - //if (ear) last_1 = sound_pos; + } + if (sound_pos>=1000) { + SDL_QueueAudio(sdlAudioDevice, sound_buffer, sound_pos); + sound_pos = 0; + while (SDL_GetQueuedAudioSize(sdlAudioDevice) > 4096 ) {} } }