| 1 | // SuperTux |
| 2 | // Copyright (C) 2006 Matthias Braun <matze@braunis.de> |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | #include "audio/openal_sound_source.hpp" |
| 18 | |
| 19 | #include "audio/sound_manager.hpp" |
| 20 | #include "util/log.hpp" |
| 21 | |
| 22 | OpenALSoundSource::OpenALSoundSource() : |
| 23 | m_source(), |
| 24 | m_gain(1.0f), |
| 25 | m_volume(1.0f) |
| 26 | { |
| 27 | alGenSources(1, &m_source); |
| 28 | try |
| 29 | { |
| 30 | SoundManager::check_al_error("Couldn't create audio source: " ); |
| 31 | } |
| 32 | catch(std::exception& e) |
| 33 | { |
| 34 | log_warning << e.what() << std::endl; |
| 35 | } |
| 36 | set_reference_distance(128); |
| 37 | } |
| 38 | |
| 39 | OpenALSoundSource::~OpenALSoundSource() |
| 40 | { |
| 41 | stop(); |
| 42 | alDeleteSources(1, &m_source); |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | OpenALSoundSource::stop() |
| 47 | { |
| 48 | alSourceRewindv(1, &m_source); // Stops the source |
| 49 | alSourcei(m_source, AL_BUFFER, AL_NONE); |
| 50 | try |
| 51 | { |
| 52 | SoundManager::check_al_error("Problem stopping audio source: " ); |
| 53 | } |
| 54 | catch(const std::exception& e) |
| 55 | { |
| 56 | // Internal OpenAL error. Don't you crash on me, baby! |
| 57 | log_warning << e.what() << std::endl; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | OpenALSoundSource::play() |
| 63 | { |
| 64 | alSourcePlay(m_source); |
| 65 | |
| 66 | try |
| 67 | { |
| 68 | SoundManager::check_al_error("Couldn't start audio source: " ); |
| 69 | } |
| 70 | catch(const std::exception& e) |
| 71 | { |
| 72 | // We probably have too many sources playing simultaneously. |
| 73 | log_warning << e.what() << std::endl; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | bool |
| 78 | OpenALSoundSource::playing() const |
| 79 | { |
| 80 | ALint state = AL_PLAYING; |
| 81 | alGetSourcei(m_source, AL_SOURCE_STATE, &state); |
| 82 | return state == AL_PLAYING; |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | OpenALSoundSource::pause() |
| 87 | { |
| 88 | alSourcePause(m_source); |
| 89 | SoundManager::check_al_error("Couldn't pause audio source: " ); |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | OpenALSoundSource::resume() |
| 94 | { |
| 95 | if ( !paused() ) |
| 96 | { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | play(); |
| 101 | } |
| 102 | |
| 103 | bool |
| 104 | OpenALSoundSource::paused() const |
| 105 | { |
| 106 | ALint state = AL_PAUSED; |
| 107 | alGetSourcei(m_source, AL_SOURCE_STATE, &state); |
| 108 | return state == AL_PAUSED; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | OpenALSoundSource::update() |
| 113 | { |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | OpenALSoundSource::set_looping(bool looping) |
| 118 | { |
| 119 | alSourcei(m_source, AL_LOOPING, looping ? AL_TRUE : AL_FALSE); |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | OpenALSoundSource::set_relative(bool relative) |
| 124 | { |
| 125 | alSourcei(m_source, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | OpenALSoundSource::set_position(const Vector& position) |
| 130 | { |
| 131 | alSource3f(m_source, AL_POSITION, position.x, position.y, 0); |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | OpenALSoundSource::set_velocity(const Vector& velocity) |
| 136 | { |
| 137 | alSource3f(m_source, AL_VELOCITY, velocity.x, velocity.y, 0); |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | OpenALSoundSource::set_gain(float gain) |
| 142 | { |
| 143 | alSourcef(m_source, AL_GAIN, gain * m_volume); |
| 144 | m_gain = gain; |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | OpenALSoundSource::set_pitch(float pitch) |
| 149 | { |
| 150 | alSourcef(m_source, AL_PITCH, pitch); |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | OpenALSoundSource::set_reference_distance(float distance) |
| 155 | { |
| 156 | alSourcef(m_source, AL_REFERENCE_DISTANCE, distance); |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | OpenALSoundSource::set_volume(float volume) |
| 161 | { |
| 162 | m_volume = volume; |
| 163 | alSourcef(m_source, AL_GAIN, m_gain * m_volume); |
| 164 | } |
| 165 | |
| 166 | /* EOF */ |
| 167 | |