X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Fsound.cc;h=2c19d265b721f0b9d0e01a94f0bd93007157f948;hp=a6fbf0310679faacc76175aa9d67b71079b1e9e5;hb=6f1b787a10d8ab1a3117a4b8c004dd2d90599608;hpb=382626aad0a683ed8642a6a807eea743db45f7f8 diff --git a/src/moof/sound.cc b/src/moof/sound.cc index a6fbf03..2c19d26 100644 --- a/src/moof/sound.cc +++ b/src/moof/sound.cc @@ -13,7 +13,6 @@ #include #include #include -#include #include #include @@ -25,22 +24,24 @@ #include "hash.hh" #include "log.hh" -#include "manager.hh" #include "sound.hh" #include "resource.hh" #include "timer.hh" #ifndef BUF_SIZE -#define BUF_SIZE (4096) +#define BUF_SIZE (4096 * 64) #endif -#define NUM_BUFFERS (16) +#define NUM_BUFFERS (4) namespace moof { +/*] Sound backend + *************************************************************************/ + class sound_backend { public: @@ -87,7 +88,6 @@ public: alcMakeContextCurrent(0); alcDestroyContext(al_context); alcCloseDevice(al_device); - log_info("unloaded sound device ALSA"); } } @@ -105,27 +105,11 @@ ALCcontext* sound_backend::al_context; class sound_resource; typedef resource_handle sound_handle; +MOOF_REGISTER_RESOURCE(sound_resource, ogg, sounds); -class sound_resource_loader -{ -public: - - sound_resource_loader() - { - resource::register_type("ogg"); - } - - ~sound_resource_loader() - { - resource::unregister_type("ogg"); - } -}; - -static sound_resource_loader loader; - - -// SOUND BUFFER +/*] Sound buffer + *************************************************************************/ class buffer { @@ -146,7 +130,6 @@ public: alBufferData(buffer_, format, data, size, freq); retain_counts_[buffer_] = 1; - log_warning("ctor buffer:", buffer_); } buffer(const buffer& buf) @@ -174,7 +157,6 @@ public: { alSourceQueueBuffers(source, 1, &buffer_); retain(); - log_warning("queued buffer:", buffer_); } } @@ -182,13 +164,11 @@ public: { ALuint buf = (ALuint)-1; alSourceUnqueueBuffers(source, 1, &buf); - log_warning("unqueued buffer:", buf); return buffer(buf); } void set(ALuint source) const { - log_warning("set buffer:", buffer_); if (*this) alSourcei(source, AL_BUFFER, buffer_); } @@ -217,7 +197,6 @@ private: { alDeleteBuffers(1, &buffer_); retain_counts_.erase(it); - log_warning("kill buffer:", buffer_); } } @@ -232,7 +211,8 @@ buffer::retcount_lookup buffer::retain_counts_; -// SOUND RESOURCE +/*] Sound resource + *************************************************************************/ class sound_resource : public boost::noncopyable { @@ -240,7 +220,6 @@ public: sound_resource(const std::string& path) { - log_info("audio path is", path); if (ov_fopen((char*)path.c_str(), &file_) < 0) { throw std::runtime_error("problem reading audio: " + path); @@ -284,14 +263,10 @@ public: } else if (result == 0 && size > 0) { - log_info("loaded", size, "bytes from vorbis"); vorbis_info* info = ov_info(&file_, section); buffer_ = buffer(data, size, get_audio_format(info), info->rate); buf = buffer_; - log_info("this section is", section); - log_info("audio format is", get_audio_format(info)); - log_info("audio freq is", info->rate); return true; } else @@ -320,14 +295,9 @@ public: if (result > 0) { - log_info("loaded", result, "bytes from vorbis"); vorbis_info* info = ov_info(&file_, section); buf = buffer(data, result, get_audio_format(info), info->rate); sample = ov_pcm_tell(&file_); - log_info("this section is", section); - log_info("next sample is", sample); - log_info("audio format is", get_audio_format(info)); - log_info("audio freq is", info->rate); return true; } @@ -350,6 +320,8 @@ private: }; +/*] Sound class + *************************************************************************/ class sound::impl { @@ -360,11 +332,10 @@ public: init(); } - impl(const std::string& path) + impl(const std::string& name) { - log_info("sound::impl constructor"); init(); - enqueue(path); + enqueue(name); } void init() @@ -375,14 +346,12 @@ public: sample_ = 0; alGenSources(1, &source_); - log_error("alGenSources:", alGetError()); ALfloat zero[] = {0.0f, 0.0f, 0.0f}; alSourcef(source_, AL_PITCH, 1.0f); alSourcef(source_, AL_GAIN, 1.0f); alSourcefv(source_, AL_POSITION, zero); alSourcefv(source_, AL_VELOCITY, zero); - log_error("init:", alGetError()); } ~impl() @@ -401,7 +370,6 @@ public: if (handle->read(buf)) { - log_info("playing source..."); buf.set(source_); alSourcei(source_, AL_LOOPING, is_looping_); alSourcePlay(source_); @@ -415,7 +383,6 @@ public: if (!is_playing_) { alSourcei(source_, AL_LOOPING, false); - log_error("set not looping:", alGetError()); sound_handle handle = queue_.front(); @@ -434,19 +401,16 @@ public: ALint queued = 0; alGetSourcei(source_, AL_BUFFERS_QUEUED, &queued); - log_info("buffers queued:", queued); } } if (!stream_timer_.is_valid()) { stream_timer_.init(boost::bind(&impl::stream_update, this, _1, _2), - 0.01, timer::repeat); + SCALAR(0.5), timer::repeat); } - log_info("streaming source..."); alSourcePlay(source_); - log_error("playing:", alGetError()); is_playing_ = true; } @@ -487,13 +451,10 @@ public: // begin the next buffer in the queue handle->read(buf, sample_); buf.queue(source_); - log_info("loading new buffer"); } else if (is_looping_) { // reload the same buffer - log_info("looping same buffer"); - queue_.push_back(handle); handle->read(buf, sample_); buf.queue(source_); @@ -544,19 +505,19 @@ public: } - void sample(const std::string& path) + void sample(const std::string& name) { stop(); alSourcei(source_, AL_BUFFER, AL_NONE); queue_.clear(); - enqueue(path); + enqueue(name); } - void enqueue(const std::string& path) + void enqueue(const std::string& name) { - sound_handle handle = resource::load(path); + sound_handle handle = resource::load(name, "ogg"); queue_.push_back(handle); } @@ -613,10 +574,7 @@ sound::sound() : sound::sound(const std::string& path) : // pass through - impl_(new sound::impl(path)) -{ - log_info("sound constructor"); -} + impl_(new sound::impl(path)) {} void sound::sample(const std::string& path) @@ -633,12 +591,6 @@ void sound::enqueue(const std::string& path) void sound::play() -{ - // pass through - impl_->play(); -} - -void sound::stream() { // pass through impl_->stream(); @@ -667,7 +619,6 @@ void sound::toggle() { if (is_playing()) pause(); else play(); - // TODO: what about streaming sources? } bool sound::is_playing() const