X-Git-Url: https://git.artsoft.org/?p=rocksndiamonds.git;a=blobdiff_plain;f=src%2Flibgame%2Fsound.c;h=e860ba5ad443acd63e2669618d3f7c5e04c0872d;hp=591ea7d444ce3ecbe416fe96cbcc2b39c5a03ef6;hb=00383dd409fde133c6738231abfcee662c03087c;hpb=3ff2e8a0b5c27b99a9920bdf5ed82bc41bf40181 diff --git a/src/libgame/sound.c b/src/libgame/sound.c index 591ea7d4..e860ba5a 100644 --- a/src/libgame/sound.c +++ b/src/libgame/sound.c @@ -122,6 +122,7 @@ static int stereo_volume[SOUND_MAX_LEFT2RIGHT + 1]; static struct SoundControl mixer[NUM_MIXER_CHANNELS]; static int mixer_active_channels = 0; +static boolean expire_loop_sounds = FALSE; static void ReloadCustomSounds(); static void ReloadCustomMusic(); @@ -150,7 +151,8 @@ static void Mixer_ResetChannelExpiration(int channel) { mixer[channel].playing_starttime = Counter(); - if (IS_LOOP(mixer[channel]) && !IS_MUSIC(mixer[channel])) + if (expire_loop_sounds && + IS_LOOP(mixer[channel]) && !IS_MUSIC(mixer[channel])) Mix_ExpireChannel(channel, SOUND_LOOP_EXPIRATION_TIME); } @@ -159,7 +161,8 @@ static boolean Mixer_ChannelExpired(int channel) if (!mixer[channel].active) return TRUE; - if (IS_LOOP(mixer[channel]) && !IS_MUSIC(mixer[channel]) && + if (expire_loop_sounds && + IS_LOOP(mixer[channel]) && !IS_MUSIC(mixer[channel]) && DelayReached(&mixer[channel].playing_starttime, SOUND_LOOP_EXPIRATION_TIME)) return TRUE; @@ -216,10 +219,10 @@ static void Mixer_PlayMusicChannel() if (mixer[audio.music_channel].type != MUS_TYPE_WAV) { - /* Mix_VolumeMusic() must be called _after_ Mix_PlayMusic() -- - this looks like a bug in the SDL_mixer library */ - Mix_PlayMusic(mixer[audio.music_channel].data_ptr, -1); + // use short fade-in to prevent "plop" sound for certain music files + // (this may happen when switching on music while playing the game) Mix_VolumeMusic(mixer[audio.music_channel].volume); + Mix_FadeInMusic(mixer[audio.music_channel].data_ptr, -1, 100); } } @@ -478,6 +481,10 @@ static void HandleSoundRequest(SoundControl snd_ctrl) if (SAME_SOUND_NR(mixer[i], snd_ctrl) || ALL_SOUNDS(snd_ctrl)) Mixer_StopChannel(i); } + else if (SET_EXPIRE_LOOPS(snd_ctrl)) /* set loop expiration on or off */ + { + expire_loop_sounds = snd_ctrl.active; + } else if (snd_ctrl.active) /* add new sound to mixer */ { Mixer_InsertSound(snd_ctrl); @@ -516,7 +523,7 @@ static void *Load_WAV(char *filename) if ((snd_info->data_ptr = Mix_LoadWAV(filename)) == NULL) { - Error(ERR_WARN, "cannot read sound file '%s'", filename); + Error(ERR_WARN, "cannot read sound file '%s': %s", filename, Mix_GetError()); free(snd_info); return NULL; } @@ -540,7 +547,7 @@ static void *Load_MOD(char *filename) if ((mod_info->data_ptr = Mix_LoadMUS(filename)) == NULL) { - Error(ERR_WARN, "cannot read music file '%s'", filename); + Error(ERR_WARN, "cannot read music file '%s': %s", filename, Mix_GetError()); free(mod_info); return NULL; } @@ -598,7 +605,6 @@ void LoadCustomMusic_NoConf(void) while ((dir_entry = readDirectory(dir)) != NULL) /* loop all entries */ { char *basename = dir_entry->basename; - char *filename = NULL; MusicInfo *mus_info = NULL; boolean music_already_used = FALSE; int i; @@ -621,12 +627,8 @@ void LoadCustomMusic_NoConf(void) if (draw_init_text) DrawInitText(basename, 150, FC_YELLOW); - filename = getPath2(music_directory, basename); - - if (FileIsMusic(basename)) - mus_info = Load_WAV_or_MOD(filename); - - free(filename); + if (FileIsMusic(dir_entry->filename)) + mus_info = Load_WAV_or_MOD(dir_entry->filename); if (mus_info) { @@ -995,6 +997,21 @@ void StopSoundExt(int nr, int state) HandleSoundRequest(snd_ctrl); } +void ExpireSoundLoops(boolean active) +{ + SoundControl snd_ctrl; + + if (!audio.sound_available) + return; + + clear_mem(&snd_ctrl, sizeof(SoundControl)); /* to make valgrind happy */ + + snd_ctrl.active = active; + snd_ctrl.state = SND_CTRL_EXPIRE_LOOPS; + + HandleSoundRequest(snd_ctrl); +} + static void ReloadCustomSounds() { LoadArtworkConfig(sound_info);