public
Description: An isometric graphical interface for the games NetHack and Slash'EM
Homepage: http://clivecrous.github.com/vultures/
Clone URL: git://github.com/clivecrous/vultures.git
Click here to lend your support to: vultures and make a donation at www.pledgie.com !
stikonas (author)
Wed Jul 01 11:34:48 -0700 2009
clivecrous (committer)
Wed Jul 01 11:34:48 -0700 2009
vultures / vultures / vultures_sound.cpp
100644 248 lines (177 sloc) 5.557 kb
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
/* NetHack may be freely redistributed. See license for details. */
 
#include <stdlib.h>
#include <string.h>
 
#include "SDL.h"
#include "SDL_audio.h"
#include "SDL_mixer.h"
 
 
#include "vultures_win.h"
#include "vultures_gen.h"
#include "vultures_sound.h"
#include "vultures_opt.h"
 
/*******************************************************************/
 
 
std::vector<vultures_event_sound> vultures_event_sounds;
int vultures_n_background_songs;
int vultures_sound_inited = 0;
 
 
/* Sound effects objects */
vultures_cached_sound *vultures_cached_sounds;
int vultures_oldest_cached_sound = 0;
 
/* Music objects */
SDL_CD *vultures_cdrom = NULL;
 
 
Mix_Music *vultures_current_music=NULL;
 
/*******************************************************************/
 
static void vultures_play_song(std::string midifilename);
static void vultures_play_cd_track(std::string cdtrackname);
static void vultures_play_sound(std::string wavefilename);
static int vultures_is_music_playing(void);
 
/*******************************************************************/
 
 
void vultures_init_sound(void)
{
int i;
 
if (vultures_sound_inited)
return;
 
if (SDL_InitSubSystem(SDL_INIT_AUDIO | SDL_INIT_CDROM) == -1) {
/* init failed */
vultures_opts.play_effects = 0;
vultures_opts.play_music = 0;
vultures_sound_inited = 0;
return;
}
 
if (Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 4096) < 0) {
vultures_opts.play_effects = 0;
vultures_opts.play_music = 0;
vultures_sound_inited = 0;
return;
}
 
Mix_AllocateChannels(4);
 
/* Create the sound cache */
vultures_cached_sounds = new vultures_cached_sound[V_MAX_CACHED_SOUNDS];
for (i = 0; i < V_MAX_CACHED_SOUNDS; i++) {
vultures_cached_sounds[i].chunk = NULL;
vultures_cached_sounds[i].filename = "";
}
 
/* Initialize cd playing. */
vultures_cdrom = NULL;
if (SDL_CDNumDrives() > 0)
/* Open default drive */
vultures_cdrom = SDL_CDOpen(0);
 
vultures_sound_inited = 1;
}
 
 
 
/* tries to play a sound matching the given str */
void vultures_play_event_sound(const char * str)
{
unsigned int i;
 
/* search the configured sounds for one that matches str */
for (i = 0; i < vultures_event_sounds.size(); i++) {
if (strstr(str, vultures_event_sounds[i].searchpattern)) {
switch (vultures_event_sounds[i].soundtype)
{
case V_EVENT_SOUND_TYPE_SND:
vultures_play_sound(vultures_event_sounds[i].filename);
break;
 
case V_EVENT_SOUND_TYPE_MUS:
vultures_play_song(vultures_event_sounds[i].filename);
break;
 
case V_EVENT_SOUND_TYPE_CD_AUDIO:
vultures_play_cd_track(vultures_event_sounds[i].filename);
break;
 
case V_EVENT_SOUND_TYPE_RANDOM_SONG:
vultures_play_ambient_sound(1);
break;
}
 
break;
}
}
}
 
 
void vultures_play_ambient_sound(int force_play)
{
int k;
char tempbuffer[256];
 
if ((!force_play) && (vultures_is_music_playing()))
return;
 
if (force_play)
vultures_stop_music();
 
k = (rand() >> 4) % vultures_n_background_songs;
 
sprintf(tempbuffer, "nhfe_music_background%03d", k);
vultures_play_event_sound(tempbuffer);
}
 
 
static void vultures_play_song(std::string midifilename)
{
if (!vultures_opts.play_music)
return;
 
if (vultures_current_music)
Mix_FreeMusic(vultures_current_music);
 
vultures_current_music = Mix_LoadMUS(midifilename.c_str());
Mix_PlayMusic(vultures_current_music,0);
}
 
 
static void vultures_play_cd_track(std::string cdtrackname)
{
int nTrack;
 
if (!vultures_opts.play_music)
return;
 
/* Parse the track number from the given string */
nTrack = atoi(cdtrackname.c_str());
if (nTrack < 0) {
vultures_write_log(V_LOG_NOTE, __FILE__, __LINE__,
                           "Invalid track number [%s]\n", cdtrackname.c_str());
return;
}
 
if (!vultures_cdrom)
return;
 
if (!CD_INDRIVE(SDL_CDStatus(vultures_cdrom))) {
vultures_write_log(V_LOG_DEBUG, __FILE__, __LINE__, "No CD in drive\n");
return;
}
 
SDL_CDPlayTracks(vultures_cdrom, nTrack, 0, 1, 0);
}
 
 
static void vultures_play_sound(std::string wavefilename)
{
int i;
int sound_exists;
Mix_Chunk *chunk = NULL;
 
if (!vultures_opts.play_effects) return;
 
/* Check if the sound exists in the sound cache */
sound_exists = 0;
for (i = 0; i < V_MAX_CACHED_SOUNDS; i++)
if (wavefilename == vultures_cached_sounds[i].filename) {
sound_exists = 1;
chunk = vultures_cached_sounds[i].chunk;
break;
}
 
if (!sound_exists) {
i = vultures_oldest_cached_sound;
 
if (vultures_cached_sounds[i].chunk)
Mix_FreeChunk(vultures_cached_sounds[i].chunk);
 
vultures_cached_sounds[i].filename = wavefilename;
 
vultures_cached_sounds[i].chunk = Mix_LoadWAV(wavefilename.c_str());
chunk = vultures_cached_sounds[i].chunk;
 
vultures_oldest_cached_sound++;
if (vultures_oldest_cached_sound >= V_MAX_CACHED_SOUNDS)
vultures_oldest_cached_sound = 0;
}
 
/* Play sound */
vultures_write_log(V_LOG_DEBUG, __FILE__, __LINE__, "Playing file %s\n", wavefilename.c_str());
Mix_PlayChannel(-1, chunk, 0);
}
 
 
void vultures_stop_music(void)
{
if (vultures_current_music) Mix_FreeMusic(vultures_current_music);
vultures_current_music = NULL;
 
/* Stop any CD tracks playing */
if (vultures_cdrom) {
if (SDL_CDStatus(vultures_cdrom) == CD_PLAYING)
SDL_CDStop(vultures_cdrom);
}
}
 
 
static int vultures_is_music_playing(void)
{
/* Check for external music files (MIDI or MP3) playing */
if (Mix_PlayingMusic() > 0)
return 1;
 
/* Check for CD tracks playing */
if (vultures_cdrom) {
if (SDL_CDStatus(vultures_cdrom) == CD_PLAYING)
return 1;
}
 
/* No music playing */
return 0;
}