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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
|
/* Copyright (C) 2013 Nikos Chantziaras.
*
* This file is part of the QTads program. This program is free software; you
* can redistribute it and/or modify it under the terms of the GNU General
* Public License as published by the Free Software Foundation; either version
* 2, or (at your option) any later version.
*
* 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 General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; see the file COPYING. If not, write to the Free Software
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <QDebug>
#include <QFileInfo>
#include <QTimer>
#ifndef Q_OS_ANDROID
#include <SDL_mixer.h>
#include <SDL_sound.h>
#endif
#include "qtadssound.h"
#include "globals.h"
#include "sysframe.h"
#include "settings.h"
#include "syssoundwav.h"
#include "syssoundogg.h"
#include "syssoundmpeg.h"
#ifndef Q_OS_ANDROID
QList<QTadsSound*> QTadsSound::fObjList;
QTadsSound::QTadsSound( QObject* parent, Mix_Chunk* chunk, SoundType type )
: QObject(parent),
fChunk(chunk),
fChannel(-1),
fType(type),
fPlaying(false),
fFadeOut(0),
fCrossFade(false),
fFadeOutTimer(new QTimer(0)),
fDone_func(0),
fDone_func_ctx(0),
fRepeats(0),
fRepeatsWanted(1)
{
// FIXME: Calculate sound length in a safer way.
this->fLength = (this->fChunk->alen * 8) / (2 * 16 * 44.1);
// Pretend that the sound is 30ms shorter than it really is in order to
// compensate for wacky OS timers (Windows and low-Hz systems).
if (this->fLength > 30) {
this->fLength -= 30;
} else {
this->fLength = 0;
}
//qDebug() << "Sound length:" << this->fLength;
connect(this, SIGNAL(readyToLoop()), SLOT(fDoLoop()));
connect(this, SIGNAL(readyToFadeOut()), SLOT(fPrepareFadeOut()));
connect(this->fFadeOutTimer, SIGNAL(timeout()), this, SLOT(fDoFadeOut()));
connect(this, SIGNAL(destroyed()), SLOT(fDeleteTimer()));
// Make sure the timer only calls our fade-out slot *once* and then stops.
this->fFadeOutTimer->setSingleShot(true);
}
QTadsSound::~QTadsSound()
{
//qDebug() << Q_FUNC_INFO;
this->fRepeatsWanted = -1;
// If our chunk is still playing, halt it.
if (Mix_GetChunk(this->fChannel) == this->fChunk and Mix_Playing(this->fChannel) == 1) {
Mix_HaltChannel(this->fChannel);
}
// Wait till it finished before deleting it.
if (Mix_GetChunk(this->fChannel) == this->fChunk) {
while (Mix_Playing(this->fChannel) != 0) {
SDL_Delay(10);
}
}
// Free the raw audio data buffer if SDL_mixer didn't allocate it itself.
if (not this->fChunk->allocated) {
free(this->fChunk->abuf);
}
Mix_FreeChunk(this->fChunk);
}
void
QTadsSound::fDoFadeOut()
{
Q_ASSERT(Mix_GetChunk(this->fChannel) == this->fChunk and Mix_Playing(this->fChannel) == 1);
// If we need to do a crossfade, call the TADS callback now.
if (this->fCrossFade and this->fDone_func) {
this->fDone_func(this->fDone_func_ctx, this->fRepeats);
// Make sure our SDL callback won't call the TADS callback a second
// time.
this->fDone_func = 0;
this->fDone_func_ctx = 0;
}
Mix_FadeOutChannel(this->fChannel, this->fFadeOut);
}
void
QTadsSound::fDoLoop()
{
// When playing again, we can't assume that we can use the same channel.
this->fChannel = Mix_PlayChannel(-1, this->fChunk, 0);
this->fTimePos.start();
++this->fRepeats;
// If this is the last iteration and we have a fade-out, set the fade-out
// timer.
if (this->fFadeOut > 0 and (this->fRepeatsWanted == -1 or this->fRepeats == this->fRepeatsWanted)) {
this->fFadeOutTimer->start(this->fLength - this->fFadeOut);
}
}
void
QTadsSound::fPrepareFadeOut()
{
this->fFadeOutTimer->start(this->fLength - this->fFadeOut);
}
void
QTadsSound::fDeleteTimer()
{
delete this->fFadeOutTimer;
}
void
QTadsSound::callback( int channel )
{
QTadsSound* mObj = 0;
int index = 0;
// Find the object that uses the specified channel.
for (int i = 0; i < fObjList.size() and mObj == 0; ++i) {
if (fObjList.at(i)->fChannel == channel) {
mObj = fObjList.at(i);
index = i;
}
}
if (mObj == 0) {
return;
}
// If it's an infinite loop sound, or it has not reached the wanted repeat
// count yet, play again.
if ((mObj->fRepeatsWanted == 0) or (mObj->fRepeats < mObj->fRepeatsWanted)) {
mObj->emitReadyToLoop();
return;
}
// Remove the object from the list. Since it can be included several
// times, only remove the instance we associated to the channel we're
// handling.
fObjList.removeAt(index);
// Sound has repeated enough times, or it has been halted. In either case,
// we need to invoke the TADS callback, if there is one.
mObj->fPlaying = false;
if (mObj->fDone_func) {
mObj->fDone_func(mObj->fDone_func_ctx, mObj->fRepeats);
}
}
int
QTadsSound::startPlaying( void (*done_func)(void*, int repeat_count), void* done_func_ctx, int repeat, int vol,
int fadeIn, int fadeOut, bool crossFade )
{
// Check if user disabled digital sound.
if (not qFrame->settings()->enableSoundEffects) {
return 1;
}
Q_ASSERT(not this->fPlaying);
// Adjust volume if it exceeds min/max levels.
if (vol < 0) {
vol = 0;
} else if (vol > 100) {
vol = 100;
}
// Convert the TADS volume level semantics [0..100] to SDL volume
// semantics [0..MIX_MAX_VOLUME].
vol = (vol * MIX_MAX_VOLUME) / 100;
// Set the volume level.
Mix_VolumeChunk(this->fChunk, vol);
this->fRepeatsWanted = repeat;
if (fadeIn > 0) {
this->fChannel = Mix_FadeInChannel(-1, this->fChunk, 0, fadeIn);
} else {
this->fChannel = Mix_PlayChannel(-1, this->fChunk, 0);
}
if (this->fChannel == -1) {
qWarning() << "ERROR:" << Mix_GetError();
Mix_SetError("");
return 1;
} else {
this->fTimePos.start();
this->fPlaying = true;
this->fCrossFade = crossFade;
this->fRepeats = 1;
QTadsSound::fObjList.append(this);
this->fDone_func = done_func;
this->fDone_func_ctx = done_func_ctx;
if (fadeOut > 0) {
this->fFadeOut = fadeOut;
if (repeat == 1) {
// The sound should only be played once. We need to set the
// fade-out timer here since otherwise the sound won't get a
// chance to fade-out.
emit readyToFadeOut();
}
}
}
return 0;
}
void
QTadsSound::cancelPlaying( bool sync, int fadeOut, bool fadeOutInBg )
{
if (not this->fPlaying) {
return;
}
this->fRepeatsWanted = -1;
if (not sync and fadeOut > 0) {
if (fadeOutInBg and this->fDone_func) {
// We need to do fade-out in the background; call the TADS callback
// now.
this->fDone_func(this->fDone_func_ctx, this->fRepeats);
// Make sure our SDL callback won't call the TADS callback a second
// time.
this->fDone_func = 0;
this->fDone_func_ctx = 0;
}
Mix_FadeOutChannel(this->fChannel, fadeOut);
} else {
Mix_HaltChannel(this->fChannel);
}
if (sync and Mix_GetChunk(this->fChannel) == this->fChunk) {
// The operation needs to be synchronous; wait for the sound to finish.
while (Mix_Playing(this->fChannel) != 0) {
SDL_Delay(10);
}
}
}
void
QTadsSound::addCrossFade( int ms )
{
this->fCrossFade = true;
this->fFadeOut = ms;
if (this->fPlaying) {
this->fRepeatsWanted = -1;
int timeFromNow = this->fLength - this->fFadeOut - this->fTimePos.elapsed();
if (timeFromNow < 1) {
timeFromNow = 1;
this->fFadeOut = this->fLength - this->fTimePos.elapsed();
}
this->fFadeOutTimer->start(timeFromNow);
}
}
#endif
CHtmlSysSound*
QTadsSound::createSound( const CHtmlUrl* /*url*/, const textchar_t* filename, unsigned long seekpos,
unsigned long filesize, CHtmlSysWin*, SoundType type )
#ifndef Q_OS_ANDROID
{
//qDebug() << "Loading sound from" << filename << "offset:" << seekpos << "size:" << filesize
// << "url:" << url->get_url();
// Check if the file exists and is readable.
QFileInfo inf(fnameToQStr(filename));
if (not inf.exists() or not inf.isReadable()) {
qWarning() << "ERROR:" << inf.filePath() << "doesn't exist or is unreadable";
return 0;
}
// Open the file and seek to the specified position.
QFile file(inf.filePath());
if (not file.open(QIODevice::ReadOnly)) {
qWarning() << "ERROR: Can't open file" << inf.filePath();
return 0;
}
if (not file.seek(seekpos)) {
qWarning() << "ERROR: Can't seek in file" << inf.filePath();
file.close();
return 0;
}
QByteArray data(file.read(filesize));
file.close();
if (data.isEmpty() or static_cast<unsigned long>(data.size()) < filesize) {
qWarning() << "ERROR: Could not read" << filesize << "bytes from file" << inf.filePath();
return 0;
}
// Create the RWops through which the data will be read later.
SDL_RWops* rw = SDL_RWFromConstMem(data.constData(), data.size());
if (rw == 0) {
qWarning() << "ERROR:" << SDL_GetError();
SDL_ClearError();
return 0;
}
// The chunk that will hold the final, decoded sound.
Mix_Chunk* chunk;
// If it's an MP3 or WAV, we'll decode it with SDL_sound. For Ogg Vorbis
// we use SDL_mixer. The reason is that SDL_mixer plays WAV at wrong
// speeds if they're not 11, 22 or 44kHz (like 48kHz or 32kHz) and crashes
// sometimes with MP3s. SDL_sound can't cope well with Ogg Vorbis that
// have more then two channels.
if (type == MPEG or type == WAV) {
if (rw == 0) {
qWarning() << "ERROR:" << SDL_GetError();
SDL_ClearError();
return 0;
}
Sound_AudioInfo wantedFormat;
wantedFormat.channels = 2;
wantedFormat.rate = 44100;
wantedFormat.format = MIX_DEFAULT_FORMAT;
// Note that we use a large buffer size to speed-up decoding of large
// MP3s on Windows; the decoding will take extremely long with small
// buffer sizes.
Sound_Sample* sample = Sound_NewSample(rw, type == WAV ? "WAV" : "MP3", &wantedFormat,
#ifdef Q_OS_WIN32
6291456
#else
131072
#endif
);
if (sample == 0) {
qWarning() << "ERROR:" << Sound_GetError();
Sound_ClearError();
return 0;
}
Sound_DecodeAll(sample);
if (sample->flags & SOUND_SAMPLEFLAG_ERROR) {
// We don't abort since some of these errors can be non-fatal.
// Unfortunately, there's no way to tell :-/
qWarning() << "WARNING:" << Sound_GetError();
Sound_ClearError();
}
Uint8* buf = static_cast<Uint8*>(malloc(sample->buffer_size));
memcpy(buf, sample->buffer, sample->buffer_size);
Sound_FreeSample(sample);
chunk = Mix_QuickLoad_RAW(buf, sample->buffer_size);
if (chunk == 0) {
qWarning() << "ERROR:" << Mix_GetError();
Mix_SetError("");
free(buf);
return 0;
}
} else {
Q_ASSERT(type == OGG);
chunk = Mix_LoadWAV_RW(rw, true);
if (chunk == 0) {
qWarning() << "ERROR:" << Mix_GetError();
Mix_SetError("");
return 0;
}
}
// Alternative way of decoding MPEG, utilizing SMPEG directly. It sucks,
// since SMPEG, for the piece of crap it is, tends to play MP3s at double
// speed (chipmunks ahoy...)
#if 0
if (type == MPEG) {
// The sound is an mp3. We'll decode it into an SDL_Mixer chunk using
// SMPEG.
SMPEG* smpeg = SMPEG_new_data(data.data(), data.size(), 0, 0);
// Set the format we want the decoded raw data to have.
SDL_AudioSpec spec;
SMPEG_wantedSpec(smpeg, &spec);
spec.channels = 2;
spec.format = MIX_DEFAULT_FORMAT;
spec.freq = 44100;
SMPEG_actualSpec(smpeg, &spec);
// We decode the mpeg stream in steps of 8192kB each and increase the
// size of the output buffer after each step.
size_t bufSize = 8192;
Uint8* buf = static_cast<Uint8*>(malloc(bufSize));
// Needs to be set to digital silence because SMPEG is mixing source
// and destination.
memset(buf, 0, bufSize);
// Prepare for decoding and decode the first bunch of data.
SMPEG_play(smpeg);
int bytesWritten = SMPEG_playAudio(smpeg, buf, 8192);
// Decode the rest. Increase buffer as needed.
while (bytesWritten > 0 and bytesWritten <= 8192) {
bufSize += 8192;
buf = static_cast<Uint8*>(realloc(buf, bufSize));
memset(buf + (bufSize - 8192), 0, 8192);
bytesWritten = SMPEG_playAudio(smpeg, buf + (bufSize - 8192), 8192);
}
// Done with decoding.
SMPEG_stop(smpeg);
if (SMPEG_error(smpeg) != 0) {
qWarning() << "ERROR: cannot decode sound data:" << SMPEG_error(smpeg);
free(buf);
SMPEG_delete(smpeg);
return 0;
}
SMPEG_delete(smpeg);
// Adjust final buffer size to fit the decoded data exactly.
buf = static_cast<Uint8*>(realloc(buf, (bufSize - 8192) + bytesWritten));
chunk = Mix_QuickLoad_RAW(buf, (bufSize - 8192) + bytesWritten);
}
#endif
// We have all the data we need; create the sound object. It is
// *important* not to pass the CHtmlSysWin object as the parent in the
// constructor; doing so would result in Qt deleting the sound object when
// the parent object gets destroyed. Therefore, we simply pass 0 to make
// the sound object parentless.
CHtmlSysSound* sound = NULL;
switch (type) {
case WAV:
//qDebug() << "Sound type: WAV";
sound = new CHtmlSysSoundWavQt(0, chunk, WAV);
break;
case OGG:
//qDebug() << "Sound type: OGG";
sound = new CHtmlSysSoundOggQt(0, chunk, OGG);
break;
case MPEG:
//qDebug() << "Sound type: MPEG";
sound = new CHtmlSysSoundMpegQt(0, chunk, MPEG);
break;
}
return sound;
}
#else
{
return 0;
}
#endif
|