|
From: Lasse Kärkkäi. <tr...@us...> - 2009-07-21 14:30:24
|
Module: performous
Branch: master
Commit: d339c1ee60a5d23c6c055cd5705cf35063999215
Author: Lasse Karkkainen <tro...@tr...>
Date: Tue Jul 21 17:29:33 2009 +0300
Made Audio use da::mixer and connect itself to it as a stream.
---
game/audio.cc | 6 +++---
game/audio.hh | 4 ++--
libs/libda/include/libda/mixer.hpp | 31 ++++++++++++++++++++-----------
3 files changed, 25 insertions(+), 16 deletions(-)
diff --git a/game/audio.cc b/game/audio.cc
index 8c830e2..82bcd37 100644
--- a/game/audio.cc
+++ b/game/audio.cc
@@ -16,15 +16,15 @@ Audio::Audio():
{}
void Audio::open(std::string const& pdev, std::size_t rate, std::size_t frames) {
- m_playback.reset();
+ m_mixer.reset();
stopMusic();
m_rs = da::settings(pdev)
- .set_callback(boost::ref(*this))
.set_channels(2)
.set_rate(rate)
.set_frames(frames)
.set_debug(std::cerr);
- m_playback.reset(new da::playback(m_rs));
+ m_mixer.reset(new da::mixer(m_rs));
+ m_mixer->add(boost::ref(*this));
}
bool Audio::operator()(da::pcm_data& areas, da::settings const&) {
diff --git a/game/audio.hh b/game/audio.hh
index 76fc877..6301310 100644
--- a/game/audio.hh
+++ b/game/audio.hh
@@ -125,7 +125,7 @@ class Audio {
/** Takes libda devstr and sample rate. Throws if the device fails. **/
void open(std::string const& pdev, std::size_t rate, std::size_t frames);
/// if audio is currently playing
- bool isOpen() const { return m_playback; }
+ bool isOpen() const { return m_mixer; }
/** Play a song beginning at startPos (defaults to 0)
* @param filename the track filename
* @param preview if the song preview is to play
@@ -178,7 +178,7 @@ class Audio {
bool m_need_resync;
Notes const* volatile m_notes;
da::settings m_rs;
- boost::scoped_ptr<da::playback> m_playback;
+ boost::scoped_ptr<da::mixer> m_mixer;
boost::ptr_map<std::string, AudioSample> m_samples;
};
diff --git a/libs/libda/include/libda/mixer.hpp b/libs/libda/include/libda/mixer.hpp
index d502a7e..fa23594 100644
--- a/libs/libda/include/libda/mixer.hpp
+++ b/libs/libda/include/libda/mixer.hpp
@@ -9,6 +9,7 @@ Link with libda when you use this.
#include "audio.hpp"
#include <boost/scoped_ptr.hpp>
+#include <boost/thread/mutex.hpp>
#include <algorithm>
namespace da {
@@ -25,21 +26,28 @@ namespace da {
return shared_reference_wrapper<T>(ptr);
}
- class chain {
+ class chain: boost::noncopyable {
+ typedef std::vector<callback_t> streams_t;
public:
chain() {}
- chain(callback_t const& cb): streams(1, cb) {}
- typedef std::vector<callback_t> streams_t;
- streams_t streams;
+ chain(callback_t const& cb): m_streams(1, cb) {}
+ void add(callback_t const& cb) {
+ boost::mutex::scoped_lock l(m_mutex);
+ m_streams.push_back(cb);
+ }
+ /** Calls every scream in the chain and removes those that return false. **/
bool operator()(pcm_data& data, settings const& s) {
- streams_t::iterator wr = streams.begin();
- for (streams_t::iterator it = wr; it != streams.end(); ++it) {
+ boost::mutex::scoped_lock l(m_mutex);
+ streams_t::iterator wr = m_streams.begin();
+ for (streams_t::iterator it = wr; it != m_streams.end(); ++it) {
if ((*it)(data, s)) *wr++ = *it;
}
- streams.erase(wr);
- return !streams.empty();
+ if (wr != m_streams.end()) m_streams.erase(wr);
+ return !m_streams.empty();
}
private:
+ streams_t m_streams;
+ boost::mutex m_mutex;
};
namespace {
@@ -49,7 +57,7 @@ namespace da {
}
}
- class buffer {
+ class buffer: boost::noncopyable {
public:
explicit buffer(size_t s): m_data(s) {}
sample_t* begin() { return &m_data[0]; }
@@ -58,7 +66,7 @@ namespace da {
std::vector<sample_t> m_data;
};
- class stream {
+ class stream: boost::noncopyable {
public:
bool operator()(pcm_data& data, settings const&) {
sample_t* b = m_buffer->begin();
@@ -79,9 +87,10 @@ namespace da {
public:
mixer(settings s = settings()):
m_chain(zero),
- m_settings(s.set_callback(m_chain)),
+ m_settings(s.set_callback(boost::ref(m_chain))),
m_playback(m_settings)
{}
+ void add(callback_t const& cb) { m_chain.add(cb); }
private:
chain m_chain;
settings m_settings;
|