ye another sound patch from Duong-Khang NGUYEN <neoneurone@users.sf.net>.
[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   return Mix_OpenAudio( frequency, format, channels, chunksize );
39 }
40
41
42 /* --- LOAD A SOUND --- */
43
44 Mix_Chunk * load_sound(char * file)
45 {
46   Mix_Chunk * snd;
47
48   snd = Mix_LoadWAV(file);
49
50   /* printf message and abort if there is an initialized audio device */
51   if ((snd == NULL) && (audio_device == YES))
52     st_abort("Can't load", file);
53
54   return(snd);
55 }
56
57
58 /* --- LOAD A SONG --- */
59
60 Mix_Music * load_song(char * file)
61 {
62   Mix_Music * sng;
63
64   sng = Mix_LoadMUS(file);
65
66   /* printf message and abort if there is an initialized audio device */
67   if ((sng == NULL) && (audio_device == YES))
68     st_abort("Can't load", file);
69   return (sng);
70 }
71
72
73 /* --- PLAY A SOUND --- */
74
75 void play_sound(Mix_Chunk * snd)
76 {
77   /* this won't call the function if the user has disabled sound
78    * either via menu or via command-line option
79    */
80   if ((use_sound == YES) && (audio_device == YES))
81     {
82       Mix_PlayChannel(-1, snd, 0);
83     }
84 }
85
86
87 void free_chunk(Mix_Chunk *chunk)
88 {
89   if (chunk != NULL)
90     {
91       DEBUG_MSG( __PRETTY_FUNCTION__ );
92       Mix_FreeChunk( chunk );
93       chunk = NULL;
94     }
95 }
96
97 int playing_music(void)
98 {
99   if (use_sound)
100     {
101       return Mix_PlayingMusic();
102     }
103   else
104     {
105       /* we are in --disable-sound we can't be playing music */
106       return 0;
107     }
108 }
109
110
111 int halt_music(void)
112 {
113   if ((use_sound == YES) && (audio_device == YES))
114     {
115       return Mix_HaltMusic();
116     }
117   else
118     {
119       return 0;
120     }
121 }
122
123
124 int play_music(Mix_Music *music, int loops)
125 {
126   if ((use_sound == YES) && (audio_device == YES))
127     {
128       DEBUG_MSG(__PRETTY_FUNCTION__);
129       return Mix_PlayMusic(music, loops);
130     }
131   else
132     {
133       /* return error since you're trying to play music in --disable-sound mode */
134       return -1;
135     }
136 }
137
138
139 void free_music(Mix_Music *music)
140 {
141   if ( music != NULL )
142     {
143       DEBUG_MSG(__PRETTY_FUNCTION__);
144       Mix_FreeMusic( music );
145       music = NULL;
146     }
147 }
148
149 #else
150
151 int open_audio (int frequency, int format, int channels, int chunksize)
152 {
153   return -1;
154 }
155
156 void* load_sound(void* file)
157 {
158   return NULL;
159 }
160 void play_sound(void * snd)
161 {}
162 void* load_song(void* file)
163 {
164   return NULL;
165 }
166
167 int playing_music()
168 {
169   return 0;
170 }
171 void halt_music()
172 {}
173 int play_music(void *music, int loops)
174 {
175   return 0;
176 }
177 void free_music(void *music)
178 {}
179 void free_chunk(void *chunk)
180 {}
181
182 #endif