|
From: Lasse Kärkkäi. <tr...@us...> - 2009-07-25 02:50:29
|
Module: performous
Branch: master
Commit: 835b6cc355384926978fde665c1c7f404ab50b47
Author: Lasse Karkkainen <tro...@tr...>
Date: Sat Jul 25 05:41:55 2009 +0300
Add da::accumulator, a helper useful for summing the results of independent chains.
Add .samples() for pcm_data, doing the often needed multiplication channels * frames.
---
libs/libda/include/libda/audio.hpp | 1 +
libs/libda/include/libda/mixer.hpp | 30 ++++++++++++++++++++++++++++--
2 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/libs/libda/include/libda/audio.hpp b/libs/libda/include/libda/audio.hpp
index 8f95601..2fa7187 100644
--- a/libs/libda/include/libda/audio.hpp
+++ b/libs/libda/include/libda/audio.hpp
@@ -63,6 +63,7 @@ namespace da {
};
iter_by_ch begin(std::size_t ch) { return iter_by_ch(rawbuf, ch, channels); }
iter_by_ch end(std::size_t ch) { return iter_by_ch(rawbuf, ch, channels) + frames; }
+ std::size_t samples() const { return channels * frames; }
};
struct settings;
diff --git a/libs/libda/include/libda/mixer.hpp b/libs/libda/include/libda/mixer.hpp
index 3408e3d..2153853 100644
--- a/libs/libda/include/libda/mixer.hpp
+++ b/libs/libda/include/libda/mixer.hpp
@@ -43,7 +43,7 @@ namespace da {
void add(callback_t const& cb) {
streams.push_back(cb);
}
- /** Calls every scream in the chain and removes those that return false. **/
+ /** Calls every stream in the chain and removes those that return false. **/
bool operator()(pcm_data& data) {
streams_t::iterator wr = streams.begin();
for (streams_t::iterator it = wr; it != streams.end(); ++it) {
@@ -63,9 +63,35 @@ namespace da {
}
}
+ class accumulate: boost::noncopyable {
+ public:
+ typedef std::vector<callback_t> streams_t;
+ accumulate() {}
+ void add(callback_t const& cb) {
+ streams.push_back(cb);
+ }
+ /** Calls every stream in the chain and removes those that return false, summing the results. **/
+ bool operator()(pcm_data& data) {
+ std::vector<sample_t> buffer(data.samples());
+ pcm_data accumbuf(&buffer[0], data.frames, data.channels, data.rate);
+ streams_t::iterator wr = streams.begin();
+ for (streams_t::iterator it = wr; it != streams.end(); ++it) {
+ if ((*it)(accumbuf)) *wr++ = *it;
+ for (std::size_t i = 0; i < buffer.size(); ++i) {
+ data.rawbuf[i] += buffer[i];
+ buffer[i] = 0.0f;
+ }
+ }
+ if (wr != streams.end()) streams.erase(wr, streams.end());
+ return !streams.empty();
+ }
+ streams_t streams;
+ private:
+ };
+
class buffer: boost::noncopyable {
public:
- explicit buffer(size_t s): m_data(s) {}
+ explicit buffer(std::size_t s): m_data(s) {}
sample_t* begin() { return &m_data[0]; }
sample_t* end() { return begin() + m_data.size(); }
private:
|