1 | /* |
---|
2 | * Abuse - dark 2D side-scrolling platform game |
---|
3 | * Copyright (c) 2001 Anthony Kruize <trandor@labyrinth.net.au> |
---|
4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software Foundation, |
---|
18 | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
---|
19 | */ |
---|
20 | |
---|
21 | #include "config.h" |
---|
22 | |
---|
23 | #include <cstring> |
---|
24 | |
---|
25 | #include <SDL.h> |
---|
26 | #include <SDL/SDL_mixer.h> |
---|
27 | |
---|
28 | #include "sound.h" |
---|
29 | #include "hmi.h" |
---|
30 | #include "readwav.h" |
---|
31 | #include "specs.h" |
---|
32 | #include "setup.h" |
---|
33 | |
---|
34 | extern flags_struct flags; |
---|
35 | static int sound_enabled = 0; |
---|
36 | static SDL_AudioSpec audioObtained; |
---|
37 | |
---|
38 | // |
---|
39 | // sound_init() |
---|
40 | // Initialise audio |
---|
41 | // |
---|
42 | int sound_init( int argc, char **argv ) |
---|
43 | { |
---|
44 | char *sfxdir, *datadir; |
---|
45 | FILE *fd = NULL; |
---|
46 | |
---|
47 | // Disable sound if requested. |
---|
48 | if( flags.nosound ) |
---|
49 | { |
---|
50 | // User requested that sound be disabled |
---|
51 | printf( "Sound: Disabled (-nosound)\n" ); |
---|
52 | return 0; |
---|
53 | } |
---|
54 | |
---|
55 | // Check for the sfx directory, disable sound if we can't find it. |
---|
56 | datadir = get_filename_prefix(); |
---|
57 | sfxdir = (char *)malloc( strlen( datadir ) + 5 + 1 ); |
---|
58 | sprintf( sfxdir, "%s/sfx/", datadir ); |
---|
59 | if( (fd = fopen( sfxdir,"r" )) == NULL ) |
---|
60 | { |
---|
61 | // Didn't find the directory, so disable sound. |
---|
62 | printf( "Sound: Disabled (couldn't find the sfx directory)\n" ); |
---|
63 | return 0; |
---|
64 | } |
---|
65 | free( sfxdir ); |
---|
66 | |
---|
67 | if (Mix_OpenAudio(11025, AUDIO_U8, 2, 128) < 0) |
---|
68 | { |
---|
69 | printf( "Sound: Unable to open audio - %s\nSound: Disabled (error)\n", SDL_GetError() ); |
---|
70 | return 0; |
---|
71 | } |
---|
72 | |
---|
73 | Mix_AllocateChannels(50); |
---|
74 | |
---|
75 | int tempChannels = 0; |
---|
76 | Mix_QuerySpec(&audioObtained.freq, &audioObtained.format, &tempChannels); |
---|
77 | audioObtained.channels = tempChannels & 0xFF; |
---|
78 | |
---|
79 | sound_enabled = SFX_INITIALIZED | MUSIC_INITIALIZED; |
---|
80 | |
---|
81 | printf( "Sound: Enabled\n" ); |
---|
82 | |
---|
83 | // It's all good |
---|
84 | return sound_enabled; |
---|
85 | } |
---|
86 | |
---|
87 | // |
---|
88 | // sound_uninit |
---|
89 | // |
---|
90 | // Shutdown audio and release any memory left over. |
---|
91 | // |
---|
92 | void sound_uninit() |
---|
93 | { |
---|
94 | if (sound_enabled) |
---|
95 | { |
---|
96 | Mix_CloseAudio(); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | // |
---|
101 | // sound_effect constructor |
---|
102 | // |
---|
103 | // Read in the requested .wav file. |
---|
104 | // |
---|
105 | sound_effect::sound_effect( char * filename ) |
---|
106 | { |
---|
107 | if( sound_enabled ) |
---|
108 | { |
---|
109 | int sample_speed; |
---|
110 | |
---|
111 | void* temp_data = (void *)read_wav( filename, sample_speed, size ); |
---|
112 | |
---|
113 | SDL_AudioCVT audiocvt; |
---|
114 | |
---|
115 | SDL_BuildAudioCVT( &audiocvt, AUDIO_U8, 1, 11025, audioObtained.format, audioObtained.channels, audioObtained.freq ); |
---|
116 | data = malloc( size * audiocvt.len_mult ); |
---|
117 | |
---|
118 | memcpy( data, temp_data, size ); |
---|
119 | audiocvt.buf = (Uint8*)data; |
---|
120 | audiocvt.len = size; |
---|
121 | SDL_ConvertAudio( &audiocvt ); |
---|
122 | size = (Uint32)((double)size * audiocvt.len_ratio); |
---|
123 | |
---|
124 | free(temp_data); |
---|
125 | |
---|
126 | this->chunk = Mix_QuickLoad_RAW((Uint8*)data, size); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | // |
---|
131 | // sound_effect destructor |
---|
132 | // |
---|
133 | // Release the audio data. |
---|
134 | // |
---|
135 | sound_effect::~sound_effect() |
---|
136 | { |
---|
137 | if( sound_enabled ) |
---|
138 | { |
---|
139 | // Sound effect deletion only happens on level load, so there |
---|
140 | // is no problem in stopping everything. But the original playing |
---|
141 | // code handles the sound effects and the "playlist" differently. |
---|
142 | // Therefore with SDL_mixer, a sound that has not finished playing |
---|
143 | // on a level load will cut off in the middle. This is most noticable |
---|
144 | // for the button sound of the load savegame dialog. |
---|
145 | Mix_FadeOutGroup(-1, 100); |
---|
146 | while (Mix_Playing(-1)) |
---|
147 | SDL_Delay(10); |
---|
148 | Mix_FreeChunk(this->chunk); |
---|
149 | |
---|
150 | if( data ) |
---|
151 | { |
---|
152 | free( data ); |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | // |
---|
158 | // sound_effect::play |
---|
159 | // |
---|
160 | // Add a new sample for playing. |
---|
161 | // panpot defines the pan position for the sound effect. |
---|
162 | // 0 - Completely to the right. |
---|
163 | // 128 - Centered. |
---|
164 | // 255 - Completely to the left. |
---|
165 | // |
---|
166 | void sound_effect::play( int volume, int pitch, int panpot ) |
---|
167 | { |
---|
168 | if (sound_enabled) |
---|
169 | { |
---|
170 | int channel = Mix_PlayChannel(-1, this->chunk, 0); |
---|
171 | if (channel > -1) |
---|
172 | { |
---|
173 | Mix_Volume(channel, volume); |
---|
174 | Mix_SetPanning(channel, panpot, 255 - panpot); |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | // Play music using SDL_Mixer |
---|
181 | |
---|
182 | song::song(char const * filename) |
---|
183 | { |
---|
184 | data = NULL; |
---|
185 | Name = strdup(filename); |
---|
186 | song_id = 0; |
---|
187 | |
---|
188 | rw = NULL; |
---|
189 | music = NULL; |
---|
190 | |
---|
191 | char realname[255]; |
---|
192 | strcpy(realname, get_filename_prefix()); |
---|
193 | strcat(realname, filename); |
---|
194 | |
---|
195 | unsigned int data_size; |
---|
196 | data = load_hmi(realname, data_size); |
---|
197 | |
---|
198 | if (!data) |
---|
199 | { |
---|
200 | printf("Sound: ERROR - could not load %s\n", realname); |
---|
201 | return; |
---|
202 | } |
---|
203 | |
---|
204 | rw = SDL_RWFromMem(data, data_size); |
---|
205 | music = Mix_LoadMUS_RW(rw); |
---|
206 | |
---|
207 | if (!music) |
---|
208 | { |
---|
209 | printf("Sound: ERROR - %s while loading %s\n", |
---|
210 | Mix_GetError(), realname); |
---|
211 | return; |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | song::~song() |
---|
216 | { |
---|
217 | if(playing()) |
---|
218 | stop(); |
---|
219 | free(data); |
---|
220 | free(Name); |
---|
221 | |
---|
222 | Mix_FreeMusic(music); |
---|
223 | SDL_FreeRW(rw); |
---|
224 | } |
---|
225 | |
---|
226 | void song::play( unsigned char volume ) |
---|
227 | { |
---|
228 | song_id = 1; |
---|
229 | |
---|
230 | Mix_PlayMusic(this->music, 0); |
---|
231 | Mix_VolumeMusic(volume); |
---|
232 | } |
---|
233 | |
---|
234 | void song::stop( long fadeout_time ) |
---|
235 | { |
---|
236 | song_id = 0; |
---|
237 | |
---|
238 | Mix_FadeOutMusic(100); |
---|
239 | } |
---|
240 | |
---|
241 | int song::playing() |
---|
242 | { |
---|
243 | return Mix_PlayingMusic(); |
---|
244 | } |
---|
245 | |
---|
246 | void song::set_volume( int volume ) |
---|
247 | { |
---|
248 | Mix_VolumeMusic(volume); |
---|
249 | } |
---|
250 | |
---|