author | nemo |
Wed, 31 Mar 2010 20:44:38 +0000 | |
changeset 3216 | d94262b521ae |
parent 2529 | 51e5df1c8462 |
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 |
|
2529 | 22 |
#ifdef __CPLUSPLUS |
23 |
extern "C" { |
|
24 |
#endif |
|
2418 | 25 |
|
2529 | 26 |
extern ALint *Sources; |
2259 | 27 |
|
2529 | 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; |
|
35 |
} |
|
36 |
||
2418 | 37 |
|
2529 | 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 |
} |
|
2418 | 46 |
|
2259 | 47 |
|
2529 | 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 |
||
55 |
return fp; |
|
56 |
} |
|
2259 | 57 |
|
2529 | 58 |
/*TODO make a proper error reporting routine*/ |
59 |
ALint AlGetError (const char *str) { |
|
60 |
ALenum error; |
|
61 |
||
62 |
error = alGetError(); |
|
63 |
if (error != AL_NO_ERROR) { |
|
64 |
err_msg(str, prog); |
|
65 |
return error; |
|
66 |
} else |
|
67 |
return AL_TRUE; |
|
68 |
} |
|
2259 | 69 |
|
2529 | 70 |
ALint AlGetError2 (const char *str, int num) { |
71 |
ALenum error; |
|
72 |
||
73 |
error = alGetError(); |
|
74 |
if (error != AL_NO_ERROR) { |
|
75 |
err_msg(str, prog, num); |
|
76 |
return error; |
|
77 |
} else |
|
78 |
return AL_TRUE; |
|
79 |
} |
|
80 |
||
81 |
void *helper_fadein(void *tmp) { |
|
2418 | 82 |
ALfloat gain; |
83 |
ALfloat target_gain; |
|
84 |
fade_t *fade; |
|
85 |
uint32_t index; |
|
86 |
uint16_t quantity; |
|
87 |
||
88 |
fade = tmp; |
|
89 |
index = fade->index; |
|
90 |
quantity = fade->quantity; |
|
91 |
free (fade); |
|
92 |
||
93 |
#ifdef DEBUG |
|
94 |
err_msg("(%s) INFO - Fade-in in progress [index %d quantity %d]", prog, index, quantity); |
|
2210 | 95 |
#endif |
2418 | 96 |
|
2494 | 97 |
/*save the volume desired after the fade*/ |
2418 | 98 |
alGetSourcef(Sources[index], AL_GAIN, &target_gain); |
2529 | 99 |
if (target_gain > 1.0f || target_gain <= 0.0f) |
2418 | 100 |
target_gain = 1.0f; |
101 |
||
102 |
alSourcePlay(Sources[index]); |
|
103 |
||
104 |
for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) { |
|
105 |
#ifdef TRACE |
|
106 |
err_msg("(%s) DEBUG - Fade-in set gain to %f", gain); |
|
2259 | 107 |
#endif |
2418 | 108 |
alSourcef(Sources[index], AL_GAIN, gain); |
109 |
usleep(10000); |
|
110 |
} |
|
111 |
||
112 |
AlGetError("(%s) WARN - Failed to set fade-in volume level"); |
|
113 |
||
114 |
#ifndef _WIN32 |
|
115 |
pthread_exit(NULL); |
|
116 |
#else |
|
117 |
_endthread(); |
|
2210 | 118 |
#endif |
2418 | 119 |
return 0; |
2259 | 120 |
} |
121 |
||
2418 | 122 |
void *helper_fadeout(void *tmp) { |
123 |
ALfloat gain; |
|
124 |
ALfloat old_gain; |
|
125 |
fade_t *fade; |
|
126 |
uint32_t index; |
|
127 |
uint16_t quantity; |
|
128 |
||
129 |
fade = tmp; |
|
130 |
index = fade->index; |
|
131 |
quantity = fade->quantity; |
|
132 |
free(fade); |
|
133 |
||
134 |
#ifdef DEBUG |
|
135 |
err_msg("(%s) INFO - Fade-out in progress [index %d quantity %d]", prog, index, quantity); |
|
136 |
#endif |
|
137 |
||
138 |
alGetSourcef(Sources[index], AL_GAIN, &old_gain); |
|
139 |
||
140 |
for (gain = old_gain; gain >= 0.00f; gain -= (float) quantity/10000) { |
|
141 |
#ifdef TRACE |
|
142 |
err_msg("(%s) DEBUG - Fade-out set gain to %f", gain); |
|
143 |
#endif |
|
144 |
alSourcef(Sources[index], AL_GAIN, gain); |
|
145 |
usleep(10000); |
|
146 |
} |
|
147 |
||
148 |
AlGetError("(%s) WARN - Failed to set fade-out volume level"); |
|
149 |
||
2494 | 150 |
/*stop that sound and reset its volume*/ |
2418 | 151 |
alSourceStop (Sources[index]); |
152 |
alSourcef (Sources[index], AL_GAIN, old_gain); |
|
153 |
||
2210 | 154 |
#ifndef _WIN32 |
2418 | 155 |
pthread_exit(NULL); |
2210 | 156 |
#else |
2418 | 157 |
_endthread(); |
2210 | 158 |
#endif |
2418 | 159 |
return 0; |
160 |
} |
|
161 |
||
2529 | 162 |
|
163 |
#ifdef __CPLUSPLUS |
|
164 |
} |
|
165 |
#endif |