2 Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "audio_buffers.h"
21 #include "audio_merger.h"
25 using boost::shared_ptr;
27 AudioMerger::AudioMerger (int channels, int frame_rate)
28 : _buffers (new AudioBuffers (channels, 0))
29 , _frame_rate (frame_rate)
36 TimedAudioBuffers<DCPTime>
37 AudioMerger::pull (DCPTime time)
39 assert (time >= _last_pull);
41 TimedAudioBuffers<DCPTime> out;
43 int64_t const to_return = DCPTime (time - _last_pull).frames (_frame_rate);
44 out.audio.reset (new AudioBuffers (_buffers->channels(), to_return));
45 /* And this is how many we will get from our buffer */
46 int64_t const to_return_from_buffers = min (to_return, int64_t (_buffers->frames ()));
48 /* Copy the data that we have to the back end of the return buffer */
49 out.audio->copy_from (_buffers.get(), to_return_from_buffers, 0, to_return - to_return_from_buffers);
50 /* Silence any gap at the start */
51 out.audio->make_silent (0, to_return - to_return_from_buffers);
53 out.time = _last_pull;
56 /* And remove the data we're returning from our buffers */
57 if (_buffers->frames() > to_return_from_buffers) {
58 _buffers->move (to_return_from_buffers, 0, _buffers->frames() - to_return_from_buffers);
60 _buffers->set_frames (_buffers->frames() - to_return_from_buffers);
66 AudioMerger::push (shared_ptr<const AudioBuffers> audio, DCPTime time)
68 assert (time >= _last_pull);
70 int64_t frame = time.frames (_frame_rate);
71 int64_t after = max (int64_t (_buffers->frames()), frame + audio->frames() - _last_pull.frames (_frame_rate));
72 _buffers->ensure_size (after);
73 _buffers->accumulate_frames (audio.get(), 0, frame - _last_pull.frames (_frame_rate), audio->frames ());
74 _buffers->set_frames (after);
77 TimedAudioBuffers<DCPTime>
80 if (_buffers->frames() == 0) {
81 return TimedAudioBuffers<DCPTime> ();
84 return TimedAudioBuffers<DCPTime> (_buffers, _last_pull);
88 AudioMerger::clear (DCPTime t)
91 _buffers.reset (new AudioBuffers (_buffers->channels(), 0));