Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3050 was 3013, checked in by bknecht, 16 years ago

merged sound changes into sound2 use sound2 from now on

  • 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: 6.3 KB
RevLine 
[2899]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 */
[2984]28#include <vector>
[2967]29#include <AL/alut.h>
30#include <vorbis/vorbisfile.h>
[2899]31
[2931]32#include "orxonox/objects/worldentities/WorldEntity.h"
33#include "util/Math.h"
[2980]34#include "SoundBase.h"
[2931]35#include "SoundManager.h"
[3013]36#include "core/Core.h"
[2899]37
38namespace orxonox
39{
[2980]40    SoundManager* SoundBase::soundmanager_s = NULL;
[2966]41
[2931]42    SoundBase::SoundBase(WorldEntity* entity)
[2899]43    {
[2980]44        if(SoundBase::soundmanager_s == NULL)
45        {
46            SoundBase::soundmanager_s = new SoundManager();
47        }
48
[2899]49        this->source_ = 0;
50        this->buffer_ = 0;
[2931]51        this->entity_ = entity;
[2980]52
53        SoundBase::soundmanager_s->addSound(this);
[2899]54    }
55
[2966]56    SoundBase::~SoundBase()
57    {
58        alSourcei(this->source_, AL_BUFFER, 0);
59        alDeleteSources(1, &this->source_);
60        alDeleteBuffers(1, &this->buffer_);
61    }
62
[2931]63    void SoundBase::attachToEntity(WorldEntity* entity)
[2899]64    {
[2931]65        this->entity_ = entity;
66        this->update();
[2899]67    }
68
[2931]69    void SoundBase::update() {
[2950]70        if(this->entity_ != NULL && alIsSource(this->source_)) {
[2931]71            Vector3 pos = this->entity_->getPosition();
72            alSource3f(this->source_, AL_POSITION, pos.x, pos.y, pos.z);
73            ALenum error = alGetError();
74            if(error == AL_INVALID_VALUE)
[2984]75                COUT(2) << "Sound: OpenAL: Invalid sound position" << std::endl;
[2931]76
77            Vector3 vel = this->entity_->getVelocity();
78            alSource3f(this->source_, AL_VELOCITY, vel.x, vel.y, vel.z);
79            error = alGetError();
80            if(error == AL_INVALID_VALUE)
[2984]81                COUT(2) << "Sound: OpenAL: Invalid sound velocity" << std::endl;
[2931]82
83            Quaternion orient = this->entity_->getOrientation();
84            Vector3 at = orient.zAxis();
85            alSource3f(this->source_, AL_DIRECTION, at.x, at.y, at.z);
86            error = alGetError();
87            if(error == AL_INVALID_VALUE)
[2984]88                COUT(2) << "Sound: OpenAL: Invalid sound direction" << std::endl;
[2931]89        }
90    }
91
92    void SoundBase::play(bool loop) {
93        if(alIsSource(this->source_)) {
94            if(loop)
95                alSourcei(this->source_, AL_LOOPING, AL_TRUE);
96            else
97                alSourcei(this->source_, AL_LOOPING, AL_FALSE);
98            alSourcePlay(this->source_);
99        }
100    }
101
102    void SoundBase::stop() {
103        if(alIsSource(this->source_)) {
104            alSourceStop(this->source_);
105        }
106    }
107
108    void SoundBase::pause() {
109        if(alIsSource(this->source_)) {
110            alSourcePause(this->source_);
111        }
112    }
113
114    bool SoundBase::isPlaying() {
115        if(alIsSource(this->source_)) {
116            return getSourceState() == AL_PLAYING;
117        }
[2950]118        return false;
[2931]119    }
120
121    bool SoundBase::isPaused() {
122        if(alIsSource(this->source_)) {
123            return getSourceState() == AL_PAUSED;
124        }
[2950]125        return true;
[2931]126    }
127
128    bool SoundBase::isStopped() {
129        if(alIsSource(this->source_)) {
130            return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED;
131        }
[2950]132        return true;
[2931]133    }
134
[2932]135    bool SoundBase::loadFile(std::string filename) {
[2967]136        filename = Core::getMediaPathString() + "/audio/" + filename;
[2998]137
138        if(!SoundBase::soundmanager_s->isSoundAvailable())
139        {
140            COUT(3) << "Sound: not available, skipping " << filename << std::endl;
141            return false;
142        }
143
[2984]144        COUT(3) << "Sound: OpenAL ALUT: loading file " << filename << std::endl;
[2932]145        this->buffer_ = alutCreateBufferFromFile(filename.c_str());
146        if(this->buffer_ == AL_NONE) {
[2984]147            COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl;
[2968]148            if(filename.find("ogg", 0) != std::string::npos)
[2967]149            {
150                this->buffer_ = loadOggFile(filename);
151            }
152
153            if(this->buffer_ == AL_NONE) 
154                return false;
[2932]155        }
156
157        alGenSources(1, &this->source_);
158        alSourcei(this->source_, AL_BUFFER, this->buffer_);
159        if(alGetError() != AL_NO_ERROR) {
[2984]160            COUT(2) << "Sound: OpenAL: Error loading sample file" << std::endl;
[2932]161            return false;
162        }
163        return true;
164    }
165
[2931]166    ALint SoundBase::getSourceState() {
167        ALint state;
168        alGetSourcei(this->source_, AL_SOURCE_STATE, &state);
169        return state;
170    }
[2984]171
[2967]172    ALuint SoundBase::loadOggFile(std::string filename)
173    {
[2984]174        COUT(2) << "Sound: Trying fallback ogg loader";
175
176        char inbuffer[4096];
177        std::vector<char> outbuffer;
178        OggVorbis_File vf;
179        int eof = false;
180        int current_section;
181
182        FILE* f = fopen(filename.c_str(), "rb");
183
184        if(ov_open(f, &vf, NULL, 0) < 0)
185        {
186            COUT(2) << "Sound: libvorbisfile: File seems not to be an Ogg Vorbis bitstream" << std::endl;
187            ov_clear(&vf);
188            return AL_NONE;
189        }
190
191        while(!eof)
192        {
193            long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
194            if (ret == 0)
195            {
196                eof = true;
197            }
198            else if (ret < 0)
199            {
200                COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
201                ov_clear(&vf);
202                return AL_NONE;
203            }
204            else
205            {
206                outbuffer.insert(outbuffer.end(), inbuffer, inbuffer + sizeof(inbuffer));
207            }
208        }
209       
210        ov_clear(&vf);
211
212        return alutCreateBufferFromFileImage(&outbuffer, outbuffer.size());
[2967]213    }
[2899]214} // namespace: orxonox
Note: See TracBrowser for help on using the repository browser.