applied patch from Duong-Khang NGUYEN <neoneurone@users.sf.net>, which
[supertux.git] / src / sound.c
1 /*
2   sound.c
3   
4   Super Tux - Audio Functions
5   
6   by Bill Kendrick
7   bill@newbreedsoftware.com
8   http://www.newbreedsoftware.com/supertux/
9  
10   April 22, 2000 - July 15, 2002
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <SDL.h>
19 #include <SDL_image.h>
20
21 #ifdef LINUX
22 #include <pwd.h>
23 #include <sys/types.h>
24 #include <ctype.h>
25 #endif
26
27 #include "defines.h"
28 #include "globals.h"
29 #include "sound.h"
30 #include "setup.h"
31
32 #ifndef NOSOUND
33
34 /* --- OPEN THE AUDIO DEVICE --- */
35
36 int open_audio (int frequency, Uint16 format, int channels, int chunksize)
37 {
38   if (use_sound) {
39     return Mix_OpenAudio( frequency, format, channels, chunksize );
40   }
41   else {
42     // let the user think that the audio device was correctly opened
43     // and keep the compiler happy :-D
44     return 0;
45   }
46 }
47
48
49 /* --- LOAD A SOUND --- */
50
51 Mix_Chunk * load_sound(char * file)
52 {
53   Mix_Chunk * snd;
54
55   if (use_sound)
56   {
57     snd = Mix_LoadWAV(file);
58
59     if (snd == NULL)
60       st_abort("Can't load", file);
61   }
62   else
63     snd = NULL;
64
65   return(snd);
66 }
67
68
69 /* --- LOAD A SONG --- */
70
71 Mix_Music * load_song(char * file)
72 {
73   Mix_Music * sng;
74
75   if (use_sound)
76   {
77     sng = Mix_LoadMUS(file);
78
79     if (sng == NULL)
80       st_abort("Can't load", file);
81   }
82   else
83     sng = NULL;
84
85   return (sng);
86 }
87
88
89 /* --- PLAY A SOUND --- */
90
91  void play_sound(Mix_Chunk * snd)
92  {
93   // this won't call the function if the user has disabled sound
94   if (use_sound) {
95     Mix_PlayChannel(-1, snd, 0);
96   }
97 }
98
99
100 void free_chunk(Mix_Chunk *chunk)
101 {
102   if (use_sound) {
103     DEBUG_MSG( __PRETTY_FUNCTION__ );
104     Mix_FreeChunk( chunk );
105   }
106 }
107
108 int playing_music(void)
109 {
110   if (use_sound) {
111     return Mix_PlayingMusic();
112   }
113   else {
114     /* we are in --disable-sound we can't be playing music */
115     return 0;
116   }
117 }
118
119
120 int halt_music(void)
121 {
122   if (use_sound) {
123     return Mix_HaltMusic();
124   }
125   else {
126     return 0;
127   }
128 }
129
130
131 int play_music(Mix_Music *music, int loops)
132 {
133   if (use_sound) {
134     DEBUG_MSG(__PRETTY_FUNCTION__);
135     return Mix_PlayMusic(music, loops);
136   }
137   else {
138     /* return error since you're trying to play music in --disable-sound mode */
139     return -1;
140   }
141 }
142
143
144 void free_music(Mix_Music *music)
145 {
146   if (use_sound) {
147     DEBUG_MSG(__PRETTY_FUNCTION__);
148     Mix_FreeMusic( music );
149   }
150 }
151
152 #else
153
154 int open_audio (int frequency, int format, int channels, int chunksize)
155 {
156   return -1;
157 }
158
159 void* load_sound(void* file) { return NULL; }
160 void play_sound(void * snd) {}
161 void* load_song(void* file) { return NULL; }
162
163 int playing_music() { return 0; }
164 void halt_music() {}
165 int play_music(int *music, int loops) { return 0;}
166 void free_music(int *music) {}
167 void free_chunk(int *chunk) {}
168
169 #endif