author | koda |
Tue, 30 Jun 2009 17:02:41 +0000 | |
changeset 2216 | 82e7da49c26a |
parent 2213 | bd51bbf06033 |
child 2218 | 59a9bebc4988 |
permissions | -rw-r--r-- |
2191 | 1 |
/* |
2 |
* OpenAL Bridge - a simple portable library for OpenAL interface |
|
3 |
* Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*/ |
|
18 |
||
19 |
#include "wrappers.h" |
|
20 |
||
21 |
#ifdef __CPLUSPLUS |
|
22 |
extern "C" { |
|
23 |
#endif |
|
24 |
||
2210 | 25 |
extern ALint *Sources; |
26 |
||
2213 | 27 |
void *Malloc (size_t nbytes) { |
2191 | 28 |
void *aptr; |
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
29 |
if ((aptr = malloc(nbytes)) == NULL) { |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
30 |
fprintf(stderr, "ERROR: not enough memory! malloc() failed\n"); |
2200
8192be6e3aef
koda/Smaxx changes to openal for crossplatform building
nemo
parents:
2194
diff
changeset
|
31 |
exit(-1); |
8192be6e3aef
koda/Smaxx changes to openal for crossplatform building
nemo
parents:
2194
diff
changeset
|
32 |
} |
2191 | 33 |
return aptr; |
34 |
} |
|
35 |
||
2210 | 36 |
|
2213 | 37 |
void *Realloc (void *aptr, size_t nbytes) { |
38 |
#ifndef _WIN32 |
|
39 |
aptr = reallocf(aptr, nbytes); |
|
40 |
#else |
|
41 |
aptr = realloc(aptr, nbytes); |
|
42 |
#endif |
|
43 |
if (aptr == NULL) { |
|
44 |
fprintf(stderr, "ERROR: not enough memory! realloc() failed\n"); |
|
45 |
#ifdef _WIN32 |
|
46 |
free(aptr); |
|
47 |
#endif |
|
48 |
exit(-1); |
|
49 |
} |
|
50 |
return aptr; |
|
51 |
} |
|
52 |
||
53 |
||
2210 | 54 |
FILE *Fopen (const char *fname, char *mode) { |
2191 | 55 |
FILE *fp; |
56 |
if ((fp=fopen(fname,mode)) == NULL) |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
57 |
fprintf (stderr, "ERROR: can't open file %s in mode '%s'\n", fname, mode); |
2191 | 58 |
return fp; |
59 |
} |
|
60 |
||
2210 | 61 |
|
2191 | 62 |
ALint AlGetError (const char *str) { |
63 |
ALenum error; |
|
64 |
||
65 |
error = alGetError(); |
|
66 |
if (error != AL_NO_ERROR) { |
|
67 |
fprintf(stderr, str, error); |
|
68 |
return -2; |
|
69 |
} else |
|
70 |
return AL_TRUE; |
|
71 |
} |
|
72 |
||
2210 | 73 |
|
74 |
#ifndef _WIN32 |
|
75 |
void *helper_fadein(void *tmp) |
|
76 |
#else |
|
2213 | 77 |
void *helper_fadein(void *tmp) |
2210 | 78 |
#endif |
79 |
{ |
|
80 |
ALfloat gain; |
|
81 |
ALfloat target_gain; |
|
82 |
fade_t *fade; |
|
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2213
diff
changeset
|
83 |
uint32_t index; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2213
diff
changeset
|
84 |
uint16_t quantity; |
2210 | 85 |
|
86 |
fade = tmp; |
|
87 |
index = fade->index; |
|
88 |
quantity = fade->quantity; |
|
89 |
free (fade); |
|
90 |
||
91 |
#ifdef DEBUG |
|
92 |
fprintf(stderr, "Fade-out: index %d quantity %d\n", index, quantity); |
|
93 |
#endif |
|
94 |
||
2212 | 95 |
/*save the volume desired after the fade*/ |
2210 | 96 |
alGetSourcef(Sources[index], AL_GAIN, &target_gain); |
97 |
if (target_gain > 1.0f || target_gain <= 0.0f) |
|
98 |
target_gain = 1.0f; |
|
99 |
||
100 |
alSourcePlay(Sources[index]); |
|
101 |
||
102 |
for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) { |
|
103 |
#ifdef DEBUG |
|
104 |
fprintf(stderr, "Fade-in: Set gain to: %f\n", gain); |
|
105 |
#endif |
|
106 |
alSourcef(Sources[index], AL_GAIN, gain); |
|
107 |
usleep(10000); |
|
108 |
} |
|
109 |
||
110 |
AlGetError("ERROR %d: Setting fade in volume\n"); |
|
111 |
||
112 |
#ifndef _WIN32 |
|
113 |
pthread_exit(NULL); |
|
114 |
#else |
|
115 |
_endthread(); |
|
116 |
#endif |
|
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2213
diff
changeset
|
117 |
return 0; |
2210 | 118 |
} |
119 |
||
120 |
||
121 |
#ifndef _WIN32 |
|
122 |
void *helper_fadeout(void *tmp) |
|
123 |
#else |
|
2213 | 124 |
void *helper_fadeout(void *tmp) |
2210 | 125 |
#endif |
126 |
{ |
|
127 |
ALfloat gain; |
|
128 |
ALfloat old_gain; |
|
129 |
fade_t *fade; |
|
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2213
diff
changeset
|
130 |
uint32_t index; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2213
diff
changeset
|
131 |
uint16_t quantity; |
2210 | 132 |
|
133 |
fade = tmp; |
|
134 |
index = fade->index; |
|
135 |
quantity = fade->quantity; |
|
136 |
free(fade); |
|
137 |
||
138 |
#ifdef DEBUG |
|
139 |
fprintf(stderr, "Fade-out: index %d quantity %d\n", index, quantity); |
|
140 |
#endif |
|
141 |
||
142 |
alGetSourcef(Sources[index], AL_GAIN, &old_gain); |
|
143 |
||
144 |
for (gain = old_gain; gain >= 0.00f; gain -= (float) quantity/10000) { |
|
145 |
#ifdef DEBUG |
|
146 |
fprintf(stderr, "Fade-out: Set gain to %f\n", gain); |
|
147 |
#endif |
|
148 |
alSourcef(Sources[index], AL_GAIN, gain); |
|
149 |
usleep(10000); |
|
150 |
} |
|
151 |
||
152 |
AlGetError("ERROR %d: Setting fade out volume\n"); |
|
153 |
||
2212 | 154 |
/*stop that sound and reset its volume*/ |
2210 | 155 |
alSourceStop (Sources[index]); |
156 |
alSourcef (Sources[index], AL_GAIN, old_gain); |
|
157 |
||
158 |
#ifndef _WIN32 |
|
159 |
pthread_exit(NULL); |
|
160 |
#else |
|
161 |
_endthread(); |
|
162 |
#endif |
|
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2213
diff
changeset
|
163 |
return 0; |
2210 | 164 |
} |
165 |
||
2213 | 166 |
|
2191 | 167 |
#ifdef __CPLUSPLUS |
168 |
} |
|
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2213
diff
changeset
|
169 |
#endif |