-
Notifications
You must be signed in to change notification settings - Fork 1
/
sounds.cpp
185 lines (152 loc) · 3.76 KB
/
sounds.cpp
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
#include "sounds.h"
#if defined(WIN32) || defined(_WIN64) || defined(__WATCOMC__)
#include <windows.h>
#else
#include "FMOD/inc/wincompat.h"
#endif
#include "FMOD/inc/fmod.h"
#include "FMOD/inc/fmod_errors.h" /* optional */
#define CHANNEL_MUSIC 1
#define CHANNEL_HEARTBEAT 2
#define HEARTBEAT_PATH "data/sounds/other/heartbeat.wav"
SOUNDS::SOUNDS()
{
music_is_playing=false;
heartbeat_is_playing=false;
initialized=false;
current_music=NULL;
if (FSOUND_GetVersion() < FMOD_VERSION)
return;
if (!FSOUND_Init(44100, 64, 0))
return;
initialized=true;
}
SOUNDS::~SOUNDS()
{
if (initialized)
FSOUND_Close();
}
bool SOUNDS::PlayMusic(const string &songname)
{
if (!initialized)
return false;
if (songname==current_song)
return true;
if (music_is_playing)
{
if (current_music!=NULL)
{
FMUSIC_StopSong(current_music);
current_music=NULL;
}
else
FSOUND_StopSound(CHANNEL_MUSIC);
}
// check if we have this song
map < string, FMUSIC_MODULE *>::iterator m;
map < string, FSOUND_SAMPLE *>::iterator s;
m = songs.find(songname);
if (m!=songs.end()) // yes, play it
{
current_music=(*m).second;
FMUSIC_PlaySong(current_music);
current_song = songname;
return true;
}
s=samples.find(songname);
if (s!=samples.end())
{
FSOUND_Sample_SetMode((*s).second, FSOUND_LOOP_NORMAL);
FSOUND_PlaySound(CHANNEL_MUSIC, (*s).second);
current_song = songname;
return true;
}
// Not in samples and not in music
current_music = LoadMusic(songname);
if (current_music!=NULL)
{
FMUSIC_PlaySong(current_music);
current_song = songname;
return true;
}
// Music not loaded
FSOUND_SAMPLE *sample = LoadSound(songname);
if (sample!=NULL)
{
FSOUND_Sample_SetMode(sample, FSOUND_LOOP_NORMAL);
FSOUND_PlaySound(CHANNEL_MUSIC, sample);
current_song = songname;
return true;
}
return false;
}
bool SOUNDS::PlaySoundOnce(const string &soundname, int volume)
{
if (!initialized)
return false;
// check if we have this sound
FSOUND_SAMPLE *sample=NULL;
map < string, FSOUND_SAMPLE *>::iterator s;
s=samples.find(soundname);
if (s!=samples.end())
sample = (*s).second;
else
sample = LoadSound(soundname);
if (sample!=NULL)
{
FSOUND_Sample_SetMode(sample, FSOUND_LOOP_OFF);
int channel = FSOUND_PlaySound(FSOUND_FREE, sample);
FSOUND_SetVolume(channel,volume);
}
return true;
}
FMUSIC_MODULE *SOUNDS::LoadMusic(const string &songname)
{
if (!initialized)
return false;
FMUSIC_MODULE *mod = FMUSIC_LoadSong(songname.c_str());
return mod;
}
FSOUND_SAMPLE *SOUNDS::LoadSound(const string &songname)
{
if (!initialized)
return false;
FSOUND_SAMPLE *sample = FSOUND_Sample_Load(FSOUND_UNMANAGED,songname.c_str(),FSOUND_NORMAL | FSOUND_2D | FSOUND_MPEGACCURATE | FSOUND_NONBLOCKING, 0, 0);
return sample;
}
bool SOUNDS::PlayHeartBeat()
{
if (!initialized)
return false;
if (heartbeat_is_playing)
return true;
FSOUND_StopSound(CHANNEL_HEARTBEAT);
// check if we have this song
map < string, FSOUND_SAMPLE *>::iterator s;
s=samples.find(HEARTBEAT_PATH);
if (s!=samples.end())
{
FSOUND_Sample_SetMode((*s).second, FSOUND_LOOP_NORMAL);
FSOUND_PlaySound(CHANNEL_HEARTBEAT, (*s).second);
return true;
}
FSOUND_SAMPLE *sample = LoadSound(HEARTBEAT_PATH);
if (sample!=NULL)
{
FSOUND_Sample_SetMode(sample, FSOUND_LOOP_NORMAL);
FSOUND_PlaySound(CHANNEL_HEARTBEAT, sample);
heartbeat_is_playing=true;
return true;
}
return false;
}
bool SOUNDS::StopHeartBeat()
{
if (!initialized)
return false;
if (!heartbeat_is_playing)
return true;
FSOUND_StopSound(CHANNEL_HEARTBEAT);
heartbeat_is_playing=false;
return true;
}