-/* ------------------------------------------------------------------------- */
-/* functions for native (non-SDL) Unix audio/mixer support */
-/* ------------------------------------------------------------------------- */
-
-#if defined(AUDIO_UNIX_NATIVE)
-
-static int OpenAudioDevice(char *audio_device_name)
-{
- int audio_device_fd;
-
- /* check if desired audio device is accessible */
- if (access(audio_device_name, W_OK) != 0)
- return -1;
-
- /* try to open audio device in non-blocking mode */
- if ((audio_device_fd = open(audio_device_name, O_WRONLY | O_NONBLOCK)) < 0)
- return audio_device_fd;
-
- /* re-open audio device in blocking mode */
- close(audio_device_fd);
- audio_device_fd = open(audio_device_name, O_WRONLY);
-
- return audio_device_fd;
-}
-
-static void CloseAudioDevice(int *audio_device_fd)
-{
- if (*audio_device_fd == 0)
- return;
-
- close(*audio_device_fd);
- *audio_device_fd = -1;
-}
-
-static boolean TestAudioDevices(void)
-{
- static char *audio_device_name[] =
- {
- DEVICENAME_DSP,
- DEVICENAME_SOUND_DSP,
- DEVICENAME_AUDIO
- };
- int audio_device_fd = -1;
- int i;
-
- /* look for available audio devices, starting with preferred ones */
- for (i = 0; i < sizeof(audio_device_name)/sizeof(char *); i++)
- if ((audio_device_fd = OpenAudioDevice(audio_device_name[i])) >= 0)
- break;
-
- if (audio_device_fd < 0)
- {
- Error(ERR_WARN, "cannot open audio device -- no sound");
- return FALSE;
- }
-
- close(audio_device_fd);
-
- audio.device_name = audio_device_name[i];
-
- return TRUE;
-}
-
-static boolean ForkAudioProcess(void)
-{
- if (pipe(audio.mixer_pipe) < 0)
- {
- Error(ERR_WARN, "cannot create pipe -- no sounds");
- return FALSE;
- }
-
- if ((audio.mixer_pid = fork()) < 0)
- {
- Error(ERR_WARN, "cannot create sound server process -- no sounds");
- return FALSE;
- }
-
- if (audio.mixer_pid == 0) /* we are the child process */
- audio.mixer_pid = getpid();
-
- if (IS_CHILD_PROCESS())
- Mixer_Main(); /* this function never returns */
- else
- close(audio.mixer_pipe[0]); /* no reading from pipe needed */
-
- return TRUE;
-}
-
-void UnixOpenAudio(void)
-{
- if (!TestAudioDevices())
- return;
-
- audio.sound_available = TRUE;
- audio.sound_enabled = TRUE;
-
-#if defined(AUDIO_STREAMING_DSP)
- audio.music_available = TRUE;
- audio.loops_available = TRUE;
-#endif
-
- audio.num_channels = NUM_MIXER_CHANNELS;
- audio.music_channel = MUSIC_CHANNEL;
- audio.first_sound_channel = FIRST_SOUND_CHANNEL;
-}
-
-void UnixCloseAudio(void)
-{
- if (audio.device_fd)
- close(audio.device_fd);
-
- if (IS_PARENT_PROCESS() && HAS_CHILD_PROCESS())
- kill(audio.mixer_pid, SIGTERM);
-}
-
-
-/* ------------------------------------------------------------------------- */
-/* functions for platform specific audio device initialization */
-/* ------------------------------------------------------------------------- */
-
-#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 = -1;
- 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 audio device -- 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;
- }
- }
-
- if (afmt->format == 0) /* no supported audio format found */
- Error(ERR_EXIT_SOUND_SERVER,
- "cannot set audio format of audio device -- no sounds");
-
- /* try if we can use stereo sound */
- afmt->stereo = TRUE;
- if (ioctl(audio.device_fd, SNDCTL_DSP_STEREO, &stereo) < 0)
- afmt->stereo = FALSE;
-
- if (ioctl(audio.device_fd, SNDCTL_DSP_SPEED, &afmt->sample_rate) < 0)
- Error(ERR_EXIT_SOUND_SERVER,
- "cannot set sample rate of audio device -- no sounds");
-
- /* get the real fragmentation size; this should return 512 */
- if (ioctl(audio.device_fd, SNDCTL_DSP_GETBLKSIZE, &fragment_size_query) < 0)
- Error(ERR_EXIT_SOUND_SERVER,
- "cannot get fragment size of audio device -- no sounds");
- if (fragment_size_query != afmt->fragment_size)
- Error(ERR_EXIT_SOUND_SERVER,
- "cannot set fragment size of audio device -- no sounds");
-}
-#endif /* AUDIO_LINUX_IOCTL */
-
-#if defined(PLATFORM_NETBSD)
-static void InitAudioDevice_NetBSD(struct AudioFormatInfo *afmt)
-{
- audio_info_t a_info;
- boolean stereo = TRUE;
-
- AUDIO_INITINFO(&a_info);
- a_info.play.encoding = AUDIO_ENCODING_LINEAR8;
- a_info.play.precision = 8;
- a_info.play.channels = 2;
- a_info.play.sample_rate = afmt->sample_rate;
- a_info.blocksize = afmt->fragment_size;
-
- afmt->format = AUDIO_FORMAT_U8;
- afmt->stereo = TRUE;
-
- if (ioctl(audio.device_fd, AUDIO_SETINFO, &a_info) < 0)
- {
- /* try to disable stereo */
- a_info.play.channels = 1;
-
- afmt->stereo = FALSE;
-
- if (ioctl(audio.device_fd, AUDIO_SETINFO, &a_info) < 0)
- Error(ERR_EXIT_SOUND_SERVER,
- "cannot set sample rate of audio device -- no sounds");
- }
-}
-#endif /* PLATFORM_NETBSD */
-
-#if defined(PLATFORM_HPUX)
-static void InitAudioDevice_HPUX(struct AudioFormatInfo *afmt)
-{
- struct audio_describe ainfo;
- int audio_ctl;
-
- audio_ctl = open("/dev/audioCtl", O_WRONLY | O_NDELAY);
- if (audio_ctl == -1)
- Error(ERR_EXIT_SOUND_SERVER, "cannot open audio device -- no sounds");
-
- if (ioctl(audio_ctl, AUDIO_DESCRIBE, &ainfo) == -1)
- Error(ERR_EXIT_SOUND_SERVER, "no audio info -- no sounds");
-
- if (ioctl(audio_ctl, AUDIO_SET_DATA_FORMAT, AUDIO_FORMAT_ULAW) == -1)
- Error(ERR_EXIT_SOUND_SERVER, "ulaw audio not available -- no sounds");
-
- ioctl(audio_ctl, AUDIO_SET_CHANNELS, 1);
- ioctl(audio_ctl, AUDIO_SET_SAMPLE_RATE, 8000);
-
- afmt->format = AUDIO_FORMAT_U8;
- afmt->stereo = FALSE;
- afmt->sample_rate = 8000;
-
- close(audio_ctl);
-}
-#endif /* PLATFORM_HPUX */
-
-static void InitAudioDevice(struct AudioFormatInfo *afmt)
-{
- afmt->stereo = TRUE;
- afmt->format = AUDIO_FORMAT_UNKNOWN;
- afmt->sample_rate = DEFAULT_AUDIO_SAMPLE_RATE;
- afmt->fragment_size = DEFAULT_AUDIO_FRAGMENT_SIZE;
-
-#if defined(AUDIO_LINUX_IOCTL)
- InitAudioDevice_Linux(afmt);
-#elif defined(PLATFORM_NETBSD)
- InitAudioDevice_NetBSD(afmt);
-#elif defined(PLATFORM_HPUX)
- InitAudioDevice_HPUX(afmt);
-#else
- /* generic /dev/audio stuff might be placed here */
-#endif
-}
-
-
-/* ------------------------------------------------------------------------- */
-/* 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");
-
- clear_mem(&snd_ctrl, sizeof(SoundControl)); /* to make valgrind happy */
-
- 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);
-
- checked_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));
-
- 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 ||
- 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;
-}