console: Format tabs semi-intelligently
[attac-man.git] / audio.c
blobcfd08153d21ec74da983e811967275b12e03441a
1 /*
2 Pacman Arena
3 Copyright (C) 2003 Nuno Subtil
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 static const char cvsid[] =
21 "$Id: audio.c,v 1.7 2003/11/22 17:32:09 nsubtil Exp $";
23 #include <SDL.h>
24 #include <SDL_mixer.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "audio.h"
30 #include "linked-lists.h"
31 #include "file.h"
33 /* nº de canais de som */
34 #define NUM_CHANNELS 16
36 static Mix_Music *music;
37 static int audio_available = 0;
39 struct audio_sample
41 char *name;
42 Mix_Chunk *sample;
44 struct audio_sample *next;
47 static struct audio_sample *samples = NULL;
49 static int audio_mute_samples = 0;
50 static int audio_mute_music = 0;
53 audio_init
54 inicializa as rotinas de áudio
56 void audio_init(int enable)
58 if (!enable)
59 return;
60 if(Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 512 * 4) == -1)
62 printf("error: audio_init: Mix_OpenAudio() -> %s\n",
63 Mix_GetError());
64 return;
67 audio_available = 1;
68 Mix_AllocateChannels(NUM_CHANNELS);
72 audio_play_music
73 inicia a reprodução de música de um ficheiro
75 void audio_play_music(char *fname)
77 if(!audio_available || audio_mute_music)
78 return;
80 music = Mix_LoadMUS((const char *)file_make_fname(fname));
81 if(music == NULL)
83 printf("erro: audio_play_music: Mix_LoadMUS() -> %s\n", Mix_GetError());
84 SDL_Quit();
85 exit(1);
88 if(Mix_PlayMusic(music, -1) == -1)
90 printf("erro: audio_play_music: Mix_PlayMusic() -> %s\n", Mix_GetError());
91 SDL_Quit();
92 exit(1);
97 audio_pause_music
98 suspende a reprodução de música
100 void audio_pause_music(void)
102 if (!audio_available)
103 return;
104 Mix_PauseMusic();
108 audio_resume_music
109 continua a reprodução de música
111 void audio_resume_music(void)
113 if (!audio_available)
114 return;
115 Mix_ResumeMusic();
119 audio_stop_music
120 termina a reprodução de música
122 void audio_stop_music(void)
124 if (!audio_available)
125 return;
126 Mix_HaltMusic();
129 void audio_fade_music(int msec)
131 if (!audio_available)
132 return;
133 Mix_FadeOutMusic(msec);
137 audio_find_sample
138 procura um sample de som pelo seu nome na lista
140 struct audio_sample *audio_find_sample(char *name)
142 struct audio_sample *cur;
143 if (!audio_available)
144 return NULL;
146 cur = samples;
147 while(cur)
149 if(strcmp(cur->name, name) == 0)
150 return cur;
152 cur = cur->next;
155 return NULL;
159 audio_play_sample
160 reproduz um sample de som, carregando-o caso necessário
162 void audio_play_sample(char *name)
164 struct audio_sample *sm;
166 if (!audio_available)
167 return;
169 sm = audio_find_sample(name);
170 if(sm == NULL)
172 /* carregar sample do ficheiro */
173 sm = malloc(sizeof(struct audio_sample));
174 sm->name = strdup(name);
175 sm->sample = Mix_LoadWAV((const char *)file_make_fname(name));
176 if(sm->sample == NULL)
178 printf("erro: audio_play_sample(%s): Mix_LoadWAV() -> %s\n",
179 name, Mix_GetError());
180 SDL_Quit();
181 exit(1);
184 LLIST_ADD(struct audio_sample, samples, sm);
187 if(audio_mute_samples)
188 return;
190 Mix_PlayChannel(-1, sm->sample, 0);