author | koda |
Sun, 11 Oct 2009 20:14:55 +0000 | |
changeset 2418 | 538a777f90c4 |
parent 2415 | 35d09cbf819a |
child 2419 | dbaaba09146d |
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 |
||
2418 | 21 |
|
2191 | 22 |
#ifdef __CPLUSPLUS |
23 |
extern "C" { |
|
24 |
#endif |
|
2418 | 25 |
|
26 |
extern ALint *Sources; |
|
2259 | 27 |
|
2418 | 28 |
void *Malloc (size_t nbytes) { |
29 |
void *aptr; |
|
30 |
||
31 |
if ((aptr = malloc(nbytes)) == NULL) |
|
32 |
err_dump("(%s) FATAL - not enough memory"); |
|
33 |
||
34 |
return aptr; |
|
2259 | 35 |
} |
2418 | 36 |
|
37 |
||
38 |
void *Realloc (void *aptr, size_t nbytes) { |
|
39 |
aptr = realloc(aptr, nbytes); |
|
40 |
||
41 |
if (aptr == NULL) |
|
42 |
err_dump("(%s) FATAL - not enough memory"); |
|
43 |
||
44 |
return aptr; |
|
45 |
} |
|
46 |
||
2259 | 47 |
|
2418 | 48 |
FILE *Fopen (const char *fname, char *mode) { |
49 |
FILE *fp; |
|
50 |
||
51 |
fp = fopen(fname,mode); |
|
52 |
if (fp == NULL) |
|
53 |
err_ret("(%s) ERROR - can't open file %s in mode '%s'", prog, fname, mode); |
|
54 |
return fp; |
|
55 |
} |
|
2259 | 56 |
|
2418 | 57 |
/*TODO make a proper error reporting routine*/ |
58 |
ALint AlGetError (const char *str) { |
|
59 |
ALenum error; |
|
60 |
||
61 |
error = alGetError(); |
|
62 |
if (error != AL_NO_ERROR) { |
|
63 |
err_msg(str, prog); |
|
64 |
return error; |
|
65 |
} else |
|
66 |
return AL_TRUE; |
|
67 |
} |
|
2259 | 68 |
|
2418 | 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; |
|
2259 | 78 |
} |
79 |
||
2418 | 80 |
void *helper_fadein(void *tmp) { |
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 |
||
92 |
#ifdef DEBUG |
|
93 |
err_msg("(%s) INFO - Fade-in in progress [index %d quantity %d]", prog, index, quantity); |
|
2210 | 94 |
#endif |
2418 | 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 TRACE |
|
105 |
err_msg("(%s) DEBUG - Fade-in set gain to %f", gain); |
|
2259 | 106 |
#endif |
2418 | 107 |
alSourcef(Sources[index], AL_GAIN, gain); |
108 |
usleep(10000); |
|
109 |
} |
|
110 |
||
111 |
AlGetError("(%s) WARN - Failed to set fade-in volume level"); |
|
112 |
||
113 |
#ifndef _WIN32 |
|
114 |
pthread_exit(NULL); |
|
115 |
#else |
|
116 |
_endthread(); |
|
2210 | 117 |
#endif |
2418 | 118 |
return 0; |
2259 | 119 |
} |
120 |
||
2418 | 121 |
void *helper_fadeout(void *tmp) { |
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 |
||
133 |
#ifdef DEBUG |
|
134 |
err_msg("(%s) INFO - Fade-out in progress [index %d quantity %d]", prog, 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 TRACE |
|
141 |
err_msg("(%s) DEBUG - Fade-out set gain to %f", gain); |
|
142 |
#endif |
|
143 |
alSourcef(Sources[index], AL_GAIN, gain); |
|
144 |
usleep(10000); |
|
145 |
} |
|
146 |
||
147 |
AlGetError("(%s) WARN - Failed to set fade-out volume level"); |
|
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 |
2418 | 154 |
pthread_exit(NULL); |
2210 | 155 |
#else |
2418 | 156 |
_endthread(); |
2210 | 157 |
#endif |
2418 | 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 |