2012-10-13 18:29:53 +00:00
|
|
|
#include "Sound.h"
|
|
|
|
#include "AudioManager.h"
|
2013-01-04 23:31:51 +00:00
|
|
|
#include "Log.h"
|
2012-10-13 18:29:53 +00:00
|
|
|
|
|
|
|
|
2013-05-14 19:31:39 +00:00
|
|
|
Sound::Sound(const std::string & path) : mSampleData(NULL), mSamplePos(0), mSampleLength(0), playing(false)
|
|
|
|
{
|
2012-10-13 18:29:53 +00:00
|
|
|
loadFile(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sound::~Sound()
|
|
|
|
{
|
|
|
|
deinit();
|
|
|
|
}
|
|
|
|
|
2013-05-14 19:31:39 +00:00
|
|
|
void Sound::loadFile(const std::string & path)
|
2012-10-13 18:29:53 +00:00
|
|
|
{
|
|
|
|
mPath = path;
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::init()
|
|
|
|
{
|
2013-05-14 19:31:39 +00:00
|
|
|
if(mSampleData != NULL)
|
2012-10-13 18:29:53 +00:00
|
|
|
deinit();
|
|
|
|
|
|
|
|
if(mPath.empty())
|
|
|
|
return;
|
|
|
|
|
2013-05-14 19:31:39 +00:00
|
|
|
//load wav file via SDL
|
|
|
|
SDL_AudioSpec wave;
|
|
|
|
Uint8 * data = NULL;
|
|
|
|
Uint32 dlen = 0;
|
|
|
|
if (SDL_LoadWAV(mPath.c_str(), &wave, &data, &dlen) == NULL) {
|
|
|
|
LOG(LogError) << "Error loading sound \"" << mPath << "\"!\n" << " " << SDL_GetError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//build conversion buffer
|
|
|
|
SDL_AudioCVT cvt;
|
|
|
|
SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 2, 44100);
|
|
|
|
//copy data to conversion buffer
|
|
|
|
cvt.len = dlen;
|
|
|
|
cvt.buf = new Uint8[cvt.len * cvt.len_mult];
|
|
|
|
memcpy(cvt.buf, data, dlen);
|
|
|
|
//convert buffer to stereo, 16bit, 44.1kHz
|
|
|
|
if (SDL_ConvertAudio(&cvt) < 0) {
|
|
|
|
LOG(LogError) << "Error converting sound \"" << mPath << "\" to 44.1kHz, 16bit, stereo format!\n" << " " << SDL_GetError();
|
|
|
|
delete[] cvt.buf;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//worked. set up member data
|
|
|
|
SDL_LockAudio();
|
|
|
|
mSampleData = cvt.buf;
|
|
|
|
mSampleLength = cvt.len_cvt;
|
|
|
|
mSamplePos = 0;
|
|
|
|
mSampleFormat.channels = 2;
|
|
|
|
mSampleFormat.freq = 44100;
|
|
|
|
mSampleFormat.format = AUDIO_S16;
|
|
|
|
SDL_UnlockAudio();
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
2013-05-14 19:31:39 +00:00
|
|
|
//free wav data now
|
|
|
|
SDL_FreeWAV(data);
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::deinit()
|
|
|
|
{
|
2013-05-14 19:31:39 +00:00
|
|
|
playing = false;
|
|
|
|
|
|
|
|
if(mSampleData != NULL)
|
2012-10-13 18:29:53 +00:00
|
|
|
{
|
2013-05-14 19:31:39 +00:00
|
|
|
SDL_LockAudio();
|
|
|
|
delete[] mSampleData;
|
|
|
|
mSampleData = NULL;
|
|
|
|
mSampleLength = 0;
|
|
|
|
mSamplePos = 0;
|
|
|
|
SDL_UnlockAudio();
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::play()
|
|
|
|
{
|
2013-05-14 19:31:39 +00:00
|
|
|
if(mSampleData == NULL)
|
2012-10-13 18:29:53 +00:00
|
|
|
return;
|
|
|
|
|
2013-05-15 08:12:51 +00:00
|
|
|
if (playing)
|
|
|
|
{
|
|
|
|
//replay from start. rewind the sample to the beginning
|
|
|
|
SDL_LockAudio();
|
|
|
|
mSamplePos = 0;
|
|
|
|
SDL_UnlockAudio();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//flag our sample as playing
|
|
|
|
playing = true;
|
|
|
|
}
|
2013-05-14 19:31:39 +00:00
|
|
|
//tell the AudioManager to start playing samples
|
|
|
|
AudioManager::play();
|
|
|
|
}
|
2012-10-13 18:29:53 +00:00
|
|
|
|
2013-05-14 19:31:39 +00:00
|
|
|
bool Sound::isPlaying() const
|
|
|
|
{
|
|
|
|
return playing;
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
2013-05-14 19:31:39 +00:00
|
|
|
void Sound::stop()
|
2012-10-13 18:29:53 +00:00
|
|
|
{
|
2013-05-14 19:31:39 +00:00
|
|
|
//flag our sample as playing and rewind its position
|
2013-05-15 08:12:51 +00:00
|
|
|
SDL_LockAudio();
|
2013-05-14 19:31:39 +00:00
|
|
|
playing = false;
|
|
|
|
mSamplePos = 0;
|
2013-05-15 08:12:51 +00:00
|
|
|
SDL_UnlockAudio();
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
2013-05-14 19:31:39 +00:00
|
|
|
const Uint8 * Sound::getData() const
|
|
|
|
{
|
|
|
|
return mSampleData;
|
|
|
|
}
|
|
|
|
|
|
|
|
Uint32 Sound::getPosition() const
|
|
|
|
{
|
|
|
|
return mSamplePos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::setPosition(Uint32 newPosition)
|
|
|
|
{
|
|
|
|
mSamplePos = newPosition;
|
|
|
|
if (mSamplePos >= mSampleLength) {
|
|
|
|
mSamplePos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Uint32 Sound::getLength() const
|
|
|
|
{
|
|
|
|
return mSampleLength;
|
|
|
|
}
|