Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/sound/BaseSound.cc @ 6195

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

Small style changes (no code changes).

  • Property svn:eol-style set to native
File size: 8.7 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 *      Reto Grieder
26 *
27 */
28
29#include "BaseSound.h"
30
31#include <cassert>
32#include <vector>
33#include <AL/alut.h>
34#include <vorbis/vorbisfile.h>
35
36#include "core/CoreIncludes.h"
37#include "core/GameMode.h"
38#include "core/Resource.h"
39#include "core/XMLPort.h"
40#include "SoundManager.h"
41
42namespace orxonox
43{
44    BaseSound::BaseSound()
45        : audioSource_(0)
46        , audioBuffer_(0)
47        , bLoop_(false)
48        , state_(Stopped)
49    {
50        RegisterRootObject(BaseSound);
51
52        if (GameMode::playsSound())
53        {
54            alGenSources(1, &this->audioSource_);
55            assert(this->audioSource_ != 0);
56        }
57    }
58
59    BaseSound::~BaseSound()
60    {
61        this->setSource("");
62        if (GameMode::playsSound())
63            alDeleteSources(1, &this->audioSource_);
64    }
65
66    void BaseSound::XMLPortExtern(Element& xmlelement, XMLPort::Mode mode)
67    {
68        XMLPortParam(BaseSound, "volume", setVolume,  getVolume,  xmlelement, mode);
69        XMLPortParam(BaseSound, "loop",   setLooping, getLooping, xmlelement, mode);
70        XMLPortParam(BaseSound, "play",   setPlaying, isPlaying,  xmlelement, mode);
71        XMLPortParam(BaseSound, "source", setSource,  getSource,  xmlelement, mode);
72    }
73
74    void BaseSound::play()
75    {
76        if (!this->isPlaying() && GameMode::showsGraphics())
77        {
78            this->state_ = Playing;
79            alSourcePlay(this->audioSource_);
80
81            if (alGetError() != AL_NO_ERROR)
82                 COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl;
83        }
84    }
85
86    void BaseSound::stop()
87    {
88        this->state_ = Stopped;
89        if (GameMode::playsSound())
90            alSourceStop(this->audioSource_);
91    }
92
93    void BaseSound::pause()
94    {
95        if (this->isStopped())
96            return;
97        this->state_ = Paused;
98        if (GameMode::playsSound())
99            alSourcePause(this->audioSource_);
100    }
101
102    void BaseSound::setVolume(float vol)
103    {
104        if (vol > 1 || vol < 0)
105        {
106            COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;
107            vol = vol > 1 ? 1 : vol;
108            vol = vol < 0 ? 0 : vol;
109        }
110        this->volume_ = vol;
111       
112        this->updateVolume();
113    }
114   
115    float BaseSound::getVolumeGain()
116    {
117        return SoundManager::getInstance().getVolume(SoundType::none);
118    }
119   
120    void BaseSound::updateVolume(void)
121    {
122        if (alIsSource(this->audioSource_))
123            alSourcef(this->audioSource_, AL_GAIN, this->volume_*this->getVolumeGain());
124    }
125
126    void BaseSound::setLooping(bool val)
127    {
128        this->bLoop_ = val;
129        if (GameMode::playsSound())
130            alSourcei(this->audioSource_, AL_LOOPING, (val ? AL_TRUE : AL_FALSE));
131    }
132
133    void BaseSound::setSource(const std::string& source)
134    {
135        if (!GameMode::playsSound() || source == this->source_) 
136        {
137            this->source_ = source;
138            return;
139        }
140
141        if (this->audioBuffer_ != 0 && alIsBuffer(this->audioBuffer_))
142        {
143            alSourceStop(this->audioSource_);
144            // Unload old sound first
145            alSourcei(this->audioSource_, AL_BUFFER, 0);
146            alDeleteBuffers(1, &this->audioBuffer_);
147            this->audioBuffer_ = 0;
148        }
149
150        this->source_ = source;
151        if (source_.empty()) 
152            return;
153
154        COUT(3) << "Sound: OpenAL ALUT: loading file " << source << std::endl;
155        // Get DataStream from the resources
156        shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(source);
157        if (fileInfo == NULL)
158        {
159            COUT(2) << "Sound: Warning: Sound file '" << source << "' not found" << std::endl;
160            return;
161        }
162        dataStream_ = Resource::open(source);
163        // Read everything into a temporary buffer
164        char* buffer = new char[fileInfo->size];
165        dataStream_->read(buffer, fileInfo->size);
166        dataStream_->seek(0);
167
168        this->audioBuffer_ = alutCreateBufferFromFileImage(buffer, fileInfo->size);
169        delete[] buffer;
170
171        if (this->audioBuffer_ == AL_NONE)
172        {
173            COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl;
174            if (source.find("ogg", 0) != std::string::npos)
175            {
176                COUT(2) << "Sound: Trying fallback ogg loader" << std::endl;
177                this->audioBuffer_ = this->loadOggFile();
178            }
179
180            if (this->audioBuffer_ == AL_NONE)
181            {
182                COUT(2) << "Sound: fallback ogg loader failed: " << alutGetErrorString(alutGetError()) << std::endl;
183                return;
184            }
185        }
186
187        alSourcei(this->audioSource_, AL_BUFFER, this->audioBuffer_);
188        if (alGetError() != AL_NO_ERROR)
189        {
190            COUT(2) << "Sound: OpenAL: Error loading sample file: " << source << std::endl;
191            return;
192        }
193
194        alSource3f(this->audioSource_, AL_POSITION,  0, 0, 0);
195        this->updateVolume();
196        alSourcei (this->audioSource_, AL_LOOPING, (this->bLoop_ ? AL_TRUE : AL_FALSE));
197        if (this->isPlaying() || this->isPaused())
198            alSourcePlay(this->audioSource_);
199        if (this->isPaused())
200            alSourcePause(this->audioSource_);
201
202        if (alGetError() != AL_NO_ERROR)
203            COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl;
204    }
205
206    size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource)
207    {
208        return static_cast<Ogre::DataStream*>(datasource)->read(ptr, size * nmemb);
209    }
210
211    int seekVorbis(void* datasource, ogg_int64_t offset, int whence)
212    {
213        Ogre::DataStream* stream = static_cast<Ogre::DataStream*>(datasource);
214        int offset_beg = offset;
215        if (whence == SEEK_CUR)
216            offset_beg = stream->tell() + offset;
217        else if (whence == SEEK_END)
218            offset_beg = stream->size() + offset;
219        else if (whence != SEEK_SET)
220            return -1;
221        stream->seek(offset_beg);
222        return 0;
223    }
224
225    long tellVorbis(void* datasource)
226    {
227        return static_cast<long>(static_cast<Ogre::DataStream*>(datasource)->tell());
228    }
229
230    ALuint BaseSound::loadOggFile()
231    {
232        char inbuffer[256*1024];
233        std::vector<char> outbuffer;
234        outbuffer.reserve(80*1024*1024);
235        OggVorbis_File vf;
236        vorbis_info* vorbisInfo;
237        int eof = false;
238        int current_section;
239        ALuint buffer;
240        ALenum format;
241
242        // Open file with custom streaming
243        ov_callbacks vorbisCallbacks;
244        vorbisCallbacks.read_func  = &readVorbis;
245        vorbisCallbacks.seek_func  = &seekVorbis;
246        vorbisCallbacks.tell_func  = &tellVorbis;
247        vorbisCallbacks.close_func = NULL;
248
249        int ret = ov_open_callbacks(dataStream_.get(), &vf, NULL, 0, vorbisCallbacks);
250        if (ret < 0)
251        {
252            COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl;
253            ov_clear(&vf);
254            return AL_NONE;
255        }
256
257        while (!eof)
258        {
259            long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
260            if (ret == 0)
261            {
262                eof = true;
263            }
264            else if (ret < 0)
265            {
266                COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
267                ov_clear(&vf);
268                return AL_NONE;
269            }
270            else
271            {
272                outbuffer.insert(outbuffer.end(), inbuffer, inbuffer + ret);
273            }
274        }
275
276        vorbisInfo = ov_info(&vf, -1);
277        if (vorbisInfo->channels == 1)
278            format = AL_FORMAT_MONO16;
279        else
280            format = AL_FORMAT_STEREO16;
281
282        alGenBuffers(1, &buffer);
283        alBufferData(buffer, format, &outbuffer[0], outbuffer.size(), vorbisInfo->rate);
284        ov_clear(&vf);
285
286        return buffer;
287    }
288}
Note: See TracBrowser for help on using the repository browser.