rnd-20020511-1-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 #if defined(TARGET_SDL)
69 /* one second fading interval == 1000 ticks (milliseconds) */
70 #define SOUND_FADING_INTERVAL                   1000
71 #define SOUND_MAX_VOLUME                        SDL_MIX_MAXVOLUME
72 #endif
73
74 #if defined(AUDIO_STREAMING_DSP)
75 #define SOUND_FADING_VOLUME_STEP                (PSND_MAX_VOLUME / 40)
76 #define SOUND_FADING_VOLUME_THRESHOLD           (SOUND_FADING_VOLUME_STEP * 2)
77 #endif
78
79 #if defined(AUDIO_STREAMING_DSP)
80 #define DEFAULT_AUDIO_SAMPLE_RATE               AUDIO_SAMPLE_RATE_22050
81 #else
82 #define DEFAULT_AUDIO_SAMPLE_RATE               AUDIO_SAMPLE_RATE_8000
83 #endif
84
85 #define DEFAULT_AUDIO_FRAGMENT_SIZE_UNIX        AUDIO_FRAGMENT_SIZE_512
86 #define DEFAULT_AUDIO_FRAGMENT_SIZE_WIN32       AUDIO_FRAGMENT_SIZE_2048
87
88 #if defined(PLATFORM_UNIX)
89 #define DEFAULT_AUDIO_FRAGMENT_SIZE     DEFAULT_AUDIO_FRAGMENT_SIZE_UNIX
90 #else
91 #define DEFAULT_AUDIO_FRAGMENT_SIZE     DEFAULT_AUDIO_FRAGMENT_SIZE_WIN32
92 #endif
93
94 #if defined(TARGET_SDL)
95 #define NUM_MIXER_CHANNELS              MIX_CHANNELS
96 #else
97 #define NUM_MIXER_CHANNELS              8
98 #endif
99
100 #define MUSIC_CHANNEL                   0
101 #define FIRST_SOUND_CHANNEL             1
102
103 #if !defined(PLATFORM_HPUX)
104 #define SND_BLOCKSIZE                   4096
105 #else
106 #define SND_BLOCKSIZE                   32768
107 #endif
108
109 /* some values for PlaySound(), StopSound() and friends */
110 #if !defined(PLATFORM_MSDOS)
111
112 #define PSND_SILENCE            0
113 #define PSND_MAX_VOLUME_BITS    15
114 #define PSND_MIN_VOLUME         0
115 #define PSND_MAX_VOLUME         (1 << PSND_MAX_VOLUME_BITS)
116 #define PSND_MIDDLE             0
117 #define PSND_MAX_STEREO_BITS    7
118 #define PSND_MAX_STEREO         (1 << PSND_MAX_STEREO_BITS)
119 #define PSND_MAX_LEFT           (-PSND_MAX_STEREO)
120 #define PSND_MAX_RIGHT          (+PSND_MAX_STEREO)
121 #define PSND_MAX_LEFT2RIGHT_BITS (PSND_MAX_STEREO_BITS+1)
122 #define PSND_MAX_LEFT2RIGHT     (1 << PSND_MAX_LEFT2RIGHT_BITS)
123
124 #else   /* PLATFORM_MSDOS */
125
126 #define PSND_SILENCE            0
127 #define PSND_MIN_VOLUME         0
128 #define PSND_MAX_VOLUME         255
129 #define PSND_MAX_LEFT           0
130 #define PSND_MAX_RIGHT          255
131 #define PSND_MIDDLE             128
132
133 #endif
134
135 #if 0
136 #define PSND_NO_LOOP            0
137 #define PSND_LOOP               1
138 #define PSND_MUSIC              2
139
140 #define SSND_FADE_SOUND         (1 << 0)
141 #define SSND_FADE_MUSIC         (1 << 1)
142 #define SSND_FADE_ALL           (1 << 2)
143 #define SSND_FADING             (SSND_FADE_SOUND | \
144                                  SSND_FADE_MUSIC | \
145                                  SSND_FADE_ALL)
146 #define SSND_STOP_SOUND         (1 << 3)
147 #define SSND_STOP_MUSIC         (1 << 4)
148 #define SSND_STOP_ALL           (1 << 5)
149 #define SSND_STOPPING           (SSND_STOP_SOUND | \
150                                  SSND_STOP_MUSIC | \
151                                  SSND_STOP_ALL)
152 #define SSND_MUSIC              (SSND_FADE_MUSIC | SSND_STOP_MUSIC)
153 #define SSND_ALL                (SSND_FADE_ALL | SSND_STOP_ALL)
154
155 #define SND_RELOAD_SOUNDS       1
156 #define SND_RELOAD_MUSIC        2
157 #endif
158
159 #define SND_TYPE_NONE           0
160 #define SND_TYPE_WAV            1
161
162 #define MUS_TYPE_NONE           0
163 #define MUS_TYPE_WAV            1
164 #define MUS_TYPE_MOD            2
165
166 /* settings for sound path, sound device, etc. */
167 #ifndef SND_PATH
168 #define SND_PATH        "./sounds"
169 #endif
170
171 #define DEVICENAME_DSP          "/dev/dsp"
172 #define DEVICENAME_AUDIO        "/dev/audio"
173 #define DEVICENAME_AUDIOCTL     "/dev/audioCtl"
174
175 #if 0
176 #if defined(AUDIO_STREAMING_DSP)
177 #define AUDIO_DEVICE    DEVICENAME_DSP
178 #else
179 #define AUDIO_DEVICE    DEVICENAME_AUDIO
180 #endif
181 #endif
182
183 /* value for undefined sound effect filename */
184 #define SND_FILE_UNDEFINED      "NONE"
185
186
187 #if 0
188 struct SoundHeader_SUN
189 {
190   unsigned long magic;
191   unsigned long hdr_size;
192   unsigned long data_size;
193   unsigned long encoding;
194   unsigned long sample_rate;
195   unsigned long channels;
196 };
197
198 struct SoundHeader_8SVX
199 {
200   char magic_FORM[4];
201   unsigned long chunk_size;
202   char magic_8SVX[4];
203 };
204 #endif
205
206 struct AudioFormatInfo
207 {
208   boolean stereo;               /* availability of stereo sound */
209   int format;                   /* size and endianess of sample data */
210   int sample_rate;              /* sample frequency */
211   int fragment_size;            /* audio device fragment size in bytes */
212 };
213
214 struct SoundEffectInfo
215 {
216   char *text;
217   char *default_filename;
218   char *filename;
219 };
220
221 struct SampleInfo
222 {
223   char *source_filename;
224   int num_references;
225
226   int type;
227   int format;
228   long data_len;
229   void *data_ptr;
230 };
231
232 typedef struct SampleInfo       SoundInfo;
233 typedef struct SampleInfo       MusicInfo;
234
235 #define SND_CTRL_NONE           (0)
236 #define SND_CTRL_MUSIC          (1 << 0)
237 #define SND_CTRL_LOOP           (1 << 1)
238 #define SND_CTRL_FADE           (1 << 2)
239 #define SND_CTRL_STOP           (1 << 3)
240 #define SND_CTRL_ALL_SOUNDS     (1 << 4)
241 #define SND_CTRL_RELOAD_SOUNDS  (1 << 5)
242 #define SND_CTRL_RELOAD_MUSIC   (1 << 6)
243
244 #define SND_CTRL_PLAY_SOUND     (SND_CTRL_NONE)
245 #define SND_CTRL_PLAY_LOOP      (SND_CTRL_LOOP)
246 #define SND_CTRL_PLAY_MUSIC     (SND_CTRL_LOOP | SND_CTRL_MUSIC)
247
248 #define SND_CTRL_FADE_SOUND     (SND_CTRL_FADE)
249 #define SND_CTRL_FADE_MUSIC     (SND_CTRL_FADE | SND_CTRL_MUSIC)
250 #define SND_CTRL_FADE_ALL       (SND_CTRL_FADE | SND_CTRL_ALL_SOUNDS)
251
252 #define SND_CTRL_STOP_SOUND     (SND_CTRL_STOP)
253 #define SND_CTRL_STOP_MUSIC     (SND_CTRL_STOP | SND_CTRL_MUSIC)
254 #define SND_CTRL_STOP_ALL       (SND_CTRL_STOP | SND_CTRL_ALL_SOUNDS)
255
256 #define IS_MUSIC(x)             ((x).state & SND_CTRL_MUSIC)
257 #define IS_LOOP(x)              ((x).state & SND_CTRL_LOOP)
258 #define IS_FADING(x)            ((x).state & SND_CTRL_FADE)
259 #define IS_STOPPING(x)          ((x).state & SND_CTRL_STOP)
260 #define IS_RELOADING(x)         ((x).state & (SND_CTRL_RELOAD_SOUNDS | \
261                                               SND_CTRL_RELOAD_MUSIC))
262 #define ALL_SOUNDS(x)           ((x).state & SND_CTRL_ALL_SOUNDS)
263
264 struct SoundControl
265 {
266   boolean active;
267
268   int nr;
269   int volume;
270   int stereo;
271
272   int state;
273
274   int playingtime;
275   long playingpos;
276
277   int type;
278   int format;
279   long data_len;
280   void *data_ptr;
281
282 #if defined(PLATFORM_MSDOS)
283   int voice;
284 #endif
285 };
286
287 typedef struct SoundControl     SoundControl;
288
289 /* general sound functions */
290 void UnixOpenAudio(void);
291 void UnixCloseAudio(void);
292
293 /* mixer functions */ 
294 void Mixer_InitChannels(void);
295 void StartMixer(void);
296
297 /* sound client functions */
298 void PlayMusic(int);
299 void PlaySound(int);
300 void PlaySoundStereo(int, int);
301 void PlaySoundLoop(int);
302 void PlaySoundMusic(int);
303 void PlaySoundExt(int, int, int, int);
304 void FadeMusic(void);
305 void FadeSound(int);
306 void FadeSounds(void);
307 void StopMusic(void);
308 void StopSound(int);
309 void StopSounds(void);
310 void StopSoundExt(int, int);
311 void InitSoundList(struct SoundEffectInfo *, int);
312 void InitReloadSounds(char *);
313 void InitReloadMusic(char *);
314 void FreeAllSounds(void);
315 void FreeAllMusic(void);
316
317 #endif