author | koda |
Mon, 29 Jun 2009 20:01:05 +0000 | |
changeset 2211 | 288360b78f30 |
parent 2210 | 1cb7118a77dd |
child 2212 | 6b5da1a2765a |
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 |
||
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 |
|
37 |
FILE *Fopen (const char *fname, char *mode) { |
|
2191 | 38 |
FILE *fp; |
39 |
if ((fp=fopen(fname,mode)) == NULL) |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
40 |
fprintf (stderr, "ERROR: can't open file %s in mode '%s'\n", fname, mode); |
2191 | 41 |
return fp; |
42 |
} |
|
43 |
||
2210 | 44 |
|
2191 | 45 |
ALint AlGetError (const char *str) { |
46 |
ALenum error; |
|
47 |
||
48 |
error = alGetError(); |
|
49 |
if (error != AL_NO_ERROR) { |
|
50 |
fprintf(stderr, str, error); |
|
51 |
return -2; |
|
52 |
} else |
|
53 |
return AL_TRUE; |
|
54 |
} |
|
55 |
||
2210 | 56 |
|
57 |
#ifndef _WIN32 |
|
58 |
void *helper_fadein(void *tmp) |
|
59 |
#else |
|
60 |
void WINAPI helper_fadein(void *tmp) |
|
61 |
#endif |
|
62 |
{ |
|
63 |
ALfloat gain; |
|
64 |
ALfloat target_gain; |
|
65 |
fade_t *fade; |
|
66 |
int index; |
|
67 |
unsigned int quantity; |
|
68 |
||
69 |
fade = tmp; |
|
70 |
index = fade->index; |
|
71 |
quantity = fade->quantity; |
|
72 |
free (fade); |
|
73 |
||
74 |
#ifdef DEBUG |
|
75 |
fprintf(stderr, "Fade-out: index %d quantity %d\n", index, quantity); |
|
76 |
#endif |
|
77 |
||
78 |
//save the volume desired after the fade |
|
79 |
alGetSourcef(Sources[index], AL_GAIN, &target_gain); |
|
80 |
if (target_gain > 1.0f || target_gain <= 0.0f) |
|
81 |
target_gain = 1.0f; |
|
82 |
||
83 |
alSourcePlay(Sources[index]); |
|
84 |
||
85 |
for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) { |
|
86 |
#ifdef DEBUG |
|
87 |
fprintf(stderr, "Fade-in: Set gain to: %f\n", gain); |
|
88 |
#endif |
|
89 |
alSourcef(Sources[index], AL_GAIN, gain); |
|
90 |
usleep(10000); |
|
91 |
} |
|
92 |
||
93 |
AlGetError("ERROR %d: Setting fade in volume\n"); |
|
94 |
||
95 |
#ifndef _WIN32 |
|
96 |
pthread_exit(NULL); |
|
97 |
#else |
|
98 |
_endthread(); |
|
99 |
#endif |
|
100 |
} |
|
101 |
||
102 |
||
103 |
#ifndef _WIN32 |
|
104 |
void *helper_fadeout(void *tmp) |
|
105 |
#else |
|
106 |
void WINAPI helper_fadeout(void *tmp) |
|
107 |
#endif |
|
108 |
{ |
|
109 |
ALfloat gain; |
|
110 |
ALfloat old_gain; |
|
111 |
fade_t *fade; |
|
112 |
int index; |
|
113 |
unsigned int quantity; |
|
114 |
||
115 |
fade = tmp; |
|
116 |
index = fade->index; |
|
117 |
quantity = fade->quantity; |
|
118 |
free(fade); |
|
119 |
||
120 |
#ifdef DEBUG |
|
121 |
fprintf(stderr, "Fade-out: index %d quantity %d\n", index, quantity); |
|
122 |
#endif |
|
123 |
||
124 |
alGetSourcef(Sources[index], AL_GAIN, &old_gain); |
|
125 |
||
126 |
for (gain = old_gain; gain >= 0.00f; gain -= (float) quantity/10000) { |
|
127 |
#ifdef DEBUG |
|
128 |
fprintf(stderr, "Fade-out: Set gain to %f\n", gain); |
|
129 |
#endif |
|
130 |
alSourcef(Sources[index], AL_GAIN, gain); |
|
131 |
usleep(10000); |
|
132 |
} |
|
133 |
||
134 |
AlGetError("ERROR %d: Setting fade out volume\n"); |
|
135 |
||
136 |
//stop that sound and reset its volume |
|
137 |
alSourceStop (Sources[index]); |
|
138 |
alSourcef (Sources[index], AL_GAIN, old_gain); |
|
139 |
||
140 |
#ifndef _WIN32 |
|
141 |
pthread_exit(NULL); |
|
142 |
#else |
|
143 |
_endthread(); |
|
144 |
#endif |
|
145 |
} |
|
146 |
||
2191 | 147 |
#ifdef __CPLUSPLUS |
148 |
} |
|
149 |
#endif |