2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** audio.cpp
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
|
|
|
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
|
|
|
|
**
|
|
|
|
** mkxp 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 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** mkxp 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 mkxp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "audio.h"
|
|
|
|
|
2014-07-23 15:28:20 +00:00
|
|
|
#include "audiostream.h"
|
|
|
|
#include "soundemitter.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-11-30 11:00:11 +00:00
|
|
|
#include <string>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-11-30 11:00:11 +00:00
|
|
|
#include <SDL_thread.h>
|
|
|
|
#include <SDL_timer.h>
|
|
|
|
|
|
|
|
struct AudioPrivate
|
|
|
|
{
|
|
|
|
AudioStream bgm;
|
|
|
|
AudioStream bgs;
|
|
|
|
AudioStream me;
|
|
|
|
|
|
|
|
SoundEmitter se;
|
|
|
|
|
|
|
|
/* The 'MeWatch' is responsible for detecting
|
|
|
|
* a playing ME, quickly fading out the BGM and
|
|
|
|
* keeping it paused/stopped while the ME plays,
|
|
|
|
* and unpausing/fading the BGM back in again
|
|
|
|
* afterwards */
|
|
|
|
enum MeWatchState
|
|
|
|
{
|
|
|
|
MeNotPlaying,
|
|
|
|
BgmFadingOut,
|
|
|
|
MePlaying,
|
|
|
|
BgmFadingIn
|
|
|
|
};
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
SDL_Thread *thread;
|
|
|
|
bool active;
|
|
|
|
bool termReq;
|
|
|
|
MeWatchState state;
|
|
|
|
} meWatch;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-11-30 11:00:11 +00:00
|
|
|
AudioPrivate()
|
2014-01-07 15:06:50 +00:00
|
|
|
: bgm(ALStream::Looped, "bgm"),
|
|
|
|
bgs(ALStream::Looped, "bgs"),
|
|
|
|
me(ALStream::NotLooped, "me")
|
2013-11-30 11:00:11 +00:00
|
|
|
{
|
|
|
|
meWatch.active = false;
|
|
|
|
meWatch.termReq = false;
|
|
|
|
meWatch.state = MeNotPlaying;
|
|
|
|
meWatch.thread = SDL_CreateThread(meWatchFun, "audio_mewatch", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AudioPrivate()
|
|
|
|
{
|
|
|
|
meWatch.termReq = true;
|
|
|
|
SDL_WaitThread(meWatch.thread, 0);
|
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-11-30 11:00:11 +00:00
|
|
|
void meWatchFunInt()
|
|
|
|
{
|
|
|
|
const float fadeOutStep = 1.f / (200 / AUDIO_SLEEP);
|
|
|
|
const float fadeInStep = 1.f / (1000 / AUDIO_SLEEP);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-11-30 11:00:11 +00:00
|
|
|
while (true)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-11-30 11:00:11 +00:00
|
|
|
if (meWatch.termReq)
|
2013-09-01 14:27:21 +00:00
|
|
|
return;
|
|
|
|
|
2013-11-30 11:00:11 +00:00
|
|
|
switch (meWatch.state)
|
|
|
|
{
|
|
|
|
case MeNotPlaying:
|
|
|
|
{
|
|
|
|
me.lockStream();
|
|
|
|
|
|
|
|
if (me.stream.queryState() == ALStream::Playing)
|
|
|
|
{
|
|
|
|
/* ME playing detected. -> FadeOutBGM */
|
|
|
|
bgm.extPaused = true;
|
|
|
|
meWatch.state = BgmFadingOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
me.unlockStream();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-11-30 11:00:11 +00:00
|
|
|
case BgmFadingOut :
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-11-30 11:00:11 +00:00
|
|
|
me.lockStream();
|
|
|
|
|
|
|
|
if (me.stream.queryState() != ALStream::Playing)
|
|
|
|
{
|
|
|
|
/* ME has ended while fading OUT BGM. -> FadeInBGM */
|
|
|
|
me.unlockStream();
|
|
|
|
meWatch.state = BgmFadingIn;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bgm.lockStream();
|
|
|
|
|
|
|
|
float vol = bgm.extVolume;
|
|
|
|
vol -= fadeOutStep;
|
|
|
|
|
|
|
|
if (vol < 0 || bgm.stream.queryState() != ALStream::Playing)
|
|
|
|
{
|
|
|
|
/* Either BGM has fully faded out, or stopped midway. -> MePlaying */
|
|
|
|
bgm.setExtVolume1(0);
|
|
|
|
bgm.stream.pause();
|
|
|
|
meWatch.state = MePlaying;
|
|
|
|
bgm.unlockStream();
|
|
|
|
me.unlockStream();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bgm.setExtVolume1(vol);
|
|
|
|
bgm.unlockStream();
|
|
|
|
me.unlockStream();
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-30 11:00:11 +00:00
|
|
|
case MePlaying :
|
|
|
|
{
|
|
|
|
me.lockStream();
|
|
|
|
|
|
|
|
if (me.stream.queryState() != ALStream::Playing)
|
|
|
|
{
|
|
|
|
/* ME has ended */
|
|
|
|
bgm.lockStream();
|
|
|
|
|
|
|
|
bgm.extPaused = false;
|
|
|
|
|
|
|
|
ALStream::State sState = bgm.stream.queryState();
|
|
|
|
|
|
|
|
if (sState == ALStream::Paused)
|
|
|
|
{
|
|
|
|
/* BGM is paused. -> FadeInBGM */
|
|
|
|
bgm.stream.play();
|
|
|
|
meWatch.state = BgmFadingIn;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* BGM is stopped. -> MeNotPlaying */
|
|
|
|
bgm.setExtVolume1(1.0);
|
2013-12-08 11:58:39 +00:00
|
|
|
|
|
|
|
if (!bgm.noResumeStop)
|
|
|
|
bgm.stream.play();
|
|
|
|
|
2013-11-30 11:00:11 +00:00
|
|
|
meWatch.state = MeNotPlaying;
|
|
|
|
}
|
|
|
|
|
|
|
|
bgm.unlockStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
me.unlockStream();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case BgmFadingIn :
|
|
|
|
{
|
|
|
|
bgm.lockStream();
|
|
|
|
|
|
|
|
if (bgm.stream.queryState() == ALStream::Stopped)
|
|
|
|
{
|
|
|
|
/* BGM stopped midway fade in. -> MeNotPlaying */
|
|
|
|
bgm.setExtVolume1(1.0);
|
|
|
|
meWatch.state = MeNotPlaying;
|
|
|
|
bgm.unlockStream();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-11-30 11:00:11 +00:00
|
|
|
me.lockStream();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-11-30 11:00:11 +00:00
|
|
|
if (me.stream.queryState() == ALStream::Playing)
|
|
|
|
{
|
|
|
|
/* ME started playing midway BGM fade in. -> FadeOutBGM */
|
|
|
|
bgm.extPaused = true;
|
|
|
|
meWatch.state = BgmFadingOut;
|
|
|
|
me.unlockStream();
|
|
|
|
bgm.unlockStream();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
float vol = bgm.extVolume;
|
|
|
|
vol += fadeInStep;
|
|
|
|
|
|
|
|
if (vol >= 1)
|
|
|
|
{
|
|
|
|
/* BGM fully faded in. -> MeNotPlaying */
|
|
|
|
vol = 1.0;
|
|
|
|
meWatch.state = MeNotPlaying;
|
|
|
|
}
|
|
|
|
|
|
|
|
bgm.setExtVolume1(vol);
|
|
|
|
|
|
|
|
me.unlockStream();
|
|
|
|
bgm.unlockStream();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Delay(AUDIO_SLEEP);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
2013-11-30 11:00:11 +00:00
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-11-30 11:00:11 +00:00
|
|
|
static int meWatchFun(void *self)
|
|
|
|
{
|
|
|
|
static_cast<AudioPrivate*>(self)->meWatchFunInt();
|
|
|
|
|
|
|
|
return 0;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Audio::Audio()
|
|
|
|
: p(new AudioPrivate)
|
2013-11-30 11:00:11 +00:00
|
|
|
{}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
void Audio::bgmPlay(const char *filename,
|
2013-12-04 23:24:18 +00:00
|
|
|
int volume,
|
|
|
|
int pitch
|
|
|
|
#ifdef RGSS3
|
|
|
|
,float pos
|
|
|
|
#endif
|
|
|
|
)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-12-04 23:24:18 +00:00
|
|
|
#ifdef RGSS3
|
|
|
|
p->bgm.play(filename, volume, pitch, pos);
|
|
|
|
#else
|
2013-09-01 14:27:21 +00:00
|
|
|
p->bgm.play(filename, volume, pitch);
|
2013-12-04 23:24:18 +00:00
|
|
|
#endif
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::bgmStop()
|
|
|
|
{
|
|
|
|
p->bgm.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::bgmFade(int time)
|
|
|
|
{
|
2013-11-30 11:00:11 +00:00
|
|
|
p->bgm.fadeOut(time);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Audio::bgsPlay(const char *filename,
|
2013-12-04 23:24:18 +00:00
|
|
|
int volume,
|
|
|
|
int pitch
|
|
|
|
#ifdef RGSS3
|
|
|
|
,float pos
|
|
|
|
#endif
|
|
|
|
)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-12-04 23:24:18 +00:00
|
|
|
#ifdef RGSS3
|
|
|
|
p->bgs.play(filename, volume, pitch, pos);
|
|
|
|
#else
|
2013-09-01 14:27:21 +00:00
|
|
|
p->bgs.play(filename, volume, pitch);
|
2013-12-04 23:24:18 +00:00
|
|
|
#endif
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::bgsStop()
|
|
|
|
{
|
|
|
|
p->bgs.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::bgsFade(int time)
|
|
|
|
{
|
2013-11-30 11:00:11 +00:00
|
|
|
p->bgs.fadeOut(time);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Audio::mePlay(const char *filename,
|
|
|
|
int volume,
|
|
|
|
int pitch)
|
|
|
|
{
|
|
|
|
p->me.play(filename, volume, pitch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::meStop()
|
|
|
|
{
|
|
|
|
p->me.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::meFade(int time)
|
|
|
|
{
|
2013-11-30 11:00:11 +00:00
|
|
|
p->me.fadeOut(time);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Audio::sePlay(const char *filename,
|
|
|
|
int volume,
|
|
|
|
int pitch)
|
|
|
|
{
|
|
|
|
p->se.play(filename, volume, pitch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::seStop()
|
|
|
|
{
|
|
|
|
p->se.stop();
|
|
|
|
}
|
|
|
|
|
2013-12-04 23:24:18 +00:00
|
|
|
#ifdef RGSS3
|
|
|
|
|
|
|
|
void Audio::setupMidi()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
float Audio::bgmPos()
|
|
|
|
{
|
|
|
|
return p->bgm.playingOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
float Audio::bgsPos()
|
|
|
|
{
|
|
|
|
return p->bgs.playingOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
Audio::~Audio() { delete p; }
|