don't (re)create disk reader/writer if they already exist
[ardour.git] / libs / ardour / track.cc
index 4896c8a28f6eff0206b9acf88b3fbd9ba0320f5d..448ac02ec7ab9b414d8a7439cfe010c20e2ea3f8 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"
@@ -50,6 +55,7 @@ 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)
+       , _disk_io_point (DiskIOPreFader)
         , _mode (mode)
        , _alignment_choice (Automatic)
 {
@@ -60,6 +66,16 @@ Track::Track (Session& sess, string name, PresentationInfo::Flag flag, TrackMode
 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
@@ -69,26 +85,11 @@ Track::init ()
                 return -1;
         }
 
-        DiskIOProcessor::Flag dflags = DiskIOProcessor::Recordable;
-
-        if (_mode == Destructive && !Profile->get_trx()) {
-               dflags = DiskIOProcessor::Flag (dflags | DiskIOProcessor::Destructive);
-        } else if (_mode == NonLayered){
-               dflags = DiskIOProcessor::Flag(dflags | DiskIOProcessor::NonLayered);
-        }
-
-        _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()));
-
-        _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()));
-
-        use_new_playlist ();
+        use_new_playlist (data_type());
 
-        add_processor (_disk_writer, PreFader);
-        add_processor (_disk_reader, PreFader);
+        /* disk writer and reader processors will be added when Route calls
+         * add_processors_oh_children_of_mine ().
+         */
 
         boost::shared_ptr<Route> rp (boost::dynamic_pointer_cast<Route> (shared_from_this()));
        boost::shared_ptr<Track> rt = boost::dynamic_pointer_cast<Track> (rp);
@@ -113,6 +114,31 @@ Track::init ()
         return 0;
 }
 
+void
+Track::add_processors_oh_children_of_mine ()
+{
+        cerr << name() << " ::apocom(), create DW + DR\n";
+
+        DiskIOProcessor::Flag dflags = DiskIOProcessor::Recordable;
+
+        if (_mode == Destructive && !Profile->get_trx()) {
+               dflags = DiskIOProcessor::Flag (dflags | DiskIOProcessor::Destructive);
+        } else if (_mode == NonLayered){
+               dflags = DiskIOProcessor::Flag(dflags | DiskIOProcessor::NonLayered);
+        }
+        if (!_disk_reader) {
+               _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 (!_disk_writer) {
+               _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()));
+        }
+}
+
 void
 Track::input_changed ()
 {
@@ -145,6 +171,7 @@ Track::state (bool full)
        root.add_child_nocopy (_record_enable_control->get_state ());
 
        root.set_property (X_("saved-meter-point"), _saved_meter_point);
+       root.set_property (X_("disk-io-point"), _disk_io_point);
        root.set_property (X_("alignment-choice"), _alignment_choice);
 
        return root;
@@ -168,7 +195,7 @@ Track::set_state (const XMLNode& node, int version)
                           (child nodes, properties, etc.) and then call
                           ::set_state() on the writer/reader.
 
-                          But at present (June 2017), there's no such state. 
+                          But at present (June 2017), there's no such state.
                        */
                }
        }
@@ -207,6 +234,10 @@ Track::set_state (const XMLNode& node, int version)
                _saved_meter_point = _meter_point;
        }
 
