4 #include "files.h" // F_findres F_getreslen
5 #include "memory.h" // M_lock M_unlock
6 #include "misc.h" // int2host
7 #include "error.h" // logo
10 # include <OpenAL/al.h>
11 # include <OpenAL/alc.h>
17 #include "SDL.h" // SDL_BuildAudioCVT SDL_ConvertAudio
19 #include <stdlib.h> // malloc
20 #include <string.h> // memcpy
22 #define TAG_OAL1 0x4F414C31
23 #define MAX_CHANNELS 8
35 typedef struct openal_snd
{
40 typedef struct openal_channel
{
50 static ALCdevice
*device
;
51 static ALCcontext
*context
;
52 static ALuint sources
[MAX_CHANNELS
];
56 void S_initmusic (void) {
60 void S_donemusic (void) {
64 void S_startmusic (int time
) {
68 void S_stopmusic (void) {
72 void S_volumemusic (int v
) {
76 void F_loadmus (char n
[8]) {
80 void F_freemus (void) {
84 void S_updatemusic (void) {
90 static void convert_this_ext (Uint32 src_format
, int src_chan
, int src_rate
, Uint32 dst_format
, int dst_chan
, int dst_rate
, const void *buf
, int len
, void **maxbuf
, int *maxlen
) {
94 if (SDL_BuildAudioCVT(&cvt
, src_format
, src_chan
, src_rate
, dst_format
, dst_chan
, dst_rate
) != -1) {
95 *maxlen
= len
* cvt
.len_mult
;
96 *maxbuf
= malloc(*maxlen
);
97 if (*maxbuf
!= NULL
) {
98 memcpy(*maxbuf
, buf
, len
);
101 if (SDL_ConvertAudio(&cvt
) == 0) {
102 *maxlen
= len
* cvt
.len_ratio
;
112 static openal_snd
*new_openal_snd (const void *data
, dword len
, dword rate
, dword lstart
, dword llen
, int sign
) {
115 openal_snd
*snd
= NULL
;
116 void *newdata
= NULL
;
118 // for some reason 8bit formats makes psshshshsh
119 // TODO do this without SDL
120 convert_this_ext(sign
? AUDIO_S8
: AUDIO_U8
, 1, rate
, AUDIO_S16SYS
, 1, rate
, data
, len
, &newdata
, &newlen
);
121 if (newdata
!= NULL
) {
122 alGenBuffers(1, &buffer
);
123 if (alGetError() == AL_NO_ERROR
) {
124 alBufferData(buffer
, AL_FORMAT_MONO16
, newdata
, newlen
, rate
);
125 if (alGetError() == AL_NO_ERROR
) {
126 snd
= malloc(sizeof(openal_snd
));
128 snd
->base
.tag
= TAG_OAL1
;
129 snd
->buffer
= buffer
;
132 alDeleteBuffers(1, &buffer
);
135 alDeleteBuffers(1, &buffer
);
143 snd_t
*S_get (int id
) {
145 openal_snd
*snd
= NULL
;
146 if (context
!= NULL
) {
148 if (handle
!= NULL
) {
150 dword len
= F_getreslen(id
);
157 dword hdr_len
= int2host(hdr
->len
);
158 dword hdr_rate
= int2host(hdr
->rate
);
159 dword hdr_lstart
= int2host(hdr
->lstart
);
160 dword hdr_llen
= int2host(hdr
->llen
);
161 if (hdr_len
<= len
- 8 && hdr_lstart
+ hdr_llen
<= len
- 16) {
170 snd
= new_openal_snd(data
, len
, rate
, lstart
, llen
, sign
);
177 snd_t
*S_load (const char name
[8]) {
178 return S_get(F_findres(name
));
181 void S_free (snd_t
*s
) {
184 openal_snd
*snd
= (openal_snd
*)s
;
186 assert(snd
->base
.tag
== TAG_OAL1
);
187 if (context
!= NULL
) {
188 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
189 alGetSourcei(sources
[i
], AL_BUFFER
, &h
);
190 if (h
== snd
->buffer
) {
191 alSourceStop(sources
[i
]);
192 alSourcei(sources
[i
], AL_BUFFER
, 0);
195 alDeleteBuffers(1, &snd
->buffer
);
196 assert(alGetError() == AL_NO_ERROR
);
204 assert(device
== NULL
&& context
== NULL
);
205 const ALCint attrs
[] = {ALC_MONO_SOURCES
, MAX_CHANNELS
, 0};
206 device
= alcOpenDevice(NULL
);
207 if (device
!= NULL
) {
208 context
= alcCreateContext(device
, attrs
);
209 if (context
!= NULL
) {
210 if (alcMakeContextCurrent(context
)) {
211 alGenSources(MAX_CHANNELS
, sources
);
212 if (alGetError() == AL_NO_ERROR
) {
213 alListenerf(AL_GAIN
, 1);
214 alListener3f(AL_POSITION
, 0, 0, 0);
215 alListener3f(AL_VELOCITY
, 0, 0, 0);
217 logo("S_init: unable to create OpenAL sources\n");
218 alcDestroyContext(context
);
219 alcCloseDevice(device
);
224 logo("S_init: unable to make OpenAL context current\n");
225 alcDestroyContext(context
);
226 alcCloseDevice(device
);
231 logo("S_init: unable to create OpenAL context\n");
232 alcCloseDevice(device
);
236 logo("S_init: unable to open OpenAL device\n");
241 if (context
!= NULL
) {
242 alcMakeContextCurrent(NULL
);
243 alcDestroyContext(context
);
244 alcCloseDevice(device
);
250 short S_play (snd_t
*s
, short c
, short v
) {
251 assert(c
>= 0 && c
<= MAX_CHANNELS
);
252 assert(v
>= 0 && v
< 256);
256 if (context
!= NULL
&& s
!= NULL
) {
257 openal_snd
*snd
= (openal_snd
*)s
;
258 assert(snd
->base
.tag
== TAG_OAL1
);
260 for (channel
= 0; channel
< MAX_CHANNELS
; channel
++) {
262 alGetSourcei(sources
[channel
], AL_SOURCE_STATE
, &state
);
263 if (state
== AL_STOPPED
|| state
== AL_INITIAL
) {
270 if (channel
< MAX_CHANNELS
) {
271 source
= sources
[channel
];
272 alSourceStop(source
);
273 alSourcei(source
, AL_BUFFER
, snd
->buffer
);
274 alSourcef(source
, AL_PITCH
, 1);
275 alSourcef(source
, AL_GAIN
, v
/ 255.0);
276 alSource3f(source
, AL_POSITION
, 0, 0, 0);
277 alSource3f(source
, AL_VELOCITY
, 0, 0, 0);
278 alSourcei(source
, AL_LOOPING
, AL_FALSE
);
279 alSourcePlay(source
);
285 void S_stop (short c
) {
286 assert(c
>= 0 && c
<= MAX_CHANNELS
);
287 if (context
!= NULL
) {
289 alSourceStop(sources
[c
- 1]);
294 void S_volume (int v
) {
295 assert(v
>= 0 && v
<= 128);
297 if (context
!= NULL
) {
298 alListenerf(AL_GAIN
, v
/ 128.0);
305 if (context
!= NULL
) {
306 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
309 alGetSourcei(sources
[i
], AL_SOURCE_STATE
, &state
);
310 } while (state
== AL_PLAYING
);