X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fsound.c;h=52fdcbfb7357d1d8d5964129fec64a35e13436dc;hb=f1685723f2d920b7d7349c57e902c7adfaffdb1c;hp=92c2253c81acad8581b67d82c9dec25e0f16bca5;hpb=e849e67ceff4a2f5d457dedee8f18de1646b9dc3;p=rocksndiamonds.git diff --git a/src/libgame/sound.c b/src/libgame/sound.c index 92c2253c..52fdcbfb 100644 --- a/src/libgame/sound.c +++ b/src/libgame/sound.c @@ -21,6 +21,7 @@ #include "system.h" #include "sound.h" #include "misc.h" +#include "setup.h" static int num_sounds = 0, num_music = 0; @@ -38,7 +39,7 @@ static int playing_sounds = 0; static struct SoundControl playlist[MAX_SOUNDS_PLAYING]; static struct SoundControl emptySoundControl = { - -1,0,0, FALSE,FALSE,FALSE,FALSE,FALSE, 0,0L,0L,NULL + -1,0,0, FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE, 0,0L,0L,NULL }; #if defined(PLATFORM_UNIX) @@ -227,6 +228,22 @@ void SoundServer(void) != sizeof(snd_ctrl)) Error(ERR_EXIT_SOUND_SERVER, "broken pipe -- no sounds"); + if (snd_ctrl.reload_sounds || snd_ctrl.reload_music) + { + for(i=0;iname = sound_name; - - filename = getPath2((is_music ? options.music_directory : - options.sounds_directory), snd_info->name); + snd_info->data_len = 0; + snd_info->data_ptr = NULL; #if defined(TARGET_SDL) if ((snd_info->mix_chunk = Mix_LoadWAV(filename)) == NULL) { - Error(ERR_WARN, "cannot read sound file '%s' -- no sounds", filename); - free(filename); + Error(ERR_WARN, "cannot read sound file '%s'", filename); return FALSE; } @@ -951,60 +973,74 @@ static boolean LoadSoundExt(char *sound_name, boolean is_music) if ((file = fopen(filename, MODE_READ)) == NULL) { - Error(ERR_WARN, "cannot open sound file '%s' -- no sounds", filename); - free(filename); + Error(ERR_WARN, "cannot open sound file '%s'", filename); return FALSE; } - /* read chunk "RIFF" */ - getFileChunk(file, chunk, &chunk_size, BYTE_ORDER_LITTLE_ENDIAN); - if (strcmp(chunk, "RIFF") != 0) + /* read chunk id "RIFF" */ + getFileChunk(file, chunk_name, &chunk_size, BYTE_ORDER_LITTLE_ENDIAN); + if (strcmp(chunk_name, "RIFF") != 0) { Error(ERR_WARN, "missing 'RIFF' chunk of sound file '%s'", filename); fclose(file); - free(filename); return FALSE; } - /* read chunk "WAVE" */ - getFileChunk(file, chunk, &dummy, BYTE_ORDER_LITTLE_ENDIAN); - if (strcmp(chunk, "WAVE") != 0) + /* read "RIFF" type id "WAVE" */ + getFileChunk(file, chunk_name, NULL, BYTE_ORDER_LITTLE_ENDIAN); + if (strcmp(chunk_name, "WAVE") != 0) { - Error(ERR_WARN, "missing 'WAVE' chunk of sound file '%s'", filename); + Error(ERR_WARN, "missing 'WAVE' type ID of sound file '%s'", filename); fclose(file); - free(filename); return FALSE; } - /* read header information */ - for (i=0; i WAV_HEADER_SIZE) + ReadUnusedBytesFromFile(file, chunk_size - WAV_HEADER_SIZE); + } + else if (strcmp(chunk_name, "data") == 0) + { + snd_info->data_len = chunk_size; + snd_info->data_ptr = checked_malloc(snd_info->data_len); + + /* read sound data */ + if (fread(snd_info->data_ptr, 1, snd_info->data_len, file) != + snd_info->data_len) + { + Error(ERR_WARN,"cannot read 'data' chunk of sound file '%s'",filename); + fclose(file); + return FALSE; + } + + /* check for odd number of sample bytes (data chunk is word aligned) */ + if ((chunk_size % 2) == 1) + ReadUnusedBytesFromFile(file, 1); + } + else /* unknown chunk -- ignore */ + ReadUnusedBytesFromFile(file, chunk_size); } - snd_info->data_len = chunk_size; - snd_info->data_ptr = checked_malloc(snd_info->data_len); + fclose(file); - /* read sound data */ - if (fread(snd_info->data_ptr, 1, snd_info->data_len, file) != - snd_info->data_len) + if (snd_info->data_ptr == NULL) { - Error(ERR_WARN, "cannot read sound file '%s' -- no sounds", filename); - fclose(file); - free(filename); + Error(ERR_WARN, "missing 'data' chunk of sound file '%s'", filename); return FALSE; } - fclose(file); - for (i=0; idata_len; i++) snd_info->data_ptr[i] = snd_info->data_ptr[i] ^ 0x80; @@ -1013,23 +1049,29 @@ static boolean LoadSoundExt(char *sound_name, boolean is_music) snd_info->sample_ptr = load_sample(filename); if (!snd_info->sample_ptr) { - Error(ERR_WARN, "cannot read sound file '%s' -- no sounds", filename); + Error(ERR_WARN, "cannot read sound file '%s'", filename); return FALSE; } #endif - free(filename); - return TRUE; } -boolean LoadSound(char *sound_name) +boolean LoadCustomSound(char *basename) { - return LoadSoundExt(sound_name, FALSE); + char *filename = getCustomSoundFilename(basename); + + if (filename == NULL) + { + Error(ERR_WARN, "cannot find sound file '%s' -- no sounds", filename); + return FALSE; + } + + return Load_WAV(filename); } -boolean LoadMod(char *mod_name) +static boolean Load_MOD(char *mod_name) { #if defined(TARGET_SDL) struct SampleInfo *mod_info; @@ -1058,8 +1100,9 @@ boolean LoadMod(char *mod_name) #endif } -int LoadMusic(void) +int LoadCustomMusic(void) { + char *music_directory = getCustomMusicDirectory(); DIR *dir; struct dirent *dir_entry; int num_wav_music = 0; @@ -1068,22 +1111,23 @@ int LoadMusic(void) if (!audio.sound_available) return 0; - if ((dir = opendir(options.music_directory)) == NULL) + if ((dir = opendir(music_directory)) == NULL) { - Error(ERR_WARN, "cannot read music directory '%s'", - options.music_directory); + Error(ERR_WARN, "cannot read music directory '%s'", music_directory); audio.music_available = FALSE; return 0; } while ((dir_entry = readdir(dir)) != NULL) /* loop until last dir entry */ { - char *filename = dir_entry->d_name; + char *filename = getPath2(music_directory, dir_entry->d_name); - if (FileIsSound(filename) && LoadSoundExt(filename, TRUE)) + if (FileIsSound(filename) && Load_WAV(filename)) num_wav_music++; - else if (FileIsMusic(filename) && LoadMod(filename)) + else if (FileIsMusic(filename) && Load_MOD(filename)) num_mod_music++; + + free(filename); } closedir(dir); @@ -1144,7 +1188,9 @@ void PlaySoundExt(int nr, int volume, int stereo, boolean loop) { struct SoundControl snd_ctrl = emptySoundControl; - if (!audio.sound_available || !audio.sound_enabled) + if (!audio.sound_available || + !audio.sound_enabled || + audio.sound_deactivated) return; if (volume