Initial commit from pacman arena 0.15, found here:
[attac-man.git] / audio.c
blob4801355aea090026b8779be933606facfe0d7871
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;
38 struct audio_sample
40 char *name;
41 Mix_Chunk *sample;
43 struct audio_sample *next;
46 static struct audio_sample *samples = NULL;
48 static int audio_mute_samples = 0;
49 static int audio_mute_music = 0;
52 audio_init
53 inicializa as rotinas de áudio
55 void audio_init(void)
57 if(Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 512 * 4) == -1)
59 printf("erro: audio_init: Mix_OpenAudio() -> %s\n", Mix_GetError());
60 SDL_Quit();
61 exit(1);
64 Mix_AllocateChannels(NUM_CHANNELS);
68 audio_play_music
69 inicia a reprodução de música de um ficheiro
71 void audio_play_music(char *fname)
73 if(audio_mute_music)
74 return;
76 music = Mix_LoadMUS((const char *)file_make_fname(fname));
77 if(music == NULL)
79 printf("erro: audio_play_music: Mix_LoadMUS() -> %s\n", Mix_GetError());
80 SDL_Quit();
81 exit(1);
84 if(Mix_PlayMusic(music, -1) == -1)
86 printf("erro: audio_play_music: Mix_PlayMusic() -> %s\n", Mix_GetError());
87 SDL_Quit();
88 exit(1);
93 audio_pause_music
94 suspende a reprodução de música
96 void audio_pause_music(void)
98 Mix_PauseMusic();
102 audio_resume_music
103 continua a reprodução de música
105 void audio_resume_music(void)
107 Mix_ResumeMusic();
111 audio_stop_music
112 termina a reprodução de música
114 void audio_stop_music(void)
116 Mix_HaltMusic();
119 void audio_fade_music(int msec)
121 Mix_FadeOutMusic(msec);
125 audio_find_sample
126 procura um sample de som pelo seu nome na lista
128 struct audio_sample *audio_find_sample(char *name)
130 struct audio_sample *cur;
132 cur = samples;
133 while(cur)
135 if(strcmp(cur->name, name) == 0)
136 return cur;
138 cur = cur->next;
141 return NULL;
145 audio_play_sample
146 reproduz um sample de som, carregando-o caso necessário
148 void audio_play_sample(char *name)
150 struct audio_sample *sm;
152 sm = audio_find_sample(name);
153 if(sm == NULL)
155 /* carregar sample do ficheiro */
156 sm = malloc(sizeof(struct audio_sample));
157 sm->name = strdup(name);
158 sm->sample = Mix_LoadWAV((const char *)file_make_fname(name));
159 if(sm->sample == NULL)
161 printf("erro: audio_play_sample(%s): Mix_LoadWAV() -> %s\n",
162 name, Mix_GetError());
163 SDL_Quit();
164 exit(1);
167 LLIST_ADD(struct audio_sample, samples, sm);
170 if(audio_mute_samples)
171 return;
173 Mix_PlayChannel(-1, sm->sample, 0);