- [FIX] Ajustat el buffer de audio

This commit is contained in:
2026-05-16 08:19:51 +02:00
parent d4e91ac56d
commit 672f4278e0
2 changed files with 42 additions and 44 deletions
+41 -43
View File
@@ -152,65 +152,63 @@ void reinit() {
void pump_audio() { void pump_audio() {
if (audio_state == AUDIO_NONE) return; if (audio_state == AUDIO_NONE) return;
const int TARGET_MS = 20; // latencia deseada const int TARGET_MS = 20;
const int BYTES_PER_MS = 44; // 44100 Hz, 8-bit mono const int BYTES_PER_MS = 22; // 44100 Hz, 8-bit mono
const int TARGET_BYTES = TARGET_MS * BYTES_PER_MS; const int TARGET_BYTES = TARGET_MS * BYTES_PER_MS;
const int CHUNK = 256; // bloque pequeño y constante
int queued = SDL_GetAudioStreamAvailable(audio_stream); int queued = SDL_GetAudioStreamAvailable(audio_stream);
// Si ya hay suficiente audio en cola, no generamos nada while (queued < TARGET_BYTES) {
if (queued >= TARGET_BYTES)
return;
// Cuánto falta para llegar al objetivo int to_generate = CHUNK;
int missing = TARGET_BYTES - queued; uint8_t* out = mix_buffer;
if (missing > sizeof(mix_buffer)) int remaining = to_generate;
missing = sizeof(mix_buffer);
// Generar exactamente "missing" bytes while (remaining > 0) {
uint8_t* out = mix_buffer;
int remaining = missing;
while (remaining > 0) { if (audio_state == AUDIO_SOUND) {
static int period = 0;
static int v = 16;
if (audio_state == AUDIO_SOUND) { for (int i = 0; i < remaining; ++i) {
static int period = 0; out[i] = v;
static int v = 16; period++;
if (period >= audio_freq) {
for (int i = 0; i < remaining; ++i) { period = 0;
out[i] = v; v = -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);
while (play_len == 0) { if (play_len == -1) {
play_len = interpret_next_token(play_buffer); audio_state = AUDIO_NONE;
if (play_len == -1) { return;
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() { void romcpy() {
+1 -1
View File
@@ -420,7 +420,7 @@ bool lua_is_playing() {
return lua_state == STATE_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) { void lua_init(const char* filename, const bool start_playing) {
if (lua_state != STATE_STOPPED) lua_quit(); if (lua_state != STATE_STOPPED) lua_quit();