fixed bug / patch by 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   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   snd = Mix_LoadWAV(file);
56
57   if (snd == NULL)
58     st_abort("Can't load", file);
59    
60   return(snd);
61 }
62
63
64 /* --- LOAD A SONG --- */
65
66 Mix_Music * load_song(char * file)
67 {
68   Mix_Music * sng;
69
70   sng = Mix_LoadMUS(file);
71
72   if (sng == NULL)
73     st_abort("Can't load", file);
74   
75   return (sng);
76 }
77
78
79 /* --- PLAY A SOUND --- */
80
81  void play_sound(Mix_Chunk * snd)
82  {
83   /* this won't call the function if the user has disabled sound */
84   if (use_sound) {
85     Mix_PlayChannel(-1, snd, 0);
86   }
87 }
88
89
90 void free_chunk(Mix_Chunk *chunk)
91 {
92   if (chunk != NULL) {
93    DEBUG_MSG( __PRETTY_FUNCTION__ );
94    Mix_FreeChunk( chunk );
95    chunk = NULL;
96   }
97 }
98
99 int playing_music(void)
100 {
101   if (use_sound) {
102     return Mix_PlayingMusic();
103   }
104   else {
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) {
114     return Mix_HaltMusic();
115   }
116   else {
117     return 0;
118   }
119 }
120
121
122 int play_music(Mix_Music *music, int loops)
123 {
124   if (use_sound) {
125     DEBUG_MSG(__PRETTY_FUNCTION__);
126     return Mix_PlayMusic(music, loops);
127   }
128   else {
129     /* return error since you're trying to play music in --disable-sound mode */
130     return -1;
131   }
132 }
133
134
135 void free_music(Mix_Music *music)
136 {
137   if ( music != NULL ) {
138     DEBUG_MSG(__PRETTY_FUNCTION__);
139     Mix_FreeMusic( music );
140     music = NULL;
141   }
142 }
143
144 #else
145
146 int open_audio (int frequency, int format, int channels, int chunksize)
147 {
148   return -1;
149 }
150
151 void* load_sound(void* file) { return NULL; }
152 void play_sound(void * snd) {}
153 void* load_song(void* file) { return NULL; }
154
155 int playing_music() { return 0; }
156 void halt_music() {}
157 int play_music(void *music, int loops) { return 0;}
158 void free_music(void *music) {}
159 void free_chunk(void *chunk) {}
160
161 #endif