author | unc0rr |
Wed, 02 Sep 2009 08:53:46 +0000 | |
changeset 2344 | 63b3da03ce46 |
parent 2259 | ca42efdce3ce |
child 2415 | 35d09cbf819a |
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 |
|
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2218
diff
changeset
|
6 |
* it under the terms of the GNU Lesser General Public License as published by |
2191 | 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 |
|
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2218
diff
changeset
|
12 |
* GNU Lesser General Public License for more details. |
2191 | 13 |
* |
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2218
diff
changeset
|
14 |
* You should have received a copy of the GNU Lesser General Public License |
2191 | 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 |
|
2259 | 24 |
|
25 |
extern ALint *Sources; |
|
26 |
||
27 |
void *Malloc (size_t nbytes) { |
|
28 |
void *aptr; |
|
29 |
if ((aptr = malloc(nbytes)) == NULL) { |
|
30 |
fprintf(stderr, "ERROR: not enough memory! malloc() failed\n"); |
|
31 |
exit(-1); |
|
32 |
} |
|
33 |
return aptr; |
|
34 |
} |
|
35 |
||
36 |
||
37 |
void *Realloc (void *aptr, size_t nbytes) { |
|
38 |
aptr = realloc(aptr, nbytes); |
|
39 |
||
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 |
||
49 |
FILE *Fopen (const char *fname, char *mode) { |
|
50 |
FILE *fp; |
|
51 |
if ((fp=fopen(fname,mode)) == NULL) |
|
52 |
fprintf (stderr, "ERROR: can't open file %s in mode '%s'\n", fname, mode); |
|
53 |
return fp; |
|
54 |
} |
|
55 |
||
56 |
||
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 |
||
68 |
ALint AlGetError2 (const char *str, int num) { |
|
69 |
ALenum error; |
|
70 |
||
71 |
error = alGetError(); |
|
72 |
if (error != AL_NO_ERROR) { |
|
73 |
fprintf(stderr, str, error, num); |
|
74 |
return -2; |
|
75 |
} else |
|
76 |
return AL_TRUE; |
|
77 |
} |
|
78 |
||
2210 | 79 |
#ifndef _WIN32 |
2259 | 80 |
void *helper_fadein(void *tmp) |
2210 | 81 |
#else |
2259 | 82 |
void *helper_fadein(void *tmp) |
2210 | 83 |
#endif |
2259 | 84 |
{ |
85 |
ALfloat gain; |
|
86 |
ALfloat target_gain; |
|
87 |
fade_t *fade; |
|
88 |
uint32_t index; |
|
89 |
uint16_t quantity; |
|
90 |
||
91 |
fade = tmp; |
|
92 |
index = fade->index; |
|
93 |
quantity = fade->quantity; |
|
94 |
free (fade); |
|
95 |
||
2210 | 96 |
#ifdef DEBUG |
2259 | 97 |
fprintf(stderr, "Fade-out: index %d quantity %d\n", index, quantity); |
2210 | 98 |
#endif |
2259 | 99 |
|
100 |
/*save the volume desired after the fade*/ |
|
101 |
alGetSourcef(Sources[index], AL_GAIN, &target_gain); |
|
102 |
if (target_gain > 1.0f || target_gain <= 0.0f) |
|
103 |
target_gain = 1.0f; |
|
104 |
||
105 |
alSourcePlay(Sources[index]); |
|
106 |
||
107 |
for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) { |
|
108 |
#ifdef DEBUG |
|
109 |
fprintf(stderr, "Fade-in: Set gain to: %f\n", gain); |
|
110 |
#endif |
|
111 |
alSourcef(Sources[index], AL_GAIN, gain); |
|
112 |
usleep(10000); |
|
113 |
} |
|
114 |
||
115 |
AlGetError("ERROR %d: Setting fade in volume\n"); |
|
116 |
||
2210 | 117 |
#ifndef _WIN32 |
2259 | 118 |
pthread_exit(NULL); |
2210 | 119 |
#else |
2259 | 120 |
_endthread(); |
2210 | 121 |
#endif |
2259 | 122 |
return 0; |
123 |
} |
|
124 |
||
125 |
||
126 |
#ifndef _WIN32 |
|
127 |
void *helper_fadeout(void *tmp) |
|
128 |
#else |
|
129 |
void *helper_fadeout(void *tmp) |
|
2210 | 130 |
#endif |
2259 | 131 |
{ |
132 |
ALfloat gain; |
|
133 |
ALfloat old_gain; |
|
134 |
fade_t *fade; |
|
135 |
uint32_t index; |
|
136 |
uint16_t quantity; |
|
137 |
||
138 |
fade = tmp; |
|
139 |
index = fade->index; |
|
140 |
quantity = fade->quantity; |
|
141 |
free(fade); |
|
142 |
||
2210 | 143 |
#ifdef DEBUG |
2259 | 144 |
fprintf(stderr, "Fade-out: index %d quantity %d\n", index, quantity); |
145 |
#endif |
|
146 |
||
147 |
alGetSourcef(Sources[index], AL_GAIN, &old_gain); |
|
148 |
||
149 |
for (gain = old_gain; gain >= 0.00f; gain -= (float) quantity/10000) { |
|
150 |
#ifdef DEBUG |
|
151 |
fprintf(stderr, "Fade-out: Set gain to %f\n", gain); |
|
2210 | 152 |
#endif |
2259 | 153 |
alSourcef(Sources[index], AL_GAIN, gain); |
154 |
usleep(10000); |
|
155 |
} |
|
156 |
||
157 |
AlGetError("ERROR %d: Setting fade out volume\n"); |
|
158 |
||
159 |
/*stop that sound and reset its volume*/ |
|
160 |
alSourceStop (Sources[index]); |
|
161 |
alSourcef (Sources[index], AL_GAIN, old_gain); |
|
162 |
||
2210 | 163 |
#ifndef _WIN32 |
2259 | 164 |
pthread_exit(NULL); |
2210 | 165 |
#else |
2259 | 166 |
_endthread(); |
2210 | 167 |
#endif |
2259 | 168 |
return 0; |
169 |
} |
|
170 |
||
171 |
||
2191 | 172 |
#ifdef __CPLUSPLUS |
173 |
} |
|
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2213
diff
changeset
|
174 |
#endif |