when calculating average slave/master delta, use absolute value.
[ardour.git] / libs / ardour / track.cc
index a738aebad918541ad059b920ec22374fb568a351..6f8d49d532eddb46d854c4511e1ba0c794b301d5 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"
@@ -55,11 +60,22 @@ Track::Track (Session& sess, string name, PresentationInfo::Flag flag, TrackMode
 {
        _freeze_record.state = NoFreeze;
         _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
@@ -85,12 +101,7 @@ Track::init ()
         _disk_writer->set_block_size (_session.get_block_size ());
         _disk_writer->set_route (boost::dynamic_pointer_cast<Route> (shared_from_this()));
 
-        use_new_playlist ();
-
-        /* ordering here is important, and needs to be generally maintained */
-
-        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<Track> rt = boost::dynamic_pointer_cast<Track> (rp);
@@ -209,6 +220,7 @@ Track::set_state (const XMLNode& node, int version)
                _saved_meter_point = _meter_point;
        }
 
+
        AlignChoice ac;
 
        if (node.get_property (X_("alignment-choice"), ac)) {
@@ -544,39 +556,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 +591,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
@@ -668,12 +653,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)
 {
@@ -691,9 +670,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);
@@ -883,10 +862,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,13 +873,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
@@ -1235,6 +1214,8 @@ Track::set_processor_state (XMLNode const & node, XMLProperty const* prop, Proce
                return true;
        }
 
+       cerr << name() << " looking for state for track procs, DR = " << _disk_reader << endl;
+
        if (prop->value() == "diskreader") {
                if (_disk_reader) {
                        _disk_reader->set_state (node, Stateful::current_state_version);
@@ -1252,3 +1233,244 @@ 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));
+}
+
+