make it possible to override CXX from the command line
[rofl0r-df-libgraphics.git] / g_src / music_and_sound_fmodex.cpp
blob7262c9487143b26b1853e5da31ce4aa0b500c343
1 #ifndef NO_FMOD
3 #include "platform.h"
4 #include <string.h>
5 #include <math.h>
6 #include <iosfwd>
7 #include <iostream>
8 #include <ios>
9 #include <streambuf>
10 #include <istream>
11 #include <ostream>
12 #include <iomanip>
13 #include <sstream>
14 #include <cstdlib>
15 #include <fstream>
16 #include <map>
18 #include "svector.h"
20 #ifdef WIN32
22 #ifndef INTEGER_TYPES
23 #define INTEGER_TYPES
24 typedef short int16_t;
25 typedef int int32_t;
26 typedef long long int64_t;
27 typedef unsigned short uint16_t;
28 typedef unsigned int uint32_t;
29 typedef unsigned long long uint64_t;
30 #endif
32 typedef int32_t VIndex;
33 typedef int32_t Ordinal;
35 #endif
37 #include "random.h"
39 using std::string;
40 using std::map;
41 using std::pair;
43 #include "basics.h"
44 #include "endian.h"
45 #include "files.h"
46 #include "enabler.h"
47 #include "init.h"
49 #include "music_and_sound_fmodex.h"
50 #include "music_and_sound_v.h"
52 void musicsoundst::startbackgroundmusic(int new_song)
54 if (!on || new_song < 0 || new_song > MAXSONGNUM || mod[new_song].sound == NULL) {
55 return;
58 if (song != new_song) {
60 stopbackgroundmusic(); /* This is safe to call, even if song isn't valid. */
62 song = new_song;
63 FMOD_CHANNELINDEX cid = static_cast<FMOD_CHANNELINDEX>(0);
64 system->playSound(cid, mod[song].sound, false, &mod[song].channel);
68 void musicsoundst::stopbackgroundmusic()
70 if (!on || song == -1) {
71 return;
74 if (mod[song].channel != NULL) {
75 mod[song].channel->stop();
76 mod[song].channel = NULL;
79 song = -1;
82 /* Set channel to less than 0 to have FMOD decide the channel for you. */
83 void musicsoundst::playsound(int s,int channel)
85 if (!on || s < 0 || s > MAXSOUNDNUM || samp[s].sound == NULL) {
86 return;
89 if (channel >= 0) {
90 FMOD_CHANNELINDEX cid = static_cast<FMOD_CHANNELINDEX>(channel);
91 system->playSound(cid, samp[s].sound, false, &samp[s].channel);
92 } else {
93 system->playSound(FMOD_CHANNEL_FREE, samp[s].sound, false, &samp[s].channel);
97 // Toady:
98 /* Yeah, I started porting all that other crap, but it wasn't all applicable to ex
99 * and besides which, you never call it anyway.
101 void musicsoundst::playsound(int s, int min_channel, int max_channel, int force_channel)
103 if (!on || s < 0 || s > MAXSOUNDNUM || samp[s].sound == NULL) {
104 return;
107 playsound(s, force_channel);
110 // Prints a relevent error message to stderr.
111 inline void err(FMOD_RESULT r)
113 std::cerr << "sound: failure: " << FMOD_ErrorString(r) << std::endl;
116 void musicsoundst::initsound()
118 FMOD_RESULT result = FMOD::System_Create(&system);
119 if (result != FMOD_OK) {
120 err(result);
121 on = 0;
122 return;
125 #if defined(linux)
126 /* Set up the sound system. Default to ALSA. */
127 switch (this->sound_system) {
128 default:
129 case ALSA:
130 result = system->setOutput(FMOD_OUTPUTTYPE_ALSA);
131 break;
132 case OSS:
133 result = system->setOutput(FMOD_OUTPUTTYPE_OSS);
134 break;
135 case ESD:
136 result = system->setOutput(FMOD_OUTPUTTYPE_ESD);
137 break;
139 if (result != FMOD_OK) {
140 err(result);
141 on = 0;
142 return;
144 #endif
146 SoftChannelNumber = SOUND_CHANNELNUM;
147 result = system->init(SoftChannelNumber, FMOD_INIT_NORMAL, NULL);
148 if (result != FMOD_OK) {
149 err(result);
150 on = 0;
151 return;
154 result = system->getMasterChannelGroup(&masterchannelgroup);
155 if (result != FMOD_OK) {
156 err(result);
157 on = 0;
158 return;
161 set_master_volume(init.media.volume);
163 on = 1;
166 void musicsoundst::deinitsound()
168 if (!on) {
169 return;
172 int s;
173 for (s = 0; s < MAXSONGNUM; s++) {
174 if (mod[s].sound != NULL) {
175 mod[s].sound->release();
176 mod[s].sound = NULL;
177 mod[s].channel = NULL;
181 for (s = 0; s < MAXSOUNDNUM; s++) {
182 if (samp[s].sound != NULL) {
183 samp[s].sound->release();
184 samp[s].sound = NULL;
185 samp[s].channel = NULL;
189 system->release();
190 on = 0;
195 void musicsoundst::set_song(string &filename, int slot)
197 if (!on || slot < 0 || slot > MAXSONGNUM) {
198 return;
201 if (mod[slot].sound != NULL) {
202 mod[slot].sound->release();
203 mod[slot].sound = NULL;
204 mod[slot].channel = NULL;
207 FMOD_RESULT result = system->createSound(filename.c_str(), FMOD_DEFAULT, 0, &mod[slot].sound);
208 if (result != FMOD_OK) {
209 mod[slot].sound = NULL;
210 mod[slot].channel = NULL;
211 return;
214 mod[slot].sound->setMode(FMOD_LOOP_NORMAL);
217 void musicsoundst::set_sound(string &filename, int slot, int pan, int priority)
219 if (!on || slot < 0 || slot > MAXSOUNDNUM) {
220 return;
223 if (samp[slot].sound != NULL) {
224 samp[slot].sound->release();
225 samp[slot].sound = NULL;
226 samp[slot].channel = NULL;
229 FMOD_RESULT result = system->createSound(filename.c_str(), FMOD_DEFAULT, 0, &samp[slot].sound);
230 if (result != FMOD_OK) {
231 samp[slot].sound = NULL;
232 samp[slot].channel = NULL;
233 return;
237 void musicsoundst::set_sound_params(int slot, int p1, int vol, int pan, int priority)
239 if (slot < 0 || slot > MAXSOUNDNUM) {
240 return;
241 } if (samp[slot].channel == NULL || on == 0) {
242 return;
245 samp[slot].channel->setPan(oldval_to_panfloat(pan));
246 samp[slot].channel->setVolume(oldval_to_volumefloat(vol));
247 samp[slot].channel->setPriority(oldval_to_priority(priority));
248 samp[slot].channel->setFrequency(static_cast<float>(p1));
251 void musicsoundst::stop_sound(int channel)
253 FMOD::Channel* c;
255 FMOD_RESULT result = system->getChannel((int)channel, &c);
256 if (result != FMOD_OK) {
257 return;
260 c->stop();
264 void musicsoundst::set_master_volume(long newvol)
266 masterchannelgroup->setVolume(oldval_to_volumefloat(newvol));
269 // Converts old FMOD 3 0 - 255 volume values to FMOD Ex 0.0 <-> 1.0 float values.
270 float musicsoundst::oldval_to_volumefloat(int val)
272 if (val < 0) {
273 return 0.0;
274 } else if (val > 255) {
275 return 1.0;
278 return static_cast<float>(val) / 255;
281 // Converts old FMOD 3 0 - 255 pan values to FMOD Ex -1.0 <-> 1.0 float values.
282 float musicsoundst::oldval_to_panfloat(int val)
284 if (val < 0) {
285 return -1.0;
286 } else if (val > 255) {
287 return 1.0;
290 float n = static_cast<float>(val) / 255;
291 return (n * 2) - 1.0;
294 // Converts old FMOD 3 0(lowest) - 255(highest) priority values to
295 // FmodEx 0(highest) - 256(lowest) priority values.
296 int musicsoundst::oldval_to_priority(int val)
298 if (val < 0) {
299 return 256;
300 } else if (val > 255) {
301 return 0;
304 return 255 - val;
307 #endif // NO_FMOD