-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsparrowSound.c
276 lines (257 loc) · 5.99 KB
/
sparrowSound.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* This file is part of sparrow3d.
* Sparrow3d is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Sparrow3d is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>
*
* For feedback and questions about my Files and Projects please mail me,
* Alexander Matthes (Ziz) , zizsdl_at_googlemail.com */
#include "sparrowSound.h"
int spSoundIsInitialized = 0;
int spNoSoundAvaible = 1;
Mix_Music *spBackgroundMusic = NULL;
int spMusicVolume = SP_VOLUME_MAX;
int spSoundVolume = SP_VOLUME_MAX;
int spSoundInPause = 0;
PREFIX int spSoundInit( void )
{
if (spSoundIsInitialized)
return !spNoSoundAvaible;
spSoundIsInitialized = 1;
// load support for the OGG and MOD sample/music formats
#if (SDL_MIXER_PATCHLEVEL >= 10)
int result = Mix_Init(MIX_INIT_OGG | MIX_INIT_MP3 | MIX_INIT_FLAC | MIX_INIT_MOD);
if(result & MIX_INIT_OGG)
printf("Support for ogg: Yes\n");
else
printf("Support for ogg: No (%s)\n",Mix_GetError());
if(result & MIX_INIT_MP3)
printf("Support for mp3: Yes\n");
else
printf("Support for mp3: No (%s)\n",Mix_GetError());
if(result & MIX_INIT_FLAC)
printf("Support for flac: Yes\n");
else
printf("Support for flac: No (%s)\n",Mix_GetError());
if(result & MIX_INIT_MOD)
printf("Support for mod: Yes\n");
else
printf("Support for mod: No (%s)\n",Mix_GetError());
#endif
if (!
#ifdef F100
Mix_OpenAudio(44100,AUDIO_S16SYS,2,256)
#else
Mix_OpenAudio(44100,AUDIO_S16SYS,2,2048)
#endif
)
{
printf("Try to open Sound: Success\n");
printf(" %i channels are avaible as default\n",Mix_AllocateChannels(-1));
spNoSoundAvaible = 0;
}
else
{
printf("Try to open Sound: Failed\n");
spNoSoundAvaible = 1;
}
spSoundSetMusicVolume(spMusicVolume);
spSoundSetVolume(spSoundVolume);
return !spNoSoundAvaible;
}
PREFIX void spSoundSetChannels(int channels)
{
Mix_AllocateChannels(channels);
}
PREFIX void spSoundPauseAll(int pause)
{
if (spNoSoundAvaible)
return;
if (pause && !spSoundInPause)
{
if (spBackgroundMusic)
Mix_PauseMusic();
Mix_Pause(-1);
spSoundInPause = 1;
}
else
if (!pause && spSoundInPause)
{
if (spBackgroundMusic)
Mix_ResumeMusic();
Mix_Resume(-1);
spSoundInPause = 0;
}
}
PREFIX spSound* spSoundLoad(char* filename)
{
if (spNoSoundAvaible)
return NULL;
spSound* sound = Mix_LoadWAV(filename);
if (!sound)
printf("Failed loading \"%s\" because %s\n", filename, Mix_GetError());
return sound;
}
PREFIX int spSoundPlay(spSound* sound,int channel,int loops,int fadeIn,int maxTime)
{
if (spNoSoundAvaible && !sound)
return 0;
Mix_VolumeChunk(sound,spSoundVolume);
int result;
if (fadeIn > 0)
{
if (maxTime >= 0)
result = Mix_FadeInChannelTimed(channel, sound,loops,fadeIn,maxTime);
else
result = Mix_FadeInChannel(channel, sound,loops,fadeIn);
}
else
{
if (maxTime >= 0)
result = Mix_PlayChannelTimed(channel, sound,loops,maxTime);
else
result = Mix_PlayChannel(channel, sound,loops);
}
if (result == -1)
{
printf("Failed playing sound because %s\n", Mix_GetError());
return 0;
}
else
if (spSoundInPause)
Mix_Pause(result);
return result;
}
PREFIX void spSoundStop(int soundChannel, int fadeOut)
{
if (spNoSoundAvaible)
return;
if (fadeOut > 0)
Mix_FadeOutChannel(soundChannel,fadeOut);
else
Mix_HaltChannel(soundChannel);
}
PREFIX void spSoundPause(int pause, int channel)
{
if (spNoSoundAvaible)
return;
if (pause)
{
Mix_Pause(channel);
}
else
Mix_Resume(channel);
}
PREFIX void spSoundDelete(spSound* sound)
{
if (spNoSoundAvaible || !sound)
return;
Mix_FreeChunk(sound);
}
PREFIX int spSoundSetMusic(char* filename)
{
if (spNoSoundAvaible)
return 0;
if (spBackgroundMusic)
Mix_FreeMusic(spBackgroundMusic);
spBackgroundMusic = Mix_LoadMUS(filename);
if (!spBackgroundMusic)
{
printf("Failed loading \"%s\" because %s\n", filename, Mix_GetError());
return 0;
}
else
Mix_VolumeMusic(spMusicVolume);
return 1;
}
PREFIX int spSoundPlayMusic(int fadeIn,int loops)
{
if (spNoSoundAvaible && !spBackgroundMusic)
return 0;
int result;
if (fadeIn > 0)
result = Mix_FadeInMusic(spBackgroundMusic, loops, fadeIn);
else
result = Mix_PlayMusic(spBackgroundMusic, loops);
if (result == -1)
{
printf("Failed playing music because %s\n", Mix_GetError());
return 0;
}
return 1;
}
PREFIX int spSoundStopMusic(int fadeOut)
{
if (spNoSoundAvaible && !spBackgroundMusic)
return 0;
int result;
if (fadeOut > 0)
result = Mix_FadeOutMusic(fadeOut);
else
result = Mix_HaltMusic();
if (result == -1)
{
printf("Failed halting music because %s\n", Mix_GetError());
return 0;
}
return 1;
}
PREFIX void spSoundPauseMusic(int pause)
{
if (spNoSoundAvaible)
return;
if (pause)
{
if (spBackgroundMusic)
Mix_PauseMusic();
}
else
if (spBackgroundMusic)
Mix_ResumeMusic();
}
PREFIX int spSoundGetMusicVolume( void )
{
return spMusicVolume;
}
PREFIX void spSoundSetMusicVolume(int volume)
{
if (volume < 0)
volume = 0;
if (volume > SP_VOLUME_MAX)
volume = SP_VOLUME_MAX;
spMusicVolume = volume;
if (!spNoSoundAvaible)
Mix_VolumeMusic(volume);
}
PREFIX int spSoundGetVolume( void )
{
return spSoundVolume;
}
PREFIX void spSoundSetVolume(int volume)
{
if (volume < 0)
volume = 0;
if (volume > SP_VOLUME_MAX)
volume = SP_VOLUME_MAX;
spSoundVolume = volume;
if (!spNoSoundAvaible)
Mix_Volume(-1,volume);
}
PREFIX void spSoundQuit( void )
{
if (spBackgroundMusic)
Mix_FreeMusic(spBackgroundMusic);
spBackgroundMusic = NULL;
Mix_CloseAudio();
#if (SDL_MIXER_PATCHLEVEL >= 10)
Mix_Quit();
#endif
}