X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Faudio_merger.h;h=226601e0ec61dffc92726bd8f98bbd976f21e9ad;hb=d087368dc0dcd6026b3a967f00f145feb701dd0e;hp=126325a9d61cf127dd10bdaa94ac5b5998e2797e;hpb=65379c3a9d78ae627519c0820c0814db0083f8af;p=dcpomatic.git diff --git a/src/lib/audio_merger.h b/src/lib/audio_merger.h index 126325a9d..226601e0e 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 AudioMerger @@ -25,46 +26,50 @@ class AudioMerger public: AudioMerger (int channels, boost::function t_to_f, boost::function f_to_t) : _buffers (new AudioBuffers (channels, 0)) - , _next_emission (0) + , _last_pull (0) , _t_to_f (t_to_f) , _f_to_t (f_to_t) {} - void push (boost::shared_ptr audio, T time) + /** Pull audio up to a given time; after this call, no more data can be pushed + * before the specified time. + */ + TimedAudioBuffers + pull (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 emit (new AudioBuffers (_buffers->channels(), to_emit)); - - /* 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); - - /* Silence any gap at the start */ - emit->make_silent (0, to_emit - to_emit_from_buffers); - - /* Emit that */ - Audio (emit, _next_emission); + TimedAudioBuffers out; + + F const to_return = _t_to_f (time - _last_pull); + out.audio.reset (new AudioBuffers (_buffers->channels(), to_return)); + /* And this is how many we will get from our buffer */ + 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 */ + out.audio->make_silent (0, to_return - to_return_from_buffers); + + out.time = _last_pull; + _last_pull = time; + + /* 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_return_from_buffers); - _next_emission += _f_to_t (to_emit); + return out; + } - /* 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); - } - _buffers->set_frames (_buffers->frames() - to_emit_from_buffers); - } + void + push (boost::shared_ptr audio, T time) + { + assert (time >= _last_pull); - /* 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 (_last_pull)); _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 (_last_pull), audio->frames ()); _buffers->set_frames (after); } @@ -86,18 +91,19 @@ public: return b; } - void flush () + TimedAudioBuffers + flush () { - if (_buffers->frames() > 0) { - Audio (_buffers, _next_emission); + if (_buffers->frames() == 0) { + return TimedAudioBuffers (); } + + return TimedAudioBuffers (_buffers, _last_pull); } - boost::signals2::signal, T)> Audio; - private: boost::shared_ptr _buffers; - T _next_emission; + T _last_pull; boost::function _t_to_f; boost::function _f_to_t; };