24 typedef short int16_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;
32 typedef int32_t VIndex
;
33 typedef int32_t Ordinal
;
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
) {
58 if (song
!= new_song
) {
60 stopbackgroundmusic(); /* This is safe to call, even if song isn't valid. */
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) {
74 if (mod
[song
].channel
!= NULL
) {
75 mod
[song
].channel
->stop();
76 mod
[song
].channel
= NULL
;
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
) {
90 FMOD_CHANNELINDEX cid
= static_cast<FMOD_CHANNELINDEX
>(channel
);
91 system
->playSound(cid
, samp
[s
].sound
, false, &samp
[s
].channel
);
93 system
->playSound(FMOD_CHANNEL_FREE
, samp
[s
].sound
, false, &samp
[s
].channel
);
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
) {
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
) {
126 /* Set up the sound system. Default to ALSA. */
127 switch (this->sound_system
) {
130 result
= system
->setOutput(FMOD_OUTPUTTYPE_ALSA
);
133 result
= system
->setOutput(FMOD_OUTPUTTYPE_OSS
);
136 result
= system
->setOutput(FMOD_OUTPUTTYPE_ESD
);
139 if (result
!= FMOD_OK
) {
146 SoftChannelNumber
= SOUND_CHANNELNUM
;
147 result
= system
->init(SoftChannelNumber
, FMOD_INIT_NORMAL
, NULL
);
148 if (result
!= FMOD_OK
) {
154 result
= system
->getMasterChannelGroup(&masterchannelgroup
);
155 if (result
!= FMOD_OK
) {
161 set_master_volume(init
.media
.volume
);
166 void musicsoundst::deinitsound()
173 for (s
= 0; s
< MAXSONGNUM
; s
++) {
174 if (mod
[s
].sound
!= NULL
) {
175 mod
[s
].sound
->release();
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
;
195 void musicsoundst::set_song(string
&filename
, int slot
)
197 if (!on
|| slot
< 0 || slot
> MAXSONGNUM
) {
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
;
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
) {
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
;
237 void musicsoundst::set_sound_params(int slot
, int p1
, int vol
, int pan
, int priority
)
239 if (slot
< 0 || slot
> MAXSOUNDNUM
) {
241 } if (samp
[slot
].channel
== NULL
|| on
== 0) {
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
)
255 FMOD_RESULT result
= system
->getChannel((int)channel
, &c
);
256 if (result
!= FMOD_OK
) {
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
)
274 } else if (val
> 255) {
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
)
286 } else if (val
> 255) {
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
)
300 } else if (val
> 255) {