#include "Log.hh"
#include "Mippleton.hh"
#include "Sound.hh"
+#include "Timer.hh"
#define BUFFER_SIZE (64 * 1024)
//#define BUFFER_SIZE (5*2048)
class Buffer : public Mippleton<Buffer>
{
- FILE* soundFile;
OggVorbis_File oggStream;
ALenum audioFormat;
ALsizei audioFreq;
public:
Buffer(const std::string& name) :
- Mippleton<Buffer>(name),
- soundFile(0)
+ Mippleton<Buffer>(name)
{
+ oggStream.datasource = 0;
openFile();
}
objects.pop_back();
}
- if (soundFile)
+ if (oggStream.datasource)
{
ov_clear(&oggStream);
}
void openFile()
{
- if (soundFile)
+ if (oggStream.datasource)
{
ov_clear(&oggStream);
- soundFile = 0;
+ oggStream.datasource = 0;
}
- //soundFile = Sound_NewSampleFromFile(soundFile::getPath(getName()).c_str(),
- //0, BUFFER_SIZE);
- soundFile = fopen(Sound::getPath(getName()).c_str(), "rb");
-
- if (!soundFile)
- {
- logWarning("error while loading sound %s", getName().c_str());
- throw Exception(Exception::FILE_NOT_FOUND);
- }
-
- int result = ov_open(soundFile, &oggStream, NULL, 0);
+ std::string filePath = Sound::getPath(getName());
+ int result = ov_fopen((char*)filePath.c_str(), &oggStream);
if (result < 0)
{
- fclose(soundFile);
- soundFile = 0;
-
- logWarning("error while loading oggvorbis stream %s",
+ logWarning("error while loading sound %s",
getName().c_str());
throw Exception(Exception::BAD_AUDIO_FORMAT);
}
audioFormat = getAudioFormat(vorbisInfo);
audioFreq = vorbisInfo->rate;
- logDebug(" version: %d", vorbisInfo->version);
logDebug(" channels: %d", vorbisInfo->channels);
logDebug(" frequency: %d", vorbisInfo->rate);
}
void loadAll(ALuint source)
{
- if (!soundFile) openFile();
- if (!soundFile) return;
+ if (!oggStream.datasource) openFile();
+ if (!oggStream.datasource) return;
char data[BUFFER_SIZE];
int size = 0;
- //unsigned decoded = Sound_DecodeAll(soundFile);
for (;;)
{
int section;
// don't need this anymore
ov_clear(&oggStream);
- soundFile = 0;
+ oggStream.datasource = 0;
}
- void beginStream(ALuint source, int nBuffers = 4)
+ void beginStream(ALuint source, int nBuffers = 8)
{
- if (!soundFile) openFile();
- if (!soundFile) return;
+ if (!oggStream.datasource) openFile();
+ if (!oggStream.datasource) return;
ALuint objs[nBuffers];
alGenBuffers(nBuffers, objs);
char data[BUFFER_SIZE];
int size = 0;
- //unsigned bytes = Sound_Decode(soundFile);
while (size < BUFFER_SIZE)
{
int section;
inline void rewind()
{
- if (!soundFile) openFile();
+ if (!oggStream.datasource) openFile();
else ov_raw_seek(&oggStream, 0);
}
alSourcei(source_, AL_LOOPING, AL_FALSE);
alSourcePlay(source_);
playing_ = true;
+
+ streamTimer.init(boost::bind(&Impl::streamUpdate, this, _1, _2), 1.0,
+ Timer::REPEAT);
}
inline void update()
std::queue<BufferP> queue_;
std::vector<BufferP> expired_;
+
+ Timer streamTimer;
+
+ void streamUpdate(Timer& timer, Scalar t)
+ {
+ // don't let the music die!
+ update();
+ }
};
Sound::Sound(const std::string& name) :
impl_->play();
}
-
void Sound::stream()
{
// pass through
impl_->stream();
}
-void Sound::update(Scalar t, Scalar dt)
-{
- // pass through
- impl_->update();
-}
-
void Sound::stop()
{