added optional button to restart game (door, panel and touch variants)
[rocksndiamonds.git] / src / libgame / sound.c
1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // sound.c
10 // ============================================================================
11
12 #include <sys/types.h>
13 #include <sys/time.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <dirent.h>
18 #include <signal.h>
19 #include <math.h>
20 #include <errno.h>
21
22 #include "platform.h"
23 #include "system.h"
24 #include "sound.h"
25 #include "misc.h"
26 #include "setup.h"
27 #include "text.h"
28
29
30 // expiration time (in milliseconds) for sound loops
31 #define SOUND_LOOP_EXPIRATION_TIME      200
32
33 // one second fading interval == 1000 ticks (milliseconds)
34 #define SOUND_FADING_INTERVAL           1000
35
36 #define SND_TYPE_NONE                   0
37 #define SND_TYPE_WAV                    1
38
39 #define MUS_TYPE_NONE                   0
40 #define MUS_TYPE_WAV                    1
41 #define MUS_TYPE_MOD                    2
42
43 #define DEVICENAME_DSP                  "/dev/dsp"
44 #define DEVICENAME_SOUND_DSP            "/dev/sound/dsp"
45 #define DEVICENAME_AUDIO                "/dev/audio"
46 #define DEVICENAME_AUDIOCTL             "/dev/audioCtl"
47
48 #define SOUND_VOLUME_LEFT(x)            (stereo_volume[x])
49 #define SOUND_VOLUME_RIGHT(x)           (stereo_volume[SOUND_MAX_LEFT2RIGHT-x])
50
51 #define SAME_SOUND_NR(x, y)             ((x).nr == (y).nr)
52 #define SAME_SOUND_DATA(x, y)           ((x).data_ptr == (y).data_ptr)
53
54 #define SOUND_VOLUME_FROM_PERCENT(v, p) ((p) < 0   ? SOUND_MIN_VOLUME : \
55                                          (p) > 100 ? (v) :              \
56                                          (p) * (v) / 100)
57
58 #define SOUND_VOLUME_SIMPLE(v) SOUND_VOLUME_FROM_PERCENT(v, setup.volume_simple)
59 #define SOUND_VOLUME_LOOPS(v)  SOUND_VOLUME_FROM_PERCENT(v, setup.volume_loops)
60 #define SOUND_VOLUME_MUSIC(v)  SOUND_VOLUME_FROM_PERCENT(v, setup.volume_music)
61
62 #define SETUP_SOUND_VOLUME(v, s)        ((s) & SND_CTRL_MUSIC ?         \
63                                          SOUND_VOLUME_MUSIC(v) :        \
64                                          (s) & SND_CTRL_LOOP ?          \
65                                          SOUND_VOLUME_LOOPS(v) :        \
66                                          SOUND_VOLUME_SIMPLE(v))
67
68 struct AudioFormatInfo
69 {
70   boolean stereo;               // availability of stereo sound
71   int format;                   // size and endianess of sample data
72   int sample_rate;              // sample frequency
73   int fragment_size;            // audio device fragment size in bytes
74 };
75
76 struct SampleInfo
77 {
78   char *source_filename;
79   int num_references;
80
81   int type;
82   int format;
83   void *data_ptr;               // pointer to first sample (8 or 16 bit)
84   int data_len;                 // number of samples, NOT number of bytes
85   int num_channels;             // mono: 1 channel, stereo: 2 channels
86 };
87 typedef struct SampleInfo SoundInfo;
88 typedef struct SampleInfo MusicInfo;
89
90 struct SoundControl
91 {
92   boolean active;
93
94   int nr;
95   int volume;
96   int stereo_position;
97
98   int state;
99
100   unsigned int playing_starttime;
101   unsigned int playing_pos;
102
103   int type;
104   int format;
105   void *data_ptr;               // pointer to first sample (8 or 16 bit)
106   int data_len;         // number of samples, NOT number of bytes
107   int num_channels;             // mono: 1 channel, stereo: 2 channels
108 };
109 typedef struct SoundControl SoundControl;
110
111 static struct ArtworkListInfo *sound_info = NULL;
112 static struct ArtworkListInfo *music_info = NULL;
113
114 static MusicInfo **Music_NoConf = NULL;
115
116 static int num_music_noconf = 0;
117 static int stereo_volume[SOUND_MAX_LEFT2RIGHT + 1];
118
119 static char *currently_playing_music_filename = NULL;
120
121
122 // ============================================================================
123 // THE STUFF BELOW IS ONLY USED BY THE SOUND SERVER CHILD PROCESS
124
125 static struct SoundControl mixer[NUM_MIXER_CHANNELS];
126 static int mixer_active_channels = 0;
127 static boolean expire_loop_sounds = FALSE;
128
129 static void ReloadCustomSounds(void);
130 static void ReloadCustomMusic(void);
131 static void FreeSound(void *);
132 static void FreeMusic(void *);
133 static void FreeAllMusic_NoConf(void);
134 static void Mixer_StopMusicChannel(void);
135
136 static SoundInfo *getSoundInfoEntryFromSoundID(int);
137 static MusicInfo *getMusicInfoEntryFromMusicID(int);
138
139
140 // ----------------------------------------------------------------------------
141 // mixer functions
142 // ----------------------------------------------------------------------------
143
144 void Mixer_InitChannels(void)
145 {
146   int i;
147
148   for (i = 0; i < audio.num_channels; i++)
149     mixer[i].active = FALSE;
150   mixer_active_channels = 0;
151 }
152
153 static void Mixer_ResetChannelExpiration(int channel)
154 {
155   mixer[channel].playing_starttime = Counter();
156
157   if (expire_loop_sounds &&
158       IS_LOOP(mixer[channel]) && !IS_MUSIC(mixer[channel]))
159     Mix_ExpireChannel(channel, SOUND_LOOP_EXPIRATION_TIME);
160 }
161
162 static boolean Mixer_ChannelExpired(int channel)
163 {
164   if (!mixer[channel].active)
165     return TRUE;
166
167   if (expire_loop_sounds &&
168       IS_LOOP(mixer[channel]) && !IS_MUSIC(mixer[channel]) &&
169       DelayReachedExt2(&mixer[channel].playing_starttime,
170                        SOUND_LOOP_EXPIRATION_TIME, Counter()))
171     return TRUE;
172
173   if (!Mix_Playing(channel))
174     return TRUE;
175
176   return FALSE;
177 }
178
179 static boolean Mixer_AllocateChannel(int channel)
180 {
181   return TRUE;
182 }
183
184 static void Mixer_SetChannelProperties(int channel)
185 {
186   Mix_Volume(channel, mixer[channel].volume);
187   Mix_SetPanning(channel,
188                  SOUND_VOLUME_LEFT(mixer[channel].stereo_position),
189                  SOUND_VOLUME_RIGHT(mixer[channel].stereo_position));
190 }
191
192 static void Mixer_StartChannel(int channel)
193 {
194   Mix_PlayChannel(channel, mixer[channel].data_ptr,
195                   IS_LOOP(mixer[channel]) ? -1 : 0);
196 }
197
198 static void Mixer_PlayChannel(int channel)
199 {
200   // start with inactive channel in case something goes wrong
201   mixer[channel].active = FALSE;
202
203   if (mixer[channel].type != MUS_TYPE_WAV)
204     return;
205
206   if (!Mixer_AllocateChannel(channel))
207     return;
208
209   Mixer_SetChannelProperties(channel);
210   Mixer_StartChannel(channel);
211
212   Mixer_ResetChannelExpiration(channel);
213
214   mixer[channel].playing_pos = 0;
215   mixer[channel].active = TRUE;
216   mixer_active_channels++;
217 }
218
219 static void Mixer_PlayMusicChannel(void)
220 {
221   Mixer_PlayChannel(audio.music_channel);
222
223   if (mixer[audio.music_channel].type != MUS_TYPE_WAV)
224   {
225     int loops = (IS_LOOP(mixer[audio.music_channel]) ? -1 : 1);
226
227     // stopping music channel before playing next track seems to be needed to
228     // prevent audio problems that may occur when playing MP3 files on Windows
229     Mixer_StopMusicChannel();
230
231     // use short fade-in to prevent "plop" sound for certain music files
232     // (this may happen when switching on music while playing the game)
233     Mix_VolumeMusic(mixer[audio.music_channel].volume);
234     Mix_FadeInMusic(mixer[audio.music_channel].data_ptr, loops, 100);
235
236 #if defined(PLATFORM_WINDOWS)
237     // playing MIDI music is broken since Windows Vista, as it sets the volume
238     // for MIDI music also for all other sounds and music, which cannot be set
239     // back to normal unless playing MIDI music again with that desired volume
240     // (more details: https://www.artsoft.org/forum/viewtopic.php?f=7&t=2253)
241     // => workaround: always play MIDI music with maximum volume
242     if (Mix_GetMusicType(NULL) == MUS_MID)
243       Mix_VolumeMusic(SOUND_MAX_VOLUME);
244 #endif
245   }
246 }
247
248 static void Mixer_StopChannel(int channel)
249 {
250   if (!mixer[channel].active)
251     return;
252
253   Mix_HaltChannel(channel);
254
255   mixer[channel].active = FALSE;
256   mixer_active_channels--;
257 }
258
259 static void Mixer_StopMusicChannel(void)
260 {
261   Mixer_StopChannel(audio.music_channel);
262
263   Mix_HaltMusic();
264
265   setString(&currently_playing_music_filename, NULL);
266 }
267
268 static void Mixer_FadeChannel(int channel)
269 {
270   if (!mixer[channel].active)
271     return;
272
273   mixer[channel].state |= SND_CTRL_FADE;
274
275   Mix_FadeOutChannel(channel, SOUND_FADING_INTERVAL);
276 }
277
278 static void Mixer_FadeMusicChannel(void)
279 {
280   Mixer_FadeChannel(audio.music_channel);
281
282   Mix_FadeOutMusic(SOUND_FADING_INTERVAL);
283
284 #if defined(PLATFORM_WINDOWS)
285   // playing MIDI music is broken since Windows Vista, as it sets the volume
286   // for MIDI music also for all other sounds and music, which cannot be set
287   // back to normal unless playing MIDI music again with that desired volume
288   // (more details: https://www.artsoft.org/forum/viewtopic.php?f=7&t=2253)
289   // => workaround: never fade MIDI music to lower volume, but just stop it
290   if (Mix_GetMusicType(NULL) == MUS_MID)
291     Mixer_StopMusicChannel();
292 #endif
293
294   setString(&currently_playing_music_filename, NULL);
295 }
296
297 static void Mixer_UnFadeChannel(int channel)
298 {
299   if (!mixer[channel].active || !IS_FADING(mixer[channel]))
300     return;
301
302   mixer[channel].state &= ~SND_CTRL_FADE;
303   mixer[channel].volume = SOUND_MAX_VOLUME;
304
305   Mix_ExpireChannel(channel, -1);
306   Mix_Volume(channel, mixer[channel].volume);
307 }
308
309 static void Mixer_InsertSound(SoundControl snd_ctrl)
310 {
311   SoundInfo *snd_info;
312   int i, k;
313
314   if (IS_MUSIC(snd_ctrl))
315     snd_info = getMusicInfoEntryFromMusicID(snd_ctrl.nr);
316   else
317     snd_info = getSoundInfoEntryFromSoundID(snd_ctrl.nr);
318
319   if (snd_info == NULL)
320     return;
321
322   // copy sound sample and format information
323   snd_ctrl.type         = snd_info->type;
324   snd_ctrl.format       = snd_info->format;
325   snd_ctrl.data_ptr     = snd_info->data_ptr;
326   snd_ctrl.data_len     = snd_info->data_len;
327   snd_ctrl.num_channels = snd_info->num_channels;
328
329   // play music samples on a dedicated music channel
330   if (IS_MUSIC(snd_ctrl))
331   {
332     Mixer_StopMusicChannel();
333
334     mixer[audio.music_channel] = snd_ctrl;
335     Mixer_PlayMusicChannel();
336
337     setString(&currently_playing_music_filename,
338               getBaseNamePtr(snd_info->source_filename));
339
340     return;
341   }
342
343   // check if (and how often) this sound sample is already playing
344   for (k = 0, i = audio.first_sound_channel; i < audio.num_channels; i++)
345     if (mixer[i].active && SAME_SOUND_DATA(mixer[i], snd_ctrl))
346       k++;
347
348   // reset expiration delay for already playing loop sounds
349   if (k > 0 && IS_LOOP(snd_ctrl))
350   {
351     for (i = audio.first_sound_channel; i < audio.num_channels; i++)
352     {
353       if (mixer[i].active && SAME_SOUND_DATA(mixer[i], snd_ctrl))
354       {
355         if (IS_FADING(mixer[i]))
356           Mixer_UnFadeChannel(i);
357
358         // restore settings like volume and stereo position
359         mixer[i].volume = snd_ctrl.volume;
360         mixer[i].stereo_position = snd_ctrl.stereo_position;
361
362         Mixer_SetChannelProperties(i);
363         Mixer_ResetChannelExpiration(i);
364       }
365     }
366
367     return;
368   }
369
370   // don't play sound more than n times simultaneously (with n == 2 for now)
371   if (k >= 2)
372   {
373     unsigned int playing_current = Counter();
374     int longest = 0, longest_nr = audio.first_sound_channel;
375
376     // look for oldest equal sound
377     for (i = audio.first_sound_channel; i < audio.num_channels; i++)
378     {
379       int playing_time = playing_current - mixer[i].playing_starttime;
380       int actual;
381
382       if (!mixer[i].active || !SAME_SOUND_NR(mixer[i], snd_ctrl))
383         continue;
384
385       actual = 1000 * playing_time / mixer[i].data_len;
386
387       if (actual >= longest)
388       {
389         longest = actual;
390         longest_nr = i;
391       }
392     }
393
394     Mixer_StopChannel(longest_nr);
395   }
396
397   /* If all (non-music) channels are active, stop the channel that has
398      played its sound sample most completely (in percent of the sample
399      length). As we cannot currently get the actual playing position
400      of the channel's sound sample when compiling with the SDL mixer
401      library, we use the current playing time (in milliseconds) instead. */
402
403 #if DEBUG
404   // channel allocation sanity check -- should not be needed
405   if (mixer_active_channels ==
406       audio.num_channels - (mixer[audio.music_channel].active ? 0 : 1))
407   {
408     for (i = audio.first_sound_channel; i < audio.num_channels; i++)
409     {
410       if (!mixer[i].active)
411       {
412         Debug("audio", "Mixer_InsertSound: Channel %d inactive", i);
413         Debug("audio", "Mixer_InsertSound: This should never happen!");
414
415         mixer_active_channels--;
416       }
417     }
418   }
419 #endif
420
421   if (mixer_active_channels ==
422       audio.num_channels - (mixer[audio.music_channel].active ? 0 : 1))
423   {
424     unsigned int playing_current = Counter();
425     int longest = 0, longest_nr = audio.first_sound_channel;
426
427     for (i = audio.first_sound_channel; i < audio.num_channels; i++)
428     {
429       int playing_time = playing_current - mixer[i].playing_starttime;
430       int actual = 1000 * playing_time / mixer[i].data_len;
431
432       if (!IS_LOOP(mixer[i]) && actual > longest)
433       {
434         longest = actual;
435         longest_nr = i;
436       }
437     }
438
439     Mixer_StopChannel(longest_nr);
440   }
441
442   // add the new sound to the mixer
443   for (i = audio.first_sound_channel; i < audio.num_channels; i++)
444   {
445     if (!mixer[i].active)
446     {
447       mixer[i] = snd_ctrl;
448       Mixer_PlayChannel(i);
449
450       break;
451     }
452   }
453 }
454
455 static void HandleSoundRequest(SoundControl snd_ctrl)
456 {
457   int i;
458
459   // deactivate channels that have expired since the last request
460   for (i = 0; i < audio.num_channels; i++)
461     if (mixer[i].active && Mixer_ChannelExpired(i))
462       Mixer_StopChannel(i);
463
464   if (IS_RELOADING(snd_ctrl))           // load new sound or music files
465   {
466     Mixer_StopMusicChannel();
467     for (i = audio.first_sound_channel; i < audio.num_channels; i++)
468       Mixer_StopChannel(i);
469
470     if (snd_ctrl.state & SND_CTRL_RELOAD_SOUNDS)
471       ReloadCustomSounds();
472     else
473       ReloadCustomMusic();
474   }
475   else if (IS_FADING(snd_ctrl))         // fade out existing sound or music
476   {
477     if (IS_MUSIC(snd_ctrl))
478     {
479       Mixer_FadeMusicChannel();
480       return;
481     }
482
483     for (i = audio.first_sound_channel; i < audio.num_channels; i++)
484       if (SAME_SOUND_NR(mixer[i], snd_ctrl) || ALL_SOUNDS(snd_ctrl))
485         Mixer_FadeChannel(i);
486   }
487   else if (IS_STOPPING(snd_ctrl))       // stop existing sound or music
488   {
489     if (IS_MUSIC(snd_ctrl))
490     {
491       Mixer_StopMusicChannel();
492       return;
493     }
494
495     for (i = audio.first_sound_channel; i < audio.num_channels; i++)
496       if (SAME_SOUND_NR(mixer[i], snd_ctrl) || ALL_SOUNDS(snd_ctrl))
497         Mixer_StopChannel(i);
498   }
499   else if (SET_EXPIRE_LOOPS(snd_ctrl))  // set loop expiration on or off
500   {
501     expire_loop_sounds = snd_ctrl.active;
502   }
503   else if (snd_ctrl.active)             // add new sound to mixer
504   {
505     Mixer_InsertSound(snd_ctrl);
506   }
507 }
508
509 void StartMixer(void)
510 {
511   int i;
512
513   if (!audio.sound_available)
514     return;
515
516   // initialize stereo position conversion information
517   for (i = 0; i <= SOUND_MAX_LEFT2RIGHT; i++)
518     stereo_volume[i] =
519       (int)sqrt((float)(SOUND_MAX_LEFT2RIGHT * SOUND_MAX_LEFT2RIGHT - i * i));
520 }
521
522
523 // THE STUFF ABOVE IS ONLY USED BY THE SOUND SERVER CHILD PROCESS
524 // ============================================================================
525 // THE STUFF BELOW IS ONLY USED BY THE MAIN PROCESS
526
527 #define CHUNK_ID_LEN            4       // IFF style chunk id length
528 #define WAV_HEADER_SIZE         16      // size of WAV file header
529
530 static void *Load_WAV(char *filename)
531 {
532   SoundInfo *snd_info;
533
534   if (!audio.sound_available)
535     return NULL;
536
537   snd_info = checked_calloc(sizeof(SoundInfo));
538
539   if ((snd_info->data_ptr = Mix_LoadWAV(filename)) == NULL)
540   {
541     Warn("cannot read sound file '%s': %s", filename, Mix_GetError());
542
543     free(snd_info);
544
545     return NULL;
546   }
547
548   snd_info->data_len = ((Mix_Chunk *)snd_info->data_ptr)->alen;
549
550   snd_info->type = SND_TYPE_WAV;
551   snd_info->source_filename = getStringCopy(filename);
552
553   return snd_info;
554 }
555
556 static void *Load_MOD(char *filename)
557 {
558   MusicInfo *mod_info;
559
560   if (!audio.sound_available)
561     return NULL;
562
563   mod_info = checked_calloc(sizeof(MusicInfo));
564
565   if ((mod_info->data_ptr = Mix_LoadMUS(filename)) == NULL)
566   {
567     Warn("cannot read music file '%s': %s", filename, Mix_GetError());
568
569     free(mod_info);
570
571     return NULL;
572   }
573
574   mod_info->type = MUS_TYPE_MOD;
575   mod_info->source_filename = getStringCopy(filename);
576
577   return mod_info;
578 }
579
580 static void *Load_WAV_or_MOD(char *filename)
581 {
582   if (FileIsMusic(filename))
583     return Load_MOD(filename);
584   else if (FileIsSound(filename))
585     return Load_WAV(filename);
586   else
587     return NULL;
588 }
589
590 static int compareMusicInfo(const void *object1, const void *object2)
591 {
592   const MusicInfo *mi1 = *((MusicInfo **)object1);
593   const MusicInfo *mi2 = *((MusicInfo **)object2);
594
595   return strcmp(mi1->source_filename, mi2->source_filename);
596 }
597
598 static void LoadCustomMusic_NoConf(void)
599 {
600   static boolean draw_init_text = TRUE;         // only draw at startup
601   static char *last_music_directory = NULL;
602   char *music_directory = getCustomMusicDirectory_NoConf();
603   Directory *dir;
604   DirectoryEntry *dir_entry;
605   int num_music = getMusicListSize();
606
607   if (!audio.sound_available)
608     return;
609
610   if (last_music_directory != NULL &&
611       strEqual(last_music_directory, music_directory))
612     return;     // old and new music directory are the same
613
614   if (last_music_directory != NULL)
615     free(last_music_directory);
616   last_music_directory = getStringCopy(music_directory);
617
618   FreeAllMusic_NoConf();
619
620   if (music_directory == NULL)
621   {
622     Warn("cannot find music directory with unconfigured music");
623
624     return;
625   }
626   else if ((dir = openDirectory(music_directory)) == NULL)
627   {
628     Warn("cannot read music directory '%s'", music_directory);
629
630     return;
631   }
632
633   if (draw_init_text)
634     DrawInitTextHead("Loading music");
635
636   while ((dir_entry = readDirectory(dir)) != NULL)      // loop all entries
637   {
638     char *basename = dir_entry->basename;
639     MusicInfo *mus_info = NULL;
640     boolean music_already_used = FALSE;
641     int i;
642
643     // skip all music files that are configured in music config file
644     for (i = 0; i < num_music; i++)
645     {
646       struct FileInfo *music = getMusicListEntry(i);
647
648       if (strEqual(basename, music->filename))
649       {
650         music_already_used = TRUE;
651         break;
652       }
653     }
654
655     if (music_already_used)
656       continue;
657
658     if (draw_init_text)
659       DrawInitTextItem(basename);
660
661     if (FileIsMusic(dir_entry->filename))
662       mus_info = Load_WAV_or_MOD(dir_entry->filename);
663
664     if (mus_info)
665     {
666       num_music_noconf++;
667       Music_NoConf = checked_realloc(Music_NoConf,
668                                      num_music_noconf * sizeof(MusicInfo *));
669       Music_NoConf[num_music_noconf - 1] = mus_info;
670     }
671   }
672
673   closeDirectory(dir);
674
675   // sort music files by filename
676   qsort(Music_NoConf, num_music_noconf, sizeof(MusicInfo *), compareMusicInfo);
677
678   draw_init_text = FALSE;
679 }
680
681 int getSoundListSize(void)
682 {
683   return (sound_info->num_file_list_entries +
684           sound_info->num_dynamic_file_list_entries);
685 }
686
687 int getMusicListSize(void)
688 {
689   return (music_info->num_file_list_entries +
690           music_info->num_dynamic_file_list_entries);
691 }
692
693 int getMusicListSize_NoConf(void)
694 {
695   return num_music_noconf;
696 }
697
698 struct FileInfo *getSoundListEntry(int pos)
699 {
700   int num_sounds = getSoundListSize();
701   int num_list_entries = sound_info->num_file_list_entries;
702   int list_pos = (pos < num_list_entries ? pos : pos - num_list_entries);
703
704   if (pos < 0 || pos >= num_sounds)     // invalid sound
705     return NULL;
706
707   return (pos < num_list_entries ? &sound_info->file_list[list_pos] :
708           &sound_info->dynamic_file_list[list_pos]);
709 }
710
711 struct FileInfo *getMusicListEntry(int pos)
712 {
713   int num_music = getMusicListSize();
714   int num_list_entries = music_info->num_file_list_entries;
715   int list_pos = (pos < num_list_entries ? pos : pos - num_list_entries);
716
717   if (pos < 0 || pos >= num_music)      // invalid music
718     return NULL;
719
720   return (pos < num_list_entries ? &music_info->file_list[list_pos] :
721           &music_info->dynamic_file_list[list_pos]);
722 }
723
724 static SoundInfo *getSoundInfoEntryFromSoundID(int pos)
725 {
726   int num_sounds = getSoundListSize();
727   int num_list_entries = sound_info->num_file_list_entries;
728   int list_pos = (pos < num_list_entries ? pos : pos - num_list_entries);
729   SoundInfo **snd_info =
730     (SoundInfo **)(pos < num_list_entries ? sound_info->artwork_list :
731                    sound_info->dynamic_artwork_list);
732
733   if (pos < 0 || pos >= num_sounds)     // invalid sound
734     return NULL;
735
736   return snd_info[list_pos];
737 }
738
739 static MusicInfo *getMusicInfoEntryFromMusicID(int pos)
740 {
741   int num_music = getMusicListSize();
742   int num_list_entries = music_info->num_file_list_entries;
743   int list_pos = (pos < num_list_entries ? pos : pos - num_list_entries);
744   MusicInfo **mus_info =
745     (MusicInfo **)(pos < num_list_entries ? music_info->artwork_list :
746                    music_info->dynamic_artwork_list);
747
748   if (pos >= num_music)                 // invalid music
749     return NULL;
750
751   if (pos < 0)                          // undefined music
752   {
753     if (num_music_noconf == 0)          // no fallback music available
754       return NULL;
755
756     pos = UNMAP_NOCONF_MUSIC(pos) % num_music_noconf;
757
758     return Music_NoConf[pos];
759   }
760
761   return mus_info[list_pos];
762 }
763
764 char *getSoundInfoEntryFilename(int pos)
765 {
766   SoundInfo *snd_info = getSoundInfoEntryFromSoundID(pos);
767
768   if (snd_info == NULL)
769     return NULL;
770
771   return getBaseNamePtr(snd_info->source_filename);
772 }
773
774 char *getMusicInfoEntryFilename(int pos)
775 {
776   MusicInfo *mus_info = getMusicInfoEntryFromMusicID(pos);
777
778   if (mus_info == NULL)
779     return NULL;
780
781   return getBaseNamePtr(mus_info->source_filename);
782 }
783
784 char *getCurrentlyPlayingMusicFilename(void)
785 {
786   return currently_playing_music_filename;
787 }
788
789 int getSoundListPropertyMappingSize(void)
790 {
791   return sound_info->num_property_mapping_entries;
792 }
793
794 int getMusicListPropertyMappingSize(void)
795 {
796   return music_info->num_property_mapping_entries;
797 }
798
799 struct PropertyMapping *getSoundListPropertyMapping(void)
800 {
801   return sound_info->property_mapping;
802 }
803
804 struct PropertyMapping *getMusicListPropertyMapping(void)
805 {
806   return music_info->property_mapping;
807 }
808
809 void InitSoundList(struct ConfigInfo *config_list, int num_file_list_entries,
810                    struct ConfigTypeInfo *config_suffix_list,
811                    char **base_prefixes, char **ext1_suffixes,
812                    char **ext2_suffixes, char **ext3_suffixes,
813                    char **ignore_tokens)
814 {
815   int i;
816
817   sound_info = checked_calloc(sizeof(struct ArtworkListInfo));
818   sound_info->type = ARTWORK_TYPE_SOUNDS;
819
820   // ---------- initialize file list and suffix lists ----------
821
822   sound_info->num_file_list_entries = num_file_list_entries;
823   sound_info->num_dynamic_file_list_entries = 0;
824
825   sound_info->file_list =
826     getFileListFromConfigList(config_list, config_suffix_list, ignore_tokens,
827                               num_file_list_entries);
828   sound_info->dynamic_file_list = NULL;
829
830   sound_info->num_suffix_list_entries = 0;
831   for (i = 0; config_suffix_list[i].token != NULL; i++)
832     sound_info->num_suffix_list_entries++;
833
834   sound_info->suffix_list = config_suffix_list;
835
836   // ---------- initialize base prefix and suffixes lists ----------
837
838   sound_info->num_base_prefixes = 0;
839   for (i = 0; base_prefixes[i] != NULL; i++)
840     sound_info->num_base_prefixes++;
841
842   sound_info->num_ext1_suffixes = 0;
843   for (i = 0; ext1_suffixes[i] != NULL; i++)
844     sound_info->num_ext1_suffixes++;
845
846   sound_info->num_ext2_suffixes = 0;
847   for (i = 0; ext2_suffixes[i] != NULL; i++)
848     sound_info->num_ext2_suffixes++;
849
850   sound_info->num_ext3_suffixes = 0;
851   for (i = 0; ext3_suffixes[i] != NULL; i++)
852     sound_info->num_ext3_suffixes++;
853
854   sound_info->num_ignore_tokens = 0;
855   for (i = 0; ignore_tokens[i] != NULL; i++)
856     sound_info->num_ignore_tokens++;
857
858   sound_info->base_prefixes = base_prefixes;
859   sound_info->ext1_suffixes = ext1_suffixes;
860   sound_info->ext2_suffixes = ext2_suffixes;
861   sound_info->ext3_suffixes = ext3_suffixes;
862   sound_info->ignore_tokens = ignore_tokens;
863
864   sound_info->num_property_mapping_entries = 0;
865
866   sound_info->property_mapping = NULL;
867
868   // ---------- initialize artwork reference and content lists ----------
869
870   sound_info->sizeof_artwork_list_entry = sizeof(SoundInfo *);
871
872   sound_info->artwork_list =
873     checked_calloc(num_file_list_entries * sizeof(SoundInfo *));
874   sound_info->dynamic_artwork_list = NULL;
875
876   sound_info->content_list = NULL;
877
878   // ---------- initialize artwork loading/freeing functions ----------
879
880   sound_info->load_artwork = Load_WAV;
881   sound_info->free_artwork = FreeSound;
882 }
883
884 void InitMusicList(struct ConfigInfo *config_list, int num_file_list_entries,
885                    struct ConfigTypeInfo *config_suffix_list,
886                    char **base_prefixes, char **ext1_suffixes,
887                    char **ext2_suffixes, char **ext3_suffixes,
888                    char **ignore_tokens)
889 {
890   int i;
891
892   music_info = checked_calloc(sizeof(struct ArtworkListInfo));
893   music_info->type = ARTWORK_TYPE_MUSIC;
894
895   // ---------- initialize file list and suffix lists ----------
896
897   music_info->num_file_list_entries = num_file_list_entries;
898   music_info->num_dynamic_file_list_entries = 0;
899
900   music_info->file_list =
901     getFileListFromConfigList(config_list, config_suffix_list, ignore_tokens,
902                               num_file_list_entries);
903   music_info->dynamic_file_list = NULL;
904
905   music_info->num_suffix_list_entries = 0;
906   for (i = 0; config_suffix_list[i].token != NULL; i++)
907     music_info->num_suffix_list_entries++;
908
909   music_info->suffix_list = config_suffix_list;
910
911   // ---------- initialize base prefix and suffixes lists ----------
912
913   music_info->num_base_prefixes = 0;
914   for (i = 0; base_prefixes[i] != NULL; i++)
915     music_info->num_base_prefixes++;
916
917   music_info->num_ext1_suffixes = 0;
918   for (i = 0; ext1_suffixes[i] != NULL; i++)
919     music_info->num_ext1_suffixes++;
920
921   music_info->num_ext2_suffixes = 0;
922   for (i = 0; ext2_suffixes[i] != NULL; i++)
923     music_info->num_ext2_suffixes++;
924
925   music_info->num_ext3_suffixes = 0;
926   for (i = 0; ext3_suffixes[i] != NULL; i++)
927     music_info->num_ext3_suffixes++;
928
929   music_info->num_ignore_tokens = 0;
930   for (i = 0; ignore_tokens[i] != NULL; i++)
931     music_info->num_ignore_tokens++;
932
933   music_info->base_prefixes = base_prefixes;
934   music_info->ext1_suffixes = ext1_suffixes;
935   music_info->ext2_suffixes = ext2_suffixes;
936   music_info->ext3_suffixes = ext3_suffixes;
937   music_info->ignore_tokens = ignore_tokens;
938
939   music_info->num_property_mapping_entries = 0;
940
941   music_info->property_mapping = NULL;
942
943   // ---------- initialize artwork reference and content lists ----------
944
945   music_info->sizeof_artwork_list_entry = sizeof(MusicInfo *);
946
947   music_info->artwork_list =
948     checked_calloc(num_file_list_entries * sizeof(MusicInfo *));
949   music_info->dynamic_artwork_list = NULL;
950
951   music_info->content_list = NULL;
952
953   // ---------- initialize artwork loading/freeing functions ----------
954
955   music_info->load_artwork = Load_WAV_or_MOD;
956   music_info->free_artwork = FreeMusic;
957 }
958
959 void PlayMusic(int nr)
960 {
961   if (!audio.music_available)
962     return;
963
964   PlaySoundMusic(nr);
965 }
966
967 void PlayMusicLoop(int nr)
968 {
969   if (!audio.music_available)
970     return;
971
972   PlaySoundMusicLoop(nr);
973 }
974
975 void PlaySound(int nr)
976 {
977   if (!setup.sound_simple)
978     return;
979
980   PlaySoundExt(nr, SOUND_MAX_VOLUME, SOUND_MIDDLE, SND_CTRL_PLAY_SOUND);
981 }
982
983 void PlaySoundStereo(int nr, int stereo_position)
984 {
985   if (!setup.sound_simple)
986     return;
987
988   PlaySoundExt(nr, SOUND_MAX_VOLUME, stereo_position, SND_CTRL_PLAY_SOUND);
989 }
990
991 void PlaySoundLoop(int nr)
992 {
993   if (!setup.sound_loops)
994     return;
995
996   PlaySoundExt(nr, SOUND_MAX_VOLUME, SOUND_MIDDLE, SND_CTRL_PLAY_LOOP);
997 }
998
999 void PlaySoundMusic(int nr)
1000 {
1001   if (!setup.sound_music)
1002     return;
1003
1004   PlaySoundExt(nr, SOUND_MAX_VOLUME, SOUND_MIDDLE, SND_CTRL_PLAY_MUSIC);
1005 }
1006
1007 void PlaySoundMusicLoop(int nr)
1008 {
1009   if (!setup.sound_music)
1010     return;
1011
1012   PlaySoundExt(nr, SOUND_MAX_VOLUME, SOUND_MIDDLE, SND_CTRL_PLAY_MUSIC_LOOP);
1013 }
1014
1015 void PlaySoundExt(int nr, int volume, int stereo_position, int state)
1016 {
1017   SoundControl snd_ctrl;
1018
1019   if (!audio.sound_available ||
1020       !audio.sound_enabled ||
1021       audio.sound_deactivated)
1022     return;
1023
1024   volume = SETUP_SOUND_VOLUME(volume, state);
1025
1026   if (volume < SOUND_MIN_VOLUME)
1027     volume = SOUND_MIN_VOLUME;
1028   else if (volume > SOUND_MAX_VOLUME)
1029     volume = SOUND_MAX_VOLUME;
1030
1031   if (stereo_position < SOUND_MAX_LEFT)
1032     stereo_position = SOUND_MAX_LEFT;
1033   else if (stereo_position > SOUND_MAX_RIGHT)
1034     stereo_position = SOUND_MAX_RIGHT;
1035
1036   clear_mem(&snd_ctrl, sizeof(SoundControl));   // to make valgrind happy
1037
1038   snd_ctrl.active = TRUE;
1039   snd_ctrl.nr = nr;
1040   snd_ctrl.volume = volume;
1041   snd_ctrl.stereo_position = stereo_position;
1042   snd_ctrl.state = state;
1043
1044   HandleSoundRequest(snd_ctrl);
1045 }
1046
1047 void FadeMusic(void)
1048 {
1049   if (!audio.music_available)
1050     return;
1051
1052   StopSoundExt(-1, SND_CTRL_FADE_MUSIC);
1053 }
1054
1055 void FadeSound(int nr)
1056 {
1057   StopSoundExt(nr, SND_CTRL_FADE_SOUND);
1058 }
1059
1060 void FadeSounds(void)
1061 {
1062   StopSoundExt(-1, SND_CTRL_FADE_ALL);
1063 }
1064
1065 void FadeSoundsAndMusic(void)
1066 {
1067   FadeSounds();
1068   FadeMusic();
1069 }
1070
1071 void StopMusic(void)
1072 {
1073   if (!audio.music_available)
1074     return;
1075
1076   StopSoundExt(-1, SND_CTRL_STOP_MUSIC);
1077 }
1078
1079 void StopSound(int nr)
1080 {
1081   StopSoundExt(nr, SND_CTRL_STOP_SOUND);
1082 }
1083
1084 void StopSounds(void)
1085 {
1086   StopMusic();
1087   StopSoundExt(-1, SND_CTRL_STOP_ALL);
1088 }
1089
1090 void StopSoundExt(int nr, int state)
1091 {
1092   SoundControl snd_ctrl;
1093
1094   if (!audio.sound_available)
1095     return;
1096
1097   clear_mem(&snd_ctrl, sizeof(SoundControl));   // to make valgrind happy
1098
1099   snd_ctrl.active = FALSE;
1100   snd_ctrl.nr = nr;
1101   snd_ctrl.state = state;
1102
1103   HandleSoundRequest(snd_ctrl);
1104 }
1105
1106 void ExpireSoundLoops(boolean active)
1107 {
1108   SoundControl snd_ctrl;
1109
1110   if (!audio.sound_available)
1111     return;
1112
1113   clear_mem(&snd_ctrl, sizeof(SoundControl));   // to make valgrind happy
1114
1115   snd_ctrl.active = active;
1116   snd_ctrl.state = SND_CTRL_EXPIRE_LOOPS;
1117
1118   HandleSoundRequest(snd_ctrl);
1119 }
1120
1121 static void ReloadCustomSounds(void)
1122 {
1123   LoadArtworkConfig(sound_info);
1124   ReloadCustomArtworkList(sound_info);
1125 }
1126
1127 static void ReloadCustomMusic(void)
1128 {
1129   LoadArtworkConfig(music_info);
1130   ReloadCustomArtworkList(music_info);
1131
1132   // load all music files from directory not defined in "musicinfo.conf"
1133   LoadCustomMusic_NoConf();
1134 }
1135
1136 void InitReloadCustomSounds(void)
1137 {
1138   if (!audio.sound_available)
1139     return;
1140
1141   ReloadCustomSounds();
1142 }
1143
1144 void InitReloadCustomMusic(void)
1145 {
1146   if (!audio.music_available)
1147     return;
1148
1149   ReloadCustomMusic();
1150 }
1151
1152 void FreeSound(void *ptr)
1153 {
1154   SoundInfo *sound = (SoundInfo *)ptr;
1155
1156   if (sound == NULL)
1157     return;
1158
1159   if (sound->data_ptr)
1160   {
1161     Mix_FreeChunk(sound->data_ptr);
1162   }
1163
1164   checked_free(sound->source_filename);
1165
1166   free(sound);
1167 }
1168
1169 void FreeMusic(void *ptr)
1170 {
1171   MusicInfo *music = (MusicInfo *)ptr;
1172
1173   if (music == NULL)
1174     return;
1175
1176   if (music->data_ptr)
1177   {
1178     if (music->type == MUS_TYPE_MOD)
1179       Mix_FreeMusic(music->data_ptr);
1180     else
1181       Mix_FreeChunk(music->data_ptr);
1182   }
1183
1184   free(music);
1185 }
1186
1187 static void FreeAllMusic_NoConf(void)
1188 {
1189   int i;
1190
1191   if (Music_NoConf == NULL)
1192     return;
1193
1194   for (i = 0; i < num_music_noconf; i++)
1195     FreeMusic(Music_NoConf[i]);
1196
1197   free(Music_NoConf);
1198
1199   Music_NoConf = NULL;
1200   num_music_noconf = 0;
1201 }
1202
1203 void FreeAllSounds(void)
1204 {
1205   FreeCustomArtworkLists(sound_info);
1206 }
1207
1208 void FreeAllMusic(void)
1209 {
1210   FreeCustomArtworkLists(music_info);
1211   FreeAllMusic_NoConf();
1212 }
1213
1214 // THE STUFF ABOVE IS ONLY USED BY THE MAIN PROCESS
1215 // ============================================================================