summaryrefslogtreecommitdiff
path: root/src/lib/audio_merger.h
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2013-07-27 21:10:45 +0100
committerCarl Hetherington <cth@carlh.net>2013-07-27 21:10:45 +0100
commitbcb746a19d5cc5377eacb33b73b59549a7372487 (patch)
tree119fcf92cdf8e294e44dbf7d8dee240141d2ec00 /src/lib/audio_merger.h
parent2777ab63db3d190a5bc0ea238541558e87ea9b47 (diff)
Adjust AudioMerger to non-signalling API.
Diffstat (limited to 'src/lib/audio_merger.h')
-rw-r--r--src/lib/audio_merger.h63
1 files changed, 34 insertions, 29 deletions
diff --git a/src/lib/audio_merger.h b/src/lib/audio_merger.h
index 126325a9d..afb21871b 100644
--- a/src/lib/audio_merger.h
+++ b/src/lib/audio_merger.h
@@ -18,6 +18,7 @@
*/
#include "audio_buffers.h"
+#include "util.h"
template <class T, class F>
class AudioMerger
@@ -25,47 +26,50 @@ class AudioMerger
public:
AudioMerger (int channels, boost::function<F (T)> t_to_f, boost::function<T (F)> f_to_t)
: _buffers (new AudioBuffers (channels, 0))
- , _next_emission (0)
+ , _next_out (0)
, _t_to_f (t_to_f)
, _f_to_t (f_to_t)
{}
- void push (boost::shared_ptr<const AudioBuffers> audio, T time)
+ TimedAudioBuffers<T>
+ push (boost::shared_ptr<const AudioBuffers> audio, T time)
{
- if (time > _next_emission) {
- /* We can emit some audio from our buffer; this is how many frames
- we are going to emit.
- */
- F const to_emit = _t_to_f (time - _next_emission);
- boost::shared_ptr<AudioBuffers> emit (new AudioBuffers (_buffers->channels(), to_emit));
+ assert (time >= _next_out);
+ TimedAudioBuffers<T> out;
+
+ if (time > _next_out) {
+ /* We can return some audio from our buffer; this is how many frames
+ we are going to return.
+ */
+ F const to_return = _t_to_f (time - _next_out);
+ out.audio.reset (new AudioBuffers (_buffers->channels(), to_return));
/* And this is how many we will get from our buffer */
- F const to_emit_from_buffers = min (to_emit, _buffers->frames ());
-
- /* Copy the data that we have to the back end of `emit' */
- emit->copy_from (_buffers.get(), to_emit_from_buffers, 0, to_emit - to_emit_from_buffers);
+ F const to_return_from_buffers = min (to_return, _buffers->frames ());
+ /* Copy the data that we have to the back end of the return buffer */
+ out.audio->copy_from (_buffers.get(), to_return_from_buffers, 0, to_return - to_return_from_buffers);
/* Silence any gap at the start */
- emit->make_silent (0, to_emit - to_emit_from_buffers);
+ out.audio->make_silent (0, to_return - to_return_from_buffers);
- /* Emit that */
- Audio (emit, _next_emission);
+ out.time = _next_out;
+ _next_out += _f_to_t (to_return);
- _next_emission += _f_to_t (to_emit);
-
- /* And remove the data we've emitted from our buffers */
- if (_buffers->frames() > to_emit_from_buffers) {
- _buffers->move (to_emit_from_buffers, 0, _buffers->frames() - to_emit_from_buffers);
+ /* And remove the data we're returning from our buffers */
+ if (_buffers->frames() > to_return_from_buffers) {
+ _buffers->move (to_return_from_buffers, 0, _buffers->frames() - to_return_from_buffers);
}
- _buffers->set_frames (_buffers->frames() - to_emit_from_buffers);
+ _buffers->set_frames (_buffers->frames() - to_return_from_buffers);
}
/* Now accumulate the new audio into our buffers */
F frame = _t_to_f (time);
- F after = max (_buffers->frames(), frame + audio->frames() - _t_to_f (_next_emission));
+ F after = max (_buffers->frames(), frame + audio->frames() - _t_to_f (_next_out));
_buffers->ensure_size (after);
- _buffers->accumulate_frames (audio.get(), 0, frame - _t_to_f (_next_emission), audio->frames ());
+ _buffers->accumulate_frames (audio.get(), 0, frame - _t_to_f (_next_out), audio->frames ());
_buffers->set_frames (after);
+
+ return out;
}
F min (F a, int b)
@@ -86,18 +90,19 @@ public:
return b;
}
- void flush ()
+ TimedAudioBuffers<T>
+ flush ()
{
- if (_buffers->frames() > 0) {
- Audio (_buffers, _next_emission);
+ if (_buffers->frames() == 0) {
+ return TimedAudioBuffers<T> ();
}
+
+ return TimedAudioBuffers<T> (_buffers, _next_out);
}
- boost::signals2::signal<void (boost::shared_ptr<const AudioBuffers>, T)> Audio;
-
private:
boost::shared_ptr<AudioBuffers> _buffers;
- T _next_emission;
+ T _next_out;
boost::function<F (T)> _t_to_f;
boost::function<T (F)> _f_to_t;
};