+
+/* ------------------------------------------------------------------------- */
+/* functions for communication between main process and sound mixer process */
+/* ------------------------------------------------------------------------- */
+
+static void SendSoundControlToMixerProcess(SoundControl *snd_ctrl)
+{
+ if (IS_CHILD_PROCESS())
+ return;
+
+ if (write(audio.mixer_pipe[1], snd_ctrl, sizeof(SoundControl)) < 0)
+ {
+ Error(ERR_WARN, "cannot pipe to child process -- no sounds");
+ audio.sound_available = audio.sound_enabled = FALSE;
+ return;
+ }
+}
+
+static void ReadSoundControlFromMainProcess(SoundControl *snd_ctrl)
+{
+ if (IS_PARENT_PROCESS())
+ return;
+
+ if (read(audio.mixer_pipe[0], snd_ctrl, sizeof(SoundControl))
+ != sizeof(SoundControl))
+ Error(ERR_EXIT_SOUND_SERVER, "broken pipe -- no sounds");
+}
+
+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;
+ boolean override_level_artwork = (type == SND_CTRL_RELOAD_SOUNDS ?
+ setup.override_level_sounds :
+ setup.override_level_music);
+
+ if (IS_CHILD_PROCESS())
+ return;
+
+ if (leveldir_current == NULL) /* should never happen */
+ Error(ERR_EXIT, "leveldir_current == NULL");
+
+ snd_ctrl.active = FALSE;
+ snd_ctrl.state = type;
+ snd_ctrl.data_len = strlen(set_identifier) + 1;
+
+ if (write(audio.mixer_pipe[1], &snd_ctrl,
+ sizeof(snd_ctrl)) < 0 ||
+ write(audio.mixer_pipe[1], set_identifier,
+ snd_ctrl.data_len) < 0 ||
+ write(audio.mixer_pipe[1], &override_level_artwork,
+ sizeof(boolean)) < 0 ||
+ write(audio.mixer_pipe[1], leveldir_current,
+ sizeof(TreeInfo)) < 0 ||
+ write(audio.mixer_pipe[1], ti,
+ sizeof(TreeInfo)) < 0 ||
+ write(audio.mixer_pipe[1], &str_size1,
+ sizeof(unsigned long)) < 0 ||
+ write(audio.mixer_pipe[1], &str_size2,
+ sizeof(unsigned long)) < 0 ||
+ write(audio.mixer_pipe[1], &str_size3,
+ sizeof(unsigned long)) < 0 ||
+ write(audio.mixer_pipe[1], &str_size4,
+ sizeof(unsigned long)) < 0 ||
+ write(audio.mixer_pipe[1], &str_size5,
+ sizeof(unsigned long)) < 0 ||
+ write(audio.mixer_pipe[1], leveldir_current->fullpath,
+ str_size1) < 0 ||
+ write(audio.mixer_pipe[1], leveldir_current->sounds_path,
+ str_size2) < 0 ||
+ write(audio.mixer_pipe[1], leveldir_current->music_path,
+ str_size3) < 0 ||
+ write(audio.mixer_pipe[1], ti->basepath,
+ str_size4) < 0 ||
+ write(audio.mixer_pipe[1], ti->fullpath,
+ str_size5) < 0)
+ {
+ Error(ERR_WARN, "cannot pipe to child process -- no sounds");
+ audio.sound_available = audio.sound_enabled = FALSE;
+ return;
+ }
+}
+
+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;
+ static char *set_identifier = NULL;
+ boolean *override_level_artwork = (snd_ctrl->state & SND_CTRL_RELOAD_SOUNDS ?
+ &setup.override_level_sounds :
+ &setup.override_level_music);
+
+ if (set_identifier)
+ free(set_identifier);
+
+ set_identifier = checked_malloc(snd_ctrl->data_len);
+
+ if (leveldir_current == NULL)
+ leveldir_current = checked_calloc(sizeof(TreeInfo));
+
+ 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);
+
+ if (read(audio.mixer_pipe[0], set_identifier,
+ snd_ctrl->data_len) != snd_ctrl->data_len ||
+ read(audio.mixer_pipe[0], override_level_artwork,
+ sizeof(boolean)) != sizeof(boolean) ||
+ read(audio.mixer_pipe[0], leveldir_current,
+ sizeof(TreeInfo)) != sizeof(TreeInfo) ||
+ read(audio.mixer_pipe[0], ti,
+ sizeof(TreeInfo)) != sizeof(TreeInfo) ||
+ read(audio.mixer_pipe[0], &str_size1,
+ sizeof(unsigned long)) != sizeof(unsigned long) ||
+ read(audio.mixer_pipe[0], &str_size2,
+ sizeof(unsigned long)) != sizeof(unsigned long) ||
+ read(audio.mixer_pipe[0], &str_size3,
+ sizeof(unsigned long)) != sizeof(unsigned long) ||
+ read(audio.mixer_pipe[0], &str_size4,
+ sizeof(unsigned long)) != sizeof(unsigned long) ||
+ read(audio.mixer_pipe[0], &str_size5,
+ sizeof(unsigned long)) != sizeof(unsigned long))
+ Error(ERR_EXIT_SOUND_SERVER, "broken pipe -- no sounds");
+
+ leveldir_current->fullpath = checked_calloc(str_size1);
+ leveldir_current->sounds_path = checked_calloc(str_size2);
+ leveldir_current->music_path = checked_calloc(str_size3);
+ ti->basepath = checked_calloc(str_size4);
+ ti->fullpath = checked_calloc(str_size5);
+
+ if (read(audio.mixer_pipe[0], leveldir_current->fullpath,
+ str_size1) != str_size1 ||
+ read(audio.mixer_pipe[0], leveldir_current->sounds_path,
+ str_size2) != str_size2 ||
+ read(audio.mixer_pipe[0], leveldir_current->music_path,
+ str_size3) != str_size3 ||
+ read(audio.mixer_pipe[0], ti->basepath,
+ str_size4) != str_size4 ||
+ read(audio.mixer_pipe[0], ti->fullpath,
+ str_size5) != str_size5)
+ Error(ERR_EXIT_SOUND_SERVER, "broken pipe -- no sounds");
+
+ if (snd_ctrl->state & SND_CTRL_RELOAD_SOUNDS)
+ artwork.snd_current_identifier = set_identifier;
+ else
+ artwork.mus_current_identifier = set_identifier;
+}
+
+#endif /* AUDIO_UNIX_NATIVE */
+
+
+/* ------------------------------------------------------------------------- */
+/* mixer functions */
+/* ------------------------------------------------------------------------- */
+
+void Mixer_InitChannels()
+{
+ int i;
+
+ for(i=0; i<audio.num_channels; i++)
+ mixer[i].active = FALSE;
+ mixer_active_channels = 0;
+}
+
+static void Mixer_ResetChannelExpiration(int channel)
+{
+ mixer[channel].playing_starttime = Counter();
+
+#if defined(TARGET_SDL)
+ if (IS_LOOP(mixer[channel]) && !IS_MUSIC(mixer[channel]))
+ Mix_ExpireChannel(channel, SOUND_LOOP_EXPIRATION_TIME);
+#endif
+}
+
+static boolean Mixer_ChannelExpired(int channel)
+{
+ if (!mixer[channel].active)
+ return TRUE;
+
+ if (IS_LOOP(mixer[channel]) && !IS_MUSIC(mixer[channel]) &&
+ DelayReached(&mixer[channel].playing_starttime,
+ SOUND_LOOP_EXPIRATION_TIME))
+ return TRUE;
+
+#if defined(TARGET_SDL)
+
+ if (!Mix_Playing(channel))
+ return TRUE;
+
+#elif defined(TARGET_ALLEGRO)
+
+ mixer[channel].playing_pos = voice_get_position(mixer[channel].voice);
+ mixer[channel].volume = voice_get_volume(mixer[channel].voice);
+
+ /* sound sample has completed playing or was completely faded out */
+ if (mixer[channel].playing_pos == -1 || mixer[channel].volume == 0)
+ return TRUE;
+
+#endif /* TARGET_ALLEGRO */
+
+ return FALSE;
+}
+
+static boolean Mixer_AllocateChannel(int channel)
+{
+#if defined(TARGET_ALLEGRO)
+ mixer[channel].voice = allocate_voice((SAMPLE *)mixer[channel].data_ptr);
+ if (mixer[channel].voice < 0)
+ return FALSE;
+#endif
+
+ return TRUE;
+}
+
+static void Mixer_SetChannelProperties(int channel)
+{
+#if defined(TARGET_SDL)
+ Mix_Volume(channel, mixer[channel].volume);
+ Mix_SetPanning(channel,
+ SOUND_VOLUME_LEFT(mixer[channel].stereo_position),
+ SOUND_VOLUME_RIGHT(mixer[channel].stereo_position));
+#elif defined(TARGET_ALLEGRO)
+ voice_set_volume(mixer[channel].voice, mixer[channel].volume);
+ voice_set_pan(mixer[channel].voice, mixer[channel].stereo_position);
+#endif
+}
+
+static void Mixer_StartChannel(int channel)
+{
+#if defined(TARGET_SDL)
+ Mix_PlayChannel(channel, mixer[channel].data_ptr,
+ IS_LOOP(mixer[channel]) ? -1 : 0);
+#elif defined(TARGET_ALLEGRO)
+ if (IS_LOOP(mixer[channel]))
+ voice_set_playmode(mixer[channel].voice, PLAYMODE_LOOP);
+
+ voice_start(mixer[channel].voice);
+#endif
+}
+
+static void Mixer_PlayChannel(int channel)
+{
+ /* start with inactive channel in case something goes wrong */
+ mixer[channel].active = FALSE;
+
+ if (mixer[channel].type != MUS_TYPE_WAV)
+ return;
+
+ if (!Mixer_AllocateChannel(channel))
+ return;
+
+ Mixer_SetChannelProperties(channel);
+ Mixer_StartChannel(channel);
+
+ Mixer_ResetChannelExpiration(channel);
+
+ mixer[channel].playing_pos = 0;
+ mixer[channel].active = TRUE;
+ mixer_active_channels++;
+}
+
+static void Mixer_PlayMusicChannel()
+{
+ Mixer_PlayChannel(audio.music_channel);
+
+#if defined(TARGET_SDL)
+ if (mixer[audio.music_channel].type != MUS_TYPE_WAV)
+ {
+ /* 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);
+ }
+#endif
+}
+
+static void Mixer_StopChannel(int channel)
+{
+ if (!mixer[channel].active)
+ return;
+
+#if defined(TARGET_SDL)
+ Mix_HaltChannel(channel);
+#elif defined(TARGET_ALLEGRO)
+ voice_set_volume(mixer[channel].voice, 0);
+ deallocate_voice(mixer[channel].voice);
+#endif
+
+ mixer[channel].active = FALSE;
+ mixer_active_channels--;
+}
+
+static void Mixer_StopMusicChannel()
+{
+ Mixer_StopChannel(audio.music_channel);
+
+#if defined(TARGET_SDL)
+ Mix_HaltMusic();
+#endif
+}
+
+static void Mixer_FadeChannel(int channel)
+{
+ if (!mixer[channel].active)
+ return;
+
+ mixer[channel].state |= SND_CTRL_FADE;
+
+#if defined(TARGET_SDL)
+ Mix_FadeOutChannel(channel, SOUND_FADING_INTERVAL);
+#elif defined(TARGET_ALLEGRO)
+ if (voice_check(mixer[channel].voice))
+ voice_ramp_volume(mixer[channel].voice, SOUND_FADING_INTERVAL, 0);
+#endif
+}
+
+static void Mixer_FadeMusicChannel()
+{
+ Mixer_FadeChannel(audio.music_channel);
+
+#if defined(TARGET_SDL)
+ Mix_FadeOutMusic(SOUND_FADING_INTERVAL);