Updated songs in levels. Removed SDL_Mixer-based music speed-up attempt.
[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 - December 28, 2003
11 */
12
13 /*
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <unistd.h>
18 */
19
20 #ifdef LINUX
21 #include <pwd.h>
22 #include <sys/types.h>
23 #include <ctype.h>
24 #endif
25
26 #include "defines.h"
27 #include "globals.h"
28 #include "sound.h"
29 #include "setup.h"
30
31 #ifndef NOSOUND
32
33 #include <SDL_mixer.h>
34
35 /* --- OPEN THE AUDIO DEVICE --- */
36
37 int open_audio (int frequency, Uint16 format, int channels, int chunksize)
38 {
39   return Mix_OpenAudio( frequency, format, channels, chunksize );
40 }
41
42
43 /* --- LOAD A SOUND --- */
44
45 Mix_Chunk * load_sound(char * file)
46 {
47   Mix_Chunk * snd;
48
49   snd = Mix_LoadWAV(file);
50
51   /* printf message and abort if there is an initialized audio device */
52   if ((snd == NULL) && (audio_device == YES))
53     st_abort("Can't load", file);
54
55   return(snd);
56 }
57
58
59 /* --- LOAD A SONG --- */
60
61 Mix_Music * load_song(char * file)
62 {
63   Mix_Music * sng;
64
65   sng = Mix_LoadMUS(file);
66
67   /* printf message and abort if there is an initialized audio device */
68   if ((sng == NULL) && (audio_device == YES))
69     st_abort("Can't load", file);
70   return (sng);
71 }
72
73
74 /* --- PLAY A SOUND --- */
75
76 void play_sound(Mix_Chunk * snd)
77 {
78   /* this won't call the function if the user has disabled sound
79    * either via menu or via command-line option
80    */
81   if ((use_sound == YES) && (audio_device == YES))
82     {
83       Mix_PlayChannel(-1, snd, 0);
84     }
85 }
86
87
88 void free_chunk(Mix_Chunk *chunk)
89 {
90   if (chunk != NULL)
91     {
92       DEBUG_MSG( __PRETTY_FUNCTION__ );
93       Mix_FreeChunk( chunk );
94       chunk = NULL;
95     }
96 }
97
98 int playing_music(void)
99 {
100   if (use_music == YES)
101     {
102       return Mix_PlayingMusic();
103     }
104   else
105     {
106       /* we are in --disable-music we can't be playing music */
107       return 0;
108     }
109 }
110
111
112 int halt_music(void)
113 {
114   if ((use_music == YES) && (audio_device == YES))
115     {
116       return Mix_HaltMusic();
117     }
118   else
119     {
120       return 0;
121     }
122 }
123
124
125 int play_music(Mix_Music *music, int loops)
126 {
127   if ((use_music == YES) && (audio_device == YES))
128     {
129       DEBUG_MSG(__PRETTY_FUNCTION__);
130       return Mix_PlayMusic(music, loops);
131     }
132   else
133     {
134       /* return error since you're trying to play music in --disable-sound mode */
135       return -1;
136     }
137 }
138
139
140 void free_music(Mix_Music *music)
141 {
142   if ( music != NULL )
143     {
144       DEBUG_MSG(__PRETTY_FUNCTION__);
145       Mix_FreeMusic( music );
146       music = NULL;
147     }
148 }
149
150 #else
151
152 int open_audio (int frequency, int format, int channels, int chunksize)
153 {
154   return -1;
155 }
156
157 void* load_sound(void* file)
158 {
159   return NULL;
160 }
161 void play_sound(void * snd)
162 {}
163 void* load_song(void* file)
164 {
165   return NULL;
166 }
167
168 int playing_music()
169 {
170   return 0;
171 }
172 void halt_music()
173 {}
174 int play_music(void *music, int loops)
175 {
176   return 0;
177 }
178 void free_music(void *music)
179 {}
180 void free_chunk(void *chunk)
181 {}
182
183 #endif