Fix changing playlist (Track is responsible for signal emission)
[ardour.git] / libs / ardour / track.cc
index a738aebad918541ad059b920ec22374fb568a351..38d13d186da4eaaf53bff03b27c10e1edeb4036c 100644 (file)
@@ -19,6 +19,8 @@
 
 #include "ardour/amp.h"
 #include "ardour/audioengine.h"
+#include "ardour/audiofilesource.h"
+#include "ardour/audioregion.h"
 #include "ardour/debug.h"
 #include "ardour/delivery.h"
 #include "ardour/disk_reader.h"
 #include "ardour/event_type_map.h"
 #include "ardour/io_processor.h"
 #include "ardour/meter.h"
+#include "ardour/midi_region.h"
 #include "ardour/monitor_control.h"
 #include "ardour/playlist.h"
 #include "ardour/playlist_factory.h"
 #include "ardour/port.h"
 #include "ardour/processor.h"
 #include "ardour/profile.h"
+#include "ardour/region_factory.h"
 #include "ardour/record_enable_control.h"
 #include "ardour/record_safe_control.h"
 #include "ardour/route_group_specialized.h"
 #include "ardour/session.h"
 #include "ardour/session_playlists.h"
+#include "ardour/smf_source.h"
 #include "ardour/track.h"
 #include "ardour/types_convert.h"
 #include "ardour/utils.h"
@@ -49,50 +54,58 @@ using namespace PBD;
 
 Track::Track (Session& sess, string name, PresentationInfo::Flag flag, TrackMode mode, DataType default_type)
        : Route (sess, name, flag, default_type)
-        , _saved_meter_point (_meter_point)
-        , _mode (mode)
+       , _saved_meter_point (_meter_point)
+       , _mode (mode)
        , _alignment_choice (Automatic)
 {
        _freeze_record.state = NoFreeze;
-        _declickable = true;
+       _declickable = true;
+
 }
 
 Track::~Track ()
 {
        DEBUG_TRACE (DEBUG::Destruction, string_compose ("track %1 destructor\n", _name));
+
+       if (_disk_reader) {
+               _disk_reader->set_route (boost::shared_ptr<Route>());
+               _disk_reader.reset ();
+       }
+
+       if (_disk_writer) {
+               _disk_writer->set_route (boost::shared_ptr<Route>());
+               _disk_writer.reset ();
+       }
 }
 
 int
 Track::init ()
 {
-        if (Route::init ()) {
-                return -1;
-        }
-
-        DiskIOProcessor::Flag dflags = DiskIOProcessor::Recordable;
+       if (Route::init ()) {
+               return -1;
+       }
 
-        if (_mode == Destructive && !Profile->get_trx()) {
-               dflags = DiskIOProcessor::Flag (dflags | DiskIOProcessor::Destructive);
-        } else if (_mode == NonLayered){
-               dflags = DiskIOProcessor::Flag(dflags | DiskIOProcessor::NonLayered);
-        }
+       DiskIOProcessor::Flag dflags = DiskIOProcessor::Recordable;
 
-        _disk_reader.reset (new DiskReader (_session, name(), dflags));
-        _disk_reader->set_block_size (_session.get_block_size ());
-        _disk_reader->set_route (boost::dynamic_pointer_cast<Route> (shared_from_this()));
+       if (_mode == Destructive && !Profile->get_trx()) {
+               dflags = DiskIOProcessor::Flag (dflags | DiskIOProcessor::Destructive);
+       } else if (_mode == NonLayered){
+               dflags = DiskIOProcessor::Flag(dflags | DiskIOProcessor::NonLayered);
+       }
 
-        _disk_writer.reset (new DiskWriter (_session, name(), dflags));
-        _disk_writer->set_block_size (_session.get_block_size ());
-        _disk_writer->set_route (boost::dynamic_pointer_cast<Route> (shared_from_this()));
+       _disk_reader.reset (new DiskReader (_session, name(), dflags));
+       _disk_reader->set_block_size (_session.get_block_size ());
+       _disk_reader->set_route (boost::dynamic_pointer_cast<Route> (shared_from_this()));
 
-        use_new_playlist ();
+       _disk_writer.reset (new DiskWriter (_session, name(), dflags));
+       _disk_writer->set_block_size (_session.get_block_size ());
+       _disk_writer->set_route (boost::dynamic_pointer_cast<Route> (shared_from_this()));
 
-        /* ordering here is important, and needs to be generally maintained */
+       set_align_choice_from_io ();
 
-        add_processor (_disk_writer, PreFader);
-        add_processor (_disk_reader, PreFader);
+       use_new_playlist (data_type());
 
-        boost::shared_ptr<Route> rp (boost::dynamic_pointer_cast<Route> (shared_from_this()));
+       boost::shared_ptr<Route> rp (boost::dynamic_pointer_cast<Route> (shared_from_this()));
        boost::shared_ptr<Track> rt = boost::dynamic_pointer_cast<Track> (rp);
 
        _record_enable_control.reset (new RecordEnableControl (_session, EventTypeMap::instance().to_symbol (RecEnableAutomation), *this));
@@ -106,13 +119,13 @@ Track::init ()
 
        _session.config.ParameterChanged.connect_same_thread (*this, boost::bind (&Track::parameter_changed, this, _1));
 
-        _monitoring_control->Changed.connect_same_thread (*this, boost::bind (&Track::monitoring_changed, this, _1, _2));
-        _record_safe_control->Changed.connect_same_thread (*this, boost::bind (&Track::record_safe_changed, this, _1, _2));
-        _record_enable_control->Changed.connect_same_thread (*this, boost::bind (&Track::record_enable_changed, this, _1, _2));
+       _monitoring_control->Changed.connect_same_thread (*this, boost::bind (&Track::monitoring_changed, this, _1, _2));
+       _record_safe_control->Changed.connect_same_thread (*this, boost::bind (&Track::record_safe_changed, this, _1, _2));
+       _record_enable_control->Changed.connect_same_thread (*this, boost::bind (&Track::record_enable_changed, this, _1, _2));
 
-        _input->changed.connect_same_thread (*this, boost::bind (&Track::input_changed, this));
+       _input->changed.connect_same_thread (*this, boost::bind (&Track::input_changed, this));
 
-        return 0;
+       return 0;
 }
 
 void
