87d9f1f9a2fc80cc2a5cf838502a16114c27e18c
7 #define NUM_CHANNELS 16
23 if (!SDL_WasInit(SDL_INIT_AUDIO
)) {
24 if (SDL_InitSubSystem(SDL_INIT_AUDIO
) < 0) {
25 fprintf(stderr
, "\nUnable to initialize audio: %s\n", SDL_GetError());
30 if (Mix_OpenAudio(22050, AUDIO_S16
, 1, 1000) < 0) {
31 fprintf(stderr
, "Error initializing SDL_mixer: %s\n", Mix_GetError());
36 if (Mix_AllocateChannels(NUM_CHANNELS
)!=NUM_CHANNELS
) {
37 fprintf(stderr
, "Error allocation channels: %s\n", Mix_GetError());
44 for (i
=0; i
<NUM_CHUNKS
; i
++) {
49 snddisabled
= (snd_vol
==0);
58 SDL_QuitSubSystem(SDL_INIT_AUDIO
);
61 Mix_Chunk
* get_chunk(snd_t
*s
, int r
, int v
)
64 for(i
=0; i
<NUM_CHUNKS
; i
++) {
65 if (chunks
[i
].s
== s
) return chunks
[i
].c
;
66 if (chunks
[i
].s
== NULL
&& fi
==-1) fi
= i
;
69 if (fi
==-1) return NULL
;
71 Uint8
*data
= (Uint8
*)s
+sizeof(snd_t
);
74 SDL_BuildAudioCVT(&cvt
, AUDIO_S8
, 1, s
->rate
, AUDIO_S16
, 1, 22050);
75 if (!(cvt
.buf
= malloc(dlen
*cvt
.len_mult
))) ERR_fatal("Out of memory\n");;
76 memcpy(cvt
.buf
, data
, dlen
);
78 SDL_ConvertAudio(&cvt
);
81 if (!(chunk
= malloc(sizeof(Mix_Chunk
)))) ERR_fatal("Out of memory\n");;
83 chunk
->alen
=cvt
.len_cvt
;
85 chunk
->volume
=(float)v
/255*SDL_MIX_MAXVOLUME
;
95 if (snddisabled
) return;
98 for (i
=0; i
<NUM_CHUNKS
; i
++) {
100 free(chunks
[i
].c
->abuf
);
108 short S_play(snd_t
*s
,short c
,unsigned r
,short v
)
110 if (snddisabled
) return 0;
111 Mix_Chunk
*chunk
= get_chunk(s
,r
,v
);
112 if (chunk
==NULL
) return 0;
113 return Mix_PlayChannel(c
, chunk
, 0);
123 if (snddisabled
) return;
125 if (snd_vol
>128) snd_vol
=128;
126 if (snd_vol
<0) snd_vol
=0;
127 Mix_Volume(-1, snd_vol
);
132 if (snddisabled
) return;
133 while (Mix_Playing(-1)) {