- FD_ZERO(&sound_fdset);
- FD_SET(audio.soundserver_pipe[0], &sound_fdset);
-
- while(1) /* wait for sound playing commands from client */
- {
- struct timeval delay = { 0, 0 };
-
- FD_SET(audio.soundserver_pipe[0], &sound_fdset);
- select(audio.soundserver_pipe[0] + 1, &sound_fdset, NULL, NULL, NULL);
- if (!FD_ISSET(audio.soundserver_pipe[0], &sound_fdset))
- continue;
- if (read(audio.soundserver_pipe[0], &snd_ctrl, sizeof(snd_ctrl))
- != sizeof(snd_ctrl))
- Error(ERR_EXIT_SOUND_SERVER, "broken pipe -- no sounds");
-
- HandleSoundRequest(snd_ctrl);
-
-#if defined(AUDIO_STREAMING_DSP)
-
- while (playing_sounds && select(audio.soundserver_pipe[0] + 1,
- &sound_fdset, NULL, NULL, &delay) < 1)
- {
- FD_SET(audio.soundserver_pipe[0], &sound_fdset);
-
- SoundServer_Mixer();
- }
-
-#else /* !AUDIO_STREAMING_DSP */
-
- if (!snd_ctrl.active || IS_LOOP(snd_ctrl) ||
- (audio.device_fd = OpenAudioDevice(audio.device_name)) < 0)
- continue;
-
- InitAudioDevice(&afmt);
-
- delay.tv_sec = 0;
- delay.tv_usec = 0;
-
- while (playing_sounds && select(audio.soundserver_pipe[0] + 1,
- &sound_fdset, NULL, NULL, &delay) < 1)
- {
- int wait_percent = 90; /* wait 90% of the real playing time */
- int sample_size;
-
- FD_SET(audio.soundserver_pipe[0], &sound_fdset);
-
- sample_size = SoundServer_SimpleAudio(snd_ctrl);
-
- delay.tv_sec = 0;
- delay.tv_usec =
- ((sample_size * 10 * wait_percent) / afmt.sample_rate) * 1000;
- }
-
- CloseAudioDevice(&audio.device_fd);
-
- /* delete all sounds from playlist */
- InitPlaylist();
-
-#endif /* !AUDIO_STREAMING_DSP */
- }
-}
-#endif /* PLATFORM_UNIX */
-
-#if defined(PLATFORM_MSDOS)
-static void sound_handler(struct SoundControl snd_ctrl)
-{
- int i;
-
- if (snd_ctrl.fade_sound)
- {
- if (!playing_sounds)
- return;
-
- for (i=0; i<audio.num_channels; i++)
- if ((snd_ctrl.stop_all_sounds ||
- (i != audio.music_channel && playlist[i].nr == snd_ctrl.nr) ||
- (i == audio.music_channel && snd_ctrl.music)) &&
- !playlist[i].fade_sound)
- {
- playlist[i].fade_sound = TRUE;
- if (voice_check(playlist[i].voice))
- voice_ramp_volume(playlist[i].voice, 1000, 0);
- playlist[i].state &= ~SND_CTRL_IS_LOOP;
- }
- }
- else if (snd_ctrl.stop_all_sounds)
- {
- if (!playing_sounds)
- return;
- SoundServer_StopAllSounds();
- }
- else if (snd_ctrl.stop_sound)
- {
- if (!playing_sounds)
- return;
- SoundServer_StopSound(snd_ctrl);
- }
-
- for (i=0; i<audio.num_channels; i++)
- {
- if (!playlist[i].active || playlist[i].loop)
- continue;
-
- playlist[i].playingpos = voice_get_position(playlist[i].voice);
- playlist[i].volume = voice_get_volume(playlist[i].voice);
- if (playlist[i].playingpos == -1 || !playlist[i].volume)
- {
- deallocate_voice(playlist[i].voice);
- PlaylistRemoveSound(i);
- }
- }
-
- if (snd_ctrl.active)
- SoundServer_InsertNewSound(snd_ctrl);
-}
-#endif /* PLATFORM_MSDOS */
-
-#if 0
-#if defined(TARGET_SDL)
-static void sound_handler_SDL(struct SoundControl snd_ctrl)
-{
- /* copy sound_handler() here ... */
-}
-#endif /* TARGET_SDL */
-#endif
-
-#if !defined(PLATFORM_WIN32)
-static void SoundServer_InsertNewSound(struct 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)
- {
-#if 0
- printf("sound/music %d undefined\n", snd_ctrl.nr);
-#endif
- return;
- }
-
-#if 0
- printf("-> %d\n", playing_sounds);
-#endif
-
- if (playing_sounds == audio.num_channels)
- {
- for (i=0; i<audio.num_channels; i++)
- {
- if (playlist[i].data_ptr == NULL)
- {
-#if 1
- printf("THIS SHOULD NEVER HAPPEN! [%d]\n", i);
-#endif
-
- PlaylistRemoveSound(i);
- }
- }
- }
-
- /* if playlist is full, remove oldest sound */
- if (playing_sounds == audio.num_channels)
- {
- int longest = 0, longest_nr = 0;
-
- for (i=audio.first_sound_channel; i<audio.num_channels; i++)
- {
-#if !defined(PLATFORM_MSDOS)
- int actual = 100 * playlist[i].playingpos / playlist[i].data_len;
-#else
- int actual = playlist[i].playingpos;
-#endif
-
- if (!IS_LOOP(playlist[i]) && actual > longest)
- {
- longest = actual;
- longest_nr = i;
- }
- }
-#if defined(PLATFORM_MSDOS)
- voice_set_volume(playlist[longest_nr].voice, 0);
- deallocate_voice(playlist[longest_nr].voice);
-#endif
- PlaylistRemoveSound(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 (playlist[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 (playlist[i].nr == snd_ctrl.nr && IS_FADING(playlist[i]))
- {
- playlist[i].state &= ~SND_CTRL_FADE;
- playlist[i].volume = PSND_MAX_VOLUME;
-#if defined(PLATFORM_MSDOS)
- playlist[i].state |= SND_CTRL_LOOP;
- voice_stop_volumeramp(playlist[i].voice);
- voice_ramp_volume(playlist[i].voice, playlist[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 = 0;
-
- /* look for oldest equal sound */
- for(i=audio.first_sound_channel; i<audio.num_channels; i++)
- {
- int actual;
-
- if (!playlist[i].active || playlist[i].nr != snd_ctrl.nr)
- continue;
-
-#if !defined(PLATFORM_MSDOS)
- actual = 100 * playlist[i].playingpos / playlist[i].data_len;
-#else
- actual = playlist[i].playingpos;
-#endif
- if (actual >= longest)
- {
- longest = actual;
- longest_nr = i;
- }
- }
-
-#if defined(PLATFORM_MSDOS)
- voice_set_volume(playlist[longest_nr].voice, 0);
- deallocate_voice(playlist[longest_nr].voice);
-#endif
- PlaylistRemoveSound(longest_nr);
- }
-
- /* add new sound to playlist */
- for(i=0; i<audio.num_channels; i++)
- {
-#if 0
- printf("CHECKING CHANNEL %d FOR SOUND %d ...\n", i, snd_ctrl.nr);
-#endif
-
- /*
- if (!playlist[i].active ||
- (IS_MUSIC(snd_ctrl) && i == audio.music_channel))
- */
- if ((i == audio.music_channel && IS_MUSIC(snd_ctrl)) ||
- (i != audio.music_channel && !playlist[i].active))
- {
- snd_ctrl.data_ptr = snd_info->data_ptr;
- snd_ctrl.data_len = snd_info->data_len;
- snd_ctrl.format = snd_info->format;
-
- snd_ctrl.playingpos = 0;
- snd_ctrl.playingtime = 0;
-
-#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 && playlist[i].active)
- {
- printf("THIS SHOULD NEVER HAPPEN! [adding music twice]\n");
-
-#if 1
- PlaylistRemoveSound(i);
-#endif
- }
-#endif
-
- playlist[i] = snd_ctrl;
- playing_sounds++;
-
-#if 0
- printf("NEW SOUND %d ADDED TO PLAYLIST\n", snd_ctrl.nr);
-#endif
-
-#if defined(PLATFORM_MSDOS)
- playlist[i].voice = allocate_voice((SAMPLE *)playlist[i].data_ptr);
-
- if (snd_ctrl.loop)
- voice_set_playmode(playlist[i].voice, PLAYMODE_LOOP);
-
- voice_set_volume(playlist[i].voice, snd_ctrl.volume);
- voice_set_pan(playlist[i].voice, snd_ctrl.stereo);
- voice_start(playlist[i].voice);
-#endif
- break;
- }
- }
-}
-#endif /* !PLATFORM_WIN32 */
-
-/*
-void SoundServer_FadeSound(int nr)
-{
- int i;
-
- if (!playing_sounds)
- return;
-
- for(i=0;i<audio.num_channels;i++)
- if (snd_ctrl.stop_all_sounds || playlist[i].nr == snd_ctrl.nr)
- playlist[i].fade_sound = TRUE;
-}
-*/
-
-#if !defined(PLATFORM_WIN32)
-#if defined(PLATFORM_MSDOS)
-static void SoundServer_StopSound(struct SoundControl snd_ctrl)
-{
- int nr = snd_ctrl.nr;
- int i;
-
- if (!playing_sounds)
- return;
-
- for(i=0; i<audio.num_channels; i++)
- {
- if ((i == audio.music_channel && snd_ctrl.music) ||
- (i != audio.music_channel && playlist[i].nr == nr))
- {
-#if defined(PLATFORM_MSDOS)
- voice_set_volume(playlist[i].voice, 0);
- deallocate_voice(playlist[i].voice);
-#endif
- PlaylistRemoveSound(i);
- }
- }
-
-#if !defined(PLATFORM_MSDOS)
- if (!playing_sounds)
- close(audio.device_fd);
-#endif
-}
-
-static void SoundServer_StopAllSounds()
-{
- int i;
-
- for(i=0; i<audio.num_channels; i++)
- {
-#if defined(PLATFORM_MSDOS)
- voice_set_volume(playlist[i].voice, 0);
- deallocate_voice(playlist[i].voice);
-#endif
- PlaylistRemoveSound(i);
- }
- playing_sounds = 0;
-
-#if !defined(PLATFORM_MSDOS)
- close(audio.device_fd);
-#endif
-}
-#endif /* PLATFORM_MSDOS */
-#endif /* !PLATFORM_WIN32 */
-
-
-/* ------------------------------------------------------------------------- */
-/* platform dependant audio initialization code */
-/* ------------------------------------------------------------------------- */
-
-#if defined(AUDIO_LINUX_IOCTL)
-static void InitAudioDevice_Linux(struct AudioFormatInfo *afmt)
-{
- /* "ioctl()" expects pointer to 'int' value for stereo flag
- (boolean is defined as 'char', which will not work here) */
- unsigned int fragment_spec = 0;
- int fragment_size_query;
- int stereo = TRUE;
- struct
- {
- int format_ioctl;
- int format_result;
- }
- formats[] =
- {
- /* supported audio format in preferred order */
- { AFMT_S16_LE, AUDIO_FORMAT_S16 | AUDIO_FORMAT_LE },
- { AFMT_S16_BE, AUDIO_FORMAT_S16 | AUDIO_FORMAT_BE },
- { AFMT_U8, AUDIO_FORMAT_U8 },
- { -1, -1 }
- };
- int i;
-
- /* determine logarithm (log2) of the fragment size */
- while ((1 << fragment_spec) < afmt->fragment_size)
- fragment_spec++;
-
- /* use two fragments (play one fragment, prepare the other);
- one fragment would result in interrupted audio output, more
- than two fragments would raise audio output latency to much */
- fragment_spec |= 0x00020000;
-
- /* Example for fragment specification:
- - 2 buffers / 512 bytes (giving 1/16 second resolution for 8 kHz)
- - (with stereo the effective buffer size will shrink to 256)
- => fragment_size = 0x00020009 */
-
- if (ioctl(audio.device_fd, SNDCTL_DSP_SETFRAGMENT, &fragment_spec) < 0)
- Error(ERR_EXIT_SOUND_SERVER,
- "cannot set fragment size of /dev/dsp -- no sounds");
-
- i = 0;
- afmt->format = 0;
- while (formats[i].format_result != -1)
- {
- unsigned int audio_format = formats[i].format_ioctl;
- if (ioctl(audio.device_fd, SNDCTL_DSP_SETFMT, &audio_format) == 0)
- {
- afmt->format = formats[i].format_result;
- break;
- }
- }