DEADSOFTWARE

Cleanup includes
[flatwaifu.git] / src / sound.c
1 /*
2 Copyright (C) Prikol Software 1996-1997
3 Copyright (C) Aleksey Volynskov 1996-1997
4 Copyright (C) <ARembo@gmail.com> 2011
6 This file is part of the Doom2D:Rembo project.
8 Doom2D:Rembo is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 Doom2D:Rembo is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/> or
19 write to the Free Software Foundation, Inc.,
20 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
23 #include "glob.h"
24 #include "sound.h"
25 #include "error.h"
26 #include <SDL.h>
27 #include <SDL_mixer.h>
29 #define NUM_CHANNELS 16
30 #define NUM_CHUNKS 300
32 short snd_vol = 50;
34 int snddisabled = 1;
36 struct {
37 snd_t *s;
38 Mix_Chunk *c;
39 } chunks[NUM_CHUNKS];
41 void S_init(void)
42 {
43 if (!SDL_WasInit(SDL_INIT_AUDIO)) {
44 if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
45 fprintf(stderr, "\nUnable to initialize audio: %s\n", SDL_GetError());
46 snddisabled=1;
47 return;
48 }
50 if (Mix_OpenAudio(22050, AUDIO_S16, 1, 1000) < 0) {
51 fprintf(stderr, "Error initializing SDL_mixer: %s\n", Mix_GetError());
52 snddisabled=1;
53 return;
54 }
56 if (Mix_AllocateChannels(NUM_CHANNELS)!=NUM_CHANNELS) {
57 fprintf(stderr, "Error allocation channels: %s\n", Mix_GetError());
58 snddisabled=1;
59 return;
60 }
61 }
63 int i;
64 for (i=0; i<NUM_CHUNKS; i++) {
65 chunks[i].s = NULL;
66 chunks[i].c = NULL;
67 }
69 snddisabled = (snd_vol==0);
71 S_volume(snd_vol);
72 }
74 void S_done(void)
75 {
76 free_chunks();
77 if (SDL_WasInit(SDL_INIT_AUDIO)) {
78 Mix_CloseAudio();
79 SDL_QuitSubSystem(SDL_INIT_AUDIO);
80 }
81 }
83 Mix_Chunk * get_chunk(snd_t *s, int r, int v)
84 {
85 int i, fi = -1;
86 for(i=0; i<NUM_CHUNKS; i++) {
87 if (chunks[i].s == s) return chunks[i].c;
88 if (chunks[i].s == NULL && fi==-1) fi = i;
89 }
91 if (fi==-1) return NULL;
93 Uint8 *data = (Uint8*)s+sizeof(snd_t);
94 Uint32 dlen = s->len;
95 SDL_AudioCVT cvt;
96 SDL_BuildAudioCVT(&cvt, AUDIO_S8, 1, s->rate, AUDIO_S16, 1, 22050);
97 if (!(cvt.buf = malloc(dlen*cvt.len_mult))) ERR_fatal("Out of memory\n");;
98 memcpy(cvt.buf, data, dlen);
99 cvt.len = dlen;
100 SDL_ConvertAudio(&cvt);
102 Mix_Chunk *chunk;
103 if (!(chunk = malloc(sizeof(Mix_Chunk)))) ERR_fatal("Out of memory\n");;
104 chunk->abuf=cvt.buf;
105 chunk->alen=cvt.len_cvt;
106 chunk->allocated=0;
107 chunk->volume=(float)v/255*SDL_MIX_MAXVOLUME;
109 chunks[fi].s = s;
110 chunks[fi].c = chunk;
112 return chunk;
115 void free_chunks()
117 if (snddisabled) return;
118 Mix_HaltChannel(-1);
119 int i;
120 for (i=0; i<NUM_CHUNKS; i++) {
121 if (chunks[i].c) {
122 free(chunks[i].c->abuf);
123 free(chunks[i].c);
124 chunks[i].c = NULL;
125 chunks[i].s = NULL;
130 short S_play(snd_t *s,short c,unsigned r,short v)
132 if (snddisabled) return 0;
133 Mix_Chunk *chunk = get_chunk(s,r,v);
134 if (chunk==NULL) return 0;
135 return Mix_PlayChannel(c, chunk, 0);
138 void S_stop(short c)
140 Mix_HaltChannel(c);
143 void S_volume(int v)
145 if (snddisabled) return;
146 snd_vol=v;
147 if (snd_vol>128) snd_vol=128;
148 if (snd_vol<0) snd_vol=0;
149 Mix_Volume(-1, snd_vol);
152 void S_wait()
154 if (snddisabled) return;
155 while (Mix_Playing(-1)) {
156 SDL_Delay(10);