Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/sound/SoundBuffer.cc @ 11948

Last change on this file since 11948 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

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