X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Faudio_merger.h;h=6db28b6c3cb2e841fa4b8996ff3c383075dda5f8;hb=89aa9d4ba69e471949f791cdafe4ae20cea554d2;hp=f068b504e8dcbff0c0215a146de00550480bea97;hpb=58588c485eac7c488a574fe923576f109ec0134e;p=dcpomatic.git diff --git a/src/lib/audio_merger.h b/src/lib/audio_merger.h index f068b504e..6db28b6c3 100644 --- a/src/lib/audio_merger.h +++ b/src/lib/audio_merger.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2013 Carl Hetherington + Copyright (C) 2013-2016 Carl Hetherington This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,101 +18,25 @@ */ #include "audio_buffers.h" +#include "dcpomatic_time.h" #include "util.h" -template class AudioMerger { public: - AudioMerger (int channels, boost::function t_to_f, boost::function f_to_t) - : _buffers (new AudioBuffers (channels, 0)) - , _last_pull (0) - , _t_to_f (t_to_f) - , _f_to_t (f_to_t) - {} + AudioMerger (int channels, int frame_rate); /** Pull audio up to a given time; after this call, no more data can be pushed * before the specified time. */ - TimedAudioBuffers - pull (T time) - { - assert (time >= _last_pull); - - 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); - - return out; - } - - void - push (boost::shared_ptr audio, T time) - { - assert (time >= _last_pull); - - F frame = _t_to_f (time); - 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 (_last_pull), audio->frames ()); - _buffers->set_frames (after); - } - - F min (F a, int b) - { - if (a < b) { - return a; - } - - return b; + std::pair, DCPTime> pull (DCPTime time); + void push (boost::shared_ptr audio, DCPTime time); + DCPTime last_pull () const { + return _last_pull; } - F max (int a, F b) - { - if (a > b) { - return a; - } - - return b; - } - - TimedAudioBuffers - flush () - { - if (_buffers->frames() == 0) { - return TimedAudioBuffers (); - } - - return TimedAudioBuffers (_buffers, _last_pull); - } - - void - clear (DCPTime t) - { - _last_pull = t; - _buffers.reset (new AudioBuffers (_buffers->channels(), 0)); - } - private: boost::shared_ptr _buffers; - T _last_pull; - boost::function _t_to_f; - boost::function _f_to_t; + DCPTime _last_pull; + int _frame_rate; };