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