X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Flib%2Faudio_merger.h;h=07e730ce2048164fe658d8e1fa155462b9457d25;hb=fb2f4b49cf0d003491ec21460051c01187c211fe;hp=afb21871bd0c72f031acae149ff69eaa7aa11c23;hpb=bcb746a19d5cc5377eacb33b73b59549a7372487;p=dcpomatic.git diff --git a/src/lib/audio_merger.h b/src/lib/audio_merger.h index afb21871b..07e730ce2 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-2021 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 @@ -17,92 +17,61 @@ */ + +/** @file src/audio_merger.h + * @brief AudioMerger class. + */ + + #include "audio_buffers.h" +#include "dcpomatic_time.h" #include "util.h" -template + +/** @class AudioMerger. + * @brief A class that can merge audio data from many sources. + */ class AudioMerger { public: - AudioMerger (int channels, boost::function t_to_f, boost::function f_to_t) - : _buffers (new AudioBuffers (channels, 0)) - , _next_out (0) - , _t_to_f (t_to_f) - , _f_to_t (f_to_t) - {} - - TimedAudioBuffers - push (boost::shared_ptr audio, T time) - { - assert (time >= _next_out); - - TimedAudioBuffers 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_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 = _next_out; - _next_out += _f_to_t (to_return); - - /* 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); - } - - /* 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_out)); - _buffers->ensure_size (after); - _buffers->accumulate_frames (audio.get(), 0, frame - _t_to_f (_next_out), audio->frames ()); - _buffers->set_frames (after); + explicit AudioMerger (int frame_rate); - return out; - } + std::list, dcpomatic::DCPTime>> pull (dcpomatic::DCPTime time); + void push (std::shared_ptr audio, dcpomatic::DCPTime time); + void clear (); - F min (F a, int b) - { - if (a < b) { - return a; - } - - return b; - } +private: + Frame frames (dcpomatic::DCPTime t) const; - F max (int a, F b) + class Buffer { - if (a > b) { - return a; + public: + /** @param c Channels + * @param f Frames + * @param t Time + * @param r Frame rate. + */ + Buffer (int c, int32_t f, dcpomatic::DCPTime t, int r) + : audio (new AudioBuffers (c, f)) + , time (t) + , frame_rate (r) + {} + + Buffer (std::shared_ptr a, dcpomatic::DCPTime t, int r) + : audio (a) + , time (t) + , frame_rate (r) + {} + + std::shared_ptr audio; + dcpomatic::DCPTime time; + int frame_rate; + + dcpomatic::DCPTimePeriod period () const { + return dcpomatic::DCPTimePeriod (time, time + dcpomatic::DCPTime::from_frames (audio->frames(), frame_rate)); } + }; - return b; - } - - TimedAudioBuffers - flush () - { - if (_buffers->frames() == 0) { - return TimedAudioBuffers (); - } - - return TimedAudioBuffers (_buffers, _next_out); - } - -private: - boost::shared_ptr _buffers; - T _next_out; - boost::function _t_to_f; - boost::function _f_to_t; + std::list _buffers; + int _frame_rate; };