Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3157 was 3157, checked in by rgrieder, 15 years ago

Small changes and cleaned out RadarViewable which saves me probably more than a minute when recompiling…

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