Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Improved synchronisability of the sound classes (not yet Synchronisable though!).

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