33 if (numClips
>=clipsSize
) {
34 clips
= realloc(clips
,clipsSize
*2*sizeof(clip
));
42 void removeClip(int pos
) {
43 for (;pos
<numClips
-1;pos
++)
44 clips
[pos
]=clips
[pos
+1];
46 if (numClips
>20&&numClips
<clipsSize
/4) {
47 clips
= realloc(clips
,clipsSize
/2*sizeof(clip
));
52 int findClipstr(char * name
) {
55 if (strcmp(name
,clips
[i
].fileName
)==0)
60 int findClipi(int id
) {
63 if (clips
[i
].bufId
==id
)
68 int addSource(ALuint id
) {
69 if (numSources
>=sourcesSize
) {
70 sources
= realloc(sources
,sizeof(ALuint
)*sourcesSize
*2);
73 sources
[numSources
]=id
;
78 void removeSource(int pos
) {
79 for (;pos
<numSources
-1;pos
++)
80 sources
[pos
]=sources
[pos
+1];
82 if (numSources
>20&&numSources
<sourcesSize
/4) {
83 sources
= realloc(sources
,sizeof(ALuint
)*sourcesSize
/2);
88 int findSource(int id
) {
90 for (i
= 0; i
< numSources
;i
++)
96 void sourcecall(void *data
, ALuint source
) {
99 int timercall(int ms
, void * data
) {
114 DECLSPEC
void initAudio() {
118 attrlist
[0] = ALC_FREQUENCY
;
121 alureInitDevice(NULL
,attrlist
);
124 clips
= malloc(sizeof(clip
)*clipsSize
);
125 sources
= malloc(sizeof(ALuint
)*sourcesSize
);
126 updater
= addTimer(10,timercall
,NULL
);
128 alGenFilters(1,&lowpass
);
129 alFilteri(lowpass
,AL_FILTER_TYPE
,AL_FILTER_LOWPASS
);
130 alFilterf(lowpass
,AL_LOWPASS_GAINHF
,0.5f
);
134 DECLSPEC
void terminateAudio() {
136 cancelTimer(updater
);
138 alDeleteSources(numSources
,sources
);
139 for (i
= 0; i
< numClips
;i
++) {
140 free(clips
[i
].fileName
);
141 if (clips
[i
].stream
==NULL
)
142 alDeleteBuffers(1,&clips
[i
].bufId
);
144 alureDestroyStream(clips
[i
].stream
,4096,&clips
[i
].bufId
);
148 alDeleteFilters(1,&lowpass
);
149 alureShutdownDevice();
153 void playsource(int sound
,int loop
) {
155 alGetSourcei(sound
, AL_BUFFER
, &bufid
);
156 int pos
= findClipi(bufid
);
159 if (clips
[pos
].stream
==NULL
)
162 alurePlaySourceStream(sound
,clips
[pos
].stream
,4,loop
,sourcecall
,NULL
);
166 DECLSPEC
int loadSound(char * file
) {
168 int pos
= findClipstr(file
);
171 bufid
= alureCreateBufferFromFile(file
);
172 clip c
= {strdup(file
),bufid
,0,NULL
};
176 bufid
= clips
[pos
].bufId
;
178 alGenSources(1, &soundid
);
179 alSourcei(soundid
, AL_BUFFER
, bufid
);
186 DECLSPEC
int streamSound(char * file
) {
188 int pos
= findClipstr(file
);
191 alureStream
* s
= alureCreateStreamFromFile(file
,4096,1,&bufid
);
192 clip c
= {strdup(file
),bufid
,0,s
};
196 bufid
= clips
[pos
].bufId
;
198 alGenSources(1, &soundid
);
199 alSourcei(soundid
, AL_BUFFER
, bufid
);
206 DECLSPEC
void unloadSound(int sound
) {
208 if (!alIsSource(sound
)) {
209 printf("bcl: Invalid source\n");
213 alGetSourcei(sound
,AL_BUFFER
,&id
);
215 removeSource(findSource(sound
));
216 alDeleteSources(1, &sound
);
218 if (clips
[pos
].count
==0) {
219 free(clips
[pos
].fileName
);
220 if (clips
[pos
].stream
==NULL
)
221 alDeleteBuffers(1,&clips
[pos
].bufId
);
223 alureDestroyStream(clips
[pos
].stream
,4096,&clips
[pos
].bufId
);
229 DECLSPEC
void setSoundPosition(int sound
, float x
, float y
, float z
) {
230 alSource3f(sound
,AL_POSITION
,x
,y
,z
);
233 DECLSPEC
void setSoundPitch(int sound
, float pitch
) {
234 alSourcef(sound
,AL_PITCH
,pitch
);
237 DECLSPEC
void playSound(int sound
) {
238 alSourcei(sound
,AL_LOOPING
,AL_FALSE
);
242 DECLSPEC
void loopSound(int sound
) {
243 alSourcei(sound
,AL_LOOPING
,AL_TRUE
);
244 playsource(sound
,-1);
247 DECLSPEC
void stopSound(int sound
) {
248 alureStopSource(sound
,0);
252 DECLSPEC
void setListenerPosition(float x
, float y
, float z
) {
253 alListener3f(AL_POSITION
, x
, y
, z
);
256 DECLSPEC
void setSoundLowpass(int sound
, int lp
) {
258 alSourcei(sound
,AL_DIRECT_FILTER
,lowpass
);
260 alSourcei(sound
,AL_DIRECT_FILTER
,AL_FILTER_NULL
);
263 DECLSPEC
int getSoundLowpass(int sound
) {
265 alGetSourcei(sound
,AL_DIRECT_FILTER
,&ret
);
269 DECLSPEC
int isSoundPlaying(int sound
) {
270 if (!alIsSource(sound
)) {
271 printf("bcl: Invalid source %d\n",sound
);
275 alGetSourcei(sound
,AL_SOURCE_STATE
,&ret
);
276 return ret
==AL_PLAYING
;
279 DECLSPEC
void setSoundGain(int sound
, float gain
) {
280 alSourcef(sound
,AL_GAIN
,gain
);
284 void setListenerOrientation(float x1
, float y1
, float z1
, float x2
, float y2
, float z2
) {
285 float attrs
[6] = {x1
,y1
,z1
,x2
,y2
,z2
};
286 alListenerfv(AL_ORIENTATION
,attrs
);