Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound3/src/orxonox/sound/BaseSound.cc @ 6070

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

OpenAL sources should be linked to the lifetime of a BaseSound.

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