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