X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fsound.c;h=adfb8147f8cb3f19076a8cd87e32eb9c5997d9a9;hb=a7c06161253796a30a0237a7f5a044f459c8cf35;hp=0d7ed67cf8255c8fdd3cd5ac8aafb399097bfe44;hpb=61c3da024802ecc0268bab42d7499fc0346e4fd3;p=rocksndiamonds.git diff --git a/src/libgame/sound.c b/src/libgame/sound.c index 0d7ed67c..adfb8147 100644 --- a/src/libgame/sound.c +++ b/src/libgame/sound.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "platform.h" @@ -70,6 +71,21 @@ #define SAME_SOUND_NR(x,y) ((x).nr == (y).nr) #define SAME_SOUND_DATA(x,y) ((x).data_ptr == (y).data_ptr) +#define SOUND_VOLUME_FROM_PERCENT(v,p) ((p) < 0 ? SOUND_MIN_VOLUME : \ + (p) > 100 ? (v) : \ + (p) * (v) / 100) + +#define SOUND_VOLUME_SIMPLE(v) SOUND_VOLUME_FROM_PERCENT(v, setup.volume_simple) +#define SOUND_VOLUME_LOOPS(v) SOUND_VOLUME_FROM_PERCENT(v, setup.volume_loops) +#define SOUND_VOLUME_MUSIC(v) SOUND_VOLUME_FROM_PERCENT(v, setup.volume_music) + +#define SETUP_SOUND_VOLUME(v,s) ((s) == SND_CTRL_PLAY_MUSIC ? \ + SOUND_VOLUME_MUSIC(v) : \ + (s) == SND_CTRL_PLAY_LOOP ? \ + SOUND_VOLUME_LOOPS(v) : \ + SOUND_VOLUME_SIMPLE(v)) + + #if defined(AUDIO_UNIX_NATIVE) struct SoundHeader_WAV { @@ -720,7 +736,7 @@ static void Mixer_PlayMusicChannel() /* 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); - Mix_VolumeMusic(SOUND_MAX_VOLUME); + Mix_VolumeMusic(mixer[audio.music_channel].volume); } #endif } @@ -1152,7 +1168,7 @@ static void Mixer_Main_DSP() for (i = 0; i < audio.num_channels; i++) { - void *sample_ptr; + // void *sample_ptr; int sample_len; int sample_pos; int sample_size; @@ -1167,7 +1183,7 @@ static void Mixer_Main_DSP() } /* pointer, lenght and actual playing position of sound sample */ - sample_ptr = mixer[i].data_ptr; + // sample_ptr = mixer[i].data_ptr; sample_len = mixer[i].data_len; sample_pos = mixer[i].playing_pos; sample_size = MIN(max_sample_size, sample_len - sample_pos); @@ -1265,7 +1281,8 @@ static void Mixer_Main_DSP() } /* finally play the sound fragment */ - write(audio.device_fd, playing_buffer, fragment_size); + if (write(audio.device_fd, playing_buffer, fragment_size) == -1) + Error(ERR_WARN, "write() failed; %s", strerror(errno)); if (!mixer_active_channels) CloseAudioDevice(&audio.device_fd); @@ -1756,14 +1773,108 @@ static void *Load_MOD(char *filename) static void *Load_WAV_or_MOD(char *filename) { +#if 1 + if (FileIsMusic(filename)) + return Load_MOD(filename); + else if (FileIsSound(filename)) + return Load_WAV(filename); + else + return NULL; +#else if (FileIsSound(filename)) return Load_WAV(filename); else if (FileIsMusic(filename)) return Load_MOD(filename); else return NULL; +#endif } +#if 1 + +void LoadCustomMusic_NoConf(void) +{ + static boolean draw_init_text = TRUE; /* only draw at startup */ + static char *last_music_directory = NULL; + char *music_directory = getCustomMusicDirectory(); + Directory *dir; + DirectoryEntry *dir_entry; + int num_music = getMusicListSize(); + + if (!audio.sound_available) + return; + + if (last_music_directory != NULL && + strEqual(last_music_directory, music_directory)) + return; /* old and new music directory are the same */ + + if (last_music_directory != NULL) + free(last_music_directory); + last_music_directory = getStringCopy(music_directory); + + FreeAllMusic_NoConf(); + + if ((dir = openDirectory(music_directory)) == NULL) + { + Error(ERR_WARN, "cannot read music directory '%s'", music_directory); + + audio.music_available = FALSE; + + return; + } + + if (draw_init_text) + DrawInitText("Loading music", 120, FC_GREEN); + + 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; + + /* skip all music files that are configured in music config file */ + for (i = 0; i < num_music; i++) + { + struct FileInfo *music = getMusicListEntry(i); + + if (strEqual(basename, music->filename)) + { + music_already_used = TRUE; + break; + } + } + + if (music_already_used) + continue; + + 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 (mus_info) + { + num_music_noconf++; + Music_NoConf = checked_realloc(Music_NoConf, + num_music_noconf * sizeof(MusicInfo *)); + Music_NoConf[num_music_noconf - 1] = mus_info; + } + } + + closeDirectory(dir); + + draw_init_text = FALSE; +} + +#else + void LoadCustomMusic_NoConf(void) { static boolean draw_init_text = TRUE; /* only draw at startup */ @@ -1789,7 +1900,9 @@ void LoadCustomMusic_NoConf(void) if ((dir = opendir(music_directory)) == NULL) { Error(ERR_WARN, "cannot read music directory '%s'", music_directory); + audio.music_available = FALSE; + return; } @@ -1843,6 +1956,8 @@ void LoadCustomMusic_NoConf(void) draw_init_text = FALSE; } +#endif + int getSoundListSize() { return (sound_info->num_file_list_entries + @@ -2075,21 +2190,33 @@ void PlayMusic(int nr) void PlaySound(int nr) { + if (!setup.sound_simple) + return; + PlaySoundExt(nr, SOUND_MAX_VOLUME, SOUND_MIDDLE, SND_CTRL_PLAY_SOUND); } void PlaySoundStereo(int nr, int stereo_position) { + if (!setup.sound_simple) + return; + PlaySoundExt(nr, SOUND_MAX_VOLUME, stereo_position, SND_CTRL_PLAY_SOUND); } void PlaySoundLoop(int nr) { + if (!setup.sound_loops) + return; + PlaySoundExt(nr, SOUND_MAX_VOLUME, SOUND_MIDDLE, SND_CTRL_PLAY_LOOP); } void PlaySoundMusic(int nr) { + if (!setup.sound_music) + return; + PlaySoundExt(nr, SOUND_MAX_VOLUME, SOUND_MIDDLE, SND_CTRL_PLAY_MUSIC); } @@ -2102,6 +2229,8 @@ void PlaySoundExt(int nr, int volume, int stereo_position, int state) audio.sound_deactivated) return; + volume = SETUP_SOUND_VOLUME(volume, state); + if (volume < SOUND_MIN_VOLUME) volume = SOUND_MIN_VOLUME; else if (volume > SOUND_MAX_VOLUME)