author | unc0rr |
Fri, 16 Oct 2009 13:01:28 +0000 | |
changeset 2482 | 36c44f4156ad |
parent 2468 | 0b62498c201a |
permissions | -rw-r--r-- |
2445 | 1 |
/* |
2 |
* OpenAL Bridge - a simple portable library for OpenAL interface |
|
3 |
* Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>, |
|
4 |
* Mario Liebisch <mario.liebisch+hw@googlemail.com> |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU Lesser General Public License as published by |
|
8 |
* the Free Software Foundation; version 2 of the License |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU Lesser General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU Lesser General Public License |
|
16 |
* along with this program; if not, write to the Free Software |
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
18 |
*/ |
|
19 |
||
2446 | 20 |
#include "ssound.h" |
21 |
#include "loaders.h" |
|
2445 | 22 |
|
2468
0b62498c201a
openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents:
2454
diff
changeset
|
23 |
extern SSound_t aSounds[MAX_SOUNDS]; |
2445 | 24 |
extern ALuint sources[MAX_SOURCES]; |
25 |
extern const ALfloat NV[3]; |
|
26 |
extern const ALfloat LO[6]; |
|
2468
0b62498c201a
openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents:
2454
diff
changeset
|
27 |
extern int iNumSounds; |
2446 | 28 |
extern char *prog; |
2445 | 29 |
|
30 |
char SSound_load (SSound_t* pSound, const char* cFilename) { |
|
31 |
uint32_t magic; |
|
32 |
ALenum format; |
|
33 |
ALsizei bitsize, freq; |
|
34 |
char *data; |
|
35 |
FILE* fp; |
|
36 |
||
37 |
snprintf(pSound->Filename, 256, "%s", cFilename); |
|
38 |
pSound->source = -1; |
|
39 |
alGenBuffers(1, &pSound->Buffer); |
|
40 |
||
41 |
if(alGetError() != AL_NO_ERROR) { |
|
42 |
fprintf(stderr, "CSound: Couldn't create buffer.\n"); |
|
43 |
return 0; |
|
44 |
} |
|
45 |
||
46 |
fp = fopen(pSound->Filename, "rb"); |
|
47 |
||
48 |
if(!fp) { |
|
49 |
fprintf(stderr, "CSound: Couldn't open file for reading.\n"); |
|
50 |
return 0; |
|
51 |
} |
|
52 |
||
53 |
if(fread(&magic, sizeof(uint32_t), 1, fp) < 1) |
|
54 |
{ |
|
55 |
fclose(fp); |
|
56 |
fprintf(stderr, "CSound: Couldn't read file header.\n"); |
|
57 |
return 0; |
|
58 |
} |
|
59 |
fclose(fp); |
|
60 |
||
61 |
switch (ENDIAN_BIG_32(magic)) { |
|
62 |
case OGG_FILE_FORMAT: |
|
63 |
load_oggvorbis (pSound->Filename, &format, &data, &bitsize, &freq); |
|
64 |
break; |
|
65 |
case WAV_FILE_FORMAT: |
|
66 |
load_wavpcm (pSound->Filename, &format, &data, &bitsize, &freq); |
|
67 |
break; |
|
68 |
default: |
|
69 |
errno = EINVAL; |
|
70 |
err_ret ("(%s) ERROR - File format (%08X) not supported", prog, ENDIAN_BIG_32(magic)); |
|
71 |
return 0; |
|
72 |
break; |
|
73 |
} |
|
74 |
||
75 |
alBufferData(pSound->Buffer, format, data, bitsize, freq); |
|
76 |
if(alGetError() != AL_NO_ERROR) |
|
77 |
{ |
|
78 |
fprintf(stderr, "CSound: Couldn't write buffer data.\n"); |
|
79 |
return 0; |
|
80 |
} |
|
81 |
free(data); |
|
82 |
||
83 |
return 1; |
|
84 |
} |
|
85 |
||
86 |
void SSound_close(SSound_t* pSound) |
|
87 |
{ |
|
88 |
SSound_stop(pSound); |
|
89 |
alDeleteBuffers(1, &pSound->Buffer); |
|
2454
c8b1fb10003c
fix a potential bug when playing two sounds at the same time (or very near)
koda
parents:
2446
diff
changeset
|
90 |
return; |
2445 | 91 |
} |
92 |
||
93 |
void SSound_play(SSound_t* pSound, const char bLoop) { |
|
2468
0b62498c201a
openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents:
2454
diff
changeset
|
94 |
int i, j; |
2445 | 95 |
|
96 |
if(pSound->source == -1) // need a new source |
|
97 |
{ |
|
98 |
for(i = 0; i < MAX_SOURCES; i++) |
|
99 |
{ |
|
100 |
ALint state; |
|
101 |
alGetSourcei(sources[i], AL_SOURCE_STATE, &state); |
|
102 |
if(state != AL_PLAYING && state != AL_PAUSED) |
|
103 |
{ |
|
104 |
#ifdef DEBUG |
|
105 |
printf("using source %d (state 0x%x) for buffer.\n", i, state); |
|
106 |
#endif |
|
107 |
alSourceStop(sources[pSound->source]); |
|
108 |
alGetError(); |
|
2468
0b62498c201a
openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents:
2454
diff
changeset
|
109 |
|
0b62498c201a
openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents:
2454
diff
changeset
|
110 |
// lookup buffers associated with this source and reset them |
0b62498c201a
openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents:
2454
diff
changeset
|
111 |
for(j = 0; j < iNumSounds; j++) |
0b62498c201a
openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents:
2454
diff
changeset
|
112 |
if(aSounds[j].source == i) |
0b62498c201a
openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents:
2454
diff
changeset
|
113 |
aSounds[j].source = -1; |
2445 | 114 |
break; |
115 |
} |
|
116 |
} |
|
117 |
if(i == MAX_SOURCES) // no available source found; skip |
|
118 |
{ |
|
119 |
#ifdef DEBUG |
|
120 |
printf("no source to play buffer %d!\n", i); |
|
121 |
#endif |
|
122 |
return; |
|
123 |
} |
|
124 |
pSound->source = i; |
|
125 |
} |
|
126 |
else // reuse already playing source |
|
127 |
{ |
|
128 |
alSourceStop(sources[pSound->source]); |
|
129 |
} |
|
130 |
alSourcei (sources[pSound->source], AL_BUFFER, pSound->Buffer); |
|
131 |
alSourcef (sources[pSound->source], AL_PITCH, 1.0f); |
|
132 |
alSourcef (sources[pSound->source], AL_GAIN, 1.0f); |
|
133 |
alSourcefv(sources[pSound->source], AL_POSITION, NV ); |
|
134 |
alSourcefv(sources[pSound->source], AL_VELOCITY, NV ); |
|
135 |
alSourcei (sources[pSound->source], AL_LOOPING, bLoop ); |
|
136 |
alSourcePlay(sources[pSound->source]); |
|
137 |
||
138 |
if((i = alGetError()) != AL_NO_ERROR) |
|
139 |
{ |
|
140 |
fprintf(stderr, "CSound: SourcePlay error 0x%4x in source %d\n", i, pSound->source); |
|
141 |
} |
|
142 |
#ifdef DEBUG |
|
143 |
fprintf(stderr, "play %s%s [%d]\n", pSound->Filename, bLoop ? " forever" : " once", pSound->source); |
|
144 |
#endif |
|
2454
c8b1fb10003c
fix a potential bug when playing two sounds at the same time (or very near)
koda
parents:
2446
diff
changeset
|
145 |
usleep(0); |
c8b1fb10003c
fix a potential bug when playing two sounds at the same time (or very near)
koda
parents:
2446
diff
changeset
|
146 |
return; |
2445 | 147 |
} |
148 |
||
149 |
void SSound_pause(const SSound_t* pSound) { |
|
150 |
if(pSound->source == -1) // not playing |
|
151 |
return; |
|
152 |
alSourcePause(sources[pSound->source]); |
|
153 |
#ifdef DEBUG |
|
154 |
fprintf(stderr, "pause %s\n", pSound->Filename); |
|
155 |
#endif |
|
2454
c8b1fb10003c
fix a potential bug when playing two sounds at the same time (or very near)
koda
parents:
2446
diff
changeset
|
156 |
return; |
2445 | 157 |
} |
158 |
||
159 |
void SSound_continue(const SSound_t* pSound) { |
|
160 |
if(pSound->source == -1) // not playing |
|
161 |
return; |
|
162 |
alSourcePlay(sources[pSound->source]); |
|
163 |
#ifdef DEBUG |
|
164 |
fprintf(stderr, "pause %s\n", pSound->Filename); |
|
165 |
#endif |
|
2454
c8b1fb10003c
fix a potential bug when playing two sounds at the same time (or very near)
koda
parents:
2446
diff
changeset
|
166 |
return; |
2445 | 167 |
} |
168 |
||
169 |
void SSound_stop(SSound_t* pSound) { |
|
170 |
if(pSound->source == -1) // not playing |
|
171 |
return; |
|
172 |
alSourceStop(sources[pSound->source]); |
|
173 |
pSound->source = -1; |
|
174 |
#ifdef DEBUG |
|
175 |
fprintf(stderr, "stop %s\n", pSound->Filename); |
|
176 |
#endif |
|
2454
c8b1fb10003c
fix a potential bug when playing two sounds at the same time (or very near)
koda
parents:
2446
diff
changeset
|
177 |
return; |
2445 | 178 |
} |
179 |
||
180 |
void SSound_volume(const SSound_t* pSound, const float fPercentage) { |
|
181 |
if(pSound->source == -1) // not playing |
|
182 |
return; |
|
183 |
alSourcef(sources[pSound->source], AL_GAIN, fPercentage); |
|
2454
c8b1fb10003c
fix a potential bug when playing two sounds at the same time (or very near)
koda
parents:
2446
diff
changeset
|
184 |
return; |
2445 | 185 |
} |