Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound/src/orxonox/sound/SoundBase.cc @ 2967

Last change on this file since 2967 was 2967, checked in by erwin, 15 years ago

SoundBase now loads files relative to the mediapath

  • Property svn:mergeinfo set to (toggle deleted branches)
    /code/branches/buildsystem/src/sound/SoundBase.cc1874-2276,​2278-2400
    /code/branches/buildsystem2/src/sound/SoundBase.cc2506-2658
    /code/branches/buildsystem3/src/sound/SoundBase.cc2662-2708
    /code/branches/ceguilua/src/sound/SoundBase.cc1802-1808
    /code/branches/core3/src/sound/SoundBase.cc1572-1739
    /code/branches/gcc43/src/sound/SoundBase.cc1580
    /code/branches/gui/src/sound/SoundBase.cc1635-1723
    /code/branches/input/src/sound/SoundBase.cc1629-1636
    /code/branches/lodfinal/src/sound/SoundBase.cc2372-2411
    /code/branches/miniprojects/src/sound/SoundBase.cc2754-2824
    /code/branches/network/src/sound/SoundBase.cc2356
    /code/branches/network64/src/sound/SoundBase.cc2210-2355
    /code/branches/objecthierarchy/src/sound/SoundBase.cc1911-2085,​2100,​2110-2169
    /code/branches/objecthierarchy2/src/sound/SoundBase.cc2171-2479
    /code/branches/overlay/src/sound/SoundBase.cc2117-2385
    /code/branches/physics/src/sound/SoundBase.cc1912-2055,​2107-2439
    /code/branches/physics_merge/src/sound/SoundBase.cc2436-2457
    /code/branches/pickups/src/sound/SoundBase.cc1926-2086,​2127
    /code/branches/pickups2/src/sound/SoundBase.cc2107-2497
    /code/branches/presentation/src/sound/SoundBase.cc2369-2652,​2654-2660
    /code/branches/questsystem/src/sound/SoundBase.cc1894-2088
    /code/branches/questsystem2/src/sound/SoundBase.cc2107-2259
    /code/branches/script_trigger/src/sound/SoundBase.cc1295-1953,​1955
    /code/branches/weapon/src/sound/SoundBase.cc1925-2094
    /code/branches/weapon2/src/sound/SoundBase.cc2107-2488
File size: 4.9 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *       Erwin 'vaiursch' Herrsche
24 *   Co-authors:
25 *      ...
26 *
27 */
28#include <AL/alut.h>
29#include <vorbis/vorbisfile.h>
30
31#include "orxonox/objects/worldentities/WorldEntity.h"
32#include "util/Math.h"
33#include "SoundManager.h"
34#include "SoundBase.h"
35
36namespace orxonox
37{
38    SoundBase::SoundBase()
39    {
40        this->source_ = 0;
41        this->buffer_ = 0;
42        this->entity_ = NULL;
43    }
44
45    SoundBase::SoundBase(WorldEntity* entity)
46    {
47        this->source_ = 0;
48        this->buffer_ = 0;
49        this->entity_ = entity;
50    }
51
52    SoundBase::~SoundBase()
53    {
54        alSourcei(this->source_, AL_BUFFER, 0);
55        alDeleteSources(1, &this->source_);
56        alDeleteBuffers(1, &this->buffer_);
57    }
58
59    void SoundBase::attachToEntity(WorldEntity* entity)
60    {
61        this->entity_ = entity;
62        this->update();
63    }
64
65    void SoundBase::update() {
66        if(this->entity_ != NULL && alIsSource(this->source_)) {
67            Vector3 pos = this->entity_->getPosition();
68            alSource3f(this->source_, AL_POSITION, pos.x, pos.y, pos.z);
69            ALenum error = alGetError();
70            if(error == AL_INVALID_VALUE)
71                COUT(2) << "OpenAL: Invalid sound position" << std::endl;
72
73            Vector3 vel = this->entity_->getVelocity();
74            alSource3f(this->source_, AL_VELOCITY, vel.x, vel.y, vel.z);
75            error = alGetError();
76            if(error == AL_INVALID_VALUE)
77                COUT(2) << "OpenAL: Invalid sound position" << std::endl;
78
79            Quaternion orient = this->entity_->getOrientation();
80            Vector3 at = orient.zAxis();
81            alSource3f(this->source_, AL_DIRECTION, at.x, at.y, at.z);
82            error = alGetError();
83            if(error == AL_INVALID_VALUE)
84                COUT(2) << "OpenAL: Invalid sound position" << std::endl;
85        }
86    }
87
88    void SoundBase::play(bool loop) {
89        if(alIsSource(this->source_)) {
90            if(loop)
91                alSourcei(this->source_, AL_LOOPING, AL_TRUE);
92            else
93                alSourcei(this->source_, AL_LOOPING, AL_FALSE);
94            alSourcePlay(this->source_);
95        }
96    }
97
98    void SoundBase::stop() {
99        if(alIsSource(this->source_)) {
100            alSourceStop(this->source_);
101        }
102    }
103
104    void SoundBase::pause() {
105        if(alIsSource(this->source_)) {
106            alSourcePause(this->source_);
107        }
108    }
109
110    bool SoundBase::isPlaying() {
111        if(alIsSource(this->source_)) {
112            return getSourceState() == AL_PLAYING;
113        }
114        return false;
115    }
116
117    bool SoundBase::isPaused() {
118        if(alIsSource(this->source_)) {
119            return getSourceState() == AL_PAUSED;
120        }
121        return true;
122    }
123
124    bool SoundBase::isStopped() {
125        if(alIsSource(this->source_)) {
126            return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED;
127        }
128        return true;
129    }
130
131    bool SoundBase::loadFile(std::string filename) {
132        filename = Core::getMediaPathString() + "/audio/" + filename;
133        COUT(3) << "OpenAL ALUT: loading file " << filename << std::endl;
134        this->buffer_ = alutCreateBufferFromFile(filename.c_str());
135        if(this->buffer_ == AL_NONE) {
136            COUT(2) << "OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl;
137            if(filename.find("ogg", 0) != string::npos)
138            {
139                this->buffer_ = loadOggFile(filename);
140            }
141
142            if(this->buffer_ == AL_NONE) 
143                return false;
144        }
145
146        alGenSources(1, &this->source_);
147        alSourcei(this->source_, AL_BUFFER, this->buffer_);
148        if(alGetError() != AL_NO_ERROR) {
149            COUT(2) << "OpenAL: Error loading sample file" << std::endl;
150            return false;
151        }
152        return true;
153    }
154
155    ALint SoundBase::getSourceState() {
156        ALint state;
157        alGetSourcei(this->source_, AL_SOURCE_STATE, &state);
158        return state;
159    }
160   
161    ALuint SoundBase::loadOggFile(std::string filename)
162    {
163        // just a dummy
164        return AL_NONE;
165    }
166} // namespace: orxonox
Note: See TracBrowser for help on using the repository browser.