@@ -209,6 +222,7 @@ Track::set_state (const XMLNode& node, int version)
                _saved_meter_point = _meter_point;
        }
 
+
        AlignChoice ac;
 
        if (node.get_property (X_("alignment-choice"), ac)) {
@@ -402,15 +416,8 @@ Track::set_name (const string& str)
        return ret;
 }
 
-void
-Track::set_latency_compensation (framecnt_t longest_session_latency)
-{
-       Route::set_latency_compensation (longest_session_latency);
-       _disk_reader->set_roll_delay (_roll_delay);
-}
-
 int
-Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool session_state_changing)
+Track::no_roll (pframes_t nframes, samplepos_t start_sample, samplepos_t end_sample, bool session_state_changing)
 {
        Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
 
@@ -445,7 +452,7 @@ Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
 
                           XXX note the absurdity of ::no_roll() being called when we ARE rolling!
                        */
-                       passthru_silence (start_frame, end_frame, nframes, 0);
+                       passthru_silence (start_sample, end_sample, nframes, 0);
                        return 0;
                }
                /* we're really not rolling, so we're either delivery silence or actually
@@ -453,7 +460,7 @@ Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
                */
        }
 
-       _disk_writer->check_record_status (start_frame, can_record);
+       _disk_writer->check_record_status (start_sample, can_record);
 
        bool be_silent;
 
@@ -481,8 +488,6 @@ Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
                be_silent = false;
        }
 
-       _amp->apply_gain_automation (false);
-
        /* if have_internal_generator, or .. */
 
        if (be_silent) {
@@ -517,14 +522,14 @@ Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
 
                        if (no_meter) {
                                BufferSet& bufs (_session.get_silent_buffers (n_process_buffers()));
-                               _meter->run (bufs, start_frame, end_frame, 1.0, nframes, true);
-                               _input->process_input (boost::shared_ptr<Processor>(), start_frame, end_frame, _session.transport_speed(), nframes);
+                               _meter->run (bufs, start_sample, end_sample, 1.0, nframes, true);
+                               _input->process_input (boost::shared_ptr<Processor>(), start_sample, end_sample, _session.transport_speed(), nframes);
                        } else {
-                               _input->process_input (_meter, start_frame, end_frame, _session.transport_speed(), nframes);
+                               _input->process_input (_meter, start_sample, end_sample, _session.transport_speed(), nframes);
                        }
                }
 
