From 672f4278e0f19137d9148c48cbfd858520c16223 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Sat, 16 May 2026 08:19:51 +0200 Subject: [PATCH] - [FIX] Ajustat el buffer de audio --- ascii.cpp | 84 +++++++++++++++++++++++++++---------------------------- lua.cpp | 2 +- 2 files changed, 42 insertions(+), 44 deletions(-) diff --git a/ascii.cpp b/ascii.cpp index ba44272..2731773 100644 --- a/ascii.cpp +++ b/ascii.cpp @@ -152,65 +152,63 @@ void reinit() { void pump_audio() { if (audio_state == AUDIO_NONE) return; - const int TARGET_MS = 20; // latencia deseada - const int BYTES_PER_MS = 44; // 44100 Hz, 8-bit mono + const int TARGET_MS = 20; + const int BYTES_PER_MS = 22; // 44100 Hz, 8-bit mono const int TARGET_BYTES = TARGET_MS * BYTES_PER_MS; + const int CHUNK = 256; // bloque pequeño y constante + int queued = SDL_GetAudioStreamAvailable(audio_stream); - // Si ya hay suficiente audio en cola, no generamos nada - if (queued >= TARGET_BYTES) - return; + while (queued < TARGET_BYTES) { - // Cuánto falta para llegar al objetivo - int missing = TARGET_BYTES - queued; - if (missing > sizeof(mix_buffer)) - missing = sizeof(mix_buffer); + int to_generate = CHUNK; + uint8_t* out = mix_buffer; + int remaining = to_generate; - // Generar exactamente "missing" bytes - uint8_t* out = mix_buffer; - int remaining = missing; + while (remaining > 0) { - while (remaining > 0) { + if (audio_state == AUDIO_SOUND) { + static int period = 0; + static int v = 16; - if (audio_state == AUDIO_SOUND) { - static int period = 0; - static int v = 16; - - for (int i = 0; i < remaining; ++i) { - out[i] = v; - period++; - if (period >= audio_freq) { - period = 0; - v = -v; + for (int i = 0; i < remaining; ++i) { + out[i] = v; + period++; + if (period >= audio_freq) { + period = 0; + v = -v; + } } + + remaining = 0; } - remaining = 0; - } + else if (audio_state == AUDIO_PLAY) { - else if (audio_state == AUDIO_PLAY) { - - while (play_len == 0) { - play_len = interpret_next_token(play_buffer); - if (play_len == -1) { - audio_state = AUDIO_NONE; - return; + while (play_len == 0) { + play_len = interpret_next_token(play_buffer); + if (play_len == -1) { + audio_state = AUDIO_NONE; + return; + } + play_pos = play_buffer; } - play_pos = play_buffer; + + int n = (remaining < play_len ? remaining : play_len); + SDL_memcpy(out, play_pos, n); + + out += n; + play_pos += n; + play_len -= n; + remaining -= n; } - - int n = (remaining < play_len ? remaining : play_len); - SDL_memcpy(out, play_pos, n); - - out += n; - play_pos += n; - play_len -= n; - remaining -= n; } + + SDL_PutAudioStreamData(audio_stream, mix_buffer, to_generate); + + queued += to_generate; } - - SDL_PutAudioStreamData(audio_stream, mix_buffer, missing); } void romcpy() { diff --git a/lua.cpp b/lua.cpp index 13e5953..57d4c2d 100644 --- a/lua.cpp +++ b/lua.cpp @@ -420,7 +420,7 @@ bool lua_is_playing() { return lua_state == STATE_PLAYING; } -const char boot[] = "function init()mode(1)cls()play('o5l0v5cegv4cegv3cegv2cegv1ceg')memcpy(360,4608,240)memcpy(1560,4848,240)ink(1)print('G A M E',8,16)ink(4)print('S Y S T E M',20,16)ink(7)print('mini',9,8)ink(8)print('v0.7.0',34,29)w=0 end function update()w=w+1 if w>90 then cls()load()end end"; +const char boot[] = "function init()mode(1)cls()play('o5l0v5cegv4cegv3cegv2cegv1ceg')memcpy(360,4608,240)memcpy(1560,4848,240)ink(1)print('G A M E',8,16)ink(4)print('S Y S T E M',20,16)ink(7)print('mini',9,8)ink(8)print('v0.7.1',34,29)w=0 end function update()w=w+1 if w>90 then cls()load()end end"; void lua_init(const char* filename, const bool start_playing) { if (lua_state != STATE_STOPPED) lua_quit();