X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fsound.c;h=b71ed27fe3d7ef4076494e33f9d641e6cb9c62ec;hb=87af02f45b63b849773c7f830d69136d130acca0;hp=60b8bc306cdc95f403c12cb03512863af16303dd;hpb=7b6d9e43fdb99e1923f4a2a401ae60e7b2975bd8;p=rocksndiamonds.git diff --git a/src/libgame/sound.c b/src/libgame/sound.c index 60b8bc30..b71ed27f 100644 --- a/src/libgame/sound.c +++ b/src/libgame/sound.c @@ -89,7 +89,7 @@ static void SoundServer_StopAllSounds(); static void ReloadCustomSounds(); static void ReloadCustomMusic(); -static void FreeSound(SoundInfo *); +static void FreeSound(void *); #if defined(PLATFORM_UNIX) static int OpenAudioDevice(char *audio_device_name) @@ -651,6 +651,7 @@ static void sound_handler(struct SoundControl snd_ctrl) #if !defined(PLATFORM_WIN32) static void SoundServer_InsertNewSound(struct SoundControl snd_ctrl) { + SoundInfo *snd_info; int i, k; if (snd_ctrl.music) @@ -658,6 +659,15 @@ static void SoundServer_InsertNewSound(struct SoundControl snd_ctrl) else if (snd_ctrl.nr >= num_sounds) return; + snd_info = (snd_ctrl.music ? Music[snd_ctrl.nr] : Sound[snd_ctrl.nr]); + if (snd_info == NULL) + { +#if 1 + printf("sound/music %d undefined\n", snd_ctrl.nr); +#endif + return; + } + /* if playlist is full, remove oldest sound */ if (playing_sounds == MAX_SOUNDS_PLAYING) { @@ -752,9 +762,6 @@ static void SoundServer_InsertNewSound(struct SoundControl snd_ctrl) if (!playlist[i].active || (snd_ctrl.music && i == audio.music_channel)) { - SoundInfo *snd_info = - (snd_ctrl.music ? Music[snd_ctrl.nr] : Sound[snd_ctrl.nr]); - snd_ctrl.data_ptr = snd_info->data_ptr; snd_ctrl.data_len = snd_info->data_len; snd_ctrl.format = snd_info->format; @@ -1123,7 +1130,7 @@ static SoundInfo *Load_WAV(char *filename) if (!audio.sound_available) return NULL; -#if 1 +#if 0 printf("loading WAV file '%s'\n", filename); #endif @@ -1230,50 +1237,87 @@ static SoundInfo *Load_WAV(char *filename) return snd_info; } -static void LoadCustomSound(SoundInfo **snd_info, char *basename) +static void deleteSoundEntry(SoundInfo **snd_info) { - char *filename = getCustomSoundFilename(basename); - - if (filename == NULL) /* (should never happen) */ - { - Error(ERR_WARN, "cannot find sound file '%s'", basename); - return; - } - if (*snd_info) { - char *filename_old = (*snd_info)->source_filename; - - if (strcmp(filename, filename_old) == 0) - { - /* The old and new sound are the same (have the same filename and path). - This usually means that this sound does not exist in this sound set - and a fallback to the existing sound is done. */ + char *filename = (*snd_info)->source_filename; #if 1 - printf("[sound '%s' already exists]\n", filename); + printf("[decrementing reference counter of sound '%s']\n", filename); #endif - return; - } - if (--(*snd_info)->num_references <= 0) { #if 1 - printf("[deleting sound '%s']\n", filename_old); + printf("[deleting sound '%s']\n", filename); #endif /* FreeSound(*snd_info); */ - deleteNodeFromList(&SoundFileList, filename_old, FreeSound); + deleteNodeFromList(&SoundFileList, filename, FreeSound); } + + *snd_info = NULL; + } +} + +static void replaceSoundEntry(SoundInfo **snd_info, char *filename) +{ + ListNode *node; + + /* check if the old and the new sound file are the same */ + if (*snd_info && strcmp((*snd_info)->source_filename, filename) == 0) + { + /* The old and new sound are the same (have the same filename and path). + This usually means that this sound does not exist in this sound set + and a fallback to the existing sound is done. */ + +#if 0 + printf("[sound '%s' already exists (same list entry)]\n", filename); +#endif + + return; } - *snd_info = Load_WAV(filename); - (*snd_info)->num_references = 1; + /* delete existing sound file entry */ + deleteSoundEntry(snd_info); - addNodeToList(&SoundFileList, (*snd_info)->source_filename, *snd_info); + /* check if the new sound file already exists in the list of sounds */ + if ((node = getNodeFromKey(SoundFileList, filename)) != NULL) + { +#if 0 + printf("[sound '%s' already exists (other list entry)]\n", filename); +#endif + + *snd_info = (SoundInfo *)node->content; + (*snd_info)->num_references++; + } + else if ((*snd_info = Load_WAV(filename)) != NULL) /* load new sound */ + { + (*snd_info)->num_references = 1; + addNodeToList(&SoundFileList, (*snd_info)->source_filename, *snd_info); + } +} + +static void LoadCustomSound(SoundInfo **snd_info, char *basename) +{ + char *filename = getCustomSoundFilename(basename); + + if (strcmp(basename, SND_FILE_UNDEFINED) == 0) + { + deleteSoundEntry(snd_info); + return; + } + + if (filename == NULL) + { + Error(ERR_WARN, "cannot find sound file '%s'", basename); + return; + } + + replaceSoundEntry(snd_info, filename); } void InitSoundList(struct SoundEffectInfo *sounds_list, int num_list_entries) @@ -1290,13 +1334,17 @@ void LoadSoundToList(char *basename, int list_pos) if (Sound == NULL || list_pos >= num_sounds) return; +#if 0 printf("loading sound '%s' ... [%d]\n", basename, getNumNodes(SoundFileList)); +#endif LoadCustomSound(&Sound[list_pos], basename); +#if 0 printf("loading sound '%s' done [%d]\n", basename, getNumNodes(SoundFileList)); +#endif } static MusicInfo *Load_MOD(char *filename) @@ -1636,7 +1684,8 @@ void dumpList(ListNode *node_first) while (node) { - printf("['%s']\n", node->key); + printf("['%s' (%d)]\n", node->key, + ((SoundInfo *)node->content)->num_references); node = node->next; } @@ -1666,7 +1715,13 @@ static void LoadSoundsInfo() #if 1 for (i=0; i '%s'\n", sound_effect[i].text, sound_effect[i].filename); + { + printf("'%s' ", sound_effect[i].text); + if (sound_effect[i].filename) + printf("-> '%s'\n", sound_effect[i].filename); + else + printf("-> UNDEFINED [-> '%s']\n", sound_effect[i].default_filename); + } #endif } } @@ -1675,7 +1730,7 @@ static void ReloadCustomSounds() { int i; -#if 1 +#if 0 printf("DEBUG: reloading sounds '%s' ...\n", artwork.sounds_set_current); #endif @@ -1692,12 +1747,15 @@ static void ReloadCustomSounds() /* printf("list size == %d\n", getNumNodes(SoundFileList)); */ + +#if 0 dumpList(SoundFileList); +#endif } static void ReloadCustomMusic() { -#if 1 +#if 0 printf("DEBUG: reloading music '%s' ...\n", artwork.music_set_current); #endif @@ -1774,8 +1832,10 @@ void InitReloadMusic(char *set_name) InitReloadSoundsOrMusic(set_name, SND_RELOAD_MUSIC); } -void FreeSound(SoundInfo *sound) +void FreeSound(void *ptr) { + SoundInfo *sound = (SoundInfo *)ptr; + if (sound == NULL) return;