-               passthru_silence (start_frame, end_frame, nframes, 0);
+               passthru_silence (start_sample, end_sample, nframes, 0);
 
        } else {
 
@@ -533,10 +538,10 @@ Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
                fill_buffers_with_input (bufs, _input, nframes);
 
                if (_meter_point == MeterInput) {
-                       _meter->run (bufs, start_frame, end_frame, _session.transport_speed(), nframes, true);
+                       _meter->run (bufs, start_sample, end_sample, _session.transport_speed(), nframes, true);
                }
 
-               passthru (bufs, start_frame, end_frame, nframes, false);
+               passthru (bufs, start_sample, end_sample, nframes, false, true);
        }
 
        flush_processor_buffers_locked (nframes);
@@ -544,39 +549,6 @@ Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
        return 0;
 }
 
-int
-Track::silent_roll (pframes_t nframes, framepos_t /*start_frame*/, framepos_t /*end_frame*/, bool& need_butler)
-{
-       Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
-       if (!lm.locked()) {
-               // XXX DISK reader needs to seek ahead the correct distance ?? OR DOES IT ?
-               //framecnt_t playback_distance = _disk_reader->calculate_playback_distance(nframes);
-               //if (can_internal_playback_seek(playback_distance)) {
-               // internal_playback_seek(playback_distance);
-               //}
-               return 0;
-       }
-
-       if (n_outputs().n_total() == 0 && _processors.empty()) {
-               return 0;
-       }
-
-       if (!_active) {
-               silence (nframes);
-               return 0;
-       }
-
-       _silent = true;
-       _amp->apply_gain_automation(false);
-
-       silence (nframes);
-       flush_processor_buffers_locked (nframes);
-
-       //BufferSet& bufs (_session.get_route_buffers (n_process_buffers(), true));
-       // XXXX DISKWRITER/READER ADVANCE, SET need_butler
-       return 0;
-}
-
 boost::shared_ptr<Playlist>
 Track::playlist ()
 {
@@ -612,9 +584,15 @@ Track::last_capture_sources ()
 }
 
 void
-Track::set_capture_offset ()
+Track::update_latency_information ()
 {
-       _disk_writer->set_capture_offset ();
+       Glib::Threads::RWLock::ReaderLock lr (_processor_lock);
+       samplecnt_t chain_latency = _input->latency ();
+
+       for (ProcessorList::iterator p = _processors.begin(); p != _processors.end(); ++p) {
+               (*p)->set_input_latency (chain_latency);
+               chain_latency += (*p)->signal_latency ();
+       }
 }
 
 std::string
@@ -660,7 +638,7 @@ Track::set_pending_overwrite (bool o)
 }
 
 int
