| 1 | /* | 
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 | Copyright (C) 2004 orx | 
|---|
| 5 |  | 
|---|
| 6 | This program is free software; you can redistribute it and/or modify | 
|---|
| 7 | it under the terms of the GNU General Public License as published by | 
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 9 | any later version. | 
|---|
| 10 |  | 
|---|
| 11 | ### File Specific: | 
|---|
| 12 | main-programmer: Benjamin Grauer | 
|---|
| 13 | co-programmer: ... | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SOUND | 
|---|
| 17 |  | 
|---|
| 18 | #include "sound_source.h" | 
|---|
| 19 | #include "sound_engine.h" | 
|---|
| 20 |  | 
|---|
| 21 | #include "alincl.h" | 
|---|
| 22 | #include "compiler.h" | 
|---|
| 23 |  | 
|---|
| 24 | using namespace std; | 
|---|
| 25 |  | 
|---|
| 26 | /** | 
|---|
| 27 | *  creates a SoundSource at position sourceNode with the SoundBuffer buffer | 
|---|
| 28 | */ | 
|---|
| 29 | SoundSource::SoundSource(const PNode* sourceNode, const SoundBuffer* buffer) | 
|---|
| 30 | { | 
|---|
| 31 | this->setClassID(CL_SOUND_SOURCE, "SoundSource"); | 
|---|
| 32 |  | 
|---|
| 33 | ALenum result; | 
|---|
| 34 |  | 
|---|
| 35 | // adding the Source to the SourcesList of the SoundEngine | 
|---|
| 36 | this->buffer = buffer; | 
|---|
| 37 | this->sourceNode = sourceNode; | 
|---|
| 38 |  | 
|---|
| 39 | this->sourceID = 0; | 
|---|
| 40 | this->bPlay = false; | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | /** | 
|---|
| 44 | *  deletes a SoundSource | 
|---|
| 45 | */ | 
|---|
| 46 | SoundSource::~SoundSource() | 
|---|
| 47 | { | 
|---|
| 48 | SoundEngine::getInstance()->pushALSource(this->sourceID); | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | /** | 
|---|
| 52 | *  Plays back a SoundSource | 
|---|
| 53 | */ | 
|---|
| 54 | void SoundSource::play() | 
|---|
| 55 | { | 
|---|
| 56 | if (this->sourceID == 0) | 
|---|
| 57 | SoundEngine::getInstance()->popALSource(this->sourceID); | 
|---|
| 58 | alSourcePlay(this->sourceID); | 
|---|
| 59 | this->bPlay = true; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | /** | 
|---|
| 63 | * Plays back buffer on this Source | 
|---|
| 64 | * @param buffer the buffer to play back on this Source | 
|---|
| 65 | */ | 
|---|
| 66 | void SoundSource::play(const SoundBuffer* buffer) | 
|---|
| 67 | { | 
|---|
| 68 | if (unlikely(this->sourceID == 0)) | 
|---|
| 69 | SoundEngine::getInstance()->popALSource(this->sourceID); | 
|---|
| 70 |  | 
|---|
| 71 | alSourceStop(this->sourceID); | 
|---|
| 72 | alSourcei (this->sourceID, AL_BUFFER, buffer->getID()); | 
|---|
| 73 | alSourcePlay(this->sourceID); | 
|---|
| 74 |  | 
|---|
| 75 | if (unlikely(this->buffer != NULL)) | 
|---|
| 76 | alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); | 
|---|
| 77 | this->bPlay = true; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | /** | 
|---|
| 81 | *  Stops playback of a SoundSource | 
|---|
| 82 | */ | 
|---|
| 83 | void SoundSource::stop() | 
|---|
| 84 | { | 
|---|
| 85 | this->bPlay = false; | 
|---|
| 86 | alSourceStop(this->sourceID); | 
|---|
| 87 | SoundEngine::getInstance()->pushALSource(this->sourceID); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | /** | 
|---|
| 91 | *  Pauses Playback of a SoundSource | 
|---|
| 92 | */ | 
|---|
| 93 | void SoundSource::pause() | 
|---|
| 94 | { | 
|---|
| 95 | alSourcePause(this->sourceID); | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | /** | 
|---|
| 99 | *  Rewinds Playback of a SoundSource | 
|---|
| 100 | */ | 
|---|
| 101 | void SoundSource::rewind() | 
|---|
| 102 | { | 
|---|
| 103 | alSourceRewind(this->sourceID); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | /** | 
|---|
| 107 | *  sets the RolloffFactor of the Sound emitted from the SoundSource | 
|---|
| 108 | * @param rolloffFactor The Factor described | 
|---|
| 109 |  | 
|---|
| 110 | this tells openAL how fast the Sounds decay outward from the Source | 
|---|
| 111 | */ | 
|---|
| 112 | void SoundSource::setRolloffFactor(ALfloat rolloffFactor) | 
|---|
| 113 | { | 
|---|
| 114 | alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|