| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE. * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
#ifndef FIFE_SOUNDEMITTER_H_ |
| 23 |
#define FIFE_SOUNDEMITTER_H_ |
| 24 |
|
| 25 |
// Standard C++ library includes |
| 26 |
|
| 27 |
// Platform specific includes |
| 28 |
|
| 29 |
// 3rd party library includes |
| 30 |
|
| 31 |
// FIFE includes |
| 32 |
// These includes are split up in two parts, separated by one empty line |
| 33 |
// First block: files included from the FIFE root src directory |
| 34 |
// Second block: files included from the same folder |
| 35 |
#include "model/metamodel/modelcoords.h" |
| 36 |
|
| 37 |
#include "soundclip.h" |
| 38 |
|
| 39 |
namespace FIFE { |
| 40 |
|
| 41 |
/** State of the audio file |
| 42 |
*/ |
| 43 |
enum SoundStateType { |
| 44 |
SD_UNKNOWN_STATE, |
| 45 |
SD_INITIAL_STATE, |
| 46 |
SD_PLAYING_STATE, |
| 47 |
SD_PAUSED_STATE, |
| 48 |
SD_STOPPED_STATE |
| 49 |
}; |
| 50 |
|
| 51 |
/** Listener interface for SoundEmitter. |
| 52 |
*/ |
| 53 |
class SoundEmitterListener { |
| 54 |
public: |
| 55 |
virtual ~SoundEmitterListener() {}; |
| 56 |
|
| 57 |
/** Called when a sound finished playing. Or stop() was called. |
| 58 |
* @param emitterId The id of emitter. |
| 59 |
* @param SoundClipId The id of sound clip aka ResourceHandle. |
| 60 |
*/ |
| 61 |
virtual void onSoundFinished(uint32_t emitterId, uint32_t soundClipId) = 0; |
| 62 |
}; |
| 63 |
|
| 64 |
class SoundEffect; |
| 65 |
class SoundFilter; |
| 66 |
class SoundManager; |
| 67 |
|
| 68 |
/** The class for playing audio files |
| 69 |
*/ |
| 70 |
class SoundEmitter { |
| 71 |
public: |
| 72 |
|
| 73 |
SoundEmitter(SoundManager* manager, uint32_t uid); |
| 74 |
~SoundEmitter(); |
| 75 |
|
| 76 |
/** Sets openAl-source. |
| 77 |
*/ |
| 78 |
void setSource(ALuint source); |
| 79 |
|
| 80 |
/** Return openAl-source. |
| 81 |
*/ |
| 82 |
ALuint getSource() const; |
| 83 |
|
| 84 |
/** Return if the Emitter is active / have an openAl-source. |
| 85 |
*/ |
| 86 |
bool isActive() const; |
| 87 |
|
| 88 |
/** Called once a frame from the SoundManager. |
| 89 |
*/ |
| 90 |
void update(); |
| 91 |
|
| 92 |
/** Returns the emitter-id |
| 93 |
*/ |
| 94 |
uint32_t getId() const; |
| 95 |
|
| 96 |
/** Sets Positioning-Type |
| 97 |
* Default is false |
| 98 |
* |
| 99 |
* @param relative If set to true, the emitters position will be interpreted relative to the listener object |
| 100 |
* |
| 101 |
*/ |
| 102 |
void setRelativePositioning(bool relative); |
| 103 |
|
| 104 |
/** Return Positioning-Type |
| 105 |
*/ |
| 106 |
bool isRelativePositioning() const; |
| 107 |
|
| 108 |
/** Sets the distance under which the volume for the SoundEmitter would normally drop by half (before |
| 109 |
* being influenced by rolloff factor or max distance. Default 1.0 |
| 110 |
*/ |
| 111 |
void setReferenceDistance(float distance); |
| 112 |
|
| 113 |
/** Return the reference distance. |
| 114 |
*/ |
| 115 |
float getReferenceDistance() const; |
| 116 |
|
| 117 |
/** Sets the max distance used with the Inverse Clamped Distance Model to set the distance where |
| 118 |
* there will no longer be any attenuation of the source. |
| 119 |
*/ |
| 120 |
void setMaxDistance(float distance); |
| 121 |
|
| 122 |
/** Return the max distance. |
| 123 |
*/ |
| 124 |
float getMaxDistance() const; |
| 125 |
|
| 126 |
/** Sets the AL_ROLEOFF_FACTOR. Rolloff factor judges the strength of attenuation over distance. |
| 127 |
* |
| 128 |
* @param rolloff Rolloff factor. You'll need to do a lot of testing to find a value which suits your needs. Default 1.0 |
| 129 |
*/ |
| 130 |
void setRolloff(float rolloff); |
| 131 |
|
| 132 |
/** Return the AL_ROLEOFF_FACTOR. Rolloff factor judges the strength of attenuation over distance. |
| 133 |
*/ |
| 134 |
float getRolloff() const; |
| 135 |
|
| 136 |
/** Sets the sound clip to be used by this emitter. |
| 137 |
* @param soundClip SoundClipPtr of the sound to be used. |
| 138 |
*/ |
| 139 |
void setSoundClip(SoundClipPtr soundClip); |
| 140 |
|
| 141 |
/** Get the current sound clip used by this emitter. |
| 142 |
* @return A SoundClipPtr of the sound clip. |
| 143 |
*/ |
| 144 |
SoundClipPtr getSoundClip(); |
| 145 |
|
| 146 |
/** Sets the sound clip to be used by this emitter. |
| 147 |
* @param name The name of the sound to be used. |
| 148 |
*/ |
| 149 |
void setSoundClip(const std::string& name); |
| 150 |
|
| 151 |
/** Reset the emitter, free all internal buffers |
| 152 |
* |
| 153 |
* @param defaultall If set to true, emitter position, velocity, gain and type will be set to the default values |
| 154 |
*/ |
| 155 |
void reset(bool defaultall = false); |
| 156 |
|
| 157 |
/** Releases the emitter |
| 158 |
*/ |
| 159 |
void release(); |
| 160 |
|
| 161 |
/** Sets the playing mode |
| 162 |
*/ |
| 163 |
void setLooping(bool loop); |
| 164 |
|
| 165 |
/** Return playing mode |
| 166 |
*/ |
| 167 |
bool isLooping() const; |
| 168 |
|
| 169 |
/** Plays the associated audio file. |
| 170 |
*/ |
| 171 |
void play(); |
| 172 |
|
| 173 |
/** Plays the associated audio file with fade in and / or out. |
| 174 |
*/ |
| 175 |
void play(float inTime, float outTime); |
| 176 |
|
| 177 |
/** Stops playing the audio file and rewinds to the beginning. |
| 178 |
*/ |
| 179 |
void stop(); |
| 180 |
|
| 181 |
/** Stops playing the audio file after time with fade out. |
| 182 |
*/ |
| 183 |
void stop(float time); |
| 184 |
|
| 185 |
/** Pauses playing the audio file. |
| 186 |
*/ |
| 187 |
void pause(); |
| 188 |
|
| 189 |
/** Rewinds the associated audio file. |
| 190 |
*/ |
| 191 |
void rewind(); |
| 192 |
|
| 193 |
/** Sets the gain of the emitter |
| 194 |
* |
| 195 |
* @param gain The gain value. 0=silence ... 1.0=normal loudness. |
| 196 |
*/ |
| 197 |
void setGain(float gain); |
| 198 |
|
| 199 |
/** Returns the gain of the emitter |
| 200 |
* |
| 201 |
* @return The gain value. 0=silence ... 1.0=normal loudness. |
| 202 |
*/ |
| 203 |
float getGain() const; |
| 204 |
|
| 205 |
/** Sets the max. gain of the emitter |
| 206 |
* |
| 207 |
* @param gain The gain value. 0=silence ... 1.0=normal loudness. |
| 208 |
*/ |
| 209 |
void setMaxGain(float gain); |
| 210 |
|
| 211 |
/** Returns the max. gain of the emitter |
| 212 |
* |
| 213 |
* @return The gain value. 0=silence ... 1.0=normal loudness. |
| 214 |
*/ |
| 215 |
float getMaxGain() const; |
| 216 |
|
| 217 |
/** Sets the min. gain of the emitter |
| 218 |
* |
| 219 |
* @param gain The gain value. 0=silence ... 1.0=normal loudness. |
| 220 |
*/ |
| 221 |
void setMinGain(float gain); |
| 222 |
|
| 223 |
/** Returns the min. gain of the emitter |
| 224 |
* |
| 225 |
* @return The gain value. 0=silence ... 1.0=normal loudness. |
| 226 |
*/ |
| 227 |
float getMinGain() const; |
| 228 |
|
| 229 |
/** Tests if the audio data is stereo data or mono. |
| 230 |
* |
| 231 |
* @return Returns true if the audio data is stereo, false if mono. |
| 232 |
*/ |
| 233 |
bool isStereo(); |
| 234 |
|
| 235 |
/** Returns the bit resolution |
| 236 |
*/ |
| 237 |
int16_t getBitResolution(); |
| 238 |
|
| 239 |
/** Returns the sample rate |
| 240 |
*/ |
| 241 |
uint64_t getSampleRate(); |
| 242 |
|
| 243 |
/** Returns the length of the decoded length in bytes |
| 244 |
*/ |
| 245 |
uint64_t getDecodedLength(); |
| 246 |
|
| 247 |
/** Returns the duration of the sound clip in milliseconds |
| 248 |
*/ |
| 249 |
uint64_t getDuration(); |
| 250 |
|
| 251 |
/** Returns timestamp of the last play start in milliseconds |
| 252 |
*/ |
| 253 |
uint32_t getPlayTimestamp(); |
| 254 |
|
| 255 |
/** Returns true if clip is finished. |
| 256 |
*/ |
| 257 |
bool isFinished(); |
| 258 |
|
| 259 |
/** Sets the cursor position in the audio file |
| 260 |
*/ |
| 261 |
void setCursor(SoundPositionType type, float value); |
| 262 |
|
| 263 |
/** Returns the cursor position in the audio file |
| 264 |
*/ |
| 265 |
float getCursor(SoundPositionType type); |
| 266 |
|
| 267 |
/** Sets the position of the SoundEmitter in the virtual audio space. |
| 268 |
*/ |
| 269 |
void setPosition(const AudioSpaceCoordinate& position); |
| 270 |
|
| 271 |
/** Return the position of the SoundEmitter in the virtual audio space. |
| 272 |
*/ |
| 273 |
AudioSpaceCoordinate getPosition() const; |
| 274 |
|
| 275 |
/** Return if it is a positional SoundEmitter. |
| 276 |
*/ |
| 277 |
bool isPosition() const; |
| 278 |
|
| 279 |
/** Sets the direction of the SoundEmitter in the virtual audio space. |
| 280 |
*/ |
| 281 |
void setDirection(const AudioSpaceCoordinate& direction); |
| 282 |
|
| 283 |
/** Return the direction of the SoundEmitter in the virtual audio space. |
| 284 |
*/ |
| 285 |
AudioSpaceCoordinate getDirection() const; |
| 286 |
|
| 287 |
/** Sets pitch multiplier. Can only be positiv. |
| 288 |
*/ |
| 289 |
void setPitch(float pitch); |
| 290 |
|
| 291 |
/** Return pitch multiplier. Can only be positiv. |
| 292 |
*/ |
| 293 |
float getPitch() const; |
| 294 |
|
| 295 |
/** Sets the velocity of the SoundEmitter in the virtual audio space. |
| 296 |
*/ |
| 297 |
void setVelocity(const AudioSpaceCoordinate& velocity); |
| 298 |
|
| 299 |
/** Return the velocity of the SoundEmitter in the virtual audio space. |
| 300 |
*/ |
| 301 |
AudioSpaceCoordinate getVelocity() const; |
| 302 |
|
| 303 |
/** Sets inner angle of the sound cone, in degrees. Default 360 |
| 304 |
*/ |
| 305 |
void setConeInnerAngle(float inner); |
| 306 |
|
| 307 |
/** Return inner angle of the sound cone, in degrees. |
| 308 |
*/ |
| 309 |
float getConeInnerAngle() const; |
| 310 |
|
| 311 |
/** Sets outer angle of the sound cone, in degrees. Default 360 |
| 312 |
*/ |
| 313 |
void setConeOuterAngle(float outer); |
| 314 |
|
| 315 |
/** Return outer angle of the sound cone, in degrees. |
| 316 |
*/ |
| 317 |
float getConeOuterAngle() const; |
| 318 |
|
| 319 |
/** Sets the gain when outside the oriented cone. |
| 320 |
*/ |
| 321 |
void setConeOuterGain(float gain); |
| 322 |
|
| 323 |
/** Return the gain when outside the oriented cone. |
| 324 |
*/ |
| 325 |
float getConeOuterGain() const; |
| 326 |
|
| 327 |
/** Returns the state of the audio file |
| 328 |
*/ |
| 329 |
SoundStateType getState(); |
| 330 |
|
| 331 |
/** Sets the group name. Adds the emitter automatically to the group. |
| 332 |
*/ |
| 333 |
void setGroup(const std::string& group); |
| 334 |
|
| 335 |
/** Return the group name. |
| 336 |
*/ |
| 337 |
const std::string& getGroup(); |
| 338 |
|
| 339 |
/** Adds effect. Used from SoundEffectManager. |
| 340 |
*/ |
| 341 |
void addEffect(SoundEffect* effect); |
| 342 |
|
| 343 |
/** Removes effect. Used from SoundEffectManager. |
| 344 |
*/ |
| 345 |
void removeEffect(SoundEffect* effect); |
| 346 |
|
| 347 |
/** Return the number of effects. Used from SoundEffectManager. |
| 348 |
*/ |
| 349 |
uint8_t getEffectCount(); |
| 350 |
|
| 351 |
/** Return the number of the given effect. Used from SoundEffectManager. |
| 352 |
*/ |
| 353 |
uint8_t getEffectNumber(SoundEffect* effect); |
| 354 |
|
| 355 |
/** Sets the direct filter. Used from SoundEffectManager. |
| 356 |
*/ |
| 357 |
void setDirectFilter(SoundFilter* filter); |
| 358 |
|
| 359 |
/** Return the direct filter. Used from SoundEffectManager. |
| 360 |
*/ |
| 361 |
SoundFilter* getDirectFilter(); |
| 362 |
|
| 363 |
/** Activates effects if the Emitter got the openAL-source. |
| 364 |
*/ |
| 365 |
void activateEffects(); |
| 366 |
|
| 367 |
/** Deactivates effects if the Emitter loses the openAL-source. |
| 368 |
*/ |
| 369 |
void deactivateEffects(); |
| 370 |
|
| 371 |
/** Sets the time difference between play and the first check if it's playable. |
| 372 |
*/ |
| 373 |
void setCheckDifference(); |
| 374 |
|
| 375 |
/** Adds new SoundEmitter listener |
| 376 |
* @param listener to add |
| 377 |
*/ |
| 378 |
void addListener(SoundEmitterListener* listener); |
| 379 |
|
| 380 |
/** Removes associated SoundEmitter listener |
| 381 |
* @param listener to remove |
| 382 |
*/ |
| 383 |
void removeListener(SoundEmitterListener* listener); |
| 384 |
|
| 385 |
private: |
| 386 |
/** Internal function to attach a SoundClip to the source |
| 387 |
*/ |
| 388 |
void attachSoundClip(); |
| 389 |
|
| 390 |
/** Internal function to detach a SoundClip from the source |
| 391 |
*/ |
| 392 |
void detachSoundClip(); |
| 393 |
|
| 394 |
/** Updates OpenAL with collected data. |
| 395 |
*/ |
| 396 |
void syncData(); |
| 397 |
|
| 398 |
/** Updates fade in and out. |
| 399 |
*/ |
| 400 |
void checkFade(); |
| 401 |
|
| 402 |
/** Resets collected data to defaults. |
| 403 |
*/ |
| 404 |
void resetInternData(); |
| 405 |
|
| 406 |
/** Calls the Listeners if a sound finished |
| 407 |
*/ |
| 408 |
void callOnSoundFinished(); |
| 409 |
|
| 410 |
//! Access to the SoundManager |
| 411 |
SoundManager* m_manager; |
| 412 |
//! The openAL-source |
| 413 |
ALuint m_source; |
| 414 |
//! Applied direct sound filter |
| 415 |
SoundFilter* m_directFilter; |
| 416 |
//! The attached sound clip |
| 417 |
SoundClipPtr m_soundClip; |
| 418 |
//! Id of the attached sound clip |
| 419 |
uint32_t m_soundClipId; |
| 420 |
//! The id of the stream |
| 421 |
uint32_t m_streamId; |
| 422 |
//! The emitter-id |
| 423 |
uint32_t m_emitterId; |
| 424 |
|
| 425 |
//! buffers OpenAL data |
| 426 |
struct internData { |
| 427 |
float volume; |
| 428 |
float maxVolume; |
| 429 |
float minVolume; |
| 430 |
float refDistance; |
| 431 |
float maxDistance; |
| 432 |
float rolloff; |
| 433 |
float pitch; |
| 434 |
float coneInnerAngle; |
| 435 |
float coneOuterAngle; |
| 436 |
float coneOuterGain; |
| 437 |
AudioSpaceCoordinate position; |
| 438 |
AudioSpaceCoordinate direction; |
| 439 |
AudioSpaceCoordinate velocity; |
| 440 |
uint32_t playTimestamp; |
| 441 |
SoundStateType soundState; |
| 442 |
bool loop; |
| 443 |
bool relative; |
| 444 |
} m_internData; |
| 445 |
|
| 446 |
//! the group name |
| 447 |
std::string m_group; |
| 448 |
//! saves sample offset for played stream parts |
| 449 |
float m_samplesOffset; |
| 450 |
//! is active |
| 451 |
bool m_active; |
| 452 |
//! fade in clip |
| 453 |
bool m_fadeIn; |
| 454 |
//! fade out clip |
| 455 |
bool m_fadeOut; |
| 456 |
//! original gain |
| 457 |
float m_origGain; |
| 458 |
//! fade in start time |
| 459 |
uint32_t m_fadeInStartTimestamp; |
| 460 |
//! fade in end time |
| 461 |
uint32_t m_fadeInEndTimestamp; |
| 462 |
//! fade out start time |
| 463 |
uint32_t m_fadeOutStartTimestamp; |
| 464 |
//! fade out end time |
| 465 |
uint32_t m_fadeOutEndTimestamp; |
| 466 |
//! time difference between play and first check |
| 467 |
uint32_t m_playCheckDifference; |
| 468 |
//! holds pointer to applied SoundEffects |
| 469 |
std::vector<SoundEffect*> m_effects; |
| 470 |
//! listeners for sound related events |
| 471 |
std::vector<SoundEmitterListener*> m_listeners; |
| 472 |
}; |
| 473 |
} |
| 474 |
|
| 475 |
#endif |