1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/***************************************************************************
* Copyright 2010 Stefan Majewsky <majewsky@gmx.net> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License *
* version 2 as published by the Free Software Foundation *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "kgsound.h"
#include <sndfile.hh>
#include <QDebug>
#include "kgopenalruntime_p.h"
#include "virtualfileqt-openal.h"
class KgSound::Private
{
public:
KgSound::PlaybackType m_type;
qreal m_volume;
QPointF m_pos;
bool m_valid;
ALuint m_buffer;
Private() : m_type(KgSound::AmbientPlayback), m_volume(1.0), m_valid(false), m_buffer(AL_NONE) {}
};
//BEGIN KgSound
KgSound::KgSound(const QString& file, QObject* parent)
: QObject(parent)
, d(new Private)
{
VirtualFileQt fileInterface(file);
if (!fileInterface.open()) {
qWarning() << "Failed to open sound file" << file;
return;
}
//open sound file
SndfileHandle handle(VirtualFileQt::getSndfileVirtualIO(), &fileInterface);
if (handle.error())
{
qWarning() << "Failed to load sound file" << file << ". Error message from libsndfile follows.";
qWarning() << handle.strError();
return;
}
const int channelCount = handle.channels();
const int sampleCount = channelCount * handle.frames();
const int sampleRate = handle.samplerate();
//load data from sound file
QVector<ALshort> samples(sampleCount);
if (handle.read(samples.data(), sampleCount) < sampleCount)
{
qWarning() << "Failed to read sound file" << file;
qWarning() << "File ended unexpectedly.";
return;
}
//determine file format from number of channels
ALenum format;
switch (channelCount)
{
case 1:
format = AL_FORMAT_MONO16;
break;
case 2:
format = AL_FORMAT_STEREO16;
break;
default:
qWarning() << "Failed to read sound file" << file;
qWarning() << "More than two channels are not supported.";
return;
}
//make sure OpenAL is initialized; clear OpenAL error storage
KgOpenALRuntime::instance();
int error; alGetError();
//create OpenAL buffer
alGenBuffers(1, &d->m_buffer);
if ((error = alGetError()) != AL_NO_ERROR)
{
qWarning() << "Failed to create OpenAL buffer: Error code" << error;
return;
}
alBufferData(d->m_buffer, format, samples.data(), sampleCount * sizeof(ALshort), sampleRate);
if ((error = alGetError()) != AL_NO_ERROR)
{
qWarning() << "Failed to fill OpenAL buffer: Error code" << error;
alDeleteBuffers(1, &d->m_buffer);
return;
}
//loading finished
d->m_valid = true;
}
KgSound::~KgSound()
{
if (d->m_valid)
{
stop();
KgOpenALRuntime::instance()->m_soundsEvents.remove(this);
alDeleteBuffers(1, &d->m_buffer);
}
delete d;
}
bool KgSound::isValid() const
{
return d->m_valid;
}
KgSound::PlaybackType KgSound::playbackType() const
{
return d->m_type;
}
void KgSound::setPlaybackType(KgSound::PlaybackType type)
{
if (d->m_type == type)
return;
d->m_type = type;
Q_EMIT playbackTypeChanged(type);
}
QPointF KgSound::pos() const
{
return d->m_pos;
}
void KgSound::setPos(const QPointF& pos)
{
if (d->m_pos == pos)
return;
d->m_pos = pos;
Q_EMIT posChanged(pos);
}
qreal KgSound::volume() const
{
return d->m_volume;
}
void KgSound::setVolume(qreal volume)
{
if (d->m_volume == volume)
return;
d->m_volume = volume;
Q_EMIT volumeChanged(volume);
}
bool KgSound::hasError() const
{
return !d->m_valid;
}
void KgSound::start()
{
start(d->m_pos);
}
void KgSound::start(const QPointF& pos)
{
if (d->m_valid)
{
KgOpenALRuntime* runtime = KgOpenALRuntime::instance();
if(!runtime->instance()->m_soundsEvents[this].isEmpty())
{
if(runtime->instance()->m_soundsEvents[this].last()->replay(pos) == false)
{
new KgPlaybackEvent(this, pos);
}
}
else
{
new KgPlaybackEvent(this, pos);
}
}
}
void KgSound::stop()
{
qDeleteAll(KgOpenALRuntime::instance()->m_soundsEvents.take(this));
}
//END KgSound
//BEGIN KgPlaybackEvent
KgPlaybackEvent::KgPlaybackEvent(KgSound* sound, const QPointF& pos)
: m_valid(false)
{
//make sure OpenAL is initialized
KgOpenALRuntime* runtime = KgOpenALRuntime::instance();
//clear OpenAL error storage
int error; alGetError();
//create source for playback
alGenSources(1, &m_source);
if ((error = alGetError()) != AL_NO_ERROR)
{
qWarning() << "Failed to create OpenAL source: Error code" << error;
return;
}
//store in OpenALRuntime
runtime->m_soundsEvents[sound] << this;
m_valid = true;
//connect to sound (buffer)
alSource3f(m_source, AL_POSITION, pos.x(), pos.y(), 0);
alSourcef(m_source, AL_PITCH, 1.0); //TODO: debug
alSourcef(m_source, AL_GAIN, sound->volume());
alSourcei(m_source, AL_BUFFER, sound->d->m_buffer);
const KgSound::PlaybackType type = sound->playbackType();
alSourcef(m_source, AL_ROLLOFF_FACTOR, type == KgSound::AmbientPlayback ? 0.0 : 1.0);
alSourcei(m_source, AL_SOURCE_RELATIVE, type == KgSound::RelativePlayback ? AL_TRUE : AL_FALSE);
if ((error = alGetError()) != AL_NO_ERROR)
{
qWarning() << "Failed to setup OpenAL source: Error code" << error;
return;
}
//start playback
alSourcePlay(m_source);
}
KgPlaybackEvent::~KgPlaybackEvent()
{
if(alIsSource(m_source) == AL_TRUE)
{
alSourceStop(m_source);
alDeleteSources(1, &m_source);
}
}
bool KgPlaybackEvent::isRunning() const
{
ALint state;
alGetSourcei(m_source, AL_SOURCE_STATE, &state);
return state == AL_PLAYING;
}
bool KgPlaybackEvent::replay(const QPointF& pos) const
{
if(alIsSource(m_source) == AL_TRUE)
{
alSourceStop(m_source);
alSource3f(m_source, AL_POSITION, pos.x(), pos.y(), 0);
alSourcePlay(m_source);
return true;
}
else
{
return false;
}
}
//END KgPlaybackEvent
#include "moc_kgsound.cpp"
|