author | koda |
Sun, 11 Oct 2009 16:03:56 +0000 | |
changeset 2415 | 35d09cbf819a |
parent 2259 | ca42efdce3ce |
child 2418 | 538a777f90c4 |
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; |
|
2415 | 29 |
|
2259 | 30 |
if ((aptr = malloc(nbytes)) == NULL) { |
2415 | 31 |
fprintf(stderr, "ERROR 'malloc()': not enough memory\n"); |
2259 | 32 |
exit(-1); |
33 |
} |
|
34 |
return aptr; |
|
35 |
} |
|
36 |
||
37 |
||
38 |
void *Realloc (void *aptr, size_t nbytes) { |
|
39 |
aptr = realloc(aptr, nbytes); |
|
40 |
||
41 |
if (aptr == NULL) { |
|
2415 | 42 |
fprintf(stderr, "ERROR 'realloc()': not enough memory\n"); |
2259 | 43 |
free(aptr); |
44 |
exit(-1); |
|
45 |
} |
|
46 |
return aptr; |
|
47 |
} |
|
48 |
||
49 |
||
50 |
FILE *Fopen (const char *fname, char *mode) { |
|
51 |
FILE *fp; |
|
52 |
if ((fp=fopen(fname,mode)) == NULL) |
|
2415 | 53 |
fprintf (stderr, "ERROR 'fopen()': can't open file %s in mode '%s'\n", fname, mode); |
2259 | 54 |
return fp; |
55 |
} |
|
56 |
||
2415 | 57 |
/*TODO make a proper error reporting routine*/ |
2259 | 58 |
ALint AlGetError (const char *str) { |
59 |
ALenum error; |
|
60 |
||
61 |
error = alGetError(); |
|
62 |
if (error != AL_NO_ERROR) { |
|
63 |
fprintf(stderr, str, error); |
|
64 |
return -2; |
|
65 |
} else |
|
66 |
return AL_TRUE; |
|
67 |
} |
|
68 |
||
69 |
ALint AlGetError2 (const char *str, int num) { |
|
70 |
ALenum error; |
|
71 |
||
72 |
error = alGetError(); |
|
73 |
if (error != AL_NO_ERROR) { |
|
74 |
fprintf(stderr, str, error, num); |
|
75 |
return -2; |
|
76 |
} else |
|
77 |
return AL_TRUE; |
|
78 |
} |
|
79 |
||
2415 | 80 |
void *helper_fadein(void *tmp) { |
2259 | 81 |
ALfloat gain; |
82 |
ALfloat target_gain; |
|
83 |
fade_t *fade; |
|
84 |
uint32_t index; |
|
85 |
uint16_t quantity; |
|
86 |
||
87 |
fade = tmp; |
|
88 |
index = fade->index; |
|
89 |
quantity = fade->quantity; |
|
90 |
free (fade); |
|
91 |
||
2210 | 92 |
#ifdef DEBUG |
2415 | 93 |
fprintf(stderr, "Fade-in: index %d quantity %d\n", index, quantity); |
2210 | 94 |
#endif |
2259 | 95 |
|
96 |
/*save the volume desired after the fade*/ |
|
97 |
alGetSourcef(Sources[index], AL_GAIN, &target_gain); |
|
98 |
if (target_gain > 1.0f || target_gain <= 0.0f) |
|
99 |
target_gain = 1.0f; |
|
100 |
||
101 |
alSourcePlay(Sources[index]); |
|
102 |
||
103 |
for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) { |
|
104 |
#ifdef DEBUG |
|
2415 | 105 |
fprintf(stderr, "Fade-in: set gain to: %f\n", gain); |
2259 | 106 |
#endif |
107 |
alSourcef(Sources[index], AL_GAIN, gain); |
|
108 |
usleep(10000); |
|
109 |
} |
|
110 |
||
2415 | 111 |
AlGetError("ERROR %d in 'helper_fadein()': Setting fade in volume\n"); |
2259 | 112 |
|
2210 | 113 |
#ifndef _WIN32 |
2259 | 114 |
pthread_exit(NULL); |
2210 | 115 |
#else |
2259 | 116 |
_endthread(); |
2210 | 117 |
#endif |
2259 | 118 |
return 0; |
119 |
} |
|
120 |
||
2415 | 121 |
void *helper_fadeout(void *tmp) { |
2259 | 122 |
ALfloat gain; |
123 |
ALfloat old_gain; |
|
124 |
fade_t *fade; |
|
125 |
uint32_t index; |
|
126 |
uint16_t quantity; |
|
127 |
||
128 |
fade = tmp; |
|
129 |
index = fade->index; |
|
130 |
quantity = fade->quantity; |
|
131 |
free(fade); |
|
132 |
||
2210 | 133 |
#ifdef DEBUG |
2259 | 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); |
|
2210 | 142 |
#endif |
2259 | 143 |
alSourcef(Sources[index], AL_GAIN, gain); |
144 |
usleep(10000); |
|
145 |
} |
|
146 |
||
2415 | 147 |
AlGetError("ERROR %d in 'helper_fadeout()': Setting fade out volume\n"); |
2259 | 148 |
|
149 |
/*stop that sound and reset its volume*/ |
|
150 |
alSourceStop (Sources[index]); |
|
151 |
alSourcef (Sources[index], AL_GAIN, old_gain); |
|
152 |
||
2210 | 153 |
#ifndef _WIN32 |
2259 | 154 |
pthread_exit(NULL); |
2210 | 155 |
#else |
2259 | 156 |
_endthread(); |
2210 | 157 |
#endif |
2259 | 158 |
return 0; |
159 |
} |
|
160 |
||
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 |