X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2FMoof%2FSound.cc;h=43714fe04872a93e968c0e44d0c7724f78b2d966;hb=5250c138b1a692e4e893a8f424d2856e519fd652;hp=368e9388bca4aee4f3665ae40b1abf1afa71f6cd;hpb=5fa5f117f28922a7e539a432367960c1a61f837d;p=chaz%2Fyoink diff --git a/src/Moof/Sound.cc b/src/Moof/Sound.cc index 368e938..43714fe 100644 --- a/src/Moof/Sound.cc +++ b/src/Moof/Sound.cc @@ -26,17 +26,21 @@ *******************************************************************************/ +#include #include +#include +#include -#include -#include #include +#include +#include #include "Log.hh" #include "Mippleton.hh" #include "Sound.hh" -#define BUFFER_SIZE (8 * 4096) +#define BUFFER_SIZE (64 * 1024) +//#define BUFFER_SIZE (5*2048) namespace Mf { @@ -44,131 +48,237 @@ namespace Mf { struct Sound::Impl { - static ALenum getAudioFormat(const Sound_AudioInfo& audioInfo) + static ALenum getAudioFormat(const vorbis_info* audioInfo) { - if (audioInfo.format == AUDIO_U8 || audioInfo.format == AUDIO_S8) - { - if (audioInfo.channels == 1) return AL_FORMAT_MONO8; - else return AL_FORMAT_STEREO8; - } - else - { - if (audioInfo.channels == 1) return AL_FORMAT_MONO16; - else return AL_FORMAT_STEREO16; - } + if (audioInfo->channels == 1) return AL_FORMAT_MONO16; + else return AL_FORMAT_STEREO16; } - struct Buffer : public Mippleton + class Buffer; + typedef boost::shared_ptr BufferP; + + class Buffer : public Mippleton { + FILE* soundFile; + OggVorbis_File oggStream; + ALenum audioFormat; + ALsizei audioFreq; + std::vector objects; + + public: + Buffer(const std::string& name) : - Mippleton(name) + Mippleton(name), + soundFile(0) { - objects[0] = 0; - objects[1] = 0; + openFile(); } ~Buffer() { - alDeleteBuffers(2, objects); + while (!objects.empty()) + { + alDeleteBuffers(1, &objects.back()); + objects.pop_back(); + } - if (sound) Sound_FreeSample(sound); + if (soundFile) + { + ov_clear(&oggStream); + } } - void loadFromFile(const std::string& filePath, bool stream) + void openFile() { - if (objects[0] != 0) return; + if (soundFile) + { + ov_clear(&oggStream); + soundFile = 0; + } - sound = Sound_NewSampleFromFile(filePath.c_str(), - NULL, BUFFER_SIZE); + //soundFile = Sound_NewSampleFromFile(soundFile::getPath(getName()).c_str(), + //0, BUFFER_SIZE); + soundFile = fopen(Sound::getPath(getName()).c_str(), "rb"); - if (!sound) + if (!soundFile) { - logWarning("audio not found: %s", getName().c_str()); + logWarning("error while loading sound %s", getName().c_str()); throw Exception(Exception::FILE_NOT_FOUND); } - if (!stream) - { - unsigned decoded = Sound_DecodeAll(sound); - if (decoded == 0) + int result = ov_open(soundFile, &oggStream, NULL, 0); + + if (result < 0) { - logWarning("decoded not bytes from %s", getName().c_str()); - throw Exception(Exception::FILE_NOT_FOUND); + fclose(soundFile); + soundFile = 0; + + logWarning("error while loading oggvorbis stream %s", + getName().c_str()); + throw Exception(Exception::BAD_AUDIO_FORMAT); } - alGenBuffers(2, objects); - alBufferData(objects[0], getAudioFormat(sound->actual), sound->buffer, - sound->buffer_size, sound->actual.rate); - logDebug("buffer size: %d", sound->buffer_size); - logDebug(" channels: %d", sound->actual.channels); - logDebug(" format: %d", sound->actual.format); - logDebug(" frequency: %d", sound->actual.rate); + vorbis_info* vorbisInfo = ov_info(&oggStream, -1); + 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; - Sound_FreeSample(sound); - sound = 0; + char data[BUFFER_SIZE]; + int size = 0; + + //unsigned decoded = Sound_DecodeAll(soundFile); + for (;;) + { + int section; + int result = ov_read(&oggStream, data + size, + BUFFER_SIZE - size, 0, 2, 1, §ion); + + if (result > 0) + { + size += result; + } + else + { + if (result < 0) logWarning("vorbis playback error"); + break; + } } - else + if (size == 0) { - logDebug("buffer size: %d", sound->buffer_size); - logDebug(" channels: %d", sound->actual.channels); - logDebug(" format: %d", sound->actual.format); - logDebug(" frequency: %d", sound->actual.rate); - alGenBuffers(2, objects); + logWarning("decoded no bytes from %s", getName().c_str()); + //throw Exception(Exception::FILE_NOT_FOUND); + return; } + + ALuint obj; + alGenBuffers(1, &obj); + + alBufferData(obj, audioFormat, data, size, audioFreq); + + objects.push_back(obj); + + alSourcei(source, AL_BUFFER, obj); + + // don't need this anymore + ov_clear(&oggStream); + soundFile = 0; } - bool stream(ALuint buffer) + + void beginStream(ALuint source, int nBuffers = 4) { - int bytes = Sound_Decode(sound); + if (!soundFile) openFile(); + if (!soundFile) return; + + ALuint objs[nBuffers]; + alGenBuffers(nBuffers, objs); - if (bytes < BUFFER_SIZE) return false; + for (int i = 0; i < nBuffers; ++i) + { + objects.push_back(objs[i]); + stream(objs[i]); + } - alBufferData(buffer, getAudioFormat(sound->actual), sound->buffer, - sound->buffer_size, sound->actual.rate); - return false; + alSourceQueueBuffers(source, nBuffers, objs); } - Sound_Sample* sound; - ALuint objects[2]; + enum StreamStatus + { + STREAM_OK = 0, + STREAM_EOF = 1, + STREAM_WRONG = 2 + }; - //ALfloat location[] = {0.0f, 0.0f, 0.0f}; - //ALfloat location2[] = {0.0f, 0.0f, 0.0f}; - //ALfloat orient[] = {0.0f, 0.0f, -1.0f, 0.0, 1.0, 0.0}; + 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; - //alListenerfv(AL_POSITION, location); - //alListenerfv(AL_VELOCITY, location); - //alListenerfv(AL_VELOCITY, orient); - }; + char data[BUFFER_SIZE]; + int size = 0; - Impl(const std::string& name, bool stream = false) : - buffer_(Buffer::getInstance(name)) - { - if (!stream) buffer_->loadFromFile(Sound::getPath(name), stream); - else buffer_->loadFromFile(SoundStream::getPath(name), stream); + //unsigned bytes = Sound_Decode(soundFile); + while (size < BUFFER_SIZE) + { + int section; + int result = ov_read(&oggStream, data + size, + BUFFER_SIZE - size, 0, 2, 1, §ion); + + if (result > 0) + { + size += result; + } + else + { + if (result < 0) logWarning("vorbis playback error"); + break; + } + } - ALfloat location[] = {0.0f, 0.0f, 0.0f}; - - alGenSources(1, &source_); + if (size == 0) return STREAM_EOF; - alSourcef(source_, AL_PITCH, 1.0f); - alSourcef(source_, AL_GAIN, 1.0f); - alSourcefv(source_, AL_POSITION, location); - alSourcefv(source_, AL_VELOCITY, location); - alSourcei(source_, AL_LOOPING, AL_FALSE); + alBufferData(buffer, audioFormat, data, size, audioFreq); - if (!stream) + return STREAM_OK; + } + + inline void rewind() { - alSourcei(source_, AL_BUFFER, buffer_->objects[0]); + if (!soundFile) openFile(); + else ov_raw_seek(&oggStream, 0); } - else + + + // delete unused buffers, return true if all buffers deleted + inline bool clear() { - buffer_->stream(buffer_->objects[0]); - buffer_->stream(buffer_->objects[1]); + // clear any openal errors + alGetError(); + + while (!objects.empty()) + { + ALuint buffer = objects.back(); + alDeleteBuffers(1, &buffer); - alSourceQueueBuffers(source_, 2, buffer_->objects); + // 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; + + objects.pop_back(); + } + + return true; } + }; + + + Impl(const std::string& name) : + buffer_(Buffer::getInstance(name)), + playing_(false), + looping_(false) + { + ALfloat zero[] = {0.0f, 0.0f, 0.0f}; + + alGenSources(1, &source_); + + alSourcef(source_, AL_PITCH, 1.0f); + alSourcef(source_, AL_GAIN, 1.0f); + alSourcefv(source_, AL_POSITION, zero); + alSourcefv(source_, AL_VELOCITY, zero); } ~Impl() @@ -177,9 +287,39 @@ struct Sound::Impl } - void update() + void play() + { + ALenum type; + alGetSourcei(source_, AL_SOURCE_TYPE, &type); + + if (type != AL_STATIC) + { + buffer_->loadAll(source_); + } + + alSourcei(source_, AL_LOOPING, looping_); + alSourcePlay(source_); + playing_ = true; + } + + + void stream() { - int finished = 0; + ALenum type; + alGetSourcei(source_, AL_SOURCE_TYPE, &type); + + alSourcei(source_, AL_BUFFER, AL_NONE); + buffer_->rewind(); + buffer_->beginStream(source_); + + alSourcei(source_, AL_LOOPING, AL_FALSE); + alSourcePlay(source_); + playing_ = true; + } + + inline void update() + { + ALint finished = 0; alGetSourcei(source_, AL_BUFFERS_PROCESSED, &finished); @@ -188,17 +328,141 @@ struct Sound::Impl ALuint buffer; alSourceUnqueueBuffers(source_, 1, &buffer); - buffer_->stream(buffer); - alSourceQueueBuffers(source_, 1, &buffer); + + Buffer::StreamStatus status = buffer_->stream(buffer); + + if (status == Buffer::STREAM_OK) + { + alSourceQueueBuffers(source_, 1, &buffer); + } + else if (status == Buffer::STREAM_EOF) + { + if (!queue_.empty()) + { + // begin the next buffer in the queue + expired_.push_back(buffer_); + buffer_ = queue_.front(); + queue_.pop(); + buffer_->beginStream(source_, 1); + } + else if (looping_) + { + // restart from the beginning + buffer_->rewind(); + buffer_->stream(buffer); + alSourceQueueBuffers(source_, 1, &buffer); + } + } + else if (status == Buffer::STREAM_WRONG) + { + clear(); + buffer_->beginStream(source_, 1); + } + } + + ALenum state; + alGetSourcei(source_, 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) + { + 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); + } + } - boost::shared_ptr buffer_; - ALuint source_; - bool playing; -}; + void stop() + { + alSourceStop(source_); + playing_ = false; + } + + inline void pause() + { + alSourcePause(source_); + playing_ = false; + } + + inline void resume() + { + alSourcePlay(source_); + playing_ = true; + } + + + inline void setSample(const std::string& name) + { + bool playing = isPlaying(); + ALenum type; + alGetSourcei(source_, AL_SOURCE_TYPE, &type); + + stop(); + + //alSourcei(source_, AL_BUFFER, AL_NONE); + buffer_ = Buffer::getInstance(name); + + if (type == AL_STREAMING) + { + if (playing) stream(); + } + else + { + if (playing) play(); + } + } + + inline void enqueue(const std::string& name) + { + BufferP buffer = Buffer::getInstance(name); + queue_.push(buffer); + } + + + inline bool isPlaying() const + { + if (playing_) return true; + + ALenum state; + alGetSourcei(source_, AL_SOURCE_STATE, &state); + + return state == AL_PLAYING; + } + + + inline void setLooping(bool looping) + { + looping_ = looping; + + ALenum type; + alGetSourcei(source_, AL_SOURCE_TYPE, &type); + + if (type != AL_STREAMING) + { + alSourcei(source_, AL_LOOPING, looping_); + } + } + + + ALuint source_; + BufferP buffer_; + + bool playing_; + bool looping_; + + std::queue queue_; + std::vector expired_; +}; Sound::Sound(const std::string& name) : // pass through @@ -207,57 +471,100 @@ Sound::Sound(const std::string& name) : void Sound::play() { - //alSourceRewind(impl_->source_); - alSourcePlay(impl_->source_); - impl_->playing = true; + // pass through + impl_->play(); } -void Sound::pause() + +void Sound::stream() { - alSourcePause(impl_->source_); - impl_->playing = false; + // pass through + impl_->stream(); } -void Sound::togglePlayPause() +void Sound::update(Scalar t, Scalar dt) { - if (impl_->playing) pause(); - else play(); + // pass through + impl_->update(); } -void Sound::setGain(Scalar gain) + +void Sound::stop() +{ + // pass through + impl_->stop(); +} + +void Sound::pause() { - alSourcef(impl_->source_, AL_GAIN, gain); + // pass through + impl_->pause(); } +void Sound::resume() +{ + // pass through + impl_->resume(); +} -std::string Sound::getPath(const std::string& name) +void Sound::toggle() { - std::string path = Resource::getPath("sounds/" + name + ".ogg"); - return path; + if (impl_->playing_) pause(); + else resume(); } -//############################################################################## +void Sound::setSample(const std::string& name) +{ + // pass through + impl_->setSample(name); +} +void Sound::enqueue(const std::string& name) +{ + // pass through + impl_->enqueue(name); +} -SoundStream::SoundStream(const std::string& name) + +bool Sound::isPlaying() const +{ // pass through - //impl_(name, true) {} + return impl_->isPlaying(); +} + +void Sound::setPosition(Vector3 position) +{ + float p[3] = {position[0], position[1], position[2]}; + alSourcefv(impl_->source_, AL_POSITION, p); +} + +void Sound::setVelocity(Vector3 velocity) { - impl_ = boost::shared_ptr(new Sound::Impl(name, true)); + float v[3] = {velocity[0], velocity[1], velocity[2]}; + alSourcefv(impl_->source_, AL_VELOCITY, v); } +void Sound::setGain(Scalar gain) +{ + alSourcef(impl_->source_, AL_GAIN, float(gain)); +} -void SoundStream::update(Scalar t, Scalar dt) +void Sound::setPitch(Scalar pitch) +{ + alSourcef(impl_->source_, AL_PITCH, float(pitch)); +} + +void Sound::setLooping(bool looping) { // pass through - impl_->update(); + impl_->setLooping(looping); } -std::string SoundStream::getPath(const std::string& name) +std::string Sound::getPath(const std::string& name) { - std::string path = Resource::getPath("sounds/" + name + ".xm"); + std::string path = Resource::getPath("sounds/" + name + ".ogg"); return path; }