+ 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);
+
+ while (finished-- > 0)
+ {
+ ALuint buffer;
+
+ alSourceUnqueueBuffers(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<BufferP>::iterator it;
+ for (it = expired_.end() - 1; it >= expired_.begin(); --it)
+ {
+ if ((*it)->clear()) expired_.erase(it);
+ }
+ }
+
+
+ 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<BufferP> queue_;
+ std::vector<BufferP> expired_;
+};