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
= 1;
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
58 if(Mix_OpenAudio(44100, AUDIO_S16SYS
, 2, 512 * 4) == -1)
60 printf("erro: audio_init: Mix_OpenAudio() -> %s\n", Mix_GetError());
67 Mix_AllocateChannels(NUM_CHANNELS
);
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
)
79 music
= Mix_LoadMUS((const char *)file_make_fname(fname
));
82 printf("erro: audio_play_music: Mix_LoadMUS() -> %s\n", Mix_GetError());
87 if(Mix_PlayMusic(music
, -1) == -1)
89 printf("erro: audio_play_music: Mix_PlayMusic() -> %s\n", Mix_GetError());
97 suspende a reprodução de música
99 void audio_pause_music(void)
101 if (!audio_available
)
108 continua a reprodução de música
110 void audio_resume_music(void)
112 if (!audio_available
)
119 termina a reprodução de música
121 void audio_stop_music(void)
123 if (!audio_available
)
128 void audio_fade_music(int msec
)
130 if (!audio_available
)
132 Mix_FadeOutMusic(msec
);
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
)
148 if(strcmp(cur
->name
, name
) == 0)
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
)
168 sm
= audio_find_sample(name
);
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());
183 LLIST_ADD(struct audio_sample
, samples
, sm
);
186 if(audio_mute_samples
)
189 Mix_PlayChannel(-1, sm
->sample
, 0);