From 1b7fdbd2301a83ad712a94977da16d09d12cea21 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Sun, 29 Dec 2024 08:06:58 +0100 Subject: [PATCH] - [FIX] La musica duraba la meitat per contar samples en compte de bytes --- jail_audio.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/jail_audio.cpp b/jail_audio.cpp index 1a0e0d2..9b6d9a1 100644 --- a/jail_audio.cpp +++ b/jail_audio.cpp @@ -20,6 +20,7 @@ struct JA_Channel_t { struct JA_Music_t { int samples {0}; + Uint32 length {0}; int pos {0}; int times {0}; short* output {NULL}; @@ -60,13 +61,13 @@ void audioCallback(void * userdata, uint8_t * stream, int len) { volume = JA_musicVolume * (1.0 - percent); } } - const int size = SDL_min(len, (current_music->samples-current_music->pos)*2); - SDL_MixAudioFormat(stream, (Uint8*)(current_music->output+current_music->pos), AUDIO_S16, size, volume); - current_music->pos += size/2; + const int size = SDL_min(len, current_music->length - current_music->pos); + SDL_MixAudioFormat(stream, (Uint8*)(current_music->output)+current_music->pos, AUDIO_S16, size, volume); + current_music->pos += size; if (size < len) { if (current_music->times != 0) { SDL_MixAudioFormat(stream+size, (Uint8*)current_music->output, AUDIO_S16, len-size, volume); - current_music->pos = (len-size)/2; + current_music->pos = len-size; if (current_music->times > 0) current_music->times--; } else { current_music->pos = 0; @@ -136,6 +137,7 @@ JA_Music_t *JA_LoadMusic(Uint8* buffer, Uint32 length) SDL_Log("Music length: %f\n", float(music->samples)/float(JA_freq)); if (cvt.needed) { cvt.len = music->samples * chan * 2; + music->length = cvt.len; cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult); SDL_memcpy(cvt.buf, music->output, cvt.len); SDL_ConvertAudio(&cvt);