Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed some bugs

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