Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/kicklib/src/orxonox/sound/SoundBuffer.cc @ 8071

Last change on this file since 8071 was 8071, checked in by rgrieder, 13 years ago

Merged ois_update branch (before it was renamed to mac_osx) into kicklib branch.

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