Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound6/src/orxonox/sound/AmbientSound.cc @ 8662

Last change on this file since 8662 was 8007, checked in by rgrieder, 14 years ago

Merged sound5 into sound6 branch.

  • 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 *      Reto Grieder
24 *   Co-authors:
25 *      Kevin Young
26 *
27 */
28
29#include "AmbientSound.h"
30
31#include "core/CoreIncludes.h"
32#include "core/GameMode.h"
33#include "core/Resource.h"
34#include "SoundManager.h"
35#include "SoundStreamer.h"
36#include "util/Sleep.h"
37
38#include <AL/alut.h>
39
40namespace orxonox
41{
42    // vorbis callbacks
43    size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource);
44    int seekVorbis(void* datasource, ogg_int64_t offset, int whence);
45    long tellVorbis(void* datasource);
46   
47    AmbientSound::AmbientSound()
48        : bPlayOnLoad_(false)
49    {
50        RegisterObject(AmbientSound);
51
52        // Ambient sounds always fade in
53        //this->setVolume(0);
54    }
55
56    void AmbientSound::preDestroy()
57    {
58        if (GameMode::playsSound())
59        {
60            // Smoothly fade out by keeping a SmartPtr
61            SoundManager::getInstance().unregisterAmbientSound(this);
62            this->soundstreamthread_.interrupt();
63        }
64    }
65
66    void AmbientSound::play()
67    {
68        if (GameMode::playsSound())
69            SoundManager::getInstance().registerAmbientSound(this);
70    }
71
72    bool AmbientSound::stop()
73    {
74        if (GameMode::playsSound()) 
75            SoundManager::getInstance().unregisterAmbientSound(this);
76        return false; // sound source not (yet) destroyed - return false
77    }
78
79    void AmbientSound::pause()
80    {
81        if (GameMode::playsSound())
82            SoundManager::getInstance().pauseAmbientSound(this);
83    }
84
85    float AmbientSound::getRealVolume()
86    {
87        assert(GameMode::playsSound());
88        return SoundManager::getInstance().getRealVolume(SoundType::Music);
89    }
90
91    void AmbientSound::setAmbientSource(const std::string& source)
92    {
93        this->ambientSource_ = source;
94        this->moodChanged(this->getMood());
95    }
96
97    void AmbientSound::moodChanged(const std::string& mood)
98    {
99        if (GameMode::playsSound())
100        {
101            const std::string& path = "ambient/" + MoodManager::getInstance().getMood() + '/' + this->ambientSource_;
102            shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path);
103            if (fileInfo != NULL)
104                this->setStreamSource(path);
105            else
106                COUT(3) << "Sound: " << this->ambientSource_ << ": Not a valid name! Ambient sound will not change." << std::endl;
107        }
108    }
109
110    void AmbientSound::setPlayOnLoad(bool val)
111    {
112        this->bPlayOnLoad_ = val;
113        if (val)
114            this->play();
115    }
116
117    // hacky solution for file streaming
118    void AmbientSound::setStreamSource(const std::string& source)
119    {
120        if (!GameMode::playsSound())
121        {
122            this->source_ = source;
123            return;
124        }
125
126        if(!alIsSource(this->audioSource_))
127            this->audioSource_ = SoundManager::getInstance().getSoundSource(this);
128
129        if (this->source_ == source)
130        {
131            return;
132        }
133
134        this->source_ = source;
135        // Don't load ""
136        if (source_.empty())
137            return;
138
139        if (this->soundstreamthread_.get_id() != boost::thread::id())
140        {
141            this->soundstreamthread_.interrupt(); // terminate an old thread if necessary
142        }
143
144        // queue some init buffers
145        COUT(4) << "Sound: Creating thread for " << source << std::endl;
146        // Get resource info
147        shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(source);
148        if (fileInfo == NULL)
149        {
150            COUT(2) << "Sound: Warning: Sound file '" << source << "' not found" << std::endl;
151            return;
152        }
153        // Open data stream
154        DataStreamPtr dataStream = Resource::open(fileInfo);
155       
156        alSourcei(this->audioSource_, AL_BUFFER, 0);
157
158        // Open file with custom streaming
159        ov_callbacks vorbisCallbacks;
160        vorbisCallbacks.read_func  = &readVorbis;
161        vorbisCallbacks.seek_func  = &seekVorbis;
162        vorbisCallbacks.tell_func  = &tellVorbis;
163        vorbisCallbacks.close_func = NULL;
164
165        OggVorbis_File* vf = new OggVorbis_File();
166        int ret = ov_open_callbacks(dataStream.get(), vf, NULL, 0, vorbisCallbacks);
167        if (ret < 0)
168        {
169            COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl;
170            ov_clear(vf);
171            return;
172        }
173        vorbis_info* vorbisInfo;
174        vorbisInfo = ov_info(vf, -1);
175        ALenum format;
176        if (vorbisInfo->channels == 1)
177            format = AL_FORMAT_MONO16;
178        else
179            format = AL_FORMAT_STEREO16;
180
181        char inbuffer[4096];
182        ALuint initbuffers[10];
183        alGenBuffers(10, initbuffers);
184        if (ALint error = alGetError()) {
185            COUT(2) << "Sound: Streamer: Could not generate buffer:" << getALErrorString(error) << std::endl;
186            return;
187        }
188        int current_section;
189
190        for(int i = 0; i < 10; i++)
191        {
192            long ret = ov_read(vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
193            if (ret == 0)
194            {
195                break;
196            }
197            else if (ret < 0)
198            {
199                COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
200                ov_clear(vf);
201                return;
202            }
203
204            alBufferData(initbuffers[i], format, &inbuffer, ret, vorbisInfo->rate);
205            if(ALint error = alGetError()) {
206                COUT(2) << "Sound: Could not fill buffer: " << getALErrorString(error) << std::endl;
207                break;
208             }
209             alSourceQueueBuffers(this->audioSource_, 1, &initbuffers[i]);
210             if (ALint error = alGetError()) {
211                 COUT(2) << "Sound: Warning: Couldn't queue buffers: " << getALErrorString(error) << std::endl;
212             }
213        }
214       
215        this->soundstreamthread_ = boost::thread(SoundStreamer(), this->audioSource_, dataStream, vf, current_section);
216        if(this->soundstreamthread_ == boost::thread())
217            COUT(2) << "Sound: Failed to create thread." << std::endl;
218        //SoundStreamer streamer;
219        //streamer(this->audioSource_, dataStream);
220
221        alSource3f(this->audioSource_, AL_POSITION,  0, 0, 0);
222        alSource3f(this->audioSource_, AL_VELOCITY,  0, 0, 0);
223        alSource3f(this->audioSource_, AL_DIRECTION, 0, 0, 0);
224        if (ALint error = alGetError())
225            COUT(2) << "Sound: Warning: Setting source parameters to 0 failed: " << getALErrorString(error) << std::endl;
226    }
227
228    bool AmbientSound::doStop()
229    {
230        bool result = BaseSound::doStop();
231        this->soundstreamthread_.interrupt();
232        return result;
233    }
234
235    void AmbientSound::doPlay()
236    {
237        BaseSound::doPlay();
238
239        if(GameMode::playsSound() && this->getSourceState() != AL_PLAYING)
240        {
241            if(!alIsSource(this->audioSource_))
242            {
243                this->audioSource_ = SoundManager::getInstance().getSoundSource(this);
244                if(!alIsSource(this->audioSource_))
245                    return;
246                this->initialiseSource();
247            }
248
249            alSourcePlay(this->audioSource_);
250            if(int error = alGetError())
251                COUT(2) << "Sound: Error playing sound: " << getALErrorString(error) << std::endl;
252        }
253    }
254}
Note: See TracBrowser for help on using the repository browser.