rnd-20020512-2-src
[rocksndiamonds.git] / src / libgame / sound.h
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1994-2001 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * sound.h                                                  *
12 ***********************************************************/
13
14 #ifndef SOUND_H
15 #define SOUND_H
16
17 #include <math.h>
18
19 #include "platform.h"
20
21 #if defined(PLATFORM_LINUX)
22 #include <sys/ioctl.h>
23 #endif
24
25 #if defined(PLATFORM_LINUX)
26 #include <linux/soundcard.h>
27 #elif defined(PLATFORM_FREEBSD)
28 #include <machine/soundcard.h>
29 #elif defined(PLATFORM_NETBSD)
30 #include <sys/ioctl.h>
31 #include <sys/audioio.h>
32 #elif defined(PLATFORM_HPUX)
33 #include <sys/audio.h>
34 #endif
35
36 #include "system.h"
37
38
39 #if defined(PLATFORM_UNIX) && !defined(TARGET_SDL)
40 #define AUDIO_UNIX_NATIVE
41 #endif
42
43 #if defined(PLATFORM_LINUX) || defined(PLATFORM_FREEBSD) || defined(VOXWARE)
44 #define AUDIO_LINUX_IOCTL
45 #endif
46
47 #if defined(AUDIO_LINUX_IOCTL) || defined(PLATFORM_NETBSD)
48 #define AUDIO_STREAMING_DSP
49 #endif
50
51 #define AUDIO_SAMPLE_RATE_8000                  8000
52 #define AUDIO_SAMPLE_RATE_22050                 22050
53
54 #define AUDIO_FRAGMENT_SIZE_512                 512
55 #define AUDIO_FRAGMENT_SIZE_1024                1024
56 #define AUDIO_FRAGMENT_SIZE_2048                2048
57 #define AUDIO_FRAGMENT_SIZE_4096                4096
58
59 #define AUDIO_NUM_CHANNELS_MONO                 1
60 #define AUDIO_NUM_CHANNELS_STEREO               2
61
62 #define AUDIO_FORMAT_UNKNOWN                    (0)
63 #define AUDIO_FORMAT_U8                         (1 << 0)
64 #define AUDIO_FORMAT_S16                        (1 << 1)
65 #define AUDIO_FORMAT_LE                         (1 << 2)
66 #define AUDIO_FORMAT_BE                         (1 << 3)
67
68 /* expiration time (in milliseconds) for sound loops */
69 #define SOUND_LOOP_EXPIRATION_TIME              200
70
71 #if defined(TARGET_SDL)
72 /* one second fading interval == 1000 ticks (milliseconds) */
73 #define SOUND_FADING_INTERVAL                   1000
74 #define SOUND_MAX_VOLUME                        SDL_MIX_MAXVOLUME
75 #endif
76
77 #if defined(AUDIO_STREAMING_DSP)
78 #define SOUND_FADING_VOLUME_STEP                (PSND_MAX_VOLUME / 40)
79 #define SOUND_FADING_VOLUME_THRESHOLD           (SOUND_FADING_VOLUME_STEP * 2)
80 #endif
81
82 #if defined(AUDIO_STREAMING_DSP)
83 #define DEFAULT_AUDIO_SAMPLE_RATE               AUDIO_SAMPLE_RATE_22050
84 #else
85 #define DEFAULT_AUDIO_SAMPLE_RATE               AUDIO_SAMPLE_RATE_8000
86 #endif
87
88 #define DEFAULT_AUDIO_FRAGMENT_SIZE_UNIX        AUDIO_FRAGMENT_SIZE_512
89 #define DEFAULT_AUDIO_FRAGMENT_SIZE_WIN32       AUDIO_FRAGMENT_SIZE_2048
90
91 #if defined(PLATFORM_UNIX)
92 #define DEFAULT_AUDIO_FRAGMENT_SIZE     DEFAULT_AUDIO_FRAGMENT_SIZE_UNIX
93 #else
94 #define DEFAULT_AUDIO_FRAGMENT_SIZE     DEFAULT_AUDIO_FRAGMENT_SIZE_WIN32
95 #endif
96
97 #if defined(TARGET_SDL)
98 #define NUM_MIXER_CHANNELS              MIX_CHANNELS
99 #else
100 #define NUM_MIXER_CHANNELS              8
101 #endif
102
103 #define MUSIC_CHANNEL                   0
104 #define FIRST_SOUND_CHANNEL             1
105
106 #if !defined(PLATFORM_HPUX)
107 #define SND_BLOCKSIZE                   4096
108 #else
109 #define SND_BLOCKSIZE                   32768
110 #endif
111
112 /* some values for PlaySound(), StopSound() and friends */
113 #if !defined(PLATFORM_MSDOS)
114
115 #define PSND_SILENCE            0
116 #define PSND_MAX_VOLUME_BITS    15
117 #define PSND_MIN_VOLUME         0
118 #define PSND_MAX_VOLUME         (1 << PSND_MAX_VOLUME_BITS)
119 #define PSND_MIDDLE             0
120 #define PSND_MAX_STEREO_BITS    7
121 #define PSND_MAX_STEREO         (1 << PSND_MAX_STEREO_BITS)
122 #define PSND_MAX_LEFT           (-PSND_MAX_STEREO)
123 #define PSND_MAX_RIGHT          (+PSND_MAX_STEREO)
124 #define PSND_MAX_LEFT2RIGHT_BITS (PSND_MAX_STEREO_BITS+1)
125 #define PSND_MAX_LEFT2RIGHT     (1 << PSND_MAX_LEFT2RIGHT_BITS)
126
127 #else   /* PLATFORM_MSDOS */
128
129 #define PSND_SILENCE            0
130 #define PSND_MIN_VOLUME         0
131 #define PSND_MAX_VOLUME         255
132 #define PSND_MAX_LEFT           0
133 #define PSND_MAX_RIGHT          255
134 #define PSND_MIDDLE             128
135
136 #endif
137
138 #if 0
139 #define PSND_NO_LOOP            0
140 #define PSND_LOOP               1
141 #define PSND_MUSIC              2
142
143 #define SSND_FADE_SOUND         (1 << 0)
144 #define SSND_FADE_MUSIC         (1 << 1)
145 #define SSND_FADE_ALL           (1 << 2)
146 #define SSND_FADING             (SSND_FADE_SOUND | \
147                                  SSND_FADE_MUSIC | \
148                                  SSND_FADE_ALL)
149 #define SSND_STOP_SOUND         (1 << 3)
150 #define SSND_STOP_MUSIC         (1 << 4)
151 #define SSND_STOP_ALL           (1 << 5)
152 #define SSND_STOPPING           (SSND_STOP_SOUND | \
153                                  SSND_STOP_MUSIC | \
154                                  SSND_STOP_ALL)
155 #define SSND_MUSIC              (SSND_FADE_MUSIC | SSND_STOP_MUSIC)
156 #define SSND_ALL                (SSND_FADE_ALL | SSND_STOP_ALL)
157
158 #define SND_RELOAD_SOUNDS       1
159 #define SND_RELOAD_MUSIC        2
160 #endif
161
162 #define SND_TYPE_NONE           0
163 #define SND_TYPE_WAV            1
164
165 #define MUS_TYPE_NONE           0
166 #define MUS_TYPE_WAV            1
167 #define MUS_TYPE_MOD            2
168
169 /* settings for sound path, sound device, etc. */
170 #ifndef SND_PATH
171 #define SND_PATH        "./sounds"
172 #endif
173
174 #define DEVICENAME_DSP          "/dev/dsp"
175 #define DEVICENAME_AUDIO        "/dev/audio"
176 #define DEVICENAME_AUDIOCTL     "/dev/audioCtl"
177
178 #if 0
179 #if defined(AUDIO_STREAMING_DSP)
180 #define AUDIO_DEVICE    DEVICENAME_DSP
181 #else
182 #define AUDIO_DEVICE    DEVICENAME_AUDIO
183 #endif
184 #endif
185
186 /* value for undefined sound effect filename */
187 #define SND_FILE_UNDEFINED      "NONE"
188
189
190 #if 0
191 struct SoundHeader_SUN
192 {
193   unsigned long magic;
194   unsigned long hdr_size;
195   unsigned long data_size;
196   unsigned long encoding;
197   unsigned long sample_rate;
198   unsigned long channels;
199 };
200
201 struct SoundHeader_8SVX
202 {
203   char magic_FORM[4];
204   unsigned long chunk_size;
205   char magic_8SVX[4];
206 };
207 #endif
208
209 struct AudioFormatInfo
210 {
211   boolean stereo;               /* availability of stereo sound */
212   int format;                   /* size and endianess of sample data */
213   int sample_rate;              /* sample frequency */
214   int fragment_size;            /* audio device fragment size in bytes */
215 };
216
217 struct SoundEffectInfo
218 {
219   char *text;
220   char *default_filename;
221   char *filename;
222 };
223
224 struct SampleInfo
225 {
226   char *source_filename;
227   int num_references;
228
229   int type;
230   int format;
231   long data_len;
232   void *data_ptr;
233 };
234
235 typedef struct SampleInfo       SoundInfo;
236 typedef struct SampleInfo       MusicInfo;
237
238 #define SND_CTRL_NONE           (0)
239 #define SND_CTRL_MUSIC          (1 << 0)
240 #define SND_CTRL_LOOP           (1 << 1)
241 #define SND_CTRL_FADE           (1 << 2)
242 #define SND_CTRL_STOP           (1 << 3)
243 #define SND_CTRL_ALL_SOUNDS     (1 << 4)
244 #define SND_CTRL_RELOAD_SOUNDS  (1 << 5)
245 #define SND_CTRL_RELOAD_MUSIC   (1 << 6)
246
247 #define SND_CTRL_PLAY_SOUND     (SND_CTRL_NONE)
248 #define SND_CTRL_PLAY_LOOP      (SND_CTRL_LOOP)
249 #define SND_CTRL_PLAY_MUSIC     (SND_CTRL_LOOP | SND_CTRL_MUSIC)
250
251 #define SND_CTRL_FADE_SOUND     (SND_CTRL_FADE)
252 #define SND_CTRL_FADE_MUSIC     (SND_CTRL_FADE | SND_CTRL_MUSIC)
253 #define SND_CTRL_FADE_ALL       (SND_CTRL_FADE | SND_CTRL_ALL_SOUNDS)
254
255 #define SND_CTRL_STOP_SOUND     (SND_CTRL_STOP)
256 #define SND_CTRL_STOP_MUSIC     (SND_CTRL_STOP | SND_CTRL_MUSIC)
257 #define SND_CTRL_STOP_ALL       (SND_CTRL_STOP | SND_CTRL_ALL_SOUNDS)
258
259 #define IS_MUSIC(x)             ((x).state & SND_CTRL_MUSIC)
260 #define IS_LOOP(x)              ((x).state & SND_CTRL_LOOP)
261 #define IS_FADING(x)            ((x).state & SND_CTRL_FADE)
262 #define IS_STOPPING(x)          ((x).state & SND_CTRL_STOP)
263 #define IS_RELOADING(x)         ((x).state & (SND_CTRL_RELOAD_SOUNDS | \
264                                               SND_CTRL_RELOAD_MUSIC))
265 #define ALL_SOUNDS(x)           ((x).state & SND_CTRL_ALL_SOUNDS)
266
267 struct SoundControl
268 {
269   boolean active;
270
271   int nr;
272   int volume;
273   int stereo_position;
274
275   int state;
276
277   unsigned long playing_starttime;
278   unsigned long playing_pos;
279
280   int type;
281   int format;
282   long data_len;
283   void *data_ptr;
284
285 #if defined(PLATFORM_MSDOS)
286   int voice;
287 #endif
288 };
289
290 typedef struct SoundControl     SoundControl;
291
292 /* general sound functions */
293 void UnixOpenAudio(void);
294 void UnixCloseAudio(void);
295
296 /* mixer functions */ 
297 void Mixer_InitChannels(void);
298 void StartMixer(void);
299
300 /* sound client functions */
301 void PlayMusic(int);
302 void PlaySound(int);
303 void PlaySoundStereo(int, int);
304 void PlaySoundLoop(int);
305 void PlaySoundMusic(int);
306 void PlaySoundExt(int, int, int, int);
307 void FadeMusic(void);
308 void FadeSound(int);
309 void FadeSounds(void);
310 void StopMusic(void);
311 void StopSound(int);
312 void StopSounds(void);
313 void StopSoundExt(int, int);
314 void InitSoundList(struct SoundEffectInfo *, int);
315 void InitReloadSounds(char *);
316 void InitReloadMusic(char *);
317 void FreeAllSounds(void);
318 void FreeAllMusic(void);
319
320 #endif