- [FIX] Evitar que sonen varies musiques a l'hora

- [FIX] ResumeAudioStream al playar la musica
- [FIX] Parar un canal de audio abans de intentar usar-lo per a un nou só
- [FIX] No permetre canals negatius
- [FIX] Alliberar el stream al parar un canal
- [FIX] Arreglat el control de volum en l'aplicació de exemple
This commit is contained in:
2025-03-27 12:53:12 +01:00
parent 37776fce4a
commit 861c227c54
3 changed files with 13 additions and 11 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@
.vscode .vscode
*.out *.out
main main
jail_audio

View File

@@ -204,10 +204,8 @@ void JA_PlayMusic(JA_Music_t *music, const int loop)
{ {
if (!JA_musicEnabled) return; if (!JA_musicEnabled) return;
if (current_music != NULL) { JA_StopMusic();
current_music->pos = 0;
current_music->state = JA_MUSIC_STOPPED;
}
current_music = music; current_music = music;
current_music->pos = 0; current_music->pos = 0;
current_music->state = JA_MUSIC_PLAYING; 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"); 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); SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume);
if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) printf("[ERROR] SDL_BindAudioStream failed!\n"); if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) printf("[ERROR] SDL_BindAudioStream failed!\n");
SDL_ResumeAudioStreamDevice(current_music->stream);
} }
void JA_PauseMusic() void JA_PauseMusic()
@@ -272,7 +271,7 @@ void JA_DeleteMusic(JA_Music_t *music)
{ {
if (current_music == music) current_music = nullptr; if (current_music == music) current_music = nullptr;
SDL_free(music->buffer); SDL_free(music->buffer);
SDL_DestroyAudioStream(music->stream); if (music->stream) SDL_DestroyAudioStream(music->stream);
delete music; delete music;
} }
@@ -337,6 +336,7 @@ int JA_PlaySound(JA_Sound_t *sound, const int loop)
int channel = 0; int channel = 0;
while (channel < JA_MAX_SIMULTANEOUS_CHANNELS && channels[channel].state != JA_CHANNEL_FREE) { channel++; } while (channel < JA_MAX_SIMULTANEOUS_CHANNELS && channels[channel].state != JA_CHANNEL_FREE) { channel++; }
if (channel == JA_MAX_SIMULTANEOUS_CHANNELS) channel = 0; if (channel == JA_MAX_SIMULTANEOUS_CHANNELS) channel = 0;
JA_StopChannel(channel);
channels[channel].sound = sound; channels[channel].sound = sound;
channels[channel].times = loop; 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 (!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].sound = sound;
channels[channel].times = loop; channels[channel].times = loop;
@@ -430,18 +431,18 @@ void JA_StopChannel(const int channel)
if (channel == -1) if (channel == -1)
{ {
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { 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].state = JA_CHANNEL_FREE;
channels[i].pos = 0; channels[i].pos = 0;
channels[i].sound = NULL; channels[i].sound = NULL;
SDL_DestroyAudioStream(channels[i].stream);
} }
} }
else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) 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].state = JA_CHANNEL_FREE;
channels[channel].pos = 0; channels[channel].pos = 0;
channels[channel].sound = NULL; channels[channel].sound = NULL;
SDL_DestroyAudioStream(channels[channel].stream);
} }
} }

View File

@@ -17,7 +17,7 @@ int main(int argc, char **argv) {
int channel = -1; int channel = -1;
JA_PlayMusic(music, -1); JA_PlayMusic(music, -1);
int volume = 128; float volume = 1.0f;
bool should_exit = false; bool should_exit = false;
while(!should_exit) { while(!should_exit) {
while(SDL_PollEvent(&event)) { while(SDL_PollEvent(&event)) {
@@ -49,10 +49,10 @@ int main(int argc, char **argv) {
JA_FadeOutMusic(1000); JA_FadeOutMusic(1000);
break; break;
case SDL_SCANCODE_UP: case SDL_SCANCODE_UP:
volume = JA_SetVolume(volume+16); volume = JA_SetVolume(volume+0.1f);
break; break;
case SDL_SCANCODE_DOWN: case SDL_SCANCODE_DOWN:
volume = JA_SetVolume(volume-16); volume = JA_SetVolume(volume-0.1f);
break; break;
case SDL_SCANCODE_ESCAPE: case SDL_SCANCODE_ESCAPE:
should_exit = true; should_exit = true;