X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fsound.c;h=0d7ed67cf8255c8fdd3cd5ac8aafb399097bfe44;hb=61c3da024802ecc0268bab42d7499fc0346e4fd3;hp=8b01135fea198c280f2a42e600ce668bbdf12b20;hpb=3607ef8e4000754063935d908053d564de92c0a2;p=rocksndiamonds.git diff --git a/src/libgame/sound.c b/src/libgame/sound.c index 8b01135f..0d7ed67c 100644 --- a/src/libgame/sound.c +++ b/src/libgame/sound.c @@ -1,7 +1,7 @@ /*********************************************************** * Artsoft Retro-Game Library * *----------------------------------------------------------* -* (c) 1994-2002 Artsoft Entertainment * +* (c) 1994-2006 Artsoft Entertainment * * Holger Schemel * * Detmolder Strasse 189 * * 33604 Bielefeld * @@ -70,32 +70,13 @@ #define SAME_SOUND_NR(x,y) ((x).nr == (y).nr) #define SAME_SOUND_DATA(x,y) ((x).data_ptr == (y).data_ptr) -#if 0 -struct SoundHeader_SUN -{ - unsigned long magic; - unsigned long hdr_size; - unsigned long data_size; - unsigned long encoding; - unsigned long sample_rate; - unsigned long channels; -}; - -struct SoundHeader_8SVX -{ - char magic_FORM[4]; - unsigned long chunk_size; - char magic_8SVX[4]; -}; -#endif - #if defined(AUDIO_UNIX_NATIVE) struct SoundHeader_WAV { unsigned short compression_code; unsigned short num_channels; - unsigned long sample_rate; - unsigned long bytes_per_second; + unsigned int sample_rate; + unsigned int bytes_per_second; unsigned short block_align; unsigned short bits_per_sample; }; @@ -117,7 +98,7 @@ struct SampleInfo int type; int format; void *data_ptr; /* pointer to first sample (8 or 16 bit) */ - long data_len; /* number of samples, NOT number of bytes */ + int data_len; /* number of samples, NOT number of bytes */ int num_channels; /* mono: 1 channel, stereo: 2 channels */ }; typedef struct SampleInfo SoundInfo; @@ -133,13 +114,13 @@ struct SoundControl int state; - unsigned long playing_starttime; - unsigned long playing_pos; + unsigned int playing_starttime; + unsigned int playing_pos; int type; int format; void *data_ptr; /* pointer to first sample (8 or 16 bit) */ - long data_len; /* number of samples, NOT number of bytes */ + int data_len; /* number of samples, NOT number of bytes */ int num_channels; /* mono: 1 channel, stereo: 2 channels */ #if defined(TARGET_ALLEGRO) @@ -151,16 +132,8 @@ typedef struct SoundControl SoundControl; static struct ArtworkListInfo *sound_info = NULL; static struct ArtworkListInfo *music_info = NULL; -#if 0 -static SoundInfo **Sound = NULL; -#endif - static MusicInfo **Music_NoConf = NULL; -#if 0 -static int num_sounds = 0; -#endif - static int num_music_noconf = 0; static int stereo_volume[SOUND_MAX_LEFT2RIGHT + 1]; @@ -237,7 +210,7 @@ static boolean TestAudioDevices(void) int i; /* look for available audio devices, starting with preferred ones */ - for (i=0; i= 0) break; @@ -271,11 +244,6 @@ static boolean ForkAudioProcess(void) if (audio.mixer_pid == 0) /* we are the child process */ audio.mixer_pid = getpid(); -#if 0 - printf("PID: %d [%s]\n", getpid(),(IS_CHILD_PROCESS() ? "child" : "parent")); - Delay(10000 * 0); -#endif - if (IS_CHILD_PROCESS()) Mixer_Main(); /* this function never returns */ else @@ -500,14 +468,14 @@ static void WriteReloadInfoToPipe(char *set_identifier, int type) SoundControl snd_ctrl; TreeInfo *ti = (type == SND_CTRL_RELOAD_SOUNDS ? artwork.snd_current : artwork.mus_current); - unsigned long str_size1 = strlen(leveldir_current->fullpath) + 1; - unsigned long str_size2 = strlen(leveldir_current->sounds_path) + 1; - unsigned long str_size3 = strlen(leveldir_current->music_path) + 1; - unsigned long str_size4 = strlen(ti->basepath) + 1; - unsigned long str_size5 = strlen(ti->fullpath) + 1; + unsigned int str_size1 = strlen(leveldir_current->fullpath) + 1; + unsigned int str_size2 = strlen(leveldir_current->sounds_path) + 1; + unsigned int str_size3 = strlen(leveldir_current->music_path) + 1; + unsigned int str_size4 = strlen(ti->basepath) + 1; + unsigned int str_size5 = strlen(ti->fullpath) + 1; boolean override_level_artwork = (type == SND_CTRL_RELOAD_SOUNDS ? - setup.override_level_sounds : - setup.override_level_music); + gfx.override_level_sounds : + gfx.override_level_music); if (IS_CHILD_PROCESS()) return; @@ -515,7 +483,7 @@ static void WriteReloadInfoToPipe(char *set_identifier, int type) if (leveldir_current == NULL) /* should never happen */ Error(ERR_EXIT, "leveldir_current == NULL"); - memset(&snd_ctrl, 0, sizeof(SoundControl)); /* to make valgrind happy */ + clear_mem(&snd_ctrl, sizeof(SoundControl)); /* to make valgrind happy */ snd_ctrl.active = FALSE; snd_ctrl.state = type; @@ -532,15 +500,15 @@ static void WriteReloadInfoToPipe(char *set_identifier, int type) write(audio.mixer_pipe[1], ti, sizeof(TreeInfo)) < 0 || write(audio.mixer_pipe[1], &str_size1, - sizeof(unsigned long)) < 0 || + sizeof(unsigned int)) < 0 || write(audio.mixer_pipe[1], &str_size2, - sizeof(unsigned long)) < 0 || + sizeof(unsigned int)) < 0 || write(audio.mixer_pipe[1], &str_size3, - sizeof(unsigned long)) < 0 || + sizeof(unsigned int)) < 0 || write(audio.mixer_pipe[1], &str_size4, - sizeof(unsigned long)) < 0 || + sizeof(unsigned int)) < 0 || write(audio.mixer_pipe[1], &str_size5, - sizeof(unsigned long)) < 0 || + sizeof(unsigned int)) < 0 || write(audio.mixer_pipe[1], leveldir_current->fullpath, str_size1) < 0 || write(audio.mixer_pipe[1], leveldir_current->sounds_path, @@ -563,14 +531,13 @@ static void ReadReloadInfoFromPipe(SoundControl *snd_ctrl) TreeInfo **ti_ptr = ((snd_ctrl->state & SND_CTRL_RELOAD_SOUNDS) ? &artwork.snd_current : &artwork.mus_current); TreeInfo *ti = *ti_ptr; - unsigned long str_size1, str_size2, str_size3, str_size4, str_size5; + unsigned int str_size1, str_size2, str_size3, str_size4, str_size5; static char *set_identifier = NULL; boolean *override_level_artwork = (snd_ctrl->state & SND_CTRL_RELOAD_SOUNDS ? - &setup.override_level_sounds : - &setup.override_level_music); + &gfx.override_level_sounds : + &gfx.override_level_music); - if (set_identifier) - free(set_identifier); + checked_free(set_identifier); set_identifier = checked_malloc(snd_ctrl->data_len); @@ -579,16 +546,12 @@ static void ReadReloadInfoFromPipe(SoundControl *snd_ctrl) if (ti == NULL) ti = *ti_ptr = checked_calloc(sizeof(TreeInfo)); - if (leveldir_current->fullpath != NULL) - free(leveldir_current->fullpath); - if (leveldir_current->sounds_path != NULL) - free(leveldir_current->sounds_path); - if (leveldir_current->music_path != NULL) - free(leveldir_current->music_path); - if (ti->basepath != NULL) - free(ti->basepath); - if (ti->fullpath != NULL) - free(ti->fullpath); + + checked_free(leveldir_current->fullpath); + checked_free(leveldir_current->sounds_path); + checked_free(leveldir_current->music_path); + checked_free(ti->basepath); + checked_free(ti->fullpath); if (read(audio.mixer_pipe[0], set_identifier, snd_ctrl->data_len) != snd_ctrl->data_len || @@ -599,15 +562,15 @@ static void ReadReloadInfoFromPipe(SoundControl *snd_ctrl) read(audio.mixer_pipe[0], ti, sizeof(TreeInfo)) != sizeof(TreeInfo) || read(audio.mixer_pipe[0], &str_size1, - sizeof(unsigned long)) != sizeof(unsigned long) || + sizeof(unsigned int)) != sizeof(unsigned int) || read(audio.mixer_pipe[0], &str_size2, - sizeof(unsigned long)) != sizeof(unsigned long) || + sizeof(unsigned int)) != sizeof(unsigned int) || read(audio.mixer_pipe[0], &str_size3, - sizeof(unsigned long)) != sizeof(unsigned long) || + sizeof(unsigned int)) != sizeof(unsigned int) || read(audio.mixer_pipe[0], &str_size4, - sizeof(unsigned long)) != sizeof(unsigned long) || + sizeof(unsigned int)) != sizeof(unsigned int) || read(audio.mixer_pipe[0], &str_size5, - sizeof(unsigned long)) != sizeof(unsigned long)) + sizeof(unsigned int)) != sizeof(unsigned int)) Error(ERR_EXIT_SOUND_SERVER, "broken pipe -- no sounds"); leveldir_current->fullpath = checked_calloc(str_size1); @@ -645,7 +608,7 @@ void Mixer_InitChannels() { int i; - for(i=0; i= num_music) /* invalid music */ @@ -862,7 +809,7 @@ static void Mixer_InsertSound(SoundControl snd_ctrl) if (num_music_noconf == 0) /* no fallback music available */ return; - snd_ctrl.nr = (-(snd_ctrl.nr + 1)) % num_music_noconf; + snd_ctrl.nr = UNMAP_NOCONF_MUSIC(snd_ctrl.nr) % num_music_noconf; snd_info = Music_NoConf[snd_ctrl.nr]; } else @@ -870,38 +817,12 @@ static void Mixer_InsertSound(SoundControl snd_ctrl) } else { - if (snd_ctrl.nr >= num_sounds) + if (snd_ctrl.nr < 0 || snd_ctrl.nr >= num_sounds) return; snd_info = getSoundInfoEntryFromSoundID(snd_ctrl.nr); } - /* - if (snd_ctrl.nr >= (IS_MUSIC(snd_ctrl) ? num_music : num_sounds)) - return; - */ -#else - if (IS_MUSIC(snd_ctrl)) - { - if (num_music_noconf == 0) - return; - - snd_ctrl.nr = snd_ctrl.nr % num_music_noconf; - } - else if (snd_ctrl.nr >= num_sounds) - return; -#endif - -#if 0 -#if 1 - snd_info = (IS_MUSIC(snd_ctrl) ? getMusicInfoEntryFromMusicID(snd_ctrl.nr) : - getSoundInfoEntryFromSoundID(snd_ctrl.nr)); -#else - snd_info = (IS_MUSIC(snd_ctrl) ? Music_NoConf[snd_ctrl.nr] : - getSoundInfoEntryFromSoundID(snd_ctrl.nr)); -#endif -#endif - if (snd_info == NULL) return; @@ -915,10 +836,6 @@ static void Mixer_InsertSound(SoundControl snd_ctrl) /* play music samples on a dedicated music channel */ if (IS_MUSIC(snd_ctrl)) { -#if 0 - printf("::: slot %d, ptr 0x%08x\n", snd_ctrl.nr, snd_ctrl.data_ptr); -#endif - Mixer_StopMusicChannel(); mixer[audio.music_channel] = snd_ctrl; @@ -928,25 +845,17 @@ static void Mixer_InsertSound(SoundControl snd_ctrl) } /* check if (and how often) this sound sample is already playing */ - for (k=0, i=audio.first_sound_channel; i 0 && IS_LOOP(snd_ctrl)) { - for(i=audio.first_sound_channel; i= 2) { - unsigned long playing_current = Counter(); + unsigned int playing_current = Counter(); int longest = 0, longest_nr = audio.first_sound_channel; /* look for oldest equal sound */ - for(i=audio.first_sound_channel; idata_len == 0) { @@ -1095,14 +987,14 @@ static void HandleSoundRequest(SoundControl snd_ctrl) #endif /* deactivate channels that have expired since the last request */ - for (i=0; idata_ptr + num_channels * sample_pos; - for (i=0; inum_channels == 1 ? 0 : i); - for (j=0; jdata_ptr + num_channels * sample_pos; - for (i=0; inum_channels == 1 ? 0 : i); - for (j=0; j= mixer[i].data_len) Mixer_StopChannel(i); - for(i=0; i> 8) ^ 0x80; /* finally play the sound fragment */ @@ -1449,7 +1339,7 @@ void Mixer_Main() FD_ZERO(&mixer_fdset); FD_SET(audio.mixer_pipe[0], &mixer_fdset); - while(1) /* wait for sound playing commands from client */ + while (1) /* wait for sound playing commands from client */ { struct timeval delay = { 0, 0 }; @@ -1643,10 +1533,6 @@ static void *Load_WAV(char *filename) if (!audio.sound_available) return NULL; -#if 0 - printf("loading WAV file '%s'\n", filename); -#endif - snd_info = checked_calloc(sizeof(SoundInfo)); #if defined(TARGET_SDL) @@ -1673,6 +1559,8 @@ static void *Load_WAV(char *filename) #else /* AUDIO_UNIX_NATIVE */ + clear_mem(&header, sizeof(struct SoundHeader_WAV)); /* to make gcc happy */ + if ((file = fopen(filename, MODE_READ)) == NULL) { Error(ERR_WARN, "cannot open sound file '%s'", filename); @@ -1682,7 +1570,7 @@ static void *Load_WAV(char *filename) /* read chunk id "RIFF" */ getFileChunkLE(file, chunk_name, &chunk_size); - if (strcmp(chunk_name, "RIFF") != 0) + if (!strEqual(chunk_name, "RIFF")) { Error(ERR_WARN, "missing 'RIFF' chunk of sound file '%s'", filename); fclose(file); @@ -1692,7 +1580,7 @@ static void *Load_WAV(char *filename) /* read "RIFF" type id "WAVE" */ getFileChunkLE(file, chunk_name, NULL); - if (strcmp(chunk_name, "WAVE") != 0) + if (!strEqual(chunk_name, "WAVE")) { Error(ERR_WARN, "missing 'WAVE' type ID of sound file '%s'", filename); fclose(file); @@ -1702,7 +1590,7 @@ static void *Load_WAV(char *filename) while (getFileChunkLE(file, chunk_name, &chunk_size)) { - if (strcmp(chunk_name, "fmt ") == 0) + if (strEqual(chunk_name, "fmt ")) { if (chunk_size < WAV_HEADER_SIZE) { @@ -1760,13 +1648,13 @@ static void *Load_WAV(char *filename) printf("WAV file: '%s'\n", filename); printf(" Compression code: %d'\n", header.compression_code); printf(" Number of channels: %d'\n", header.num_channels); - printf(" Sample rate: %ld'\n", header.sample_rate); - printf(" Average bytes per second: %ld'\n", header.bytes_per_second); + printf(" Sample rate: %d'\n", header.sample_rate); + printf(" Average bytes per second: %d'\n", header.bytes_per_second); printf(" Block align: %d'\n", header.block_align); printf(" Significant bits per sample: %d'\n", header.bits_per_sample); #endif } - else if (strcmp(chunk_name, "data") == 0) + else if (strEqual(chunk_name, "data")) { data_byte_len = chunk_size; @@ -1822,11 +1710,11 @@ static void *Load_WAV(char *filename) int i; if (snd_ctrl->format == AUDIO_FORMAT_U8) - for (i=0; i '%s'\n", music->filename); -#endif - - if (strcmp(basename, music->filename) == 0) + if (strEqual(basename, music->filename)) { music_already_used = TRUE; break; @@ -1924,21 +1819,13 @@ void LoadCustomMusic_NoConf(void) if (music_already_used) continue; -#if 0 - if (FileIsSound(basename) || FileIsMusic(basename)) - printf("DEBUG: loading music '%s' ... [%d]\n", - basename, music_already_used); -#endif - if (draw_init_text) DrawInitText(basename, 150, FC_YELLOW); filename = getPath2(music_directory, basename); - if (FileIsSound(basename)) - mus_info = Load_WAV(filename); - else if (FileIsMusic(basename)) - mus_info = Load_MOD(filename); + if (FileIsMusic(basename)) + mus_info = Load_WAV_or_MOD(filename); free(filename); @@ -1954,10 +1841,6 @@ void LoadCustomMusic_NoConf(void) closedir(dir); draw_init_text = FALSE; - - if (num_music_noconf == 0) - Error(ERR_WARN, "cannot find any valid music files in directory '%s'", - music_directory); } int getSoundListSize() @@ -2033,7 +1916,7 @@ struct PropertyMapping *getMusicListPropertyMapping() } void InitSoundList(struct ConfigInfo *config_list, int num_file_list_entries, - struct ConfigInfo *config_suffix_list, + struct ConfigTypeInfo *config_suffix_list, char **base_prefixes, char **ext1_suffixes, char **ext2_suffixes, char **ext3_suffixes, char **ignore_tokens) @@ -2054,7 +1937,7 @@ void InitSoundList(struct ConfigInfo *config_list, int num_file_list_entries, sound_info->dynamic_file_list = NULL; sound_info->num_suffix_list_entries = 0; - for (i=0; config_suffix_list[i].token != NULL; i++) + for (i = 0; config_suffix_list[i].token != NULL; i++) sound_info->num_suffix_list_entries++; sound_info->suffix_list = config_suffix_list; @@ -2062,23 +1945,23 @@ void InitSoundList(struct ConfigInfo *config_list, int num_file_list_entries, /* ---------- initialize base prefix and suffixes lists ---------- */ sound_info->num_base_prefixes = 0; - for (i=0; base_prefixes[i] != NULL; i++) + for (i = 0; base_prefixes[i] != NULL; i++) sound_info->num_base_prefixes++; sound_info->num_ext1_suffixes = 0; - for (i=0; ext1_suffixes[i] != NULL; i++) + for (i = 0; ext1_suffixes[i] != NULL; i++) sound_info->num_ext1_suffixes++; sound_info->num_ext2_suffixes = 0; - for (i=0; ext2_suffixes[i] != NULL; i++) + for (i = 0; ext2_suffixes[i] != NULL; i++) sound_info->num_ext2_suffixes++; sound_info->num_ext3_suffixes = 0; - for (i=0; ext3_suffixes[i] != NULL; i++) + for (i = 0; ext3_suffixes[i] != NULL; i++) sound_info->num_ext3_suffixes++; sound_info->num_ignore_tokens = 0; - for (i=0; ignore_tokens[i] != NULL; i++) + for (i = 0; ignore_tokens[i] != NULL; i++) sound_info->num_ignore_tokens++; sound_info->base_prefixes = base_prefixes; @@ -2105,15 +1988,10 @@ void InitSoundList(struct ConfigInfo *config_list, int num_file_list_entries, sound_info->load_artwork = Load_WAV; sound_info->free_artwork = FreeSound; - -#if 0 - num_sounds = sound_info->num_file_list_entries; - Sound = (SoundInfo **)sound_info->artwork_list; -#endif } void InitMusicList(struct ConfigInfo *config_list, int num_file_list_entries, - struct ConfigInfo *config_suffix_list, + struct ConfigTypeInfo *config_suffix_list, char **base_prefixes, char **ext1_suffixes, char **ext2_suffixes, char **ext3_suffixes, char **ignore_tokens) @@ -2134,7 +2012,7 @@ void InitMusicList(struct ConfigInfo *config_list, int num_file_list_entries, music_info->dynamic_file_list = NULL; music_info->num_suffix_list_entries = 0; - for (i=0; config_suffix_list[i].token != NULL; i++) + for (i = 0; config_suffix_list[i].token != NULL; i++) music_info->num_suffix_list_entries++; music_info->suffix_list = config_suffix_list; @@ -2142,23 +2020,23 @@ void InitMusicList(struct ConfigInfo *config_list, int num_file_list_entries, /* ---------- initialize base prefix and suffixes lists ---------- */ music_info->num_base_prefixes = 0; - for (i=0; base_prefixes[i] != NULL; i++) + for (i = 0; base_prefixes[i] != NULL; i++) music_info->num_base_prefixes++; music_info->num_ext1_suffixes = 0; - for (i=0; ext1_suffixes[i] != NULL; i++) + for (i = 0; ext1_suffixes[i] != NULL; i++) music_info->num_ext1_suffixes++; music_info->num_ext2_suffixes = 0; - for (i=0; ext2_suffixes[i] != NULL; i++) + for (i = 0; ext2_suffixes[i] != NULL; i++) music_info->num_ext2_suffixes++; music_info->num_ext3_suffixes = 0; - for (i=0; ext3_suffixes[i] != NULL; i++) + for (i = 0; ext3_suffixes[i] != NULL; i++) music_info->num_ext3_suffixes++; music_info->num_ignore_tokens = 0; - for (i=0; ignore_tokens[i] != NULL; i++) + for (i = 0; ignore_tokens[i] != NULL; i++) music_info->num_ignore_tokens++; music_info->base_prefixes = base_prefixes; @@ -2183,7 +2061,7 @@ void InitMusicList(struct ConfigInfo *config_list, int num_file_list_entries, /* ---------- initialize artwork loading/freeing functions ---------- */ - music_info->load_artwork = Load_MOD; + music_info->load_artwork = Load_WAV_or_MOD; music_info->free_artwork = FreeMusic; } @@ -2234,7 +2112,7 @@ void PlaySoundExt(int nr, int volume, int stereo_position, int state) else if (stereo_position > SOUND_MAX_RIGHT) stereo_position = SOUND_MAX_RIGHT; - memset(&snd_ctrl, 0, sizeof(SoundControl)); /* to make valgrind happy */ + clear_mem(&snd_ctrl, sizeof(SoundControl)); /* to make valgrind happy */ snd_ctrl.active = TRUE; snd_ctrl.nr = nr; @@ -2295,7 +2173,7 @@ void StopSoundExt(int nr, int state) if (!audio.sound_available) return; - memset(&snd_ctrl, 0, sizeof(SoundControl)); /* to make valgrind happy */ + clear_mem(&snd_ctrl, sizeof(SoundControl)); /* to make valgrind happy */ snd_ctrl.active = FALSE; snd_ctrl.nr = nr; @@ -2307,30 +2185,24 @@ void StopSoundExt(int nr, int state) static void ReloadCustomSounds() { #if 0 - printf("DEBUG: reloading sounds '%s' ...\n", artwork.snd_current_identifier); + printf("::: reloading sounds '%s' ...\n", artwork.snd_current_identifier); #endif LoadArtworkConfig(sound_info); ReloadCustomArtworkList(sound_info); - -#if 0 - num_sounds = getSoundListSize(); -#endif } static void ReloadCustomMusic() { #if 0 - printf("DEBUG: reloading music '%s' ...\n", artwork.mus_current_identifier); + printf("::: reloading music '%s' ...\n", artwork.mus_current_identifier); #endif LoadArtworkConfig(music_info); ReloadCustomArtworkList(music_info); -#if 1 /* load all music files from directory not defined in "musicinfo.conf" */ LoadCustomMusic_NoConf(); -#endif } void InitReloadCustomSounds(char *set_identifier) @@ -2377,8 +2249,7 @@ void FreeSound(void *ptr) #endif } - if (sound->source_filename) - free(sound->source_filename); + checked_free(sound->source_filename); free(sound); } @@ -2414,7 +2285,7 @@ static void FreeAllMusic_NoConf() if (Music_NoConf == NULL) return; - for(i=0; i < num_music_noconf; i++) + for (i = 0; i < num_music_noconf; i++) FreeMusic(Music_NoConf[i]); free(Music_NoConf);