author | nemo |
Thu, 09 Jul 2009 14:36:41 +0000 | |
changeset 2245 | c011aecc95e5 |
parent 2218 | 59a9bebc4988 |
child 2257 | 7eb31efcfb9b |
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 |
aptr = realloc(aptr, nbytes); |
|
2218
59a9bebc4988
-Smaxx' solution for compiling frontend with static libs in windows
koda
parents:
2216
diff
changeset
|
39 |
|
2213 | 40 |
if (aptr == NULL) { |
41 |
fprintf(stderr, "ERROR: not enough memory! realloc() failed\n"); |
|
42 |
free(aptr); |
|
43 |
exit(-1); |
|
44 |
} |
|
45 |
return aptr; |
|
46 |
} |
|
47 |
||
48 |
||
2210 | 49 |
FILE *Fopen (const char *fname, char *mode) { |
2191 | 50 |
FILE *fp; |
51 |
if ((fp=fopen(fname,mode)) == NULL) |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
52 |
fprintf (stderr, "ERROR: can't open file %s in mode '%s'\n", fname, mode); |
2191 | 53 |
return fp; |
54 |
} |
|
55 |
||
2210 | 56 |
|
2191 | 57 |
ALint AlGetError (const char *str) { |
58 |
ALenum error; |
|
59 |
||
60 |
error = alGetError(); |
|
61 |
if (error != AL_NO_ERROR) { |
|
62 |
fprintf(stderr, str, error); |
|
63 |
return -2; |
|
64 |
} else |
|
65 |
return AL_TRUE; |
|
66 |
} |
|
67 |
||
2210 | 68 |
|
69 |
#ifndef _WIN32 |
|
70 |
void *helper_fadein(void *tmp) |
|
71 |
#else |
|
2213 | 72 |
void *helper_fadein(void *tmp) |
2210 | 73 |
#endif |
74 |
{ |
|
75 |
ALfloat gain; |
|
76 |
ALfloat target_gain; |
|
77 |
fade_t *fade; |
|
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2213
diff
changeset
|
78 |
uint32_t index; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2213
diff
changeset
|
79 |
uint16_t quantity; |
2210 | 80 |
|
81 |
fade = tmp; |
|
82 |
index = fade->index; |
|
83 |
quantity = fade->quantity; |
|
84 |
free (fade); |
|
85 |
||
86 |
#ifdef DEBUG |
|
87 |
fprintf(stderr, "Fade-out: index %d quantity %d\n", index, quantity); |
|
88 |
#endif |
|
89 |
||
2212 | 90 |
/*save the volume desired after the fade*/ |
2210 | 91 |
alGetSourcef(Sources[index], AL_GAIN, &target_gain); |
92 |
if (target_gain > 1.0f || target_gain <= 0.0f) |
|
93 |
target_gain = 1.0f; |
|
94 |
||
95 |
alSourcePlay(Sources[index]); |
|
96 |
||
97 |
for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) { |
|
98 |
#ifdef DEBUG |
|
99 |
fprintf(stderr, "Fade-in: Set gain to: %f\n", gain); |
|
100 |
#endif |
|
101 |
alSourcef(Sources[index], AL_GAIN, gain); |
|
102 |
usleep(10000); |
|
103 |
} |
|
104 |
||
105 |
AlGetError("ERROR %d: Setting fade in volume\n"); |
|
106 |
||
107 |
#ifndef _WIN32 |
|
108 |
pthread_exit(NULL); |
|
109 |
#else |
|
110 |
_endthread(); |
|
111 |
#endif |
|
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2213
diff
changeset
|
112 |
return 0; |
2210 | 113 |
} |
114 |
||
115 |
||
116 |
#ifndef _WIN32 |
|
117 |
void *helper_fadeout(void *tmp) |
|
118 |
#else |
|
2213 | 119 |
void *helper_fadeout(void *tmp) |
2210 | 120 |
#endif |
121 |
{ |
|
122 |
ALfloat gain; |
|
123 |
ALfloat old_gain; |
|
124 |
fade_t *fade; |
|
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2213
diff
changeset
|
125 |
uint32_t index; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2213
diff
changeset
|
126 |
uint16_t quantity; |
2210 | 127 |
|
128 |
fade = tmp; |
|
129 |
index = fade->index; |
|
130 |
quantity = fade->quantity; |
|
131 |
free(fade); |
|
132 |
||
133 |
#ifdef DEBUG |
|
134 |
fprintf(stderr, "Fade-out: index %d quantity %d\n", index, quantity); |
|
135 |
#endif |
|
136 |
||
137 |
alGetSourcef(Sources[index], AL_GAIN, &old_gain); |
|
138 |
||
139 |
for (gain = old_gain; gain >= 0.00f; gain -= (float) quantity/10000) { |
|
140 |
#ifdef DEBUG |
|
141 |
fprintf(stderr, "Fade-out: Set gain to %f\n", gain); |
|
142 |
#endif |
|
143 |
alSourcef(Sources[index], AL_GAIN, gain); |
|
144 |
usleep(10000); |
|
145 |
} |
|
146 |
||
147 |
AlGetError("ERROR %d: Setting fade out volume\n"); |
|
148 |
||
2212 | 149 |
/*stop that sound and reset its volume*/ |
2210 | 150 |
alSourceStop (Sources[index]); |
151 |
alSourcef (Sources[index], AL_GAIN, old_gain); |
|
152 |
||
153 |
#ifndef _WIN32 |
|
154 |
pthread_exit(NULL); |
|
155 |
#else |
|
156 |
_endthread(); |
|
157 |
#endif |
|
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2213
diff
changeset
|
158 |
return 0; |
2210 | 159 |
} |
160 |
||
2213 | 161 |
|
2191 | 162 |
#ifdef __CPLUSPLUS |
163 |
} |
|
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2213
diff
changeset
|
164 |
#endif |