+ /* "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;