diff --git a/jail_audio.cpp b/jail_audio.cpp index 0944eca..9bff8a8 100644 --- a/jail_audio.cpp +++ b/jail_audio.cpp @@ -45,6 +45,11 @@ bool JA_soundEnabled { true }; SDL_AudioDeviceID sdlAudioDevice { 0 }; SDL_TimerID JA_timerID { 0 }; +bool fading = false; +int fade_start_time; +int fade_duration; +int fade_initial_volume; + /* void audioCallback(void * userdata, uint8_t * stream, int len) { SDL_memset(stream, 0, len); @@ -87,10 +92,23 @@ Uint32 JA_UpdateCallback(void *userdata, SDL_TimerID timerID, Uint32 interval) { if (JA_musicEnabled && current_music && current_music->state == JA_MUSIC_PLAYING) { + if (fading) { + int time = SDL_GetTicks(); + if (time > (fade_start_time+fade_duration)) { + fading = false; + JA_StopMusic(); + } else { + const int time_passed = time - fade_start_time; + const float percent = (float)time_passed / (float)fade_duration; + SDL_SetAudioStreamGain(current_music->stream, 1.0 - percent); + } + } + if (current_music->times != 0) { - if (SDL_GetAudioStreamAvailable(current_music->stream) < (current_music->length/2)) + if (SDL_GetAudioStreamAvailable(current_music->stream) < (current_music->length/2)) { SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length); + } if (current_music->times>0) current_music->times--; } else @@ -123,9 +141,15 @@ Uint32 JA_UpdateCallback(void *userdata, SDL_TimerID timerID, Uint32 interval) void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) { + #ifdef DEBUG + SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG); + #endif + + SDL_Log("Iniciant JailAudio..."); JA_audioSpec = {format, channels, freq }; if (!sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice); sdlAudioDevice = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &JA_audioSpec); + SDL_Log( (sdlAudioDevice==0) ? "Failed to initialize SDL audio!\n" : "OK!\n"); SDL_PauseAudioDevice(sdlAudioDevice); JA_timerID = SDL_AddTimer(30, JA_UpdateCallback, nullptr); } @@ -225,6 +249,17 @@ void JA_StopMusic() current_music->stream = nullptr; } +void JA_FadeOutMusic(const int milliseconds) +{ + if (!JA_musicEnabled) return; + if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return; + + fading = true; + fade_start_time = SDL_GetTicks(); + fade_duration = milliseconds; + fade_initial_volume = JA_musicVolume; +} + JA_Music_state JA_GetMusicState() { if (!JA_musicEnabled) return JA_MUSIC_DISABLED; @@ -248,6 +283,18 @@ float JA_SetMusicVolume(float volume) return JA_musicVolume; } +void JA_SetMusicPosition(float value) +{ + if (!current_music) return; + current_music->pos = value * current_music->spec.freq; +} + +float JA_GetMusicPosition() +{ + if (!current_music) return 0; + return float(current_music->pos)/float(current_music->spec.freq); +} + void JA_EnableMusic(const bool value) { if ( !value && current_music && (current_music->state==JA_MUSIC_PLAYING) ) JA_StopMusic(); @@ -303,6 +350,24 @@ int JA_PlaySound(JA_Sound_t *sound, const int loop) return channel; } +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; + + channels[channel].sound = sound; + channels[channel].times = loop; + channels[channel].pos = 0; + channels[channel].state = JA_CHANNEL_PLAYING; + channels[channel].stream = SDL_CreateAudioStream(&channels[channel].sound->spec, &JA_audioSpec); + SDL_PutAudioStreamData(channels[channel].stream, channels[channel].sound->buffer, channels[channel].sound->length); + SDL_SetAudioStreamGain(channels[channel].stream, JA_soundVolume); + SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream); + + return channel; +} + void JA_DeleteSound(JA_Sound_t *sound) { for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { diff --git a/jail_audio.h b/jail_audio.h index f4cde72..53e1a0f 100644 --- a/jail_audio.h +++ b/jail_audio.h @@ -16,15 +16,19 @@ void JA_PlayMusic(JA_Music_t *music, const int loop = -1); void JA_PauseMusic(); void JA_ResumeMusic(); void JA_StopMusic(); +void JA_FadeOutMusic(const int milliseconds); JA_Music_state JA_GetMusicState(); void JA_DeleteMusic(JA_Music_t *music); float JA_SetMusicVolume(float volume); +void JA_SetMusicPosition(float value); +float JA_GetMusicPosition(); void JA_EnableMusic(const bool value); JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length); JA_Sound_t *JA_LoadSound(Uint8* buffer, Uint32 length); JA_Sound_t *JA_LoadSound(const char* filename); int JA_PlaySound(JA_Sound_t *sound, const int loop = 0); +int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop = 0); void JA_PauseChannel(const int channel); void JA_ResumeChannel(const int channel); void JA_StopChannel(const int channel); diff --git a/main.cpp b/main.cpp index 437c406..33ffe11 100644 --- a/main.cpp +++ b/main.cpp @@ -16,7 +16,7 @@ int main(int argc, char **argv) { JA_Sound_t *peiv = JA_LoadSound("menu_select.wav"); int channel = -1; - JA_PlayMusic(music, true); + JA_PlayMusic(music, -1); int volume = 128; bool should_exit = false; while(!should_exit) { @@ -42,6 +42,12 @@ int main(int argc, char **argv) { case SDL_SCANCODE_6: // Si pulsem la tecla '6' stopem definitivament el wav infinit JA_StopChannel(channel); break; + case SDL_SCANCODE_7: + JA_PlayMusic(music, 0); // Si pulsem la tecla '7' fa sonar la música UNA negada nomes. + break; + case SDL_SCANCODE_0: // Si pulsem la tecla '0' stopem la música amb un fade + JA_FadeOutMusic(1000); + break; case SDL_SCANCODE_UP: volume = JA_SetVolume(volume+16); break; diff --git a/valgrind-sup.txt b/valgrind-sup.txt new file mode 100644 index 0000000..bd6fa5b --- /dev/null +++ b/valgrind-sup.txt @@ -0,0 +1,12 @@ +{ + ignore_unversioned_libs + Memcheck:Leak + ... + obj:*/lib*/lib*.so +} +{ + ignore_versioned_libs + Memcheck:Leak + ... + obj:*/lib*/lib*.so.* +}