Make audio failure non-fatal (e.g. run without it)
[attac-man.git] / audio.c
blobbe2c715ee236b126a03632d1d898aa3d620388eb
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 = 1;
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(void)
58 if(Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 512 * 4) == -1)
60 printf("erro: audio_init: Mix_OpenAudio() -> %s\n", Mix_GetError());
61 //SDL_Quit();
62 //exit(1);
63 audio_available = 0;
64 return;
67 Mix_AllocateChannels(NUM_CHANNELS);
71 audio_play_music
72 inicia a reprodução de música de um ficheiro
74 void audio_play_music(char *fname)
76 if(!audio_available || audio_mute_music)
77 return;
79 music = Mix_LoadMUS((const char *)file_make_fname(fname));
80 if(music == NULL)
82 printf("erro: audio_play_music: Mix_LoadMUS() -> %s\n", Mix_GetError());
83 SDL_Quit();
84 exit(1);
87 if(Mix_PlayMusic(music, -1) == -1)
89 printf("erro: audio_play_music: Mix_PlayMusic() -> %s\n", Mix_GetError());
90 SDL_Quit();
91 exit(1);
96 audio_pause_music
97 suspende a reprodução de música
99 void audio_pause_music(void)
101 if (!audio_available)
102 return;
103 Mix_PauseMusic();
107 audio_resume_music
108 continua a reprodução de música
110 void audio_resume_music(void)
112 if (!audio_available)
113 return;
114 Mix_ResumeMusic();
118 audio_stop_music
119 termina a reprodução de música
121 void audio_stop_music(void)
123 if (!audio_available)
124 return;
125 Mix_HaltMusic();
128 void audio_fade_music(int msec)
130 if (!audio_available)
131 return;
132 Mix_FadeOutMusic(msec);
136 audio_find_sample
137 procura um sample de som pelo seu nome na lista
139 struct audio_sample *audio_find_sample(char *name)
141 struct audio_sample *cur;
142 if (!audio_available)
143 return NULL;
145 cur = samples;
146 while(cur)
148 if(strcmp(cur->name, name) == 0)
149 return cur;
151 cur = cur->next;
154 return NULL;
158 audio_play_sample
159 reproduz um sample de som, carregando-o caso necessário
161 void audio_play_sample(char *name)
163 struct audio_sample *sm;
165 if (!audio_available)
166 return;
168 sm = audio_find_sample(name);
169 if(sm == NULL)
171 /* carregar sample do ficheiro */
172 sm = malloc(sizeof(struct audio_sample));
173 sm->name = strdup(name);
174 sm->sample = Mix_LoadWAV((const char *)file_make_fname(name));
175 if(sm->sample == NULL)
177 printf("erro: audio_play_sample(%s): Mix_LoadWAV() -> %s\n",
178 name, Mix_GetError());
179 SDL_Quit();
180 exit(1);
183 LLIST_ADD(struct audio_sample, samples, sm);
186 if(audio_mute_samples)
187 return;
189 Mix_PlayChannel(-1, sm->sample, 0);