/***********************************************************
* Artsoft Retro-Game Library *
*----------------------------------------------------------*
-* (c) 1994-2000 Artsoft Entertainment *
+* (c) 1994-2001 Artsoft Entertainment *
* Holger Schemel *
* Detmolder Strasse 189 *
* 33604 Bielefeld *
* sound.c *
***********************************************************/
+#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
+#include <dirent.h>
+#include <signal.h>
+#include "system.h"
#include "sound.h"
#include "misc.h"
-static int num_sounds = 0;
+static int num_sounds = 0, num_music = 0;
static struct SampleInfo *Sound = NULL;
+#if defined(TARGET_SDL)
+static int num_mods = 0;
+static struct SampleInfo *Mod = NULL;
+#endif
/*** THE STUFF BELOW IS ONLY USED BY THE SOUND SERVER CHILD PROCESS ***/
#endif
#if defined(PLATFORM_UNIX)
-int OpenAudioDevice(char *audio_device_name)
+static int OpenAudioDevice(char *audio_device_name)
{
int audio_fd;
return audio_fd;
}
-void UnixOpenAudio(struct AudioSystemInfo *audio)
+static boolean TestAudioDevices(void)
{
static char *audio_device_name[] =
{
if (audio_fd < 0)
{
Error(ERR_WARN, "cannot open audio device - no sound");
- return;
+ return FALSE;
}
close(audio_fd);
- audio->device_name = audio_device_name[i];
- audio->sound_available = TRUE;
- audio->sound_enabled = TRUE;
+ audio.device_name = audio_device_name[i];
+
+ return TRUE;
+}
+
+#if !defined(TARGET_SDL)
+static boolean ForkAudioProcess(void)
+{
+ if (pipe(audio.soundserver_pipe) < 0)
+ {
+ Error(ERR_WARN, "cannot create pipe - no sounds");
+ return FALSE;
+ }
+
+ if ((audio.soundserver_pid = fork()) < 0)
+ {
+ Error(ERR_WARN, "cannot create sound server process - no sounds");
+ return FALSE;
+ }
+
+ if (audio.soundserver_pid == 0) /* we are child */
+ {
+ SoundServer();
+
+ /* never reached */
+ exit(0);
+ }
+ else /* we are parent */
+ close(audio.soundserver_pipe[0]); /* no reading from pipe needed */
+
+ return TRUE;
+}
+#endif
+
+void UnixOpenAudio(void)
+{
+ if (!TestAudioDevices())
+ return;
+
+ audio.sound_available = TRUE;
+ audio.sound_enabled = TRUE;
#if defined(AUDIO_STREAMING_DSP)
- audio->loops_available = TRUE;
+ audio.music_available = TRUE;
+ audio.loops_available = TRUE;
#endif
}
-void UnixCloseAudio(struct AudioSystemInfo *audio)
+void UnixCloseAudio(void)
{
- if (audio->device_fd)
- close(audio->device_fd);
-}
+ if (audio.device_fd)
+ close(audio.device_fd);
+ if (audio.soundserver_pid)
+ kill(audio.soundserver_pid, SIGTERM);
+}
#endif /* PLATFORM_UNIX */
-void SoundServer()
+void InitPlaylist(void)
{
int i;
+
+ for(i=0;i<MAX_SOUNDS_PLAYING;i++)
+ playlist[i] = emptySoundControl;
+ playing_sounds = 0;
+}
+
+void StartSoundserver(void)
+{
+ if (!audio.sound_available)
+ return;
+
+#if defined(PLATFORM_UNIX) && !defined(TARGET_SDL)
+ if (!ForkAudioProcess())
+ audio.sound_available = FALSE;
+#endif
+}
+
#if defined(PLATFORM_UNIX)
+void SoundServer(void)
+{
+ int i;
+
struct SoundControl snd_ctrl;
fd_set sound_fdset;
close(audio.soundserver_pipe[1]); /* no writing into pipe needed */
-#endif
- for(i=0;i<MAX_SOUNDS_PLAYING;i++)
- playlist[i] = emptySoundControl;
- playing_sounds = 0;
+ InitPlaylist();
-#if defined(PLATFORM_UNIX)
stereo_volume[PSND_MAX_LEFT2RIGHT] = 0;
for(i=0;i<PSND_MAX_LEFT2RIGHT;i++)
stereo_volume[i] =
byte *sample_ptr;
long sample_size;
static long max_sample_size = 0;
- static long fragment_size = 0;
- /* Even if the stereo flag is used as being boolean, it must be
- defined as an integer, else 'ioctl()' will fail! */
+ static long fragment_size = DEFAULT_AUDIO_FRAGMENT_SIZE;
+ int sample_rate = DEFAULT_AUDIO_SAMPLE_RATE;
int stereo = TRUE;
-#if 0
- int sample_rate = 8000;
-#else
- int sample_rate = 22050;
-#endif
+ /* 'ioctl()' expects pointer to integer value for stereo flag
+ (boolean is defined as 'char', which will not work here) */
if (playing_sounds ||
(audio.device_fd = OpenAudioDevice(audio.device_name)) >= 0)
{
if (!playing_sounds) /* we just opened the audio device */
{
- /* 2 buffers / 512 bytes, giving 1/16 second resolution */
- /* (with stereo the effective buffer size will shrink to 256) */
- fragment_size = 0x00020009;
+ unsigned long fragment_spec = 0;
+
+ /* determine logarithm (log2) of the fragment size */
+ for (fragment_spec=0; (1 << fragment_spec) < 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;
- if (ioctl(audio.device_fd,SNDCTL_DSP_SETFRAGMENT,&fragment_size) < 0)
+ /* 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");
FD_SET(audio.soundserver_pipe[0], &sound_fdset);
/* first clear the last premixing buffer */
- memset(premix_last_buffer,0,fragment_size*sizeof(int));
+ memset(premix_last_buffer, 0, fragment_size * sizeof(int));
for(i=0;i<MAX_SOUNDS_PLAYING;i++)
{
/* decrease volume if sound is fading out */
if (playlist[i].fade_sound &&
- playlist[i].volume>=PSND_MAX_VOLUME/10)
- playlist[i].volume-=PSND_MAX_VOLUME/20;
+ playlist[i].volume >= SOUND_FADING_VOLUME_THRESHOLD)
+ playlist[i].volume -= SOUND_FADING_VOLUME_STEP;
/* adjust volume of actual sound sample */
if (playlist[i].volume != PSND_MAX_VOLUME)
playing_sounds--;
}
}
- else if (playlist[i].volume <= PSND_MAX_VOLUME/10)
+ else if (playlist[i].volume <= SOUND_FADING_VOLUME_THRESHOLD)
{
playlist[i] = emptySoundControl;
playing_sounds--;
}
/* put last mixing buffer to final playing buffer */
- for(i=0;i<fragment_size;i++)
+ for(i=0; i<fragment_size; i++)
{
if (premix_last_buffer[i]<-255)
playing_buffer[i] = 0;
}
/* finally play the sound fragment */
- write(audio.device_fd, playing_buffer,fragment_size);
+ write(audio.device_fd, playing_buffer, fragment_size);
}
/* if no sounds playing, free device for other sound programs */
close(audio.device_fd);
}
}
-
#endif /* !AUDIO_STREAMING_DSP */
-
}
-
-#endif /* PLATFORM_UNIX */
-
}
+#endif /* PLATFORM_UNIX */
#if defined(PLATFORM_MSDOS)
static void sound_handler(struct SoundControl snd_ctrl)
/*** THE STUFF BELOW IS ONLY USED BY THE MAIN PROCESS ***/
-void AllocSoundArray(int num)
-{
- num_sounds = num;
- Sound = checked_calloc(num_sounds * sizeof(struct SampleInfo));
-}
-
#define CHUNK_ID_LEN 4 /* IFF style chunk id length */
#define WAV_HEADER_SIZE 20 /* size of WAV file header */
-boolean LoadSound(int sound_nr, char *sound_name)
+static boolean LoadSoundExt(char *sound_name, boolean is_music)
{
- struct SampleInfo *snd_info = &Sound[sound_nr];
+ struct SampleInfo *snd_info;
char filename[256];
- char *sound_ext = "wav";
-#if !defined(TARGET_SDL)
-#if !defined(PLATFORM_MSDOS)
+#if !defined(TARGET_SDL) && !defined(PLATFORM_MSDOS)
byte sound_header_buffer[WAV_HEADER_SIZE];
char chunk[CHUNK_ID_LEN + 1];
int chunk_length, dummy;
FILE *file;
int i;
#endif
-#endif
+ if (!audio.sound_available)
+ return FALSE;
+
+ num_sounds++;
+ Sound = checked_realloc(Sound, num_sounds * sizeof(struct SampleInfo));
+
+ snd_info = &Sound[num_sounds - 1];
snd_info->name = sound_name;
- sprintf(filename, "%s/%s/%s.%s",
- options.ro_base_directory, SOUNDS_DIRECTORY,
- snd_info->name, sound_ext);
+ sprintf(filename, "%s/%s/%s", options.ro_base_directory,
+ (is_music ? MUSIC_DIRECTORY : SOUNDS_DIRECTORY), snd_info->name);
#if defined(TARGET_SDL)
- snd_info->mix_chunk = Mix_LoadWAV(filename);
- if (snd_info->mix_chunk == NULL)
+ if ((snd_info->mix_chunk = Mix_LoadWAV(filename)) == NULL)
{
Error(ERR_WARN, "cannot read sound file '%s' - no sounds", filename);
return FALSE;
}
-#else /* !TARGET_SDL */
-
-#if !defined(PLATFORM_MSDOS)
+#elif defined(PLATFORM_UNIX)
- if ((file = fopen(filename, "r")) == NULL)
+ if ((file = fopen(filename, MODE_READ)) == NULL)
{
Error(ERR_WARN, "cannot open sound file '%s' - no sounds", filename);
return FALSE;
return FALSE;
}
-#endif /* PLATFORM_MSDOS */
-#endif /* !TARGET_SDL */
+#endif
return TRUE;
}
+boolean LoadSound(char *sound_name)
+{
+ return LoadSoundExt(sound_name, FALSE);
+}
+
+boolean LoadMod(char *mod_name)
+{
+#if defined(TARGET_SDL)
+ struct SampleInfo *mod_info;
+ char filename[256];
+
+ num_mods++;
+ Mod = checked_realloc(Mod, num_mods * sizeof(struct SampleInfo));
+
+ mod_info = &Mod[num_mods - 1];
+ mod_info->name = mod_name;
+
+ sprintf(filename, "%s/%s/%s", options.ro_base_directory,
+ MUSIC_DIRECTORY, mod_info->name);
+
+ if ((mod_info->mix_music = Mix_LoadMUS(filename)) == NULL)
+ {
+ Error(ERR_WARN, "cannot read music file '%s' - no music", filename);
+ return FALSE;
+ }
+
+ return TRUE;
+#else
+ return FALSE;
+#endif
+}
+
+int LoadMusic(void)
+{
+ DIR *dir;
+ struct dirent *dir_entry;
+ char *music_directory = getPath2(options.ro_base_directory, MUSIC_DIRECTORY);
+ int num_wav_music = 0;
+ int num_mod_music = 0;
+
+ if (!audio.sound_available)
+ return 0;
+
+ if ((dir = opendir(music_directory)) == NULL)
+ {
+ Error(ERR_WARN, "cannot read music directory '%s'", music_directory);
+ audio.music_available = FALSE;
+ free(music_directory);
+ return 0;
+ }
+
+ while ((dir_entry = readdir(dir)) != NULL) /* loop until last dir entry */
+ {
+ char *filename = dir_entry->d_name;
+
+ if (strlen(filename) > 4 &&
+ strcmp(&filename[strlen(filename) - 4], ".wav") == 0)
+ {
+ if (LoadSoundExt(filename, TRUE))
+ num_wav_music++;
+ }
+ else if (strlen(filename) > 4 &&
+ (strcmp(&filename[strlen(filename) - 4], ".mod") == 0 ||
+ strcmp(&filename[strlen(filename) - 4], ".MOD") == 0 ||
+ strncmp(filename, "mod.", 4) == 0 ||
+ strncmp(filename, "MOD.", 4) == 0))
+ {
+ if (LoadMod(filename))
+ num_mod_music++;
+ }
+ }
+
+ closedir(dir);
+
+ if (num_wav_music == 0 && num_mod_music == 0)
+ Error(ERR_WARN, "cannot find any valid music files in directory '%s'",
+ music_directory);
+
+ free(music_directory);
+
+ num_music = (num_mod_music > 0 ? num_mod_music : num_wav_music);
+
+ audio.mods_available = (num_mod_music > 0);
+ audio.music_available = (num_music > 0);
+
+ return num_music;
+}
+
+void PlayMusic(int nr)
+{
+ if (!audio.music_available)
+ return;
+
+ if (!audio.mods_available)
+ nr = num_sounds - num_music + nr;
+
+#if defined(TARGET_SDL)
+ if (audio.mods_available) /* play MOD music */
+ {
+ Mix_PlayMusic(Mod[nr].mix_music, -1);
+ Mix_VolumeMusic(SOUND_MAX_VOLUME); /* must be _after_ Mix_PlayMusic()! */
+ }
+ else /* play WAV music loop */
+ {
+ Mix_Volume(audio.music_channel, SOUND_MAX_VOLUME);
+ Mix_PlayChannel(audio.music_channel, Sound[nr].mix_chunk, -1);
+ }
+#else
+ audio.music_nr = nr;
+ PlaySoundLoop(nr);
+#endif
+}
+
void PlaySound(int nr)
{
PlaySoundExt(nr, PSND_MAX_VOLUME, PSND_MIDDLE, PSND_NO_LOOP);
snd_ctrl.data_len = Sound[nr].data_len;
#if defined(TARGET_SDL)
-
- Mix_Volume(-1, SDL_MIX_MAXVOLUME / 4);
- Mix_VolumeMusic(SDL_MIX_MAXVOLUME / 4);
-
+ Mix_Volume(-1, SOUND_MAX_VOLUME);
Mix_PlayChannel(-1, Sound[nr].mix_chunk, (loop ? -1 : 0));
-
-#else
-#if !defined(PLATFORM_MSDOS)
+#elif defined(PLATFORM_UNIX)
if (write(audio.soundserver_pipe[1], &snd_ctrl, sizeof(snd_ctrl)) < 0)
{
Error(ERR_WARN, "cannot pipe to child process - no sounds");
audio.sound_available = audio.sound_enabled = FALSE;
return;
}
-#else
+#elif defined(PLATFORM_MSDOS)
sound_handler(snd_ctrl);
#endif
+}
+
+void FadeMusic(void)
+{
+#if defined(TARGET_SDL)
+ if (!audio.sound_available)
+ return;
+
+ if (audio.mods_available)
+ Mix_FadeOutMusic(SOUND_FADING_INTERVAL);
+ else
+ Mix_FadeOutChannel(audio.music_channel, SOUND_FADING_INTERVAL);
+#else
+ FadeSound(audio.music_nr);
#endif
}
void FadeSounds()
{
+ FadeMusic();
StopSoundExt(-1, SSND_FADE_ALL_SOUNDS);
}
+void StopMusic(void)
+{
+#if defined(TARGET_SDL)
+ if (audio.mods_available)
+ Mix_HaltMusic();
+ else
+ Mix_HaltChannel(audio.music_channel);
+#else
+ StopSound(audio.music_nr);
+#endif
+}
+
void StopSound(int nr)
{
StopSoundExt(nr, SSND_STOP_SOUND);
if (SSND_FADING(method))
{
- Mix_FadeOutChannel(-1, 1000);
- Mix_FadeOutMusic(1000);
+ int i;
+
+ for (i=0; i<audio.channels; i++)
+ if (i != audio.music_channel || snd_ctrl.stop_all_sounds)
+ Mix_FadeOutChannel(i, SOUND_FADING_INTERVAL);
+ if (snd_ctrl.stop_all_sounds)
+ Mix_FadeOutMusic(SOUND_FADING_INTERVAL);
}
else
{
- Mix_HaltChannel(-1);
- Mix_HaltMusic();
+ int i;
+
+ for (i=0; i<audio.channels; i++)
+ if (i != audio.music_channel || snd_ctrl.stop_all_sounds)
+ Mix_HaltChannel(i);
+ if (snd_ctrl.stop_all_sounds)
+ Mix_HaltMusic();
}
#else
return;
for(i=0; i<num_sounds; i++)
-#if !defined(PLATFORM_MSDOS)
+#if defined(TARGET_SDL)
+ free(Sound[i].mix_chunk);
+#elif !defined(PLATFORM_MSDOS)
free(Sound[i].data_ptr);
#else
destroy_sample(Sound[i].sample_ptr);