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 $";
24 #include <SDL_mixer.h>
30 #include "linked-lists.h"
33 /* nº de canais de som */
34 #define NUM_CHANNELS 16
36 static Mix_Music
*music
;
37 static int audio_available
= 0;
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;
54 inicializa as rotinas de áudio
56 void audio_init(int enable
)
60 if(Mix_OpenAudio(44100, AUDIO_S16SYS
, 2, 512 * 4) == -1)
62 printf("error: audio_init: Mix_OpenAudio() -> %s\n",
68 Mix_AllocateChannels(NUM_CHANNELS
);
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
)
80 music
= Mix_LoadMUS((const char *)file_make_fname(fname
));
83 printf("erro: audio_play_music: Mix_LoadMUS() -> %s\n", Mix_GetError());
88 if(Mix_PlayMusic(music
, -1) == -1)
90 printf("erro: audio_play_music: Mix_PlayMusic() -> %s\n", Mix_GetError());
98 suspende a reprodução de música
100 void audio_pause_music(void)
102 if (!audio_available
)
109 continua a reprodução de música
111 void audio_resume_music(void)
113 if (!audio_available
)
120 termina a reprodução de música
122 void audio_stop_music(void)
124 if (!audio_available
)
129 void audio_fade_music(int msec
)
131 if (!audio_available
)
133 Mix_FadeOutMusic(msec
);
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
)
149 if(strcmp(cur
->name
, name
) == 0)
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
)
169 sm
= audio_find_sample(name
);
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());
184 LLIST_ADD(struct audio_sample
, samples
, sm
);
187 if(audio_mute_samples
)
190 Mix_PlayChannel(-1, sm
->sample
, 0);