-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.cpp
More file actions
executable file
·175 lines (144 loc) · 3.57 KB
/
audio.cpp
File metadata and controls
executable file
·175 lines (144 loc) · 3.57 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
#include "mastrix.hpp"
Audio audio; //external declaration
Audio::Audio()
{
currentVolume = 255;
currentMusicVolume = 128;
musicChannel = 0;
currentSpeakerMode = speaker_stereo;
}
Audio::~Audio()
{}
void Audio::initialize()
{
FSOUND_Init(44100, 32, 0);
setSpeakerMode(currentSpeakerMode);
currentVolume = FSOUND_GetSFXMasterVolume();
}
void Audio::update()
{
FSOUND_Update();
}
void Audio::shutdown()
{
FSOUND_Close();
}
//play a 2D (non-positional) sound. Make sure that the sound was loaded
//with 2D parameters, not 3D!
void Audio::playSound(const Sound &sound)
{
FSOUND_PlaySound(FSOUND_FREE, sound.getSoundData());
}
//play a 3D (positional) sound. Make sure that thes ound was loaded with
//3D parameters, not 2D!
void Audio::playSoundEx(
const Sound &sound,
float x,
float y,
float z,
bool repeat)
{
float soundOrientation[3] = {x, y, z};
int channel = FSOUND_PlaySoundEx(
FSOUND_FREE,
sound.getSoundData(),
0,
repeat);
FSOUND_3D_SetAttributes(
channel,
soundOrientation,
0);
}
//music should be loaded by this point!
void Audio::playMusic(const Music &music, bool repeat)
{
musicChannel = FSOUND_Stream_PlayEx(
FSOUND_FREE,
music.getMusicData(),
0,
false);
setMusicVolume(currentMusicVolume);
}
void Audio::stopMusic(const Music &music)
{
FSOUND_Stream_Close(music.getMusicData());
}
const int Audio::maxVolume = 255;
const int Audio::minVolume = 0;
//change the master volume
void Audio::setVolume(int volume)
{
FSOUND_SetSFXMasterVolume(volume);
}
//change the music volume only
void Audio::setMusicVolume(int volume)
{
currentMusicVolume = volume;
FSOUND_SetVolume(musicChannel, currentMusicVolume);
}
//change the speaker settings to match your speaker setup
void Audio::setSpeakerMode(speaker_mode speakerMode)
{
currentSpeakerMode = speakerMode;
FSOUND_SetSpeakerMode(currentSpeakerMode);
}
const Audio::speaker_mode Audio::getSpeakerModeByName(
const std::string &speakerModeName) const
{
if(speakerModeName == "headphones")
return speaker_headphones;
else if(speakerModeName == "mono")
return speaker_mono;
else if(speakerModeName == "quad")
return speaker_quad;
else
return speaker_stereo;
}
static std::string currentSoundtrack("");
static Music *currentMusic = NULL;
void setVolume(int volume);
void setMusicVolume(int volume);
ClientConsoleVar<int> volume("volume", 255, setVolume);
ClientConsoleVar<int> musicVolume("music_volume", 128, setMusicVolume);
void setSoundtrack(const char *filename, bool repeat)
{
if(filename)
currentSoundtrack = filename;
int oldVolume = musicVolume;
stopMusic();
resumeMusic(repeat);
setMusicVolume(musicVolume);
}
void stopMusic(void)
{
if(currentMusic) {
audio.stopMusic(*currentMusic);
delete currentMusic;
currentMusic = NULL;
}
}
void resumeMusic(bool repeat)
{
if(currentMusic) return;
currentMusic = new Music();
currentMusic->load(currentSoundtrack.c_str(), repeat);
audio.playMusic(*currentMusic);
}
void setVolume(int volume)
{
FSOUND_SetSFXMasterVolume(volume);
}
void setMusicVolume(int volume)
{
FSOUND_SetVolume(audio.getMusicChannel(), volume);
}
CLIENT_CONSOLE_COMMAND(set_speaker_mode)
{
if (argc < 1)
{
printfToClient(who, "Not enough arguments to set_speaker_mode (should be 1)");
printfToClient(who, "Usage: set_speaker_mode mode");
return;
}
audio.setSpeakerMode(audio.getSpeakerModeByName(argv[0]));
}