Attempt to use Mix_SetMusicPosition to speed up MOD music. (Not very good)
[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 27, 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 set_music_position(int pos)
141 {
142   if ((use_music == YES) && (audio_device == YES))
143   {
144     if (Mix_PlayingMusic())
145     {
146       Mix_SetMusicPosition(pos);
147     }
148   }
149 }
150
151
152
153 void free_music(Mix_Music *music)
154 {
155   if ( music != NULL )
156     {
157       DEBUG_MSG(__PRETTY_FUNCTION__);
158       Mix_FreeMusic( music );
159       music = NULL;
160     }
161 }
162
163 #else
164
165 int open_audio (int frequency, int format, int channels, int chunksize)
166 {
167   return -1;
168 }
169
170 void* load_sound(void* file)
171 {
172   return NULL;
173 }
174 void play_sound(void * snd)
175 {}
176 void* load_song(void* file)
177 {
178   return NULL;
179 }
180
181 int playing_music()
182 {
183   return 0;
184 }
185 void halt_music()
186 {}
187 int play_music(void *music, int loops)
188 {
189   return 0;
190 }
191 void free_music(void *music)
192 {}
193 void free_chunk(void *chunk)
194 {}
195
196 #endif