Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/sound/SoundBuffer.cc @ 6340

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

Fixed bug: Ambient sounds were sometimes not played at all (caused by the source management changes).
And removed some debug output.

  • Property svn:eol-style set to native
File size: 5.9 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 *      Erwin 'vaiursch' Herrsche
25 *   Co-authors:
26 *      ...
27 *
28 */
29
30#include "SoundBuffer.h"
31
32#include <AL/alut.h>
33#include <vorbis/vorbisfile.h>
34
35#include "util/Exception.h"
36#include "util/StringUtils.h"
37#include "core/Resource.h"
38#include "sound/SoundManager.h"
39
40namespace orxonox
41{
42    SoundBuffer::SoundBuffer(const std::string& filename, std::list<shared_ptr<SoundBuffer> >::iterator poolIterator)
43        : filename_(filename)
44        , audioBuffer_(AL_NONE)
45        , poolIterator_(poolIterator)
46    {
47        if (this->filename_.empty())
48            ThrowException(General, "SoundBuffer construction: fileInfo was NULL");
49
50        // Get resource info
51        shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(filename);
52        if (fileInfo == NULL)
53        {
54            COUT(2) << "Sound: Warning: Sound file '" << filename << "' not found" << std::endl;
55            return;
56        }
57        // Open data stream
58        DataStreamPtr dataStream = Resource::open(fileInfo);
59
60        std::string extension(this->filename_.substr(this->filename_.find_last_of('.') + 1));
61        if (getLowercase(extension) == "ogg")
62        {
63            // Try ogg loader
64            this->loadOgg(fileInfo, dataStream);
65        }
66        else
67        {
68            // Try standard OpenAL loader
69            this->loadStandard(fileInfo, dataStream);
70        }
71    }
72
73    SoundBuffer::~SoundBuffer()
74    {
75        // Destroy buffer
76        alDeleteBuffers(1, &this->audioBuffer_);
77    }
78
79    unsigned int SoundBuffer::getSize() const
80    {
81        ALint size;
82        alGetBufferi(this->audioBuffer_, AL_SIZE, &size);
83        return alGetError() ? 0 : size;
84    }
85
86    void SoundBuffer::loadStandard(const shared_ptr<ResourceInfo>& fileInfo, DataStreamPtr dataStream)
87    {
88        // Read everything into a temporary buffer
89        char* buffer = new char[fileInfo->size];
90        dataStream->read(buffer, fileInfo->size);
91        dataStream->seek(0);
92
93        this->audioBuffer_ = alutCreateBufferFromFileImage(buffer, fileInfo->size);
94        delete[] buffer;
95
96        if (!alIsBuffer(this->audioBuffer_))
97            ThrowException(General, "Sound Error: Standard file loader failed: " << alutGetErrorString(alutGetError()));
98    }
99
100    size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource)
101    {
102        return static_cast<Ogre::DataStream*>(datasource)->read(ptr, size * nmemb);
103    }
104
105    int seekVorbis(void* datasource, ogg_int64_t offset, int whence)
106    {
107        Ogre::DataStream* stream = static_cast<Ogre::DataStream*>(datasource);
108        switch (whence)
109        {
110        case SEEK_SET:
111            stream->seek(offset);
112            break;
113        case SEEK_CUR:
114            stream->skip(offset);
115            break;
116        case SEEK_END:
117            stream->seek(stream->size() + offset);
118            break;
119        default:
120            return -1;
121        }
122        return 0;
123    }
124
125    long tellVorbis(void* datasource)
126    {
127        return static_cast<long>(static_cast<Ogre::DataStream*>(datasource)->tell());
128    }
129
130    void SoundBuffer::loadOgg(const shared_ptr<ResourceInfo>& fileInfo, DataStreamPtr dataStream)
131    {
132        char inbuffer[256*1024];
133        std::vector<char> outbuffer;
134        outbuffer.reserve(80*1024*1024);
135
136        // Open file with custom streaming
137        ov_callbacks vorbisCallbacks;
138        vorbisCallbacks.read_func  = &readVorbis;
139        vorbisCallbacks.seek_func  = &seekVorbis;
140        vorbisCallbacks.tell_func  = &tellVorbis;
141        vorbisCallbacks.close_func = NULL;
142
143        OggVorbis_File vf;
144        int ret = ov_open_callbacks(dataStream.get(), &vf, NULL, 0, vorbisCallbacks);
145        if (ret < 0)
146        {
147            COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl;
148            ov_clear(&vf);
149            ThrowException(General, "Sound Error: Ogg file loader failed when opening the bitstream");
150        }
151
152        int current_section;
153        int eof = false;
154        while (!eof)
155        {
156            long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
157            if (ret == 0)
158            {
159                eof = true;
160            }
161            else if (ret < 0)
162            {
163                COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
164                ov_clear(&vf);
165                ThrowException(General, "Sound Error: Ogg file loader failed when decoding the file");
166            }
167            else
168            {
169                outbuffer.insert(outbuffer.end(), inbuffer, inbuffer + ret);
170            }
171        }
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        alGenBuffers(1, &this->audioBuffer_);
182        alBufferData(this->audioBuffer_, format, &outbuffer[0], outbuffer.size(), vorbisInfo->rate);
183        ov_clear(&vf);
184
185        if (!alIsBuffer(this->audioBuffer_))
186            ThrowException(General, "Sound: Ogg file loader failed when creating the buffer.");
187    }
188}
Note: See TracBrowser for help on using the repository browser.