- /* Write the part we can */
- shared_ptr<AudioBuffers> part (new AudioBuffers (audio->channels(), reel_space));
- part->copy_from (audio.get(), reel_space, offset, 0);
- _audio_reel->write (part);
+ /* This audio is over a reel boundary; split the audio into two and write the first part */
+ DCPTime part_lengths[2] = {
+ _audio_reel->period().to - t,
+ end - _audio_reel->period().to
+ };
+
+ Frame part_frames[2] = {
+ part_lengths[0].frames_ceil(afr),
+ part_lengths[1].frames_ceil(afr)
+ };
+
+ if (part_frames[0]) {
+ shared_ptr<AudioBuffers> part (new AudioBuffers(audio, part_frames[0], 0));
+ _audio_reel->write (part);
+ }
+
+ if (part_frames[1]) {
+ audio.reset (new AudioBuffers(audio, part_frames[1], part_frames[0]));
+ } else {
+ audio.reset ();
+ }
+