author | smxx |
Tue, 04 May 2010 15:43:31 +0000 | |
changeset 3418 | 8545f646a40d |
parent 3364 | e5403e2bf02c |
child 3487 | b1d00f1950c8 |
permissions | -rw-r--r-- |
3353 | 1 |
/* |
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
2 |
* OpenAL Bridge - a simple portable library for OpenAL interface |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
3 |
* Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com> |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
4 |
* |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or modify |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
6 |
* it under the terms of the GNU Lesser General Public License as published by |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
7 |
* the Free Software Foundation; version 2 of the License |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
8 |
* |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
12 |
* GNU Lesser General Public License for more details. |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
13 |
* |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
14 |
* You should have received a copy of the GNU Lesser General Public License |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
17 |
*/ |
3353 | 18 |
|
19 |
#include "wrappers.h" |
|
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
20 |
#include "openalbridge_t.h" |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
21 |
|
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
22 |
extern ALint *Sources; |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
23 |
|
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
24 |
void *Malloc (size_t nbytes) { |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
25 |
void *aptr; |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
26 |
|
3364 | 27 |
if ((aptr = malloc(nbytes)) == NULL) { |
28 |
fprintf(stderr,"(Bridge Fatal Error) - not enough memory"); |
|
29 |
abort(); |
|
30 |
} |
|
31 |
||
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
32 |
return aptr; |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
33 |
} |
3353 | 34 |
|
35 |
||
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
36 |
void *Realloc (void *aptr, size_t nbytes) { |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
37 |
aptr = realloc(aptr, nbytes); |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
38 |
|
3364 | 39 |
if (aptr == NULL) { |
40 |
fprintf(stderr,"(Bridge Fatal Error) - not enough memory"); |
|
41 |
abort(); |
|
42 |
} |
|
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
43 |
|
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
44 |
return aptr; |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
45 |
} |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
46 |
|
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
47 |
|
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
48 |
FILE *Fopen (const char *fname, char *mode) { |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
49 |
FILE *fp; |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
50 |
|
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
51 |
fp = fopen(fname,mode); |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
52 |
if (fp == NULL) |
3364 | 53 |
fprintf(stderr,"(Bridge Error) - can't open file %s in mode '%s'", fname, mode); |
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
54 |
|
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
55 |
return fp; |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
56 |
} |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
57 |
|
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
58 |
|
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
59 |
void helper_fade(void *tmp) { |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
60 |
ALfloat gain; |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
61 |
ALfloat target_gain; |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
62 |
fade_t *fade; |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
63 |
uint32_t index; |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
64 |
uint16_t quantity; |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
65 |
al_fade_t type; |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
66 |
|
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
67 |
fade = tmp; |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
68 |
index = fade->index; |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
69 |
quantity = fade->quantity; |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
70 |
type = fade->type; |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
71 |
free (fade); |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
72 |
|
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
73 |
if (type == AL_FADE_IN) { |
3353 | 74 |
#ifdef DEBUG |
75 |
err_msg("(%s) INFO - Fade-in in progress [index %d quantity %d]", prog, index, quantity); |
|
76 |
#endif |
|
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
77 |
|
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
78 |
// save the volume desired after the fade |
3353 | 79 |
alGetSourcef(Sources[index], AL_GAIN, &target_gain); |
80 |
if (target_gain > 1.0f || target_gain <= 0.0f) |
|
81 |
target_gain = 1.0f; |
|
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
82 |
|
3353 | 83 |
alSourcePlay(Sources[index]); |
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
84 |
|
3353 | 85 |
for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) { |
86 |
#ifdef TRACE |
|
87 |
err_msg("(%s) DEBUG - Fade-in set gain to %f", gain); |
|
88 |
#endif |
|
89 |
alSourcef(Sources[index], AL_GAIN, gain); |
|
90 |
usleep(10000); |
|
91 |
} |
|
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
92 |
} else { |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
93 |
alGetSourcef(Sources[index], AL_GAIN, &target_gain); |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
94 |
|
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
95 |
for (gain = target_gain; gain >= 0.00f; gain -= (float) quantity/10000) { |
3353 | 96 |
#ifdef TRACE |
97 |
err_msg("(%s) DEBUG - Fade-out set gain to %f", gain); |
|
98 |
#endif |
|
99 |
alSourcef(Sources[index], AL_GAIN, gain); |
|
100 |
usleep(10000); |
|
101 |
} |
|
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
102 |
|
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
103 |
if (AL_NO_ERROR != alGetError()) |
3364 | 104 |
fprintf(stderr,"(Bridge Warning) - Failed to set fade-out effect"); |
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
105 |
|
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
106 |
// stop that sound and reset its volume |
3353 | 107 |
alSourceStop (Sources[index]); |
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
108 |
alSourcef (Sources[index], AL_GAIN, target_gain); |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
109 |
} |
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
110 |
|
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
111 |
if (AL_NO_ERROR != alGetError()) |
3364 | 112 |
fprintf(stderr,"(Bridge Warning) - Failed to set fade effect"); |
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
113 |
|
3353 | 114 |
#ifndef _WIN32 |
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
115 |
pthread_exit(NULL); |
3353 | 116 |
#else |
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
117 |
_endthread(); |
3353 | 118 |
#endif |
119 |
} |
|
3362
8d3b4d19ce27
in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents:
3353
diff
changeset
|
120 |