Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 132 lines (111 sloc) 3.141 kb
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
#include "SoundMusic.h"
#include "libs.h" //for clamp
#include "Pi.h"
#include <map>

namespace Sound {

MusicEvent::MusicEvent() : Event() { }

MusicEvent::MusicEvent(Uint32 id) : Event(id) { }

MusicEvent::~MusicEvent() { }

void MusicEvent::Play(const char *fx, const float volume_left, const float volume_right, Op op)
{
Stop();
eid = PlayMusic(fx, volume_left, volume_right, op);
}

MusicPlayer::MusicPlayer() :
m_volume(0.8f),
m_playing(false),
m_eventOnePlaying(false),
m_currentSongName(""),
m_enabled(true)
{

}

MusicPlayer::~MusicPlayer()
{

}

float MusicPlayer::GetVolume() const
{
return m_volume;
}

void MusicPlayer::SetVolume(const float vol)
{
m_volume = Clamp(vol, 0.f, 1.f);
//the other song might be fading out so don't set its volume
if (m_eventOnePlaying && m_eventOne.IsPlaying())
m_eventOne.SetVolume(m_volume);
else if (m_eventTwo.IsPlaying())
m_eventTwo.SetVolume(m_volume);
}

void MusicPlayer::Play(const std::string& name, const bool repeat /* = false */ , const float fadeDelta /* = 1.f */ )
{
if (!m_enabled) return;
Sound::Op op = 0;
if (repeat)
op |= Sound::OP_REPEAT;
if (m_eventOnePlaying) {
m_eventOne.VolumeAnimate(0.f, 0.f, fadeDelta, fadeDelta);
m_eventOne.SetOp(Sound::OP_STOP_AT_TARGET_VOLUME);
m_eventTwo.Play(name.c_str(), 0.f, 0.f, op);
m_eventTwo.VolumeAnimate(m_volume, m_volume, fadeDelta, fadeDelta);
m_eventOnePlaying = false;
} else {
m_eventTwo.VolumeAnimate(0.f, 0.f, fadeDelta, fadeDelta);
m_eventTwo.SetOp(Sound::OP_STOP_AT_TARGET_VOLUME);
m_eventOne.Play(name.c_str(), 0.f, 0.f, op);
m_eventOne.VolumeAnimate(m_volume, m_volume, fadeDelta, fadeDelta);
m_eventOnePlaying = true;
}
m_playing = true;
m_currentSongName = name;
}

void MusicPlayer::Stop()
{
m_eventOne.Stop();
m_eventTwo.Stop();
m_playing = false;
}

void MusicPlayer::FadeOut(const float fadeDelta)
{
if (m_eventOnePlaying) {//2 might be already fading out
m_eventOne.SetOp(Sound::OP_STOP_AT_TARGET_VOLUME);
m_eventOne.VolumeAnimate(0.f, 0.f, fadeDelta, fadeDelta);
} else { // 1 might be already fading out
m_eventTwo.SetOp(Sound::OP_STOP_AT_TARGET_VOLUME);
m_eventTwo.VolumeAnimate(0.f, 0.f, fadeDelta, fadeDelta);
}
}

void MusicPlayer::Update()
{
if (m_playing) { //expecting report
if ((m_eventOnePlaying && !m_eventOne.IsPlaying()) || (!m_eventOnePlaying && !m_eventTwo.IsPlaying())) {
Pi::luaOnSongFinished->Signal();
m_playing = false;
}
}
}

const std::string MusicPlayer::GetCurrentSongName() const
{
return m_currentSongName;
}

const std::vector<std::string> MusicPlayer::GetSongList() const
{
using std::string;
using std::pair;
std::vector<string> songs;
const std::map<string, Sample> samples = Sound::GetSamples();
for (std::map<string, Sample>::const_iterator it = samples.begin();
it != samples.end(); ++it) {
if (it->second.isMusic)
songs.push_back(it->first.c_str());
}

return songs;
}

bool MusicPlayer::IsPlaying() const
{
return (m_eventOne.IsPlaying() || m_eventTwo.IsPlaying());
}

void MusicPlayer::SetEnabled(const bool en)
{
m_enabled = en;
if (!en && IsPlaying()) Stop();
}

} /* namespace sound */
Something went wrong with that request. Please try again.