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