Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Switched from ResourceInfo to std::string for the buffer identification. That should speed up BaseSound::setSource() by factor 10 if the buffer already exists.

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