return TRUE;
}
-void PlaySoundLevel(int x, int y, int sound_nr)
+void PlaySoundLevel(int x, int y, int nr)
{
int sx = SCREENX(x), sy = SCREENY(y);
- int volume, stereo;
+ int volume, stereo_position;
int silence_distance = 8;
+ int type = (IS_LOOP_SOUND(nr) ? SND_CTRL_PLAY_LOOP : SND_CTRL_PLAY_SOUND);
- if ((!setup.sound_simple && !IS_LOOP_SOUND(sound_nr)) ||
- (!setup.sound_loops && IS_LOOP_SOUND(sound_nr)))
+ if ((!setup.sound_simple && !IS_LOOP_SOUND(nr)) ||
+ (!setup.sound_loops && IS_LOOP_SOUND(nr)))
return;
if (!IN_LEV_FIELD(x, y) ||
- sx < -silence_distance || sx >= SCR_FIELDX+silence_distance ||
- sy < -silence_distance || sy >= SCR_FIELDY+silence_distance)
+ sx < -silence_distance || sx >= SCR_FIELDX + silence_distance ||
+ sy < -silence_distance || sy >= SCR_FIELDY + silence_distance)
return;
volume = PSND_MAX_VOLUME;
#if !defined(PLATFORM_MSDOS)
- stereo = (sx - SCR_FIELDX/2) * 12;
+ stereo_position = (sx - SCR_FIELDX / 2) * 12;
#else
- stereo = PSND_MIDDLE + (2 * sx - (SCR_FIELDX - 1)) * 5;
- if (stereo > PSND_MAX_RIGHT)
- stereo = PSND_MAX_RIGHT;
- if (stereo < PSND_MAX_LEFT)
- stereo = PSND_MAX_LEFT;
+ stereo_position = PSND_MIDDLE + (2 * sx - (SCR_FIELDX - 1)) * 5;
+ if (stereo_position > PSND_MAX_RIGHT)
+ stereo_position = PSND_MAX_RIGHT;
+ if (stereo_position < PSND_MAX_LEFT)
+ stereo_position = PSND_MAX_LEFT;
#endif
if (!IN_SCR_FIELD(sx, sy))
volume -= volume * (dx > dy ? dx : dy) / silence_distance;
}
- PlaySoundExt(sound_nr, volume, stereo, SND_CTRL_PLAY_SOUND);
+ PlaySoundExt(nr, volume, stereo_position, type);
}
void RaiseScore(int value)
audio.music_channel = MUSIC_CHANNEL;
audio.first_sound_channel = FIRST_SOUND_CHANNEL;
- /* reserve first channel for music loops */
- if (Mix_ReserveChannels(1) == 1)
- audio.music_channel = 0;
- else
- audio.music_available = FALSE;
-
- Mix_Volume(-1, SOUND_MAX_VOLUME);
- Mix_VolumeMusic(SOUND_MAX_VOLUME);
-
Mixer_InitChannels();
}
#include <fcntl.h>
#include <dirent.h>
#include <signal.h>
+#include <math.h>
+
+#include "platform.h"
+
+#if defined(PLATFORM_LINUX)
+#include <sys/ioctl.h>
+#include <linux/soundcard.h>
+#elif defined(PLATFORM_FREEBSD)
+#include <machine/soundcard.h>
+#elif defined(PLATFORM_NETBSD)
+#include <sys/ioctl.h>
+#include <sys/audioio.h>
+#elif defined(PLATFORM_HPUX)
+#include <sys/audio.h>
+#endif
#include "system.h"
#include "sound.h"
#include "setup.h"
-#define IS_PARENT_PROCESS(pid) ((pid) > 0)
-#define IS_CHILD_PROCESS(pid) ((pid) == 0)
+/* expiration time (in milliseconds) for sound loops */
+#define SOUND_LOOP_EXPIRATION_TIME 200
+
+#if defined(TARGET_SDL)
+/* one second fading interval == 1000 ticks (milliseconds) */
+#define SOUND_FADING_INTERVAL 1000
+#define SOUND_MAX_VOLUME SDL_MIX_MAXVOLUME
+#endif
+
+#if defined(AUDIO_STREAMING_DSP)
+#define SOUND_FADING_VOLUME_STEP (PSND_MAX_VOLUME / 40)
+#define SOUND_FADING_VOLUME_THRESHOLD (SOUND_FADING_VOLUME_STEP * 2)
+#endif
+
+#if !defined(PLATFORM_HPUX)
+#define SND_BLOCKSIZE 4096
+#else
+#define SND_BLOCKSIZE 32768
+#endif
+
+#define SND_TYPE_NONE 0
+#define SND_TYPE_WAV 1
+
+#define MUS_TYPE_NONE 0
+#define MUS_TYPE_WAV 1
+#define MUS_TYPE_MOD 2
+
+#define DEVICENAME_DSP "/dev/dsp"
+#define DEVICENAME_AUDIO "/dev/audio"
+#define DEVICENAME_AUDIOCTL "/dev/audioCtl"
+
+
+#if 0
+struct SoundHeader_SUN
+{
+ unsigned long magic;
+ unsigned long hdr_size;
+ unsigned long data_size;
+ unsigned long encoding;
+ unsigned long sample_rate;
+ unsigned long channels;
+};
+
+struct SoundHeader_8SVX
+{
+ char magic_FORM[4];
+ unsigned long chunk_size;
+ char magic_8SVX[4];
+};
+#endif
+
+struct AudioFormatInfo
+{
+ boolean stereo; /* availability of stereo sound */
+ int format; /* size and endianess of sample data */
+ int sample_rate; /* sample frequency */
+ int fragment_size; /* audio device fragment size in bytes */
+};
+
+struct SampleInfo
+{
+ char *source_filename;
+ int num_references;
+
+ int type;
+ int format;
+ long data_len;
+ void *data_ptr;
+};
+typedef struct SampleInfo SoundInfo;
+typedef struct SampleInfo MusicInfo;
+
+struct SoundControl
+{
+ boolean active;
+
+ int nr;
+ int volume;
+ int stereo_position;
+
+ int state;
+
+ unsigned long playing_starttime;
+ unsigned long playing_pos;
+
+ int type;
+ int format;
+ long data_len;
+ void *data_ptr;
+
+#if defined(PLATFORM_MSDOS)
+ int voice;
+#endif
+};
+typedef struct SoundControl SoundControl;
struct ListNode
{
mixer_active_channels = 0;
}
+static void Mixer_ResetChannelExpiration(int channel)
+{
+ mixer[channel].playing_starttime = Counter();
+
+#if defined(TARGET_SDL)
+ if (IS_LOOP(mixer[channel]) && !IS_MUSIC(mixer[channel]))
+ Mix_ExpireChannel(channel, SOUND_LOOP_EXPIRATION_TIME);
+#endif
+}
+
+static boolean Mixer_ChannelExpired(int channel)
+{
+ if (!mixer[channel].active)
+ return TRUE;
+
+ if (IS_LOOP(mixer[channel]) && !IS_MUSIC(mixer[channel]) &&
+ DelayReached(&mixer[channel].playing_starttime,
+ SOUND_LOOP_EXPIRATION_TIME))
+ return TRUE;
+
+#if defined(TARGET_SDL)
+
+ if (!Mix_Playing(channel))
+ return TRUE;
+
+#elif defined(TARGET_ALLEGRO)
+
+ mixer[channel].playing_pos = voice_get_position(mixer[channel].voice);
+ mixer[channel].volume = voice_get_volume(mixer[channel].voice);
+
+ /* sound sample has completed playing or was completely faded out */
+ if (mixer[channel].playing_pos == -1 || mixer[channel].volume == 0)
+ return TRUE;
+
+#endif /* TARGET_ALLEGRO */
+
+ return FALSE;
+}
+
static void Mixer_PlayChannel(int channel)
{
/* start with inactive channel in case something goes wrong */
if (mixer[channel].type != MUS_TYPE_WAV)
return;
- mixer[channel].playing_pos = 0;
- mixer[channel].playing_starttime = Counter();
-
#if defined(TARGET_SDL)
Mix_Volume(channel, SOUND_MAX_VOLUME);
Mix_PlayChannel(channel, mixer[channel].data_ptr,
voice_start(mixer[channel].voice);
#endif
+ Mixer_ResetChannelExpiration(channel);
+
+ mixer[channel].playing_pos = 0;
mixer[channel].active = TRUE;
mixer_active_channels++;
}
static void Mixer_StopChannel(int channel)
{
-#if 0
-#if defined(TARGET_SDL)
- printf("----------> %d [%d]\n", Mix_Playing(channel), Mix_Playing(0));
-#endif
-#endif
-
if (!mixer[channel].active)
return;
/* 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)
+ if (mixer[i].active && mixer[i].nr == snd_ctrl.nr)
k++;
- /* restart loop sounds only if they are just fading out */
- if (k >= 1 && IS_LOOP(snd_ctrl))
+#if 0
+ printf("SOUND %d [CURRENTLY PLAYING %d TIMES]\n", snd_ctrl.nr, k);
+#endif
+
+ /* reset expiration delay for already playing loop sounds */
+ if (k > 0 && 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_UnFadeChannel(i);
+ {
+ if (mixer[i].active && mixer[i].nr == snd_ctrl.nr)
+ {
+#if 0
+ printf("RESETTING EXPIRATION FOR SOUND %d\n", snd_ctrl.nr);
+#endif
+
+#if 1
+ Mixer_ResetChannelExpiration(i);
+#endif
+ if (IS_FADING(mixer[i]))
+ Mixer_UnFadeChannel(i);
+ }
+ }
return;
}
if (!mixer[i].active || mixer[i].nr != snd_ctrl.nr)
continue;
-#if 1
actual = 1000 * playing_time / mixer[i].data_len;
-#else
- actual = 100 * mixer[i].playing_pos / mixer[i].data_len;
-#endif
if (actual >= longest)
{
for (i=audio.first_sound_channel; i<audio.num_channels; i++)
{
-#if 1
int playing_time = playing_current - mixer[i].playing_starttime;
int actual = 1000 * playing_time / mixer[i].data_len;
-#else
- int actual = 100 * mixer[i].playing_pos / mixer[i].data_len;
-#endif
if (!IS_LOOP(mixer[i]) && actual > longest)
{
Mixer_StopChannel(longest_nr);
}
- /* add new sound to mixer */
+ /* add the new sound to the mixer */
for(i=0; i<audio.num_channels; i++)
{
#if 0
}
#endif
+ /* deactivate channels that have expired since the last request */
for (i=0; i<audio.num_channels; i++)
- {
- if (!mixer[i].active)
- continue;
-
- if (i != audio.music_channel &&
- DelayReached(&mixer[i].playing_starttime, SOUND_LOOP_EXPIRATION_TIME))
- {
- Mixer_StopChannel(i);
- continue;
- }
-
-#if defined(TARGET_SDL)
-
- if (!Mix_Playing(i))
+ if (mixer[i].active && Mixer_ChannelExpired(i))
Mixer_StopChannel(i);
-#elif defined(TARGET_ALLEGRO)
-
- mixer[i].playing_pos = voice_get_position(mixer[i].voice);
- mixer[i].volume = voice_get_volume(mixer[i].voice);
-
- /* sound sample has completed playing or was completely faded out */
- if (mixer[i].playing_pos == -1 || mixer[i].volume == 0)
- Mixer_StopChannel(i);
-
-#endif /* TARGET_ALLEGRO */
- }
-
if (IS_RELOADING(snd_ctrl)) /* load new sound or music files */
{
Mixer_StopMusicChannel();
if (!mixer[i].active)
continue;
+ if (Mixer_ChannelExpired(i))
+ {
+ Mixer_StopChannel(i);
+ continue;
+ }
+
/* pointer, lenght and actual playing position of sound sample */
sample_ptr = mixer[i].data_ptr;
sample_len = mixer[i].data_len;
#ifndef SOUND_H
#define SOUND_H
-#include <math.h>
-
#include "platform.h"
-#if defined(PLATFORM_LINUX)
-#include <sys/ioctl.h>
-#endif
-
-#if defined(PLATFORM_LINUX)
-#include <linux/soundcard.h>
-#elif defined(PLATFORM_FREEBSD)
-#include <machine/soundcard.h>
-#elif defined(PLATFORM_NETBSD)
-#include <sys/ioctl.h>
-#include <sys/audioio.h>
-#elif defined(PLATFORM_HPUX)
-#include <sys/audio.h>
-#endif
-
-#include "system.h"
-
#if defined(PLATFORM_UNIX) && !defined(TARGET_SDL)
#define AUDIO_UNIX_NATIVE
#define AUDIO_STREAMING_DSP
#endif
-#define AUDIO_SAMPLE_RATE_8000 8000
-#define AUDIO_SAMPLE_RATE_22050 22050
-
-#define AUDIO_FRAGMENT_SIZE_512 512
-#define AUDIO_FRAGMENT_SIZE_1024 1024
-#define AUDIO_FRAGMENT_SIZE_2048 2048
-#define AUDIO_FRAGMENT_SIZE_4096 4096
+/* values for platform specific sound initialization */
+#define AUDIO_SAMPLE_RATE_8000 8000
+#define AUDIO_SAMPLE_RATE_22050 22050
-#define AUDIO_NUM_CHANNELS_MONO 1
-#define AUDIO_NUM_CHANNELS_STEREO 2
+#define AUDIO_FRAGMENT_SIZE_512 512
+#define AUDIO_FRAGMENT_SIZE_1024 1024
+#define AUDIO_FRAGMENT_SIZE_2048 2048
+#define AUDIO_FRAGMENT_SIZE_4096 4096
-#define AUDIO_FORMAT_UNKNOWN (0)
-#define AUDIO_FORMAT_U8 (1 << 0)
-#define AUDIO_FORMAT_S16 (1 << 1)
-#define AUDIO_FORMAT_LE (1 << 2)
-#define AUDIO_FORMAT_BE (1 << 3)
+#define AUDIO_NUM_CHANNELS_MONO 1
+#define AUDIO_NUM_CHANNELS_STEREO 2
-/* expiration time (in milliseconds) for sound loops */
-#define SOUND_LOOP_EXPIRATION_TIME 200
+#define AUDIO_FORMAT_UNKNOWN (0)
+#define AUDIO_FORMAT_U8 (1 << 0)
+#define AUDIO_FORMAT_S16 (1 << 1)
+#define AUDIO_FORMAT_LE (1 << 2)
+#define AUDIO_FORMAT_BE (1 << 3)
-#if defined(TARGET_SDL)
-/* one second fading interval == 1000 ticks (milliseconds) */
-#define SOUND_FADING_INTERVAL 1000
-#define SOUND_MAX_VOLUME SDL_MIX_MAXVOLUME
-#endif
-
-#if defined(AUDIO_STREAMING_DSP)
-#define SOUND_FADING_VOLUME_STEP (PSND_MAX_VOLUME / 40)
-#define SOUND_FADING_VOLUME_THRESHOLD (SOUND_FADING_VOLUME_STEP * 2)
-#endif
-
-#if defined(AUDIO_STREAMING_DSP)
-#define DEFAULT_AUDIO_SAMPLE_RATE AUDIO_SAMPLE_RATE_22050
+#if defined(AUDIO_UNIX_NATIVE) && !defined(AUDIO_STREAMING_DSP)
+#define DEFAULT_AUDIO_SAMPLE_RATE AUDIO_SAMPLE_RATE_8000
#else
-#define DEFAULT_AUDIO_SAMPLE_RATE AUDIO_SAMPLE_RATE_8000
+#define DEFAULT_AUDIO_SAMPLE_RATE AUDIO_SAMPLE_RATE_22050
#endif
-#define DEFAULT_AUDIO_FRAGMENT_SIZE_UNIX AUDIO_FRAGMENT_SIZE_512
-#define DEFAULT_AUDIO_FRAGMENT_SIZE_WIN32 AUDIO_FRAGMENT_SIZE_2048
-
-#if defined(PLATFORM_UNIX)
-#define DEFAULT_AUDIO_FRAGMENT_SIZE DEFAULT_AUDIO_FRAGMENT_SIZE_UNIX
+#if defined(PLATFORM_WIN32)
+#define DEFAULT_AUDIO_FRAGMENT_SIZE AUDIO_FRAGMENT_SIZE_2048
#else
-#define DEFAULT_AUDIO_FRAGMENT_SIZE DEFAULT_AUDIO_FRAGMENT_SIZE_WIN32
+#define DEFAULT_AUDIO_FRAGMENT_SIZE AUDIO_FRAGMENT_SIZE_512
#endif
#if defined(TARGET_SDL)
#define MUSIC_CHANNEL 0
#define FIRST_SOUND_CHANNEL 1
-#if !defined(PLATFORM_HPUX)
-#define SND_BLOCKSIZE 4096
-#else
-#define SND_BLOCKSIZE 32768
-#endif
-
-/* some values for PlaySound(), StopSound() and friends */
-#if !defined(PLATFORM_MSDOS)
-
-#define PSND_SILENCE 0
-#define PSND_MAX_VOLUME_BITS 15
-#define PSND_MIN_VOLUME 0
-#define PSND_MAX_VOLUME (1 << PSND_MAX_VOLUME_BITS)
-#define PSND_MIDDLE 0
-#define PSND_MAX_STEREO_BITS 7
-#define PSND_MAX_STEREO (1 << PSND_MAX_STEREO_BITS)
-#define PSND_MAX_LEFT (-PSND_MAX_STEREO)
-#define PSND_MAX_RIGHT (+PSND_MAX_STEREO)
-#define PSND_MAX_LEFT2RIGHT_BITS (PSND_MAX_STEREO_BITS+1)
-#define PSND_MAX_LEFT2RIGHT (1 << PSND_MAX_LEFT2RIGHT_BITS)
-
-#else /* PLATFORM_MSDOS */
-
-#define PSND_SILENCE 0
-#define PSND_MIN_VOLUME 0
-#define PSND_MAX_VOLUME 255
-#define PSND_MAX_LEFT 0
-#define PSND_MAX_RIGHT 255
-#define PSND_MIDDLE 128
-
-#endif
-
-#if 0
-#define PSND_NO_LOOP 0
-#define PSND_LOOP 1
-#define PSND_MUSIC 2
-
-#define SSND_FADE_SOUND (1 << 0)
-#define SSND_FADE_MUSIC (1 << 1)
-#define SSND_FADE_ALL (1 << 2)
-#define SSND_FADING (SSND_FADE_SOUND | \
- SSND_FADE_MUSIC | \
- SSND_FADE_ALL)
-#define SSND_STOP_SOUND (1 << 3)
-#define SSND_STOP_MUSIC (1 << 4)
-#define SSND_STOP_ALL (1 << 5)
-#define SSND_STOPPING (SSND_STOP_SOUND | \
- SSND_STOP_MUSIC | \
- SSND_STOP_ALL)
-#define SSND_MUSIC (SSND_FADE_MUSIC | SSND_STOP_MUSIC)
-#define SSND_ALL (SSND_FADE_ALL | SSND_STOP_ALL)
-
-#define SND_RELOAD_SOUNDS 1
-#define SND_RELOAD_MUSIC 2
-#endif
-
-#define SND_TYPE_NONE 0
-#define SND_TYPE_WAV 1
-
-#define MUS_TYPE_NONE 0
-#define MUS_TYPE_WAV 1
-#define MUS_TYPE_MOD 2
-
-/* settings for sound path, sound device, etc. */
-#ifndef SND_PATH
-#define SND_PATH "./sounds"
-#endif
-
-#define DEVICENAME_DSP "/dev/dsp"
-#define DEVICENAME_AUDIO "/dev/audio"
-#define DEVICENAME_AUDIOCTL "/dev/audioCtl"
-
-#if 0
-#if defined(AUDIO_STREAMING_DSP)
-#define AUDIO_DEVICE DEVICENAME_DSP
-#else
-#define AUDIO_DEVICE DEVICENAME_AUDIO
-#endif
-#endif
-
-/* value for undefined sound effect filename */
-#define SND_FILE_UNDEFINED "NONE"
-
-
-#if 0
-struct SoundHeader_SUN
-{
- unsigned long magic;
- unsigned long hdr_size;
- unsigned long data_size;
- unsigned long encoding;
- unsigned long sample_rate;
- unsigned long channels;
-};
-
-struct SoundHeader_8SVX
-{
- char magic_FORM[4];
- unsigned long chunk_size;
- char magic_8SVX[4];
-};
-#endif
-
-struct AudioFormatInfo
-{
- boolean stereo; /* availability of stereo sound */
- int format; /* size and endianess of sample data */
- int sample_rate; /* sample frequency */
- int fragment_size; /* audio device fragment size in bytes */
-};
-
-struct SoundEffectInfo
-{
- char *text;
- char *default_filename;
- char *filename;
-};
-
-struct SampleInfo
-{
- char *source_filename;
- int num_references;
-
- int type;
- int format;
- long data_len;
- void *data_ptr;
-};
-
-typedef struct SampleInfo SoundInfo;
-typedef struct SampleInfo MusicInfo;
+/* values for PlaySound(), StopSound() and friends */
#define SND_CTRL_NONE (0)
#define SND_CTRL_MUSIC (1 << 0)
#define SND_CTRL_LOOP (1 << 1)
SND_CTRL_RELOAD_MUSIC))
#define ALL_SOUNDS(x) ((x).state & SND_CTRL_ALL_SOUNDS)
-struct SoundControl
-{
- boolean active;
- int nr;
- int volume;
- int stereo_position;
+#if !defined(TARGET_ALLEGRO)
- int state;
+#define PSND_SILENCE 0
+#define PSND_MAX_VOLUME_BITS 15
+#define PSND_MIN_VOLUME 0
+#define PSND_MAX_VOLUME (1 << PSND_MAX_VOLUME_BITS)
+#define PSND_MIDDLE 0
+#define PSND_MAX_STEREO_BITS 7
+#define PSND_MAX_STEREO (1 << PSND_MAX_STEREO_BITS)
+#define PSND_MAX_LEFT (-PSND_MAX_STEREO)
+#define PSND_MAX_RIGHT (+PSND_MAX_STEREO)
+#define PSND_MAX_LEFT2RIGHT_BITS (PSND_MAX_STEREO_BITS+1)
+#define PSND_MAX_LEFT2RIGHT (1 << PSND_MAX_LEFT2RIGHT_BITS)
- unsigned long playing_starttime;
- unsigned long playing_pos;
+#else /* TARGET_ALLEGRO */
- int type;
- int format;
- long data_len;
- void *data_ptr;
+#define PSND_SILENCE 0
+#define PSND_MIN_VOLUME 0
+#define PSND_MAX_VOLUME 255
+#define PSND_MAX_LEFT 0
+#define PSND_MAX_RIGHT 255
+#define PSND_MIDDLE 128
-#if defined(PLATFORM_MSDOS)
- int voice;
#endif
+
+/* value for undefined sound effect filename */
+#define SND_FILE_UNDEFINED "NONE"
+
+
+struct SoundEffectInfo
+{
+ char *text;
+ char *default_filename;
+ char *filename;
};
-typedef struct SoundControl SoundControl;
/* general sound functions */
void UnixOpenAudio(void);
#define VERSION_MINOR(x) (((x) % 10000) / 100)
#define VERSION_PATCH(x) ((x) % 100)
+/* functions for parent/child process identification */
+#define IS_PARENT_PROCESS(pid) ((pid) > 0)
+#define IS_CHILD_PROCESS(pid) ((pid) == 0)
+
/* type definitions */
typedef int (*EventFilter)(const Event *);
-#define COMPILE_DATE_STRING "[2002-05-10 16:21]"
+#define COMPILE_DATE_STRING "[2002-05-12 22:43]"