+ list<pair<shared_ptr<AudioBuffers>, DCPTime>> out;
+
+ list<Buffer> new_buffers;
+
+ _buffers.sort ([](Buffer const& a, Buffer const& b) {
+ return a.time < b.time;
+ });
+
+ for (auto i: _buffers) {
+ if (i.period().to <= time) {
+ /* Completely within the pull period */
+ DCPOMATIC_ASSERT (i.audio->frames() > 0);
+ out.push_back (make_pair (i.audio, i.time));
+ } else if (i.time < time) {
+ /* Overlaps the end of the pull period */
+ int32_t const overlap = frames(DCPTime(time - i.time));
+ /* Though time > i.time, overlap could be 0 if the difference in time is less than one frame */
+ if (overlap > 0) {
+ auto audio = make_shared<AudioBuffers>(i.audio, overlap, 0);
+ out.push_back (make_pair(audio, i.time));
+ i.audio->trim_start (overlap);
+ i.time += DCPTime::from_frames(overlap, _frame_rate);
+ DCPOMATIC_ASSERT (i.audio->frames() > 0);
+ new_buffers.push_back (i);
+ }
+ } else {
+ /* Not involved */
+ DCPOMATIC_ASSERT (i.audio->frames() > 0);
+ new_buffers.push_back (i);
+ }
+ }
+
+ _buffers = new_buffers;
+
+ for (auto const& i: out) {
+ DCPOMATIC_ASSERT (i.first->frames() > 0);
+ }