+       if (!node.get_property (X_("saved-meter-point"), _disk_io_point)) {
+               _disk_io_point = DiskIOPreFader;
+       }
+
        AlignChoice ac;
 
        if (node.get_property (X_("alignment-choice"), ac)) {
@@ -610,9 +641,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);
+       framecnt_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
@@ -666,12 +703,6 @@ 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)
 {
@@ -689,9 +720,9 @@ Track::non_realtime_locate (framepos_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);
@@ -782,7 +813,7 @@ Track::set_slaved (bool s)
 ChanCount
 Track::n_channels ()
 {
-       return _disk_reader->output_streams(); // XXX DISK
+       return _disk_reader->output_streams();
 }
 
 framepos_t
@@ -881,10 +912,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);
@@ -892,13 +923,13 @@ 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
@@ -1012,7 +1043,7 @@ Track::monitoring_state () const
 {
        /* Explicit requests */
 
-       if (_monitoring & MonitorInput) {
+       if (_monitoring != MonitorInput) {
                return MonitoringInput;
        }
 
@@ -1064,12 +1095,19 @@ Track::monitoring_state () const
        /* Explicit requests */
        MonitorChoice m (_monitoring_control->monitoring_choice());
 
-       if (m & MonitorInput) {
-               return MonitoringInput;
-       }
+       if (m != MonitorAuto) {
 
-       if (m & MonitorDisk) {
-               return MonitoringDisk;
+               MonitorState ms ((MonitorState) 0);
+
+               if (m & MonitorInput) {
+                       ms = MonitoringInput;
+               }
+
+               if (m & MonitorDisk) {
+                       ms = MonitorState (ms | MonitoringDisk);
+               }
+
+               return ms;
        }
 
        switch (_session.config.get_session_monitoring ()) {
@@ -1243,3 +1281,297 @@ Track::set_processor_state (XMLNode const & node, XMLProperty const* prop, Proce
        error << string_compose(_("unknown Processor type \"%1\"; ignored"), prop->value()) << endmsg;
        return false;
 }
+
+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());
+
+       if (afs) {
+               use_captured_audio_sources (srcs, capture_info);
+       }
+
+       if (mfs) {
+               use_captured_midi_sources (srcs, capture_info);
+       }
+}
+
+void
+Track::use_captured_midi_sources (SourceList& srcs, CaptureInfos const & capture_info)
+{
+       if (srcs.empty() || data_type() != DataType::MIDI) {
+               return;
+       }
+
+       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;
+
+       if (!mfs || !pl) {
+               return;
+       }
+
+       framecnt_t total_capture = 0;
+
+       for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
+               total_capture += (*ci)->frames;
+       }
+
+       /* 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);
+
+               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);
+       }
+
+       catch (failed_constructor& err) {
+               error << string_compose(_("%1: could not create region for complete midi file"), _name) << endmsg;
+               /* XXX what now? */
+       }
+
+       pl->clear_changes ();
+       pl->freeze ();
+
+       /* Session frame time of the initial capture in this pass, which is where the source starts */
+       framepos_t initial_capture = 0;
+       if (!capture_info.empty()) {
+               initial_capture = capture_info.front()->start;
+       }
+
+       BeatsFramesConverter converter (_session.tempo_map(), capture_info.front()->start);
+       const framepos_t preroll_off = _session.preroll_record_trim_len ();
+
+       for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
+
+               string region_name;
+
+               RegionFactory::region_name (region_name, mfs->name(), false);
+
+               DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 capture start @ %2 length %3 add new region %4\n",
+                                                                     _name, (*ci)->start, (*ci)->frames, region_name));
+
+
+               // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " 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)->frames);
+                       plist.add (Properties::length_beats, converter.from((*ci)->frames).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);
+       }
+
+       pl->thaw ();
+       _session.add_command (new StatefulDiffCommand (pl));
+}
+
+void
+Track::use_captured_audio_sources (SourceList& srcs, CaptureInfos const & capture_info)
+{
+       if (srcs.empty() || data_type() != DataType::AUDIO) {
+               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 (!afs || !pl) {
+               return;
+       }
+
+       /* destructive tracks have a single, never changing region */
+
+       if (destructive()) {
+
+               /* 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.
+                */
+
+               pl->LayeringChanged(); // XXX this may not get the UI to do the right thing
+               return;
+       }
+
+       string whole_file_region_name;
+       whole_file_region_name = region_name_from_path (afs->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::start, afs->last_capture_start_frame());
+               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());
+       }
+
+
+       catch (failed_constructor& err) {
+               error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
+               /* XXX what now? */
+       }
+
+       pl->clear_changes ();
+       pl->set_capture_insertion_in_progress (true);
+       pl->freeze ();
+
+       const framepos_t preroll_off = _session.preroll_record_trim_len ();
+       framecnt_t buffer_position = afs->last_capture_start_frame ();
+       CaptureInfos::const_iterator ci;
+
+       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)->frames, region_name, buffer_position));
+
+               try {
+
+                       PropertyList plist;
+
+                       plist.add (Properties::start, buffer_position);
+                       plist.add (Properties::length, (*ci)->frames);
+                       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);
+                       }
+               }
+
+               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)->frames;
+       }
+
+       pl->thaw ();
+       pl->set_capture_insertion_in_progress (false);
+       _session.add_command (new StatefulDiffCommand (pl));
+}
+
+#ifdef __clang__
+__attribute__((annotate("realtime")))
+#endif
+void
+Track::setup_invisible_processors_oh_children_of_mine (ProcessorList& processors)
+{
+       ProcessorList::iterator insert_pos;
+
+       switch (_disk_io_point) {
+       case DiskIOPreFader:
+               insert_pos = find (processors.begin(), processors.end(), _trim);
+               if (insert_pos != processors.end()) {
+                       insert_pos = processors.insert (insert_pos, _disk_writer);
+                       processors.insert (insert_pos, _disk_reader);
+               }
+               break;
+       case DiskIOPostFader:
+               insert_pos = find (processors.begin(), processors.end(), _main_outs);
+               if (insert_pos != processors.end()) {
+                       insert_pos = processors.insert (insert_pos, _disk_writer);
+                       processors.insert (insert_pos, _disk_reader);
+               }
+       case DiskIOCustom:
+               break;
+       }
+}
+
+void
+Track::set_disk_io_position (DiskIOPoint diop)
+{
+       bool display = false;
+
+       switch (diop) {
+       case DiskIOCustom:
+               display = true;
+               break;
+       default:
+               display = false;
+       }
+
+       _disk_writer->set_display_to_user (display);
+       _disk_reader->set_display_to_user (display);
+
+       const bool changed = (diop != _disk_io_point);
+
+       _disk_io_point = diop;
+
+       if (changed) {
+               Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
+               configure_processors (0);
+       }
+
+       processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
+}