4c503746fa29c350c1971b5605d6e7dd20735970
7 #define NUM_CHANNELS 16
21 if (!SDL_WasInit(SDL_INIT_AUDIO
)) {
22 if (SDL_InitSubSystem(SDL_INIT_AUDIO
) < 0) {
23 fprintf(stderr
, "\nUnable to initialize audio: %s\n", SDL_GetError());
28 if (Mix_OpenAudio(22050, AUDIO_S16
, 1, 1000) < 0) {
29 fprintf(stderr
, "Error initializing SDL_mixer: %s\n", Mix_GetError());
34 if (Mix_AllocateChannels(NUM_CHANNELS
)!=NUM_CHANNELS
) {
35 fprintf(stderr
, "Error allocation channels: %s\n", Mix_GetError());
42 for (i
=0; i
<NUM_CHUNKS
; i
++) {
47 snddisabled
= (snd_vol
==0);
56 SDL_QuitSubSystem(SDL_INIT_AUDIO
);
59 Mix_Chunk
* get_chunk(snd_t
*s
, int r
, int v
)
62 for(i
=0; i
<NUM_CHUNKS
; i
++) {
63 if (chunks
[i
].s
== s
) return chunks
[i
].c
;
64 if (chunks
[i
].s
== NULL
&& fi
==-1) fi
= i
;
67 if (fi
==-1) return NULL
;
69 Uint8
*data
= (Uint8
*)s
+sizeof(snd_t
);
72 SDL_BuildAudioCVT(&cvt
, AUDIO_S8
, 1, s
->rate
, AUDIO_S16
, 1, 22050);
73 if (!(cvt
.buf
= malloc(dlen
*cvt
.len_mult
))) ERR_fatal("Out of memory\n");;
74 memcpy(cvt
.buf
, data
, dlen
);
76 SDL_ConvertAudio(&cvt
);
79 if (!(chunk
= malloc(sizeof(Mix_Chunk
)))) ERR_fatal("Out of memory\n");;
81 chunk
->alen
=cvt
.len_cvt
;
83 chunk
->volume
=(float)v
/255*SDL_MIX_MAXVOLUME
;
93 if (snddisabled
) return;
96 for (i
=0; i
<NUM_CHUNKS
; i
++) {
98 free(chunks
[i
].c
->abuf
);
106 short S_play(snd_t
*s
,short c
,unsigned r
,short v
)
108 if (snddisabled
) return 0;
109 Mix_Chunk
*chunk
= get_chunk(s
,r
,v
);
110 if (chunk
==NULL
) return 0;
111 return Mix_PlayChannel(c
, chunk
, 0);
121 if (snddisabled
) return;
123 if (snd_vol
>128) snd_vol
=128;
124 if (snd_vol
<0) snd_vol
=0;
125 Mix_Volume(-1, snd_vol
);
130 if (snddisabled
) return;
131 while (Mix_Playing(-1)) {