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
{
46 static ALCdevice
*device
;
47 static ALCcontext
*context
;
48 static ALuint sources
[MAX_CHANNELS
];
52 const cfg_t
*MUS_args (void) {
56 const cfg_t
*MUS_conf (void) {
60 const menu_t
*MUS_menu (void) {
64 void MUS_init (void) {
68 void MUS_done (void) {
72 void MUS_start (int time
) {
76 void MUS_stop (void) {
80 void MUS_volume (int v
) {
84 void MUS_load (char n
[8]) {
88 void MUS_free (void) {
92 void MUS_update (void) {
98 static int sound_menu_handler (menu_msg_t
*msg
, const menu_t
*m
, void *data
, int i
) {
100 enum { VOLUME
, __NUM__
};
101 static const simple_menu_t sm
= {
102 GM_BIG
, "Sound", NULL
,
109 case GM_GETENTRY
: return GM_init_int0(msg
, GM_SCROLLER
, 0, 0, 0);
110 case GM_GETINT
: return GM_init_int(msg
, snd_vol
, 0, 128, 8);
111 case GM_SETINT
: S_volume(msg
->integer
.i
); return 1;
114 return simple_menu_handler(msg
, i
, __NUM__
, &sm
, &cur
);
117 const menu_t
*S_menu (void) {
118 static const menu_t m
= {
119 NULL
, &sound_menu_handler
124 const cfg_t
*S_args (void) {
125 static const cfg_t args
[] = {
126 { "sndvol", &snd_vol
, Y_WORD
},
132 const cfg_t
*S_conf (void) {
133 static const cfg_t conf
[] = {
134 { "sound_volume", &snd_vol
, Y_WORD
},
140 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
) {
144 if (SDL_BuildAudioCVT(&cvt
, src_format
, src_chan
, src_rate
, dst_format
, dst_chan
, dst_rate
) != -1) {
145 *maxlen
= len
* cvt
.len_mult
;
146 *maxbuf
= malloc(*maxlen
);
147 if (*maxbuf
!= NULL
) {
148 memcpy(*maxbuf
, buf
, len
);
151 if (SDL_ConvertAudio(&cvt
) == 0) {
152 *maxlen
= len
* cvt
.len_ratio
;
162 static openal_snd
*new_openal_snd (const void *data
, dword len
, dword rate
, dword lstart
, dword llen
, int sign
) {
165 openal_snd
*snd
= NULL
;
166 void *newdata
= NULL
;
168 // for some reason 8bit formats makes psshshshsh
169 // TODO do this without SDL
170 convert_this_ext(sign
? AUDIO_S8
: AUDIO_U8
, 1, rate
, AUDIO_S16SYS
, 1, rate
, data
, len
, &newdata
, &newlen
);
171 if (newdata
!= NULL
) {
172 alGenBuffers(1, &buffer
);
173 if (alGetError() == AL_NO_ERROR
) {
174 alBufferData(buffer
, AL_FORMAT_MONO16
, newdata
, newlen
, rate
);
175 if (alGetError() == AL_NO_ERROR
) {
176 snd
= malloc(sizeof(openal_snd
));
178 snd
->base
.tag
= TAG_OAL1
;
179 snd
->buffer
= buffer
;
182 alDeleteBuffers(1, &buffer
);
185 alDeleteBuffers(1, &buffer
);
193 snd_t
*S_get (int id
) {
195 openal_snd
*snd
= NULL
;
196 if (context
!= NULL
) {
198 if (handle
!= NULL
) {
200 dword len
= F_getreslen(id
);
207 dword hdr_len
= int2host(hdr
->len
);
208 dword hdr_rate
= int2host(hdr
->rate
);
209 dword hdr_lstart
= int2host(hdr
->lstart
);
210 dword hdr_llen
= int2host(hdr
->llen
);
211 if (hdr_len
<= len
- 8 && hdr_lstart
+ hdr_llen
<= len
- 16) {
220 snd
= new_openal_snd(data
, len
, rate
, lstart
, llen
, sign
);
227 snd_t
*S_load (const char name
[8]) {
228 return S_get(F_findres(name
));
231 void S_free (snd_t
*s
) {
234 openal_snd
*snd
= (openal_snd
*)s
;
236 assert(snd
->base
.tag
== TAG_OAL1
);
237 if (context
!= NULL
) {
238 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
239 alGetSourcei(sources
[i
], AL_BUFFER
, &h
);
240 if (h
== snd
->buffer
) {
241 alSourceStop(sources
[i
]);
242 alSourcei(sources
[i
], AL_BUFFER
, 0);
245 alDeleteBuffers(1, &snd
->buffer
);
246 assert(alGetError() == AL_NO_ERROR
);
254 assert(device
== NULL
&& context
== NULL
);
255 const ALCint attrs
[] = {ALC_MONO_SOURCES
, MAX_CHANNELS
, 0};
256 device
= alcOpenDevice(NULL
);
257 if (device
!= NULL
) {
258 context
= alcCreateContext(device
, attrs
);
259 if (context
!= NULL
) {
260 if (alcMakeContextCurrent(context
)) {
261 alGenSources(MAX_CHANNELS
, sources
);
262 if (alGetError() == AL_NO_ERROR
) {
263 alListenerf(AL_GAIN
, 1);
264 alListener3f(AL_POSITION
, 0, 0, 0);
265 alListener3f(AL_VELOCITY
, 0, 0, 0);
267 logo("S_init: unable to create OpenAL sources\n");
268 alcDestroyContext(context
);
269 alcCloseDevice(device
);
274 logo("S_init: unable to make OpenAL context current\n");
275 alcDestroyContext(context
);
276 alcCloseDevice(device
);
281 logo("S_init: unable to create OpenAL context\n");
282 alcCloseDevice(device
);
286 logo("S_init: unable to open OpenAL device\n");
291 if (context
!= NULL
) {
292 alcMakeContextCurrent(NULL
);
293 alcDestroyContext(context
);
294 alcCloseDevice(device
);
300 short S_play (snd_t
*s
, short c
, short v
) {
301 assert(c
>= 0 && c
<= MAX_CHANNELS
);
302 assert(v
>= 0 && v
< 256);
306 if (context
!= NULL
&& s
!= NULL
) {
307 openal_snd
*snd
= (openal_snd
*)s
;
308 assert(snd
->base
.tag
== TAG_OAL1
);
310 for (channel
= 0; channel
< MAX_CHANNELS
; channel
++) {
312 alGetSourcei(sources
[channel
], AL_SOURCE_STATE
, &state
);
313 if (state
== AL_STOPPED
|| state
== AL_INITIAL
) {
320 if (channel
< MAX_CHANNELS
) {
321 source
= sources
[channel
];
322 alSourceStop(source
);
323 alSourcei(source
, AL_BUFFER
, snd
->buffer
);
324 alSourcef(source
, AL_PITCH
, 1);
325 alSourcef(source
, AL_GAIN
, v
/ 255.0);
326 alSource3f(source
, AL_POSITION
, 0, 0, 0);
327 alSource3f(source
, AL_VELOCITY
, 0, 0, 0);
328 alSourcei(source
, AL_LOOPING
, AL_FALSE
);
329 alSourcePlay(source
);
335 void S_stop (short c
) {
336 assert(c
>= 0 && c
<= MAX_CHANNELS
);
337 if (context
!= NULL
) {
339 alSourceStop(sources
[c
- 1]);
344 void S_volume (int v
) {
345 assert(v
>= 0 && v
<= 128);
347 if (context
!= NULL
) {
348 alListenerf(AL_GAIN
, v
/ 128.0);
355 if (context
!= NULL
) {
356 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
359 alGetSourcei(sources
[i
], AL_SOURCE_STATE
, &state
);
360 } while (state
== AL_PLAYING
);