DEADSOFTWARE

headers describes that c-files implements
[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 static int snddisabled = 1;
35 static struct {
36 snd_t *s;
37 Mix_Chunk *c;
38 } chunks[NUM_CHUNKS];
40 void S_init (void) {
41 if (!SDL_WasInit(SDL_INIT_AUDIO)) {
42 if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
43 fprintf(stderr, "\nUnable to initialize audio: %s\n", SDL_GetError());
44 snddisabled=1;
45 return;
46 }
48 if (Mix_OpenAudio(22050, AUDIO_S16, 1, 1000) < 0) {
49 fprintf(stderr, "Error initializing SDL_mixer: %s\n", Mix_GetError());
50 snddisabled=1;
51 return;
52 }
54 if (Mix_AllocateChannels(NUM_CHANNELS)!=NUM_CHANNELS) {
55 fprintf(stderr, "Error allocation channels: %s\n", Mix_GetError());
56 snddisabled=1;
57 return;
58 }
59 }
61 int i;
62 for (i=0; i<NUM_CHUNKS; i++) {
63 chunks[i].s = NULL;
64 chunks[i].c = NULL;
65 }
67 snddisabled = (snd_vol==0);
69 S_volume(snd_vol);
70 }
72 void S_done (void) {
73 free_chunks();
74 if (SDL_WasInit(SDL_INIT_AUDIO)) {
75 Mix_CloseAudio();
76 SDL_QuitSubSystem(SDL_INIT_AUDIO);
77 }
78 }
80 static Mix_Chunk *get_chunk (snd_t *s, int r, int v) {
81 int i, fi = -1;
82 for(i=0; i<NUM_CHUNKS; i++) {
83 if (chunks[i].s == s) return chunks[i].c;
84 if (chunks[i].s == NULL && fi==-1) fi = i;
85 }
87 if (fi==-1) return NULL;
89 Uint8 *data = (Uint8*)s+sizeof(snd_t);
90 Uint32 dlen = s->len;
91 SDL_AudioCVT cvt;
92 SDL_BuildAudioCVT(&cvt, AUDIO_S8, 1, s->rate, AUDIO_S16, 1, 22050);
93 if (!(cvt.buf = malloc(dlen*cvt.len_mult))) ERR_fatal("Out of memory\n");;
94 memcpy(cvt.buf, data, dlen);
95 cvt.len = dlen;
96 SDL_ConvertAudio(&cvt);
98 Mix_Chunk *chunk;
99 if (!(chunk = malloc(sizeof(Mix_Chunk)))) ERR_fatal("Out of memory\n");;
100 chunk->abuf=cvt.buf;
101 chunk->alen=cvt.len_cvt;
102 chunk->allocated=0;
103 chunk->volume=(float)v/255*SDL_MIX_MAXVOLUME;
105 chunks[fi].s = s;
106 chunks[fi].c = chunk;
108 return chunk;
111 void free_chunks (void) {
112 if (snddisabled) return;
113 Mix_HaltChannel(-1);
114 int i;
115 for (i=0; i<NUM_CHUNKS; i++) {
116 if (chunks[i].c) {
117 free(chunks[i].c->abuf);
118 free(chunks[i].c);
119 chunks[i].c = NULL;
120 chunks[i].s = NULL;
125 short S_play(snd_t *s, short c, unsigned r, short v) {
126 if (snddisabled) return 0;
127 Mix_Chunk *chunk = get_chunk(s,r,v);
128 if (chunk==NULL) return 0;
129 return Mix_PlayChannel(c, chunk, 0);
132 void S_stop (short c) {
133 Mix_HaltChannel(c);
136 void S_volume (int v) {
137 if (snddisabled) return;
138 snd_vol=v;
139 if (snd_vol>128) snd_vol=128;
140 if (snd_vol<0) snd_vol=0;
141 Mix_Volume(-1, snd_vol);
144 void S_wait (void) {
145 if (snddisabled) return;
146 while (Mix_Playing(-1)) {
147 SDL_Delay(10);