-Track::seek (framepos_t p, bool complete_refill)
+Track::seek (samplepos_t p, bool complete_refill)
 {
        if (_disk_reader->seek (p, complete_refill)) {
                return -1;
@@ -668,32 +646,26 @@ Track::seek (framepos_t p, bool complete_refill)
        return _disk_writer->seek (p, complete_refill);
 }
 
-bool
-Track::hidden () const
-{
-       return _disk_writer->hidden () || _disk_reader->hidden();
-}
-
 int
-Track::can_internal_playback_seek (framecnt_t p)
+Track::can_internal_playback_seek (samplecnt_t p)
 {
        return _disk_reader->can_internal_playback_seek (p);
 }
 
 int
-Track::internal_playback_seek (framecnt_t p)
+Track::internal_playback_seek (samplecnt_t p)
 {
        return _disk_reader->internal_playback_seek (p);
 }
 
 void
-Track::non_realtime_locate (framepos_t p)
+Track::non_realtime_locate (samplepos_t p)
 {
        Route::non_realtime_locate (p);
 
-       if (!hidden()) {
+       if (!is_private_route()) {
                /* don't waste i/o cycles and butler calls
-                  for hidden (secret) tracks
+                  for private tracks (e.g.auditioner)
                */
                _disk_reader->non_realtime_locate (p);
                _disk_writer->non_realtime_locate (p);
@@ -712,10 +684,10 @@ Track::overwrite_existing_buffers ()
        return _disk_reader->overwrite_existing_buffers ();
 }
 
-framecnt_t
-Track::get_captured_frames (uint32_t n) const
+samplecnt_t
+Track::get_captured_samples (uint32_t n) const
 {
-       return _disk_writer->get_captured_frames (n);
+       return _disk_writer->get_captured_samples (n);
 }
 
 int
@@ -728,7 +700,7 @@ Track::set_loop (Location* l)
 }
 
 void
-Track::transport_looped (framepos_t p)
+Track::transport_looped (samplepos_t p)
 {
        return _disk_writer->transport_looped (p);
 }
@@ -769,7 +741,7 @@ Track::pending_overwrite () const
 }
 
 void
-Track::prepare_to_stop (framepos_t t, framepos_t a)
+Track::prepare_to_stop (samplepos_t t, samplepos_t a)
 {
        _disk_writer->prepare_to_stop (t, a);
 }
@@ -787,10 +759,10 @@ Track::n_channels ()
        return _disk_reader->output_streams();
 }
 
-framepos_t
-Track::get_capture_start_frame (uint32_t n) const
+samplepos_t
+Track::get_capture_start_sample (uint32_t n) const
 {
-       return _disk_writer->get_capture_start_frame (n);
+       return _disk_writer->get_capture_start_sample (n);
 }
 
 AlignStyle
@@ -802,16 +774,16 @@ Track::alignment_style () const
 AlignChoice
 Track::alignment_choice () const
 {
-       return _disk_writer->alignment_choice ();
+       return _alignment_choice;
 }
 
-framepos_t
+samplepos_t
 Track::current_capture_start () const
 {
        return _disk_writer->current_capture_start ();
 }
 
-framepos_t
+samplepos_t
 Track::current_capture_end () const
 {
        return _disk_writer->current_capture_end ();
@@ -855,6 +827,9 @@ Track::use_playlist (DataType dt, boost::shared_ptr<Playlist> p)
                _playlists[dt] = p;
        }
 
+       _session.set_dirty ();
+       PlaylistChanged (); /* EMIT SIGNAL */
+
        return ret;
 }
 
@@ -883,10 +858,10 @@ Track::use_copy_playlist ()
 }
 
 int
-Track::use_new_playlist ()
+Track::use_new_playlist (DataType dt)
 {
        string newname;
-       boost::shared_ptr<Playlist> playlist = _playlists[data_type()];
+       boost::shared_ptr<Playlist> playlist = _playlists[dt];
 
        if (playlist) {
                newname = Playlist::bump_name (playlist->name(), _session);
@@ -894,29 +869,30 @@ Track::use_new_playlist ()
                newname = Playlist::bump_name (_name, _session);
        }
 
-       playlist = PlaylistFactory::create (data_type(), _session, newname, hidden());
+       playlist = PlaylistFactory::create (dt, _session, newname, is_private_route());
 
        if (!playlist) {
                return -1;
        }
 
-       return use_playlist (data_type(), playlist);
+       return use_playlist (dt, playlist);
 }
 
 void
 Track::set_align_choice (AlignChoice ac, bool force)
 {
+       _alignment_choice = ac;
        switch (ac) {
-       case Automatic:
-               _alignment_choice = Automatic;
-               set_align_choice_from_io ();
-               return;
-       default:
-               break;
+               case Automatic:
+                       set_align_choice_from_io ();
+                       break;
+               case UseCaptureTime:
+                       _disk_writer->set_align_style (CaptureTime, force);
+                       break;
+               case UseExistingMaterial:
+                       _disk_writer->set_align_style (ExistingMaterial, force);
+                       break;
        }
-
-       _disk_writer->set_align_choice (ac, force);
-       _alignment_choice = ac;
 }
 
 void
@@ -1001,254 +977,315 @@ Track::adjust_capture_buffering ()
         }
 }
 
-#ifdef USE_TRACKS_CODE_FEATURES
-
-/* This is the Tracks version of Track::monitoring_state().
- *
- * Ardour developers: try to flag or fix issues if parts of the libardour API
- * change in ways that invalidate this
- */
 
-MonitorState
-Track::monitoring_state () const
+void
+Track::maybe_declick (BufferSet& bufs, samplecnt_t nframes, int declick)
 {
-       /* Explicit requests */
+        /* never declick if there is an internal generator - we just want it to
+           keep generating sound without interruption.
+
+          ditto if we are monitoring inputs.
+        */
 
-       if (_monitoring != MonitorInput) {
-               return MonitoringInput;
+       if (_have_internal_generator || (_monitoring_control->monitoring_choice() == MonitorInput)) {
+                return;
+        }
+
+        if (!declick) {
+               declick = _pending_declick;
        }
 
-       if (_monitoring & MonitorDisk) {
-               return MonitoringDisk;
+       if (declick != 0) {
+               Amp::declick (bufs, nframes, declick);
        }
+}
 
-       /* This is an implementation of the truth table in doc/monitor_modes.pdf;
-          I don't think it's ever going to be too pretty too look at.
-       */
+void
+Track::monitoring_changed (bool, Controllable::GroupControlDisposition)
+{
+       for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
+               (*i)->monitoring_changed ();
+       }
+}
 
-       // GZ: NOT USED IN TRACKS
-       //bool const auto_input = _session.config.get_auto_input ();
-       //bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
-       //bool const tape_machine_mode = Config->get_tape_machine_mode ();
+MeterState
+Track::metering_state () const
+{
+       bool rv;
+       if (_session.transport_rolling ()) {
+               // audio_track.cc || midi_track.cc roll() runs meter IFF:
+               rv = _meter_point == MeterInput && ((_monitoring_control->monitoring_choice() & MonitorInput) || _disk_writer->record_enabled());
+       } else {
+               // track no_roll() always metering if
+               rv = _meter_point == MeterInput;
+       }
+       return rv ? MeteringInput : MeteringRoute;
+}
 
-       bool const roll = _session.transport_rolling ();
-       bool const track_rec = _diskstream->record_enabled ();
-       bool session_rec = _session.actively_recording ();
+bool
+Track::set_processor_state (XMLNode const & node, XMLProperty const* prop, ProcessorList& new_order, bool& must_configure)
+{
+       if (Route::set_processor_state (node, prop, new_order, must_configure)) {
+               return true;
+       }
 
-       if (track_rec) {
+       cerr << name() << " looking for state for track procs, DR = " << _disk_reader << endl;
 
-               if (!session_rec && roll) {
-                       return MonitoringDisk;
-               } else {
-                       return MonitoringInput;
+       if (prop->value() == "diskreader") {
+               if (_disk_reader) {
+                       _disk_reader->set_state (node, Stateful::current_state_version);
+                       new_order.push_back (_disk_reader);
+                       return true;
                }
-
-       } else {
-
-               if (roll) {
-                       return MonitoringDisk;
+       } else if (prop->value() == "diskwriter") {
+               if (_disk_writer) {
+                       _disk_writer->set_state (node, Stateful::current_state_version);
+                       new_order.push_back (_disk_writer);
+                       return true;
                }
        }
 
-       return MonitoringSilence;
+       error << string_compose(_("unknown Processor type \"%1\"; ignored"), prop->value()) << endmsg;
+       return false;
 }
 
-#else
+void
+Track::use_captured_sources (SourceList& srcs, CaptureInfos const & capture_info)
+{
+       if (srcs.empty()) {
+               return;
+       }
+
+       boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (srcs.front());
+       boost::shared_ptr<SMFSource> mfs = boost::dynamic_pointer_cast<SMFSource> (srcs.front());
 
-/* This is the Ardour/Mixbus version of Track::monitoring_state().
- *
- * Tracks developers: do NOT modify this method under any circumstances.
- */
+       if (afs) {
+               use_captured_audio_sources (srcs, capture_info);
+       }
+
+       if (mfs) {
+               use_captured_midi_sources (srcs, capture_info);
+       }
+}
 
-MonitorState
-Track::monitoring_state () const
+void
+Track::use_captured_midi_sources (SourceList& srcs, CaptureInfos const & capture_info)
 {
-       /* Explicit requests */
-       MonitorChoice m (_monitoring_control->monitoring_choice());
+       if (srcs.empty() || data_type() != DataType::MIDI) {
+               return;
+       }
 
-       if (m != MonitorAuto) {
+       boost::shared_ptr<SMFSource> mfs = boost::dynamic_pointer_cast<SMFSource> (srcs.front());
+       boost::shared_ptr<Playlist> pl = _playlists[DataType::MIDI];
+       boost::shared_ptr<MidiRegion> midi_region;
+       CaptureInfos::const_iterator ci;
 
-               MonitorState ms ((MonitorState) 0);
+       if (!mfs || !pl) {
+               return;
+       }
 
-               if (m & MonitorInput) {
-                       ms = MonitoringInput;
-               }
+       samplecnt_t total_capture = 0;
 
-               if (m & MonitorDisk) {
-                       ms = MonitorState (ms | MonitoringDisk);
-               }
+       for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
+               total_capture += (*ci)->samples;
+       }
+
+       /* we will want to be able to keep (over)writing the source
+          but we don't want it to be removable. this also differs
+          from the audio situation, where the source at this point
+          must be considered immutable. luckily, we can rely on
+          MidiSource::mark_streaming_write_completed() to have
+          already done the necessary work for that.
+       */
+
+       string whole_file_region_name;
+       whole_file_region_name = region_name_from_path (mfs->name(), true);
+
+       /* Register a new region with the Session that
+          describes the entire source. Do this first
+          so that any sub-regions will obviously be
+          children of this one (later!)
+       */
+
+       try {
+               PropertyList plist;
+
+               plist.add (Properties::name, whole_file_region_name);
+               plist.add (Properties::whole_file, true);
+               plist.add (Properties::automatic, true);
+               plist.add (Properties::start, 0);
+               plist.add (Properties::length, total_capture);
+               plist.add (Properties::layer, 0);
 
-               return ms;
+               boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
+
+               midi_region = boost::dynamic_pointer_cast<MidiRegion> (rx);
+               midi_region->special_set_position (capture_info.front()->start);
        }
 
-       switch (_session.config.get_session_monitoring ()) {
-               case MonitorDisk:
-                       return MonitoringDisk;
-                       break;
-               case MonitorInput:
-                       return MonitoringInput;
-                       break;
-               default:
-                       break;
+       catch (failed_constructor& err) {
+               error << string_compose(_("%1: could not create region for complete midi file"), _name) << endmsg;
+               /* XXX what now? */
        }
 
-       /* This is an implementation of the truth table in doc/monitor_modes.pdf;
-          I don't think it's ever going to be too pretty too look at.
-       */
+       pl->clear_changes ();
+       pl->freeze ();
 
-       bool const roll = _session.transport_rolling ();
-       bool const track_rec = _disk_writer->record_enabled ();
-       bool const auto_input = _session.config.get_auto_input ();
-       bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
-       bool const tape_machine_mode = Config->get_tape_machine_mode ();
-       bool session_rec;
-
-       /* I suspect that just use actively_recording() is good enough all the
-        * time, but just to keep the semantics the same as they were before
-        * sept 26th 2012, we differentiate between the cases where punch is
-        * enabled and those where it is not.
-        *
-        * rg: I suspect this is not the case: monitoring may differ
-        */
-
-       if (_session.config.get_punch_in() || _session.config.get_punch_out() || _session.preroll_record_punch_enabled ()) {
-               session_rec = _session.actively_recording ();
-       } else {
-               session_rec = _session.get_record_enabled();
+       /* Session sample time of the initial capture in this pass, which is where the source starts */
+       samplepos_t initial_capture = 0;
+       if (!capture_info.empty()) {
+               initial_capture = capture_info.front()->start;
        }
 
-       if (track_rec) {
+       BeatsSamplesConverter converter (_session.tempo_map(), capture_info.front()->start);
+       const samplepos_t preroll_off = _session.preroll_record_trim_len ();
 
-               if (!session_rec && roll && auto_input) {
-                       return MonitoringDisk;
-               } else {
-                       return software_monitor ? MonitoringInput : MonitoringSilence;
-               }
+       for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
 
-       } else {
+               string region_name;
 
-               if (tape_machine_mode) {
+               RegionFactory::region_name (region_name, mfs->name(), false);
 
-                       return MonitoringDisk;
+               DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 capture start @ %2 length %3 add new region %4\n",
+                                                                     _name, (*ci)->start, (*ci)->samples, region_name));
 
-               } else {
 
-                       if (!roll && auto_input) {
-                               return software_monitor ? MonitoringInput : MonitoringSilence;
-                       } else {
-                               return MonitoringDisk;
+               // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->samples << " add a region\n";
+
+               try {
+                       PropertyList plist;
+
+                       /* start of this region is the offset between the start of its capture and the start of the whole pass */
+                       plist.add (Properties::start, (*ci)->start - initial_capture);
+                       plist.add (Properties::length, (*ci)->samples);
+                       plist.add (Properties::length_beats, converter.from((*ci)->samples).to_double());
+                       plist.add (Properties::name, region_name);
+
+                       boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
+                       midi_region = boost::dynamic_pointer_cast<MidiRegion> (rx);
+                       if (preroll_off > 0) {
+                               midi_region->trim_front ((*ci)->start - initial_capture + preroll_off);
                        }
+               }
 
+               catch (failed_constructor& err) {
+                       error << _("MidiDiskstream: could not create region for captured midi!") << endmsg;
+                       continue; /* XXX is this OK? */
                }
+
+               // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
+
+               pl->add_region (midi_region, (*ci)->start + preroll_off, _disk_writer->non_layered());
        }
 
-       abort(); /* NOTREACHED */
-       return MonitoringSilence;
+       pl->thaw ();
+       _session.add_command (new StatefulDiffCommand (pl));
 }
 
-#endif
-
 void
-Track::maybe_declick (BufferSet& bufs, framecnt_t nframes, int declick)
+Track::use_captured_audio_sources (SourceList& srcs, CaptureInfos const & capture_info)
 {
-        /* never declick if there is an internal generator - we just want it to
-           keep generating sound without interruption.
-
-          ditto if we are monitoring inputs.
-        */
+       if (srcs.empty() || data_type() != DataType::AUDIO) {
+               return;
+       }
 
-       if (_have_internal_generator || (_monitoring_control->monitoring_choice() == MonitorInput)) {
-                return;
-        }
+       boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (srcs.front());
+       boost::shared_ptr<Playlist> pl = _playlists[DataType::AUDIO];
+       boost::shared_ptr<AudioRegion> region;
 
-        if (!declick) {
-               declick = _pending_declick;
+       if (!afs || !pl) {
+               return;
        }
 
-       if (declick != 0) {
-               Amp::declick (bufs, nframes, declick);
-       }
-}
+       /* destructive tracks have a single, never changing region */
 
-framecnt_t
-Track::check_initial_delay (framecnt_t nframes, framepos_t& transport_frame)
-{
-       if (_roll_delay > nframes) {
+       if (destructive()) {
 
-               _roll_delay -= nframes;
-               silence_unlocked (nframes);
-               /* transport frame is not legal for caller to use */
-               return 0;
+               /* send a signal that any UI can pick up to do the right thing. there is
+                  a small problem here in that a UI may need the peak data to be ready
+                  for the data that was recorded and this isn't interlocked with that
+                  process. this problem is deferred to the UI.
+                */
 
-       } else if (_roll_delay > 0) {
+               pl->LayeringChanged(); // XXX this may not get the UI to do the right thing
+               return;
+       }
 
-               nframes -= _roll_delay;
-               silence_unlocked (_roll_delay);
-               transport_frame += _roll_delay;
+       string whole_file_region_name;
+       whole_file_region_name = region_name_from_path (afs->name(), true);
 
-               /* shuffle all the port buffers for things that lead "out" of this Route
-                  to reflect that we just wrote _roll_delay frames of silence.
-               */
+       /* Register a new region with the Session that
+          describes the entire source. Do this first
+          so that any sub-regions will obviously be
+          children of this one (later!)
+       */
 
-               Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
-               for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
-                       boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
-                       if (iop) {
-                               iop->increment_port_buffer_offset (_roll_delay);
-                       }
-               }
-               _output->increment_port_buffer_offset (_roll_delay);
+       try {
+               PropertyList plist;
 
-               _roll_delay = 0;
+               plist.add (Properties::start, afs->last_capture_start_sample());
+               plist.add (Properties::length, afs->length(0));
+               plist.add (Properties::name, whole_file_region_name);
+               boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
+               rx->set_automatic (true);
+               rx->set_whole_file (true);
 
+               region = boost::dynamic_pointer_cast<AudioRegion> (rx);
+               region->special_set_position (afs->natural_position());
        }
 
-       return nframes;
-}
 
-void
-Track::monitoring_changed (bool, Controllable::GroupControlDisposition)
-{
-       for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
-               (*i)->monitoring_changed ();
+       catch (failed_constructor& err) {
+               error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
+               /* XXX what now? */
        }
-}
 
-MeterState
-Track::metering_state () const
-{
-       bool rv;
-       if (_session.transport_rolling ()) {
-               // audio_track.cc || midi_track.cc roll() runs meter IFF:
-               rv = _meter_point == MeterInput && ((_monitoring_control->monitoring_choice() & MonitorInput) || _disk_writer->record_enabled());
-       } else {
-               // track no_roll() always metering if
-               rv = _meter_point == MeterInput;
-       }
-       return rv ? MeteringInput : MeteringRoute;
-}
+       pl->clear_changes ();
+       pl->set_capture_insertion_in_progress (true);
+       pl->freeze ();
 
-bool
-Track::set_processor_state (XMLNode const & node, XMLProperty const* prop, ProcessorList& new_order, bool& must_configure)
-{
-       if (Route::set_processor_state (node, prop, new_order, must_configure)) {
-               return true;
-       }
+       const samplepos_t preroll_off = _session.preroll_record_trim_len ();
+       samplecnt_t buffer_position = afs->last_capture_start_sample ();
+       CaptureInfos::const_iterator ci;
 
-       if (prop->value() == "diskreader") {
-               if (_disk_reader) {
-                       _disk_reader->set_state (node, Stateful::current_state_version);
-                       new_order.push_back (_disk_reader);
-                       return true;
+       for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
+
+               string region_name;
+
+               RegionFactory::region_name (region_name, whole_file_region_name, false);
+
+               DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 capture bufpos %5 start @ %2 length %3 add new region %4\n",
+                                                                     _name, (*ci)->start, (*ci)->samples, region_name, buffer_position));
+
+               try {
+
+                       PropertyList plist;
+
+                       plist.add (Properties::start, buffer_position);
+                       plist.add (Properties::length, (*ci)->samples);
+                       plist.add (Properties::name, region_name);
+
+                       boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
+                       region = boost::dynamic_pointer_cast<AudioRegion> (rx);
+                       if (preroll_off > 0) {
+                               region->trim_front (buffer_position + preroll_off);
+                       }
                }
-       } else if (prop->value() == "diskwriter") {
-               if (_disk_writer) {
-                       _disk_writer->set_state (node, Stateful::current_state_version);
-                       new_order.push_back (_disk_writer);
-                       return true;
+
+               catch (failed_constructor& err) {
+                       error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
+                       continue; /* XXX is this OK? */
                }
+
+               pl->add_region (region, (*ci)->start + preroll_off, 1, _disk_writer->non_layered());
+               pl->set_layer (region, DBL_MAX);
+
+               buffer_position += (*ci)->samples;
        }
 
-       error << string_compose(_("unknown Processor type \"%1\"; ignored"), prop->value()) << endmsg;
-       return false;
+       pl->thaw ();
+       pl->set_capture_insertion_in_progress (false);
+       _session.add_command (new StatefulDiffCommand (pl));
 }
+
+