From 861c227c54ed136c2d77f882a22a9e51d311e4b4 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Thu, 27 Mar 2025 12:53:12 +0100 Subject: [PATCH] =?UTF-8?q?-=20[FIX]=20Evitar=20que=20sonen=20varies=20mus?= =?UTF-8?q?iques=20a=20l'hora=20-=20[FIX]=20ResumeAudioStream=20al=20playa?= =?UTF-8?q?r=20la=20musica=20-=20[FIX]=20Parar=20un=20canal=20de=20audio?= =?UTF-8?q?=20abans=20de=20intentar=20usar-lo=20per=20a=20un=20nou=20s?= =?UTF-8?q?=C3=B3=20-=20[FIX]=20No=20permetre=20canals=20negatius=20-=20[F?= =?UTF-8?q?IX]=20Alliberar=20el=20stream=20al=20parar=20un=20canal=20-=20[?= =?UTF-8?q?FIX]=20Arreglat=20el=20control=20de=20volum=20en=20l'aplicaci?= =?UTF-8?q?=C3=B3=20de=20exemple?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + jail_audio.cpp | 17 +++++++++-------- main.cpp | 6 +++--- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 4c61493..5a06f6b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .vscode *.out main +jail_audio \ No newline at end of file diff --git a/jail_audio.cpp b/jail_audio.cpp index 0293d51..29e0628 100644 --- a/jail_audio.cpp +++ b/jail_audio.cpp @@ -204,10 +204,8 @@ void JA_PlayMusic(JA_Music_t *music, const int loop) { if (!JA_musicEnabled) return; - if (current_music != NULL) { - current_music->pos = 0; - current_music->state = JA_MUSIC_STOPPED; - } + JA_StopMusic(); + current_music = music; current_music->pos = 0; current_music->state = JA_MUSIC_PLAYING; @@ -217,6 +215,7 @@ void JA_PlayMusic(JA_Music_t *music, const int loop) if (!SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length)) printf("[ERROR] SDL_PutAudioStreamData failed!\n"); SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume); if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) printf("[ERROR] SDL_BindAudioStream failed!\n"); + SDL_ResumeAudioStreamDevice(current_music->stream); } void JA_PauseMusic() @@ -272,7 +271,7 @@ void JA_DeleteMusic(JA_Music_t *music) { if (current_music == music) current_music = nullptr; SDL_free(music->buffer); - SDL_DestroyAudioStream(music->stream); + if (music->stream) SDL_DestroyAudioStream(music->stream); delete music; } @@ -337,6 +336,7 @@ int JA_PlaySound(JA_Sound_t *sound, const int loop) int channel = 0; while (channel < JA_MAX_SIMULTANEOUS_CHANNELS && channels[channel].state != JA_CHANNEL_FREE) { channel++; } if (channel == JA_MAX_SIMULTANEOUS_CHANNELS) channel = 0; + JA_StopChannel(channel); channels[channel].sound = sound; channels[channel].times = loop; @@ -354,7 +354,8 @@ int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop) { if (!JA_soundEnabled) return -1; - if (channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return -1; + if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return -1; + JA_StopChannel(channel); channels[channel].sound = sound; channels[channel].times = loop; @@ -430,18 +431,18 @@ void JA_StopChannel(const int channel) if (channel == -1) { for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { + if (channels[i].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[i].stream); channels[i].state = JA_CHANNEL_FREE; channels[i].pos = 0; channels[i].sound = NULL; - SDL_DestroyAudioStream(channels[i].stream); } } else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) { + if (channels[channel].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[channel].stream); channels[channel].state = JA_CHANNEL_FREE; channels[channel].pos = 0; channels[channel].sound = NULL; - SDL_DestroyAudioStream(channels[channel].stream); } } diff --git a/main.cpp b/main.cpp index 33ffe11..87aac8a 100644 --- a/main.cpp +++ b/main.cpp @@ -17,7 +17,7 @@ int main(int argc, char **argv) { int channel = -1; JA_PlayMusic(music, -1); - int volume = 128; + float volume = 1.0f; bool should_exit = false; while(!should_exit) { while(SDL_PollEvent(&event)) { @@ -49,10 +49,10 @@ int main(int argc, char **argv) { JA_FadeOutMusic(1000); break; case SDL_SCANCODE_UP: - volume = JA_SetVolume(volume+16); + volume = JA_SetVolume(volume+0.1f); break; case SDL_SCANCODE_DOWN: - volume = JA_SetVolume(volume-16); + volume = JA_SetVolume(volume-0.1f); break; case SDL_SCANCODE_ESCAPE: should_exit = true;