Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6202 was 6202, checked in by youngk, 16 years ago

Implemented speed dependent audio pitch. BUG pitching doesn't currently work.

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