Avoid non-alpha characters in _qdgdfv_alnum in win32.
[qdgdf.git] / qdgdf_audio.c
blob02756e58464d4d7557b64f6d4952ade3bd52a191
1 /*
3 Quick and Dirty Game Development Framework (QDGDF)
5 Copyright (C) 2001/2005 Angel Ortega <angel@triptico.com>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 http://www.triptico.com
25 #include "config.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "qdgdf_audio.h"
33 /**
34 * _qdgdfa_sound - Main sound switch.
36 * If set to 0 previous to startup, disables the whole sound system.
37 * If it has a value of 0 after startup, something wrong occurred
38 * in the initialization of the sound card.
39 * [Audio Variables]
41 int _qdgdfa_sound = 1;
43 /**
44 * _qdgdfa_16_bit - 16 bit sound card toggle.
46 * If set to 0 previous to startup, use only 8 bit sound output.
47 * If it has a value of 0 after startup, the sound card does not
48 * support 16 bit PCM audio.
49 * [Audio Variables]
51 int _qdgdfa_16_bit = 1;
53 /**
54 * _qdgdfa_window_title - title of the window.
56 * This string holds the title of the window.
57 * [Audio Variables]
59 char _qdgdfa_window_title[150] = "";
61 char *_qdgdfa_version = VERSION;
63 /**
64 * _qdgdfa_big_sound_threshold - Threshold for a sound to be considered big.
66 * This integer holds the minimum size a sound must have
67 * to be considered a 'big' sound (a sound that is paged
68 * in disk instead of being loaded completely in memory).
69 * [Audio Variables]
71 int _qdgdfa_big_sound_threshold = 1024 * 1024;
74 /**
75 * _qdgdfa_fopen_path - Search path for _qdgdfa_fopen().
77 * This string can contain a semicolon-separated list of paths
78 * where files to be open with _qdgdfa_fopen() will be tried.
79 * [Support Variables]
81 char _qdgdfa_fopen_path[250] = "";
83 /* audio driver */
84 static struct _qdgdfa_driver *drv = NULL;
87 /** code **/
89 #include "qdgdf_path.c"
91 FILE *_qdgdfa_fopen(char *file, char *mode)
93 return _path_fopen(_qdgdfa_fopen_path, file, mode);
97 /**
98 * qdgdfa_path_find - Finds a file in _qdgdfa_fopen_path.
99 * @file: the file to be found
101 * Searches for @file in the path stored in _qdgdfa_fopen_path.
102 * If it's found in some of the directories there, a string is
103 * allocated contaning the full path, that should be freed
104 * when no longer needed. Otherwise, returns NULL.
106 char *qdgdfa_path_find(const char *file)
108 return _path_find(_qdgdfa_fopen_path, file);
112 FILE *qdgdfa_load_wav(char *filename, int *size, int *bits)
114 FILE *f;
115 char dummydata[256];
116 int rlen, flen;
117 short int b_per_sec, num_channels, tag;
118 char riffid[5], waveid[5], fmtid[5], dataid[5];
119 int _wav_frequency;
121 if ((f = _qdgdfa_fopen(filename, "rb")) == NULL)
122 return NULL;
124 fread(riffid, 1, 4, f);
125 riffid[4] = 0;
126 fread(&rlen, 1, 4, f);
127 fread(waveid, 1, 4, f);
128 waveid[4] = 0;
130 if (strcmp(waveid, "WAVE")) {
131 fclose(f);
132 return NULL;
135 fread(fmtid, 1, 4, f);
136 fmtid[4] = 0;
137 flen = fgetc(f);
138 flen += (fgetc(f) * 256);
139 flen += (fgetc(f) * 65536);
140 flen += (fgetc(f) * 16777216);
142 if (flen > 240)
143 flen = 240;
145 fread(&tag, 1, 2, f);
147 num_channels = fgetc(f);
148 num_channels += (fgetc(f) * 256);
149 _wav_frequency = fgetc(f);
150 _wav_frequency += (fgetc(f) * 256);
151 _wav_frequency += (fgetc(f) * 65536);
152 _wav_frequency += (fgetc(f) * 16777216);
153 b_per_sec = fgetc(f);
154 b_per_sec += (fgetc(f) * 256);
155 b_per_sec += (fgetc(f) * 65536);
156 b_per_sec += (fgetc(f) * 16777216);
158 *bits = fgetc(f);
159 (*bits) += (fgetc(f) * 256);
160 (*bits) *= 8;
162 fread(dummydata, 1, (size_t) flen - 14, f);
163 fread(dataid, 1, 4, f);
164 dataid[4] = 0;
166 *size = fgetc(f);
167 (*size) += (fgetc(f) * 256);
168 (*size) += (fgetc(f) * 65536);
169 (*size) += (fgetc(f) * 16777216);
171 return f;
176 * qdgdfa_load_sound - Loads a sound.
177 * @wavfile: the file containing the sound
179 * Loads a sound file (in .WAV format). A sound handle
180 * is returned.
181 * [Audio Functions]
183 int qdgdfa_load_sound(char *wavfile)
185 if (drv == NULL)
186 return 0;
188 return drv->load_sound(wavfile);
193 * qdgdfa_load_big_sound - Loads a big sound (deprecated).
194 * @wavfile: the file containing the sound
196 * In previous versions, sounds considered (by the programmer)
197 * to be big were loaded by using this function, but now the
198 * 'bigness' of a file is controlled by the value in the
199 * _qdgdfa_big_sound_threshold variable, so it's use is deprecated.
200 * Now it's exactly equivalent to qdgdfa_load_sound().
201 * [Audio Functions]
203 int qdgdfa_load_big_sound(char *wavfile)
205 return qdgdfa_load_sound(wavfile);
210 * qdgdfa_dup_sound - Duplicates a sound.
211 * @sound: the sound to be duplicated
213 * Duplicates a sound. The new sound will share the
214 * sound data with the old one. Returns the
215 * id for the new sound.
216 * [Audio Functions]
218 int qdgdfa_dup_sound(int snd)
220 if (drv == NULL)
221 return 0;
223 return drv->dup_sound(snd);
228 * qdgdfa_play_sound - Starts playing a sound.
229 * @snd: the sound handle
230 * @loop: neverending loop flag
232 * Starts playing a sound. If @loop is set, the sound restarts
233 * from the beginning when reaches the end. If the sound is
234 * already playing, nothing is done.
235 * [Audio Functions]
237 void qdgdfa_play_sound(int snd, int loop)
239 if (drv != NULL)
240 drv->play_sound(snd, loop);
245 * qdgdfa_respawn_sound - Respawns a sound.
246 * @snd: the sound to be respawn
248 * Respawns a sound, playing it even if the sound is
249 * already playing. This new, duplicated sound inherits
250 * all properties (as pan, etc.) from the original one
251 * and cannot be stopped nor controlled in any way. The
252 * poll of respawned sounds is limited; if it can't hold
253 * more sounds, nothing is done. A respawned sound is
254 * automatically deleted when finished.
255 * [Audio Functions]
257 void qdgdfa_respawn_sound(int snd)
259 if (drv != NULL)
260 drv->respawn_sound(snd);
265 * qdgdfa_stop_sound - Stop playing a sound.
266 * @snd: the sound handle
268 * Stops playing a sound.
269 * [Audio Functions]
271 void qdgdfa_stop_sound(int snd)
273 if (drv != NULL)
274 drv->stop_sound(snd);
279 * qdgdfa_set_pan - Sets the pan of a sound.
280 * @snd: the sound handle
281 * @pan: pan of the sound
283 * Sets the pan of a sound. The @pan argument can be -1 (left channel only),
284 * 0 (both channels, center) or 1 (right channel only).
285 * [Audio Functions]
287 void qdgdfa_set_pan(int snd, int pan)
289 if (drv != NULL)
290 drv->set_pan(snd, pan);
295 * qdgdfa_set_attenuation - Sets the attenuation of a sound.
296 * @snd: the sound handle
297 * @att: the attenuation
299 * Sets the attenuation of a sound. It ranges from 0 (no attenuation,
300 * sound is as is) to 63 (total silence).
301 * [Audio Functions]
303 void qdgdfa_set_attenuation(int snd, int att)
305 if (att < 0)
306 att = 0;
307 if (att > 63)
308 att = 63;
310 if (drv != NULL)
311 drv->set_attenuation(snd, att);
316 * qdgdfa_reset - Resets the sound system.
318 * Resets the sound system, shuting up all sounds and unloading
319 * them from memory.
320 * [Audio Functions]
322 void qdgdfa_reset(void)
324 if (drv != NULL)
325 drv->reset();
330 * qdgdfa_pause - Pause the sound system.
331 * @p: toggle flag
333 * Pauses the sound system. The @p argument acts as a boolean
334 * to pause or unpause the sound system.
335 * [Audio Functions]
337 void qdgdfa_pause(int p)
339 if (drv != NULL)
340 drv->pause(p);
345 * qdgdfa_home_dir - Returns the home user directory.
347 * Returns a system-dependent directory where the user can write
348 * documents and create subdirectories.
349 * [File Management]
351 char *qdgdfa_home_dir(void)
353 return _home_dir();
358 * qdgdfa_app_dir - Returns the applications directory.
360 * Returns a system-dependent directory where the applications store
361 * their private data, as components or resources.
362 * [File Management]
364 char *qdgdfa_app_dir(void)
366 return _app_dir();
371 * qdgdfa_startup - Starts the sound system.
373 * Starts the sound system. If _qdgdfa_sound is not set,
374 * no action is done. If _qdgdfa_16_bit is set, it tries
375 * to use the 16 bit capabilities of the sound card. It
376 * also sets both previous variables accordingly to the
377 * current configuration.
378 * [Audio Functions]
380 void qdgdfa_startup(void)
382 if (!_qdgdfa_sound)
383 return;
385 if (!TRY_AUDIO_DRIVERS())
386 _qdgdfa_sound = 0;
388 atexit(qdgdfa_shutdown);
393 * qdgdfa_shutdown - Shuts down the sound system.
395 * Shuts down the sound system.
396 * [Audio Functions]
398 void qdgdfa_shutdown(void)
400 if (drv != NULL)
401 drv->shutdown();
403 drv = NULL;