diff options
| author | Carl Hetherington <cth@carlh.net> | 2013-07-27 21:44:29 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2013-07-27 21:44:29 +0100 |
| commit | e899b255083acafeac601253a33e109b30285707 (patch) | |
| tree | 78255e3fede3e970bfde9710af729ca173684ff1 /src/lib/audio_merger.h | |
| parent | bcb746a19d5cc5377eacb33b73b59549a7372487 (diff) | |
More attempts to fix audio / merging etc.
Diffstat (limited to 'src/lib/audio_merger.h')
| -rw-r--r-- | src/lib/audio_merger.h | 65 |
1 files changed, 33 insertions, 32 deletions
diff --git a/src/lib/audio_merger.h b/src/lib/audio_merger.h index afb21871b..226601e0e 100644 --- a/src/lib/audio_merger.h +++ b/src/lib/audio_merger.h @@ -26,50 +26,51 @@ 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_out (0) + , _last_pull (0) , _t_to_f (t_to_f) , _f_to_t (f_to_t) {} + /** Pull audio up to a given time; after this call, no more data can be pushed + * before the specified time. + */ TimedAudioBuffers<T> - push (boost::shared_ptr<const AudioBuffers> audio, T time) + pull (T time) { - 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_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); + 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); - out.time = _next_out; - _next_out += _f_to_t (to_return); + return out; + } - /* 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); - } + void + push (boost::shared_ptr<const AudioBuffers> 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_out)); + 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_out), audio->frames ()); + _buffers->accumulate_frames (audio.get(), 0, frame - _t_to_f (_last_pull), audio->frames ()); _buffers->set_frames (after); - - return out; } F min (F a, int b) @@ -97,12 +98,12 @@ public: return TimedAudioBuffers<T> (); } - return TimedAudioBuffers<T> (_buffers, _next_out); + return TimedAudioBuffers<T> (_buffers, _last_pull); } private: boost::shared_ptr<AudioBuffers> _buffers; - T _next_out; + T _last_pull; boost::function<F (T)> _t_to_f; boost::function<T (F)> _f_to_t; }; |
