Otclient  14/8/2020
soundbuffer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "soundbuffer.h"
24 #include "soundfile.h"
25 
27 {
28  m_bufferId = 0;
29  alGenBuffers(1, &m_bufferId);
30  assert(alGetError() == AL_NO_ERROR);
31 }
32 
34 {
35  alDeleteBuffers(1, &m_bufferId);
36  assert(alGetError() == AL_NO_ERROR);
37 }
38 
39 bool SoundBuffer::fillBuffer(const SoundFilePtr& soundFile)
40 {
41  ALenum format = soundFile->getSampleFormat();
42  if(format == AL_UNDETERMINED) {
43  g_logger.error(stdext::format("unable to determine sample format for '%s'", soundFile->getName()));
44  return false;
45  }
46 
47  DataBuffer<char> samples(soundFile->getSize());
48  int read = soundFile->read(&samples[0], soundFile->getSize());
49  if(read == 0) {
50  g_logger.error(stdext::format("unable to fill audio buffer data for '%s'", soundFile->getName()));
51  return false;
52  }
53 
54  return fillBuffer(format, samples, samples.size(), soundFile->getRate());
55 }
56 
57 bool SoundBuffer::fillBuffer(ALenum sampleFormat, const DataBuffer<char>& data, int size, int rate)
58 {
59  alBufferData(m_bufferId, sampleFormat, &data[0], size, rate);
60  ALenum err = alGetError();
61  if(err != AL_NO_ERROR) {
62  g_logger.error(stdext::format("unable to fill audio buffer data: %s", alGetString(err)));
63  return false;
64  }
65  return true;
66 }
SoundFile::getName
std::string getName()
Definition: soundfile.h:46
DataBuffer
Definition: databuffer.h:27
Logger::error
void error(const std::string &what)
Definition: logger.h:54
DataBuffer::size
uint size() const
Definition: databuffer.h:47
soundbuffer.h
stdext::format
std::string format()
Definition: format.h:82
SoundFile::getSize
int getSize()
Definition: soundfile.h:45
SoundFile::getSampleFormat
ALenum getSampleFormat()
Definition: soundfile.cpp:57
soundfile.h
g_logger
Logger g_logger
Definition: logger.cpp:35
SoundFile::getRate
int getRate()
Definition: soundfile.h:43
SoundBuffer::SoundBuffer
SoundBuffer()
Definition: soundbuffer.cpp:26
SoundBuffer::fillBuffer
bool fillBuffer(const SoundFilePtr &soundFile)
Definition: soundbuffer.cpp:39
stdext::shared_object_ptr< SoundFile >
SoundBuffer::~SoundBuffer
~SoundBuffer()
Definition: soundbuffer.cpp:33
SoundFile::read
virtual int read(void *buffer, int bufferSize)
Definition: soundfile.h:36