-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.c
198 lines (136 loc) · 3.58 KB
/
sound.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
/*
* sound.c
*
* Sound subsystem for SR389 engine
*
*/
/* standard c and allegro includes */
#include <stdio.h>
#include <string.h>
#include "allegro5/allegro.h"
#include "allegro5/allegro_audio.h"
#include "allegro5/allegro_acodec.h"
#include "medlib.h"
#include "sound.h"
/* global music container - if music is playing, it's here */
ALLEGRO_AUDIO_STREAM *music;
/* voice for music output */
ALLEGRO_VOICE *voice;
/* allegro sound mixer */
ALLEGRO_MIXER *mixer;
/* functions - used to call to this library from other parts of engine */
unsigned int setupSound(void)
{
/* set up sound library for first use, and init allegro soundsystem */
music = NULL;
voice = NULL;
mixer = NULL;
if (!al_install_audio()) {
/* fail if audio does not load for some reason */
return 0;
}
if (!al_init_acodec_addon()) {
/* fail if audio does not load for some reason */
return 0;
}
if (!al_reserve_samples(8)) {
/* fail if audio does not load for some reason */
return 0;
}
mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2);
al_attach_mixer_to_voice(mixer, voice);
return true;
}
void playSound(const unsigned int index)
{
/* play sound by index number from library, standard volume */
al_play_sample(sndLib[index].data, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
}
void playSoundByName(char *filename)
{
/* play sound by filename instead of index */
playSound(searchForSnd(filename));
}
void playMusic(const char *filename)
{
/* play music in background, on loop - unpause if music is already playing */
if (isMusicPaused())
{
unPauseMusic();
return;
}
/* music not playing, start over from beginning */
ALLEGRO_PLAYMODE loop = ALLEGRO_PLAYMODE_LOOP;
al_stop_samples();
if (music != NULL)
{
al_drain_audio_stream(music);
al_destroy_audio_stream(music);
}
music = al_load_audio_stream(filename, 4, 2048);
al_set_audio_stream_playmode(music, loop);
al_attach_audio_stream_to_mixer(music, mixer);
}
void playMusicOnce(const char *filename)
{
/* play music only once in background, terminating old music */
ALLEGRO_PLAYMODE loop = ALLEGRO_PLAYMODE_ONCE;
al_stop_samples();
if (music != NULL)
{
al_drain_audio_stream(music);
al_destroy_audio_stream(music);
}
music = al_load_audio_stream(filename, 4, 2048);
al_set_audio_stream_playmode(music, loop);
al_attach_audio_stream_to_mixer(music, mixer);
}
void pauseMusic(void)
{
/* pause game music */
if (music != NULL)
{
al_set_audio_stream_playing(music, false);
}
}
int isMusicPaused(void)
{
/* return true if music is paused */
if (music != NULL)
{
return !(al_get_audio_stream_playing(music));
}
return false;
}
void unPauseMusic(void)
{
/* unpause game music */
if (music != NULL)
{
al_set_audio_stream_playing(music, true);
}
}
void killSound(void)
{
/* terminate all sound and music playing in background */
al_stop_samples();
if (music != NULL)
{
al_drain_audio_stream(music);
al_destroy_audio_stream(music);
music = NULL;
}
}
void shutdownSound(void)
{
/* shutdown sound system when done */
al_stop_samples();
if (music != NULL)
{
al_destroy_audio_stream(music);
}
al_destroy_mixer(mixer);
al_destroy_voice(voice);
al_uninstall_audio();
}