+ for(i=0; i<audio.num_channels; i++)
+ mixer[i].active = FALSE;
+ mixer_active_channels = 0;
+}
+
+static void Mixer_PlayChannel(int channel)
+{
+ /* start with inactive channel in case something goes wrong */
+ mixer[channel].active = FALSE;
+
+ if (IS_MUSIC_MODULE(mixer[channel]))
+ return;
+
+ mixer[channel].playingpos = 0;
+ mixer[channel].playingtime = 0;
+
+#if defined(TARGET_SDL)
+ Mix_Volume(channel, SOUND_MAX_VOLUME);
+ Mix_PlayChannel(channel, mixer[channel].data_ptr,
+ IS_LOOP(mixer[channel]) ? -1 : 0);
+#elif defined(PLATFORM_MSDOS)
+ mixer[channel].voice = allocate_voice((SAMPLE *)mixer[channel].data_ptr);
+ if (mixer[channel].voice < 0)
+ return;
+
+ if (IS_LOOP(mixer[channel]))
+ voice_set_playmode(mixer[channel].voice, PLAYMODE_LOOP);
+
+ voice_set_volume(mixer[channel].voice, mixer[channel].volume);
+ voice_set_pan(mixer[channel].voice, mixer[channel].stereo);
+ voice_start(mixer[channel].voice);
+#endif
+
+ mixer[channel].active = TRUE;
+ mixer_active_channels++;
+}
+
+static void Mixer_PlayMusicChannel()
+{
+ Mixer_PlayChannel(audio.music_channel);
+
+#if defined(TARGET_SDL)
+ if (IS_MUSIC_MODULE(mixer[audio.music_channel]))
+ {
+ /* 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_active_channels || !mixer[channel].active)
+ return;
+
+#if defined(TARGET_SDL)
+ Mix_HaltChannel(channel);
+#elif defined(PLATFORM_MSDOS)
+ 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)
+{
+ mixer[channel].state |= SND_CTRL_FADE;
+
+#if defined(TARGET_SDL)
+ Mix_FadeOutChannel(channel, SOUND_FADING_INTERVAL);
+#elif defined(PLATFORM_MSDOS)
+ if (voice_check(mixer[channel].voice))
+ voice_ramp_volume(mixer[channel].voice, SOUND_FADING_INTERVAL, 0);
+ mixer[channel].state &= ~SND_CTRL_IS_LOOP;
+#endif
+}
+
+static void Mixer_FadeMusicChannel()
+{
+ Mixer_FadeChannel(audio.music_channel);
+
+#if defined(TARGET_SDL)
+ Mix_FadeOutMusic(SOUND_FADING_INTERVAL);
+#endif
+}
+
+static void Mixer_InsertSound(SoundControl snd_ctrl)
+{
+ SoundInfo *snd_info;
+ int i, k;
+
+#if 0
+ printf("NEW SOUND %d HAS ARRIVED [%d]\n", snd_ctrl.nr, num_sounds);
+#endif
+
+ if (IS_MUSIC(snd_ctrl))
+ snd_ctrl.nr = snd_ctrl.nr % num_music;
+ else if (snd_ctrl.nr >= num_sounds)
+ return;
+
+ snd_info = (IS_MUSIC(snd_ctrl) ? Music[snd_ctrl.nr] : Sound[snd_ctrl.nr]);
+ if (snd_info == NULL)
+ return;
+
+ /* copy sound sample and format information */
+ snd_ctrl.data_ptr = snd_info->data_ptr;
+ snd_ctrl.data_len = snd_info->data_len;
+ snd_ctrl.format = snd_info->format;
+
+ if (IS_MUSIC(snd_ctrl))
+ {
+ mixer[audio.music_channel] = snd_ctrl;
+
+ Mixer_PlayChannel(audio.music_channel);
+
+ return;
+ }
+
+#if 0
+ printf("-> %d\n", mixer_active_channels);
+#endif
+
+ if (mixer_active_channels == audio.num_channels)
+ {
+ for (i=0; i<audio.num_channels; i++)
+ {
+ if (mixer[i].data_ptr == NULL)
+ {
+#if 1
+ printf("THIS SHOULD NEVER HAPPEN! [%d]\n", i);
+#endif
+
+ Mixer_StopChannel(i);
+ }
+ }
+ }
+
+ /* if mixer is full, remove oldest sound */
+ if (mixer_active_channels == audio.num_channels)
+ {
+ int longest = 0, longest_nr = audio.first_sound_channel;
+
+ for (i=audio.first_sound_channel; i<audio.num_channels; i++)
+ {
+#if !defined(PLATFORM_MSDOS)
+ int actual = 100 * mixer[i].playingpos / mixer[i].data_len;
+#else
+ int actual = mixer[i].playingpos;
+#endif
+
+ if (!IS_LOOP(mixer[i]) && actual > longest)
+ {
+ longest = actual;
+ longest_nr = i;
+ }
+ }
+
+ Mixer_StopChannel(longest_nr);
+ }
+
+ /* check if sound is already being played (and how often) */
+ for (k=0, i=audio.first_sound_channel; i<audio.num_channels; i++)
+ if (mixer[i].nr == snd_ctrl.nr)
+ k++;
+
+ /* restart loop sounds only if they are just fading out */
+ if (k >= 1 && IS_LOOP(snd_ctrl))
+ {
+ for(i=audio.first_sound_channel; i<audio.num_channels; i++)
+ {
+ if (mixer[i].nr == snd_ctrl.nr && IS_FADING(mixer[i]))
+ {
+ mixer[i].state &= ~SND_CTRL_FADE;
+ mixer[i].volume = PSND_MAX_VOLUME;
+#if defined(PLATFORM_MSDOS)
+ mixer[i].state |= SND_CTRL_LOOP;
+ voice_stop_volumeramp(mixer[i].voice);
+ voice_ramp_volume(mixer[i].voice, mixer[i].volume, 1000);
+#endif
+ }
+ }
+
+ return;
+ }
+
+ /* don't play sound more than n times simultaneously (with n == 2 for now) */
+ if (k >= 2)
+ {
+ int longest = 0, longest_nr = audio.first_sound_channel;
+
+ /* look for oldest equal sound */
+ for(i=audio.first_sound_channel; i<audio.num_channels; i++)
+ {
+ int actual;
+
+ if (!mixer[i].active || mixer[i].nr != snd_ctrl.nr)
+ continue;
+
+#if !defined(PLATFORM_MSDOS)
+ actual = 100 * mixer[i].playingpos / mixer[i].data_len;
+#else
+ actual = mixer[i].playingpos;
+#endif
+ if (actual >= longest)
+ {
+ longest = actual;
+ longest_nr = i;
+ }
+ }
+
+ Mixer_StopChannel(longest_nr);
+ }
+
+ /* add new sound to mixer */
+ for(i=0; i<audio.num_channels; i++)
+ {
+#if 0
+ printf("CHECKING CHANNEL %d FOR SOUND %d ...\n", i, snd_ctrl.nr);
+#endif
+
+ /*
+ if (!mixer[i].active ||
+ (IS_MUSIC(snd_ctrl) && i == audio.music_channel))
+ */
+ if ((i == audio.music_channel && IS_MUSIC(snd_ctrl)) ||
+ (i != audio.music_channel && !mixer[i].active))
+ {
+#if 0
+ printf("ADDING NEW SOUND %d TO MIXER\n", snd_ctrl.nr);
+#endif
+
+#if 1
+ if (snd_info->data_len == 0)
+ {
+ printf("THIS SHOULD NEVER HAPPEN! [snd_info->data_len == 0]\n");
+ }
+#endif
+
+#if 1
+ if (IS_MUSIC(snd_ctrl) && i == audio.music_channel && mixer[i].active)
+ {
+ printf("THIS SHOULD NEVER HAPPEN! [adding music twice]\n");
+
+#if 1
+ Mixer_StopChannel(i);
+#endif
+ }
+#endif
+
+#if 0
+ snd_ctrl.data_ptr = snd_info->data_ptr;
+ snd_ctrl.data_len = snd_info->data_len;
+ snd_ctrl.format = snd_info->format;
+#endif
+
+ mixer[i] = snd_ctrl;
+
+ Mixer_PlayChannel(i);
+
+ break;
+ }
+ }
+}
+
+static void HandleSoundRequest(SoundControl snd_ctrl)
+{
+ int i;
+
+#if defined(AUDIO_UNIX_NATIVE)
+ if (IS_PARENT_PROCESS(audio.mixer_pid))
+ {
+ SendSoundControlToMixerProcess(&snd_ctrl);
+ return;
+ }
+#elif defined(TARGET_ALLEGRO)
+ for (i=0; i<audio.num_channels; i++)
+ {
+ if (!mixer[i].active || IS_LOOP(mixer[i]))
+ continue;
+
+ mixer[i].playingpos = voice_get_position(mixer[i].voice);
+ mixer[i].volume = voice_get_volume(mixer[i].voice);
+
+ if (mixer[i].playingpos == -1 || mixer[i].volume == 0)
+ Mixer_StopChannel(i);
+ }
+#endif /* TARGET_ALLEGRO */
+