X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FSound.cc;h=4c60e328085f60d7d282c2e70144079c49c5dbc2;hp=43714fe04872a93e968c0e44d0c7724f78b2d966;hb=c9e20ac06383b20ceb5404c9237e319c2e90d157;hpb=5250c138b1a692e4e893a8f424d2856e519fd652 diff --git a/src/Moof/Sound.cc b/src/Moof/Sound.cc index 43714fe..4c60e32 100644 --- a/src/Moof/Sound.cc +++ b/src/Moof/Sound.cc @@ -27,17 +27,19 @@ *******************************************************************************/ #include +#include #include -#include #include #include #include #include +#include "Exception.hh" +#include "Library.hh" #include "Log.hh" -#include "Mippleton.hh" #include "Sound.hh" +#include "Timer.hh" #define BUFFER_SIZE (64 * 1024) //#define BUFFER_SIZE (5*2048) @@ -45,8 +47,9 @@ namespace Mf { -struct Sound::Impl +class Sound::Impl { +public: static ALenum getAudioFormat(const vorbis_info* audioInfo) { @@ -54,76 +57,54 @@ struct Sound::Impl else return AL_FORMAT_STEREO16; } + class Buffer; typedef boost::shared_ptr BufferP; - class Buffer : public Mippleton + class Buffer : public Library { - FILE* soundFile; - OggVorbis_File oggStream; - ALenum audioFormat; - ALsizei audioFreq; - std::vector objects; - public: Buffer(const std::string& name) : - Mippleton(name), - soundFile(0) + Library(name), + mBuffer(-1) { + mOggStream.datasource = 0; openFile(); } ~Buffer() { - while (!objects.empty()) + if (mOggStream.datasource) { - alDeleteBuffers(1, &objects.back()); - objects.pop_back(); - } - - if (soundFile) - { - ov_clear(&oggStream); + ov_clear(&mOggStream); } + if (int(mBuffer) != -1) alDeleteBuffers(1, &mBuffer); } void openFile() { - if (soundFile) + if (mOggStream.datasource) { - ov_clear(&oggStream); - soundFile = 0; + ov_clear(&mOggStream); + mOggStream.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(), &mOggStream); 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); + throw Exception(ErrorCode::UNKNOWN_AUDIO_FORMAT); } - vorbis_info* vorbisInfo = ov_info(&oggStream, -1); - audioFormat = getAudioFormat(vorbisInfo); - audioFreq = vorbisInfo->rate; + vorbis_info* vorbisInfo = ov_info(&mOggStream, -1); + mFormat = getAudioFormat(vorbisInfo); + mFreq = vorbisInfo->rate; - logDebug(" version: %d", vorbisInfo->version); logDebug(" channels: %d", vorbisInfo->channels); logDebug(" frequency: %d", vorbisInfo->rate); } @@ -131,17 +112,16 @@ struct Sound::Impl void loadAll(ALuint source) { - if (!soundFile) openFile(); - if (!soundFile) return; + if (!mOggStream.datasource) openFile(); + if (!mOggStream.datasource) return; char data[BUFFER_SIZE]; int size = 0; - //unsigned decoded = Sound_DecodeAll(soundFile); for (;;) { int section; - int result = ov_read(&oggStream, data + size, + int result = ov_read(&mOggStream, data + size, BUFFER_SIZE - size, 0, 2, 1, §ion); if (result > 0) @@ -157,65 +137,29 @@ struct Sound::Impl if (size == 0) { logWarning("decoded no bytes from %s", getName().c_str()); - //throw Exception(Exception::FILE_NOT_FOUND); + //throw Exception("file_not_found"); return; } - ALuint obj; - alGenBuffers(1, &obj); + alGenBuffers(1, &mBuffer); - alBufferData(obj, audioFormat, data, size, audioFreq); + alBufferData(mBuffer, mFormat, data, size, mFreq); + alSourcei(source, AL_BUFFER, mBuffer); - objects.push_back(obj); - - alSourcei(source, AL_BUFFER, obj); - - // don't need this anymore - ov_clear(&oggStream); - soundFile = 0; + // don't need to keep this loaded + ov_clear(&mOggStream); + mOggStream.datasource = 0; } - - void beginStream(ALuint source, int nBuffers = 4) + bool stream(ALuint buffer) { - if (!soundFile) openFile(); - if (!soundFile) return; - - ALuint objs[nBuffers]; - alGenBuffers(nBuffers, objs); - - for (int i = 0; i < nBuffers; ++i) - { - objects.push_back(objs[i]); - stream(objs[i]); - } - - alSourceQueueBuffers(source, nBuffers, objs); - } - - enum StreamStatus - { - STREAM_OK = 0, - STREAM_EOF = 1, - STREAM_WRONG = 2 - }; - - StreamStatus stream(ALuint buffer) - { - std::vector::iterator it = - std::find(objects.begin(), objects.end(), buffer); - - // that buffer doesn't belong to us - if (it == objects.end()) return STREAM_WRONG; - char data[BUFFER_SIZE]; int size = 0; - //unsigned bytes = Sound_Decode(soundFile); while (size < BUFFER_SIZE) { int section; - int result = ov_read(&oggStream, data + size, + int result = ov_read(&mOggStream, data + size, BUFFER_SIZE - size, 0, 2, 1, §ion); if (result > 0) @@ -229,287 +173,322 @@ struct Sound::Impl } } - if (size == 0) return STREAM_EOF; + if (size == 0) return false; - alBufferData(buffer, audioFormat, data, size, audioFreq); + alBufferData(buffer, mFormat, data, size, mFreq); - return STREAM_OK; + return true; } - inline void rewind() + void rewind() { - if (!soundFile) openFile(); - else ov_raw_seek(&oggStream, 0); + if (!mOggStream.datasource) openFile(); + else ov_raw_seek(&mOggStream, 0); } - // delete unused buffers, return true if all buffers deleted - inline bool clear() - { - // clear any openal errors - alGetError(); - - while (!objects.empty()) - { - ALuint buffer = objects.back(); - alDeleteBuffers(1, &buffer); + private: - // if an error occured, the buffer was not deleted because it's - // still in use by some source - if (alGetError() != AL_NO_ERROR) return false; + OggVorbis_File mOggStream; + ALenum mFormat; + ALsizei mFreq; + ALuint mBuffer; + }; - objects.pop_back(); - } - return true; - } - }; + Impl() + { + init(); + } + Impl(const std::string& name) + { + init(); + enqueue(name); + } - Impl(const std::string& name) : - buffer_(Buffer::getInstance(name)), - playing_(false), - looping_(false) + void init() { ALfloat zero[] = {0.0f, 0.0f, 0.0f}; - alGenSources(1, &source_); + alGenSources(1, &mSource); - alSourcef(source_, AL_PITCH, 1.0f); - alSourcef(source_, AL_GAIN, 1.0f); - alSourcefv(source_, AL_POSITION, zero); - alSourcefv(source_, AL_VELOCITY, zero); + alSourcef(mSource, AL_PITCH, 1.0f); + alSourcef(mSource, AL_GAIN, 1.0f); + alSourcefv(mSource, AL_POSITION, zero); + alSourcefv(mSource, AL_VELOCITY, zero); + + mIsPlaying = false; + mIsLooping = false; } ~Impl() { - alDeleteSources(1, &source_); + stop(); + + alDeleteSources(1, &mSource); + + while (!mBufferObjects.empty()) + { + alDeleteBuffers(1, &mBufferObjects.back()); + mBufferObjects.pop_back(); + } } void play() { + if (mQueue.empty()) return; + ALenum type; - alGetSourcei(source_, AL_SOURCE_TYPE, &type); + alGetSourcei(mSource, AL_SOURCE_TYPE, &type); if (type != AL_STATIC) { - buffer_->loadAll(source_); + mQueue.front()->loadAll(mSource); } - alSourcei(source_, AL_LOOPING, looping_); - alSourcePlay(source_); - playing_ = true; + alSourcei(mSource, AL_LOOPING, mIsLooping); + alSourcePlay(mSource); + mIsPlaying = true; } void stream() { - ALenum type; - alGetSourcei(source_, AL_SOURCE_TYPE, &type); + stop(); - alSourcei(source_, AL_BUFFER, AL_NONE); - buffer_->rewind(); - buffer_->beginStream(source_); + alSourcei(mSource, AL_BUFFER, AL_NONE); + mQueue.front()->rewind(); + beginStream(); - alSourcei(source_, AL_LOOPING, AL_FALSE); - alSourcePlay(source_); - playing_ = true; + alSourcei(mSource, AL_LOOPING, AL_FALSE); + alSourcePlay(mSource); + mIsPlaying = true; + + mStreamTimer.init(boost::bind(&Impl::streamUpdate, this, _1, _2), 1.0, + Timer::REPEAT); } - inline void update() + void beginStream() + { + ALuint buffer; + for (int i = mBufferObjects.size(); i < 8; ++i) + { + alGenBuffers(1, &buffer); + mBufferObjects.push_back(buffer); + } + for (int i = 0; i < 8; ++i) + { + buffer = mBufferObjects[i]; + mQueue.front()->stream(buffer); + alSourceQueueBuffers(mSource, 1, &buffer); + } + } + + + void update() { ALint finished = 0; - alGetSourcei(source_, AL_BUFFERS_PROCESSED, &finished); + alGetSourcei(mSource, AL_BUFFERS_PROCESSED, &finished); while (finished-- > 0) { - ALuint buffer; - - alSourceUnqueueBuffers(source_, 1, &buffer); + ALuint bufferObj; + alSourceUnqueueBuffers(mSource, 1, &bufferObj); - Buffer::StreamStatus status = buffer_->stream(buffer); + BufferP buffer = mQueue.front(); + bool streamed = buffer->stream(bufferObj); - if (status == Buffer::STREAM_OK) + if (streamed) { - alSourceQueueBuffers(source_, 1, &buffer); + alSourceQueueBuffers(mSource, 1, &bufferObj); } - else if (status == Buffer::STREAM_EOF) + else { - if (!queue_.empty()) + // the buffer couldn't be streamed, so get rid of it + mQueue.pop_front(); + + if (!mQueue.empty()) { // begin the next buffer in the queue - expired_.push_back(buffer_); - buffer_ = queue_.front(); - queue_.pop(); - buffer_->beginStream(source_, 1); + mQueue.front()->rewind(); + mQueue.front()->stream(bufferObj); + alSourceQueueBuffers(mSource, 1, &bufferObj); + logInfo("loading new buffer"); } - else if (looping_) + else if (mIsLooping) { - // restart from the beginning - buffer_->rewind(); - buffer_->stream(buffer); - alSourceQueueBuffers(source_, 1, &buffer); + // reload the same buffer + mQueue.push_back(buffer); + buffer->rewind(); + buffer->stream(bufferObj); + alSourceQueueBuffers(mSource, 1, &bufferObj); + logInfo("looping same buffer"); } } - else if (status == Buffer::STREAM_WRONG) - { - clear(); - buffer_->beginStream(source_, 1); - } } ALenum state; - alGetSourcei(source_, AL_SOURCE_STATE, &state); + alGetSourcei(mSource, AL_SOURCE_STATE, &state); // restart playing if we're stopped but supposed to be playing... this - // means we didn't queue enough and the audio skipped - if (playing_ && state != AL_PLAYING) + // means we didn't queue enough and the audio skipped :-( + if (mIsPlaying && state != AL_PLAYING) { - alSourcePlay(source_); - } - } - - inline void clear() - { - // try to remove expired buffers - std::vector::iterator it; - for (it = expired_.end() - 1; it >= expired_.begin(); --it) - { - if ((*it)->clear()) expired_.erase(it); + alSourcePlay(mSource); } } void stop() { - alSourceStop(source_); - playing_ = false; + alSourceStop(mSource); + mIsPlaying = false; + + mStreamTimer.invalidate(); } - inline void pause() + void pause() { - alSourcePause(source_); - playing_ = false; + alSourcePause(mSource); + mIsPlaying = false; + + mStreamTimer.invalidate(); } - inline void resume() + void resume() { - alSourcePlay(source_); - playing_ = true; + alSourcePlay(mSource); + mIsPlaying = true; + + ALenum type; + alGetSourcei(mSource, AL_SOURCE_TYPE, &type); + + if (type == AL_STREAMING) + { + mStreamTimer.init(boost::bind(&Impl::streamUpdate, this, _1, _2), + 1.0, Timer::REPEAT); + } } - inline void setSample(const std::string& name) + void setSample(const std::string& name) { bool playing = isPlaying(); ALenum type; - alGetSourcei(source_, AL_SOURCE_TYPE, &type); + alGetSourcei(mSource, AL_SOURCE_TYPE, &type); stop(); + mQueue.clear(); - //alSourcei(source_, AL_BUFFER, AL_NONE); - buffer_ = Buffer::getInstance(name); + //alSourcei(mSource, AL_BUFFER, AL_NONE); + enqueue(name); - if (type == AL_STREAMING) + if (playing) { - if (playing) stream(); - } - else - { - if (playing) play(); + if (type == AL_STREAMING) stream(); + else play(); } } - inline void enqueue(const std::string& name) + void enqueue(const std::string& name) { BufferP buffer = Buffer::getInstance(name); - queue_.push(buffer); + mQueue.push_back(buffer); } - inline bool isPlaying() const + bool isPlaying() const { - if (playing_) return true; + if (mIsPlaying) return true; ALenum state; - alGetSourcei(source_, AL_SOURCE_STATE, &state); + alGetSourcei(mSource, AL_SOURCE_STATE, &state); return state == AL_PLAYING; } - inline void setLooping(bool looping) + void setLooping(bool looping) { - looping_ = looping; + mIsLooping = looping; ALenum type; - alGetSourcei(source_, AL_SOURCE_TYPE, &type); + alGetSourcei(mSource, AL_SOURCE_TYPE, &type); if (type != AL_STREAMING) { - alSourcei(source_, AL_LOOPING, looping_); + alSourcei(mSource, AL_LOOPING, mIsLooping); } } - ALuint source_; - BufferP buffer_; + void streamUpdate(Timer& timer, Scalar t) + { + // don't let the music die! + update(); + // TODO - might be nice to also allow using threads for streaming rather + // than a timer, probably as a compile-time option + } + + + ALuint mSource; + std::vector mBufferObjects; + + bool mIsPlaying; + bool mIsLooping; - bool playing_; - bool looping_; + std::deque mQueue; - std::queue queue_; - std::vector expired_; + Timer mStreamTimer; }; + +Sound::Sound() : + // pass through + mImpl(new Sound::Impl) {} + Sound::Sound(const std::string& name) : // pass through - impl_(new Sound::Impl(name)) {} + mImpl(new Sound::Impl(name)) {} void Sound::play() { // pass through - impl_->play(); + mImpl->play(); } - void Sound::stream() { // pass through - impl_->stream(); -} - -void Sound::update(Scalar t, Scalar dt) -{ - // pass through - impl_->update(); + mImpl->stream(); } void Sound::stop() { // pass through - impl_->stop(); + mImpl->stop(); } void Sound::pause() { // pass through - impl_->pause(); + mImpl->pause(); } void Sound::resume() { // pass through - impl_->resume(); + mImpl->resume(); } void Sound::toggle() { - if (impl_->playing_) pause(); + if (mImpl->mIsPlaying) pause(); else resume(); } @@ -517,48 +496,73 @@ void Sound::toggle() void Sound::setSample(const std::string& name) { // pass through - impl_->setSample(name); + mImpl->setSample(name); } void Sound::enqueue(const std::string& name) { // pass through - impl_->enqueue(name); + mImpl->enqueue(name); } bool Sound::isPlaying() const { // pass through - return impl_->isPlaying(); + return mImpl->isPlaying(); } -void Sound::setPosition(Vector3 position) +void Sound::setPosition(const Vector3& position) { float p[3] = {position[0], position[1], position[2]}; - alSourcefv(impl_->source_, AL_POSITION, p); + alSourcefv(mImpl->mSource, AL_POSITION, p); } -void Sound::setVelocity(Vector3 velocity) +void Sound::setVelocity(const Vector3& velocity) { float v[3] = {velocity[0], velocity[1], velocity[2]}; - alSourcefv(impl_->source_, AL_VELOCITY, v); + alSourcefv(mImpl->mSource, AL_VELOCITY, v); } void Sound::setGain(Scalar gain) { - alSourcef(impl_->source_, AL_GAIN, float(gain)); + alSourcef(mImpl->mSource, AL_GAIN, float(gain)); } void Sound::setPitch(Scalar pitch) { - alSourcef(impl_->source_, AL_PITCH, float(pitch)); + alSourcef(mImpl->mSource, AL_PITCH, float(pitch)); } void Sound::setLooping(bool looping) { // pass through - impl_->setLooping(looping); + mImpl->setLooping(looping); +} + + +void Sound::setListenerPosition(const Vector3& position) +{ + alListener3f(AL_POSITION, float(position[0]), float(position[1]), + float(position[2])); +} + +void Sound::setListenerVelocity(const Vector3& velocity) +{ + alListener3f(AL_VELOCITY, float(velocity[0]), float(velocity[1]), + float(velocity[2])); +} + +void Sound::setListenerOrientation(const Vector3& forward, const Vector3& up) +{ + float vec[6]; + vec[0] = float(forward[0]); + vec[1] = float(forward[1]); + vec[2] = float(forward[2]); + vec[3] = float(up[0]); + vec[4] = float(up[1]); + vec[5] = float(up[2]); + alListenerfv(AL_ORIENTATION, vec); }