2012-04-13 21:54:08 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "streamsoundsource.h"
|
|
|
|
#include "soundbuffer.h"
|
|
|
|
#include "soundfile.h"
|
|
|
|
|
|
|
|
#include <framework/util/databuffer.h>
|
|
|
|
|
|
|
|
StreamSoundSource::StreamSoundSource()
|
|
|
|
{
|
|
|
|
for(auto& buffer : m_buffers)
|
|
|
|
buffer = SoundBufferPtr(new SoundBuffer);
|
2012-04-14 15:15:51 +02:00
|
|
|
m_downMix = NoDownMix;
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
StreamSoundSource::~StreamSoundSource()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamSoundSource::setSoundFile(const SoundFilePtr& soundFile)
|
|
|
|
{
|
|
|
|
m_soundFile = soundFile;
|
2012-04-14 15:15:51 +02:00
|
|
|
}
|
2012-04-13 21:54:08 +02:00
|
|
|
|
2012-04-14 15:15:51 +02:00
|
|
|
void StreamSoundSource::play()
|
|
|
|
{
|
|
|
|
if(!m_soundFile) {
|
|
|
|
logError("there is not sound file to play the stream");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
queueBuffers();
|
|
|
|
|
|
|
|
SoundSource::play();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamSoundSource::stop()
|
|
|
|
{
|
|
|
|
SoundSource::stop();
|
|
|
|
unqueueBuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamSoundSource::queueBuffers()
|
|
|
|
{
|
2012-04-13 21:54:08 +02:00
|
|
|
ALint queued;
|
|
|
|
alGetSourcei(m_sourceId, AL_BUFFERS_QUEUED, &queued);
|
|
|
|
for(int i = 0; i < STREAM_FRAGMENTS - queued; ++i) {
|
|
|
|
if(!fillBufferAndQueue(m_buffers[i]->getBufferId()))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-14 15:15:51 +02:00
|
|
|
void StreamSoundSource::unqueueBuffers()
|
|
|
|
{
|
|
|
|
ALint queued;
|
|
|
|
alGetSourcei(m_sourceId, AL_BUFFERS_QUEUED, &queued);
|
|
|
|
for(int i = 0; i < queued; ++i) {
|
|
|
|
ALuint buffer;
|
|
|
|
alSourceUnqueueBuffers(m_sourceId, 1, &buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
void StreamSoundSource::update()
|
|
|
|
{
|
2012-04-14 16:19:58 +02:00
|
|
|
SoundSource::update();
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
ALint processed = 0;
|
|
|
|
alGetSourcei(m_sourceId, AL_BUFFERS_PROCESSED, &processed);
|
|
|
|
for(ALint i = 0; i < processed; ++i) {
|
|
|
|
ALuint buffer;
|
|
|
|
alSourceUnqueueBuffers(m_sourceId, 1, &buffer);
|
|
|
|
//SoundManager::check_al_error("Couldn't unqueue audio buffer: ");
|
|
|
|
|
|
|
|
if(!fillBufferAndQueue(buffer))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!isPlaying()) {
|
|
|
|
if(processed == 0 || !m_looping)
|
|
|
|
return;
|
|
|
|
|
2012-04-14 15:15:51 +02:00
|
|
|
logTraceError("restarting audio source because of buffer underrun");
|
|
|
|
play();
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StreamSoundSource::fillBufferAndQueue(ALuint buffer)
|
|
|
|
{
|
|
|
|
// fill buffer
|
2012-04-14 21:52:00 +02:00
|
|
|
static DataBuffer<char> bufferData(2*STREAM_FRAGMENT_SIZE);
|
2012-04-14 15:15:51 +02:00
|
|
|
ALenum format = m_soundFile->getSampleFormat();
|
2012-04-13 21:54:08 +02:00
|
|
|
|
2012-04-14 21:52:00 +02:00
|
|
|
int maxRead = STREAM_FRAGMENT_SIZE;
|
|
|
|
if(m_downMix != NoDownMix)
|
|
|
|
maxRead *= 2;
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
int bytesRead = 0;
|
|
|
|
do {
|
2012-04-14 21:52:00 +02:00
|
|
|
bytesRead += m_soundFile->read(&bufferData[bytesRead], maxRead - bytesRead);
|
2012-04-13 21:54:08 +02:00
|
|
|
|
|
|
|
// end of sound file
|
2012-04-14 21:52:00 +02:00
|
|
|
if(bytesRead < maxRead) {
|
2012-04-13 21:54:08 +02:00
|
|
|
if(m_looping)
|
|
|
|
m_soundFile->reset();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2012-04-14 21:52:00 +02:00
|
|
|
} while(bytesRead < maxRead);
|
2012-04-14 15:15:51 +02:00
|
|
|
|
2012-04-14 21:52:00 +02:00
|
|
|
if(bytesRead > 0) {
|
|
|
|
if(m_downMix != NoDownMix) {
|
|
|
|
if(format == AL_FORMAT_STEREO16) {
|
|
|
|
assert(bytesRead % 2 == 0);
|
2012-04-14 15:15:51 +02:00
|
|
|
bytesRead /= 2;
|
2012-04-14 21:52:00 +02:00
|
|
|
uint16_t *data = (uint16_t*)bufferData.data();
|
|
|
|
for(int i=0;i<bytesRead/2;i++)
|
2012-04-14 15:15:51 +02:00
|
|
|
data[i] = data[2*i + (m_downMix == DownMixLeft ? 0 : 1)];
|
2012-04-14 21:52:00 +02:00
|
|
|
format = AL_FORMAT_MONO16;
|
2012-04-14 15:15:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
alBufferData(buffer, format, &bufferData[0], bytesRead, m_soundFile->getRate());
|
|
|
|
ALenum err = alGetError();
|
|
|
|
if(err != AL_NO_ERROR)
|
2012-05-29 00:04:44 +02:00
|
|
|
logError("unable to refill audio buffer for '%s': %s", m_soundFile->getName(), alGetString(err));
|
2012-04-13 21:54:08 +02:00
|
|
|
|
|
|
|
alSourceQueueBuffers(m_sourceId, 1, &buffer);
|
|
|
|
err = alGetError();
|
|
|
|
if(err != AL_NO_ERROR)
|
2012-05-29 00:04:44 +02:00
|
|
|
logError("unable to queue audio buffer for '%s': %s", m_soundFile->getName(), alGetString(err));
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// return false if there aren't more buffers to fill
|
2012-04-14 21:52:00 +02:00
|
|
|
return bytesRead >= STREAM_FRAGMENT_SIZE;
|
2012-04-14 15:15:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StreamSoundSource::downMix(StreamSoundSource::DownMix downMix)
|
|
|
|
{
|
|
|
|
if(!m_soundFile) {
|
|
|
|
logError("down mix must be set after setting a sound file");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m_soundFile->getSampleFormat() != AL_FORMAT_STEREO16) {
|
|
|
|
logError("can only downmix 16 bit stereo audio files");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_downMix = downMix;
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|