| [4744] | 1 | /* |
|---|
| [1853] | 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. |
|---|
| [1855] | 10 | |
|---|
| 11 | ### File Specific: |
|---|
| [5386] | 12 | main-programmer: Benjamin Grauer |
|---|
| [1855] | 13 | co-programmer: ... |
|---|
| [1853] | 14 | */ |
|---|
| 15 | |
|---|
| [5386] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SOUND |
|---|
| [1853] | 17 | |
|---|
| [5386] | 18 | #include "sound_source.h" |
|---|
| 19 | #include "sound_engine.h" |
|---|
| [5917] | 20 | |
|---|
| [5386] | 21 | #include "alincl.h" |
|---|
| 22 | #include "compiler.h" |
|---|
| [1853] | 23 | |
|---|
| [1856] | 24 | using namespace std; |
|---|
| [1853] | 25 | |
|---|
| [5386] | 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"); |
|---|
| [1856] | 32 | |
|---|
| [5386] | 33 | ALenum result; |
|---|
| 34 | |
|---|
| 35 | // adding the Source to the SourcesList of the SoundEngine |
|---|
| 36 | this->buffer = buffer; |
|---|
| 37 | this->sourceNode = sourceNode; |
|---|
| 38 | |
|---|
| [5930] | 39 | this->sourceID = 0; |
|---|
| 40 | this->bPlay = false; |
|---|
| [5386] | 41 | } |
|---|
| 42 | |
|---|
| [3245] | 43 | /** |
|---|
| [5386] | 44 | * deletes a SoundSource |
|---|
| 45 | */ |
|---|
| 46 | SoundSource::~SoundSource() |
|---|
| [3365] | 47 | { |
|---|
| [7291] | 48 | this->stop(); |
|---|
| [5386] | 49 | } |
|---|
| [4320] | 50 | |
|---|
| [5386] | 51 | /** |
|---|
| 52 | * Plays back a SoundSource |
|---|
| 53 | */ |
|---|
| 54 | void SoundSource::play() |
|---|
| 55 | { |
|---|
| [5930] | 56 | if (this->sourceID == 0) |
|---|
| 57 | SoundEngine::getInstance()->popALSource(this->sourceID); |
|---|
| [5386] | 58 | alSourcePlay(this->sourceID); |
|---|
| [7290] | 59 | if (DEBUG >= 3) |
|---|
| 60 | SoundEngine::checkError("Play Source", __LINE__); |
|---|
| [5930] | 61 | this->bPlay = true; |
|---|
| [5386] | 62 | } |
|---|
| [4320] | 63 | |
|---|
| [5386] | 64 | /** |
|---|
| 65 | * Plays back buffer on this Source |
|---|
| 66 | * @param buffer the buffer to play back on this Source |
|---|
| 67 | */ |
|---|
| 68 | void SoundSource::play(const SoundBuffer* buffer) |
|---|
| 69 | { |
|---|
| [5930] | 70 | if (unlikely(this->sourceID == 0)) |
|---|
| [7291] | 71 | { |
|---|
| [5930] | 72 | SoundEngine::getInstance()->popALSource(this->sourceID); |
|---|
| [7291] | 73 | if (sourceID == 0) |
|---|
| 74 | { |
|---|
| 75 | PRINTF(2)("No more Free source\n"); |
|---|
| 76 | return; |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| [7288] | 79 | // assert (this->sourceID != 0); |
|---|
| [5930] | 80 | |
|---|
| [5386] | 81 | alSourceStop(this->sourceID); |
|---|
| 82 | alSourcei (this->sourceID, AL_BUFFER, buffer->getID()); |
|---|
| 83 | alSourcePlay(this->sourceID); |
|---|
| 84 | |
|---|
| 85 | if (unlikely(this->buffer != NULL)) |
|---|
| 86 | alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); |
|---|
| [5930] | 87 | this->bPlay = true; |
|---|
| [7290] | 88 | |
|---|
| 89 | if (DEBUG >= 3) |
|---|
| 90 | SoundEngine::checkError("Play Source", __LINE__); |
|---|
| [3365] | 91 | } |
|---|
| [1853] | 92 | |
|---|
| [5386] | 93 | /** |
|---|
| 94 | * Stops playback of a SoundSource |
|---|
| 95 | */ |
|---|
| 96 | void SoundSource::stop() |
|---|
| 97 | { |
|---|
| [5930] | 98 | this->bPlay = false; |
|---|
| [7291] | 99 | if (this->sourceID != 0) |
|---|
| 100 | { |
|---|
| 101 | alSourceStop(this->sourceID); |
|---|
| 102 | if (DEBUG >= 3) |
|---|
| 103 | SoundEngine::checkError("StopSource", __LINE__); |
|---|
| 104 | SoundEngine::getInstance()->pushALSource(this->sourceID); |
|---|
| 105 | this->sourceID = 0; |
|---|
| 106 | } |
|---|
| [5386] | 107 | } |
|---|
| [1853] | 108 | |
|---|
| [3245] | 109 | /** |
|---|
| [5386] | 110 | * Pauses Playback of a SoundSource |
|---|
| 111 | */ |
|---|
| 112 | void SoundSource::pause() |
|---|
| [3543] | 113 | { |
|---|
| [5386] | 114 | alSourcePause(this->sourceID); |
|---|
| [7290] | 115 | if (DEBUG >= 3) |
|---|
| 116 | SoundEngine::checkError("Pause Source", __LINE__); |
|---|
| [3543] | 117 | } |
|---|
| [5386] | 118 | |
|---|
| 119 | /** |
|---|
| 120 | * Rewinds Playback of a SoundSource |
|---|
| 121 | */ |
|---|
| 122 | void SoundSource::rewind() |
|---|
| 123 | { |
|---|
| 124 | alSourceRewind(this->sourceID); |
|---|
| [7290] | 125 | |
|---|
| 126 | if (DEBUG >= 3) |
|---|
| 127 | SoundEngine::checkError("Rewind Source", __LINE__); |
|---|
| [5386] | 128 | } |
|---|
| 129 | |
|---|
| 130 | /** |
|---|
| 131 | * sets the RolloffFactor of the Sound emitted from the SoundSource |
|---|
| 132 | * @param rolloffFactor The Factor described |
|---|
| 133 | |
|---|
| 134 | this tells openAL how fast the Sounds decay outward from the Source |
|---|
| 135 | */ |
|---|
| 136 | void SoundSource::setRolloffFactor(ALfloat rolloffFactor) |
|---|
| 137 | { |
|---|
| 138 | alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor); |
|---|
| [7290] | 139 | |
|---|
| 140 | if (DEBUG >= 3) |
|---|
| 141 | SoundEngine::checkError("Set Source Rolloff-factor", __LINE__); |
|---|
| [5386] | 142 | } |
|---|
| 143 | |
|---|