lots of stuff related to capture alignment. things appear to be working now, but...
[ardour.git] / libs / ardour / session.cc
index 52123c18f137228c1c20bec122e14c91e66c5a8e..759495351a8fbc9cf40743b574b5ee29f86b3584 100644 (file)
@@ -238,10 +238,6 @@ Session::destroy ()
 
        delete state_tree;
 
-       /* remove all stubfiles that might still be lurking */
-
-       cleanup_stubfiles ();
-
        /* reset dynamic state version back to default */
 
        Stateful::loading_state_version = 0;
@@ -301,8 +297,6 @@ Session::destroy ()
        }
        routes.flush ();
 
-       boost::shared_ptr<RouteList> r = routes.reader ();
-
        DEBUG_TRACE (DEBUG::Destruction, "delete sources\n");
        for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
                DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for source %1 ; pre-ref = %2\n", i->second->path(), i->second.use_count()));
@@ -334,6 +328,10 @@ Session::destroy ()
 void
 Session::set_worst_io_latencies ()
 {
+        if (_state_of_the_state & InitialConnecting) {
+                return;
+        }
+
        _worst_output_latency = 0;
        _worst_input_latency = 0;
 
@@ -347,6 +345,9 @@ Session::set_worst_io_latencies ()
                _worst_output_latency = max (_worst_output_latency, (*i)->output()->latency());
                _worst_input_latency = max (_worst_input_latency, (*i)->input()->latency());
        }
+        
+        DEBUG_TRACE (DEBUG::Latency, string_compose ("Worst output latency: %1 Worst input latency: %2\n",
+                                                     _worst_output_latency, _worst_input_latency));
 }
 
 void
@@ -435,8 +436,6 @@ Session::when_engine_running ()
 
        BootMessage (_("Compute I/O Latencies"));
 
-       set_worst_io_latencies ();
-
        if (_clicking) {
                // XXX HOW TO ALERT UI TO THIS ? DO WE NEED TO?
        }
@@ -650,7 +649,7 @@ Session::when_engine_running ()
                }
        }
 
-       /* catch up on send+insert cnts */
+       set_worst_io_latencies ();
 
        _state_of_the_state = StateOfTheState (_state_of_the_state & ~(CannotSave|Dirty));
 
@@ -765,6 +764,7 @@ Session::track_playlist_changed (boost::weak_ptr<Track> wp)
        if ((playlist = track->playlist()) != 0) {
                playlist->RegionAdded.connect_same_thread (*this, boost::bind (&Session::playlist_region_added, this, _1));
                playlist->RangesMoved.connect_same_thread (*this, boost::bind (&Session::playlist_ranges_moved, this, _1));
+               playlist->RegionsExtended.connect_same_thread (*this, boost::bind (&Session::playlist_regions_extended, this, _1));
        }
 }
 
@@ -782,30 +782,26 @@ Session::record_enabling_legal () const
        return true;
 }
 
+void
+Session::set_track_monitor_input_status (bool yn)
+{
+        boost::shared_ptr<RouteList> rl = routes.reader ();
+        for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
+                boost::shared_ptr<AudioTrack> tr = boost::dynamic_pointer_cast<AudioTrack> (*i);
+                if (tr && tr->record_enabled ()) {
+                        //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
+                        tr->monitor_input (yn);
+                }
+        }
+}
+
 void
 Session::reset_input_monitor_state ()
 {
        if (transport_rolling()) {
-
-               boost::shared_ptr<RouteList> rl = routes.reader ();
-               for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
-                       boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
-                       if (tr && tr->record_enabled ()) {
-                               //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
-                               tr->monitor_input (Config->get_monitoring_model() == HardwareMonitoring && !config.get_auto_input());
-                       }
-               }
-               
+                set_track_monitor_input_status (Config->get_monitoring_model() == HardwareMonitoring && !config.get_auto_input());
        } else {
-               
-               boost::shared_ptr<RouteList> rl = routes.reader ();
-               for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
-                       boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
-                       if (tr && tr->record_enabled ()) {
-                               //cerr << "switching to input = " << !Config->get_auto_input() << __FILE__ << __LINE__ << endl << endl;
-                               tr->monitor_input (Config->get_monitoring_model() == HardwareMonitoring);
-                       }
-               }
+                set_track_monitor_input_status (Config->get_monitoring_model() == HardwareMonitoring);
        }
 }
 
@@ -1016,14 +1012,7 @@ Session::enable_record ()
                        MIDI::Manager::instance()->mmc()->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdRecordStrobe));
                         
                        if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
-                                
-                               boost::shared_ptr<RouteList> rl = routes.reader ();
-                               for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
-                                       boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
-                                       if (tr && tr->record_enabled ()) {
-                                               tr->monitor_input (true);
-                                       }
-                               }
+                                set_track_monitor_input_status (true);
                        }
                         
                        RecordStateChanged ();
@@ -1049,14 +1038,7 @@ Session::disable_record (bool rt_context, bool force)
                }
 
                if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
-
-                       boost::shared_ptr<RouteList> rl = routes.reader ();
-                       for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
-                               boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
-                               if (tr && tr->record_enabled ()) {
-                                       tr->monitor_input (false);
-                               }
-                       }
+                        set_track_monitor_input_status (false);
                }
 
                RecordStateChanged (); /* emit signal */
@@ -1073,14 +1055,7 @@ Session::step_back_from_record ()
        if (g_atomic_int_compare_and_exchange (&_record_status, Recording, Enabled)) {
 
                if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
-                       boost::shared_ptr<RouteList> rl = routes.reader ();
-                       for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
-                               boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
-                               if (tr && tr->record_enabled ()) {
-                                       //cerr << "switching from input" << __FILE__ << __LINE__ << endl << endl;
-                                       tr->monitor_input (false);
-                               }
-                       }
+                        set_track_monitor_input_status (false);
                }
        }
 }
@@ -1486,8 +1461,9 @@ Session::new_midi_track (TrackMode mode, RouteGroup* route_group, uint32_t how_m
 
        control_id = ntracks() + nbusses();
 
+       bool const use_number = (how_many != 1);
+       
        while (how_many) {
-               bool const use_number = (how_many != 1);
                if (!find_route_name (name_template.empty() ? _("Midi") : name_template, ++track_id, track_name, sizeof(track_name), use_number)) {
                        error << "cannot find name for new midi track" << endmsg;
                        goto failed;
@@ -1659,8 +1635,9 @@ Session::new_audio_track (
 
        control_id = ntracks() + nbusses() + 1;
 
+       bool const use_number = (how_many != 1);
+       
        while (how_many) {
-               bool const use_number = (how_many != 1);
                if (!find_route_name (name_template.empty() ? _("Audio") : name_template, ++track_id, track_name, sizeof(track_name), use_number)) {
                        error << "cannot find name for new audio track" << endmsg;
                        goto failed;
@@ -1783,8 +1760,8 @@ Session::new_audio_route (int input_channels, int output_channels, RouteGroup* r
 
        control_id = ntracks() + nbusses() + 1;
 
+       bool const use_number = (how_many != 1);
        while (how_many) {
-               bool const use_number = (how_many != 1);
                if (!find_route_name (name_template.empty () ? _("Bus") : name_template, ++bus_id, bus_name, sizeof(bus_name), use_number)) {
                        error << "cannot find name for new audio bus" << endmsg;
                        goto failure;
@@ -2543,6 +2520,14 @@ Session::playlist_ranges_moved (list<Evoral::RangeMove<framepos_t> > const & ran
        }
 }
 
+void
+Session::playlist_regions_extended (list<Evoral::Range<framepos_t> > const & ranges)
+{
+       for (list<Evoral::Range<framepos_t> >::const_iterator i = ranges.begin(); i != ranges.end(); ++i) {
+               maybe_update_session_range (i->from, i->to);
+       }
+}
+
 /* Region management */
 
 boost::shared_ptr<Region>
@@ -2674,12 +2659,18 @@ Session::add_source (boost::shared_ptr<Source> source)
                                Analyser::queue_source_for_analysis (source, false);
                        }
                }
+
+               source->DropReferences.connect_same_thread (*this, boost::bind (&Session::remove_source, this, boost::weak_ptr<Source> (source)));
         }
 }
 
 void
 Session::remove_source (boost::weak_ptr<Source> src)
 {
+       if (_state_of_the_state & Deletion) {
+               return;
+       }
+       
        SourceMap::iterator i;
        boost::shared_ptr<Source> source = src.lock();
 
@@ -2691,7 +2682,6 @@ Session::remove_source (boost::weak_ptr<Source> src)
                Glib::Mutex::Lock lm (source_lock);
 
                if ((i = sources.find (source->id())) != sources.end()) {
-                       cerr << "Removing source " << source->name() << endl;
                        sources.erase (i);
                }
        }
@@ -2864,7 +2854,7 @@ Session::change_source_path_by_name (string path, string oldname, string newname
  *         (e.g. as returned by new_*_source_name)
  */
 string
-Session::new_source_path_from_name (DataType type, const string& name, bool as_stub)
+Session::new_source_path_from_name (DataType type, const string& name)
 {
        assert(name.find("/") == string::npos);
 
@@ -2872,9 +2862,9 @@ Session::new_source_path_from_name (DataType type, const string& name, bool as_s
 
        sys::path p;
        if (type == DataType::AUDIO) {
-               p = (as_stub ? sdir.sound_stub_path() : sdir.sound_path());
+               p = sdir.sound_path();
        } else if (type == DataType::MIDI) {
-               p = (as_stub ? sdir.midi_stub_path() : sdir.midi_path());
+               p = sdir.midi_path();
        } else {
                error << "Unknown source type, unable to create file path" << endmsg;
                return "";
@@ -2954,7 +2944,6 @@ Session::new_audio_source_name (const string& base, uint32_t nchan, uint32_t cha
                        SessionDirectory sdir((*i).path);
 
                        string spath = sdir.sound_path().to_string();
-                       string spath_stubs = sdir.sound_stub_path().to_string();
 
                        /* note that we search *without* the extension so that
                           we don't end up both "Audio 1-1.wav" and "Audio 1-1.caf" 
@@ -2962,8 +2951,7 @@ Session::new_audio_source_name (const string& base, uint32_t nchan, uint32_t cha
                           a file format change.
                        */
 
-                       if (matching_unsuffixed_filename_exists_in (spath, buf) ||
-                           matching_unsuffixed_filename_exists_in (spath_stubs, buf)) {
+                       if (matching_unsuffixed_filename_exists_in (spath, buf)) {
                                existing++;
                                break;
                        }
@@ -2987,10 +2975,10 @@ Session::new_audio_source_name (const string& base, uint32_t nchan, uint32_t cha
 
 /** Create a new within-session audio source */
 boost::shared_ptr<AudioFileSource>
-Session::create_audio_source_for_session (size_t n_chans, string const & n, uint32_t chan, bool destructive, bool as_stub)
+Session::create_audio_source_for_session (size_t n_chans, string const & n, uint32_t chan, bool destructive)
 {
        const string name    = new_audio_source_name (n, n_chans, chan, destructive);
-       const string path    = new_source_path_from_name(DataType::AUDIO, name, as_stub);
+       const string path    = new_source_path_from_name(DataType::AUDIO, name);
 
        return boost::dynamic_pointer_cast<AudioFileSource> (
                SourceFactory::createWritable (DataType::AUDIO, *this, path, string(), destructive, frame_rate()));
@@ -3047,7 +3035,7 @@ Session::new_midi_source_name (const string& base)
 
 /** Create a new within-session MIDI source */
 boost::shared_ptr<MidiSource>
-Session::create_midi_source_for_session (Track* track, string const & n, bool as_stub)
+Session::create_midi_source_for_session (Track* track, string const & n)
 {
        /* try to use the existing write source for the track, to keep numbering sane 
         */
@@ -3066,7 +3054,7 @@ Session::create_midi_source_for_session (Track* track, string const & n, bool as
        }
 
        const string name = new_midi_source_name (n);
-       const string path = new_source_path_from_name (DataType::MIDI, name, as_stub);
+       const string path = new_source_path_from_name (DataType::MIDI, name);
 
        return boost::dynamic_pointer_cast<SMFSource> (
                SourceFactory::createWritable (
@@ -3762,45 +3750,18 @@ BufferSet&
 Session::get_silent_buffers (ChanCount count)
 {
        return ProcessThread::get_silent_buffers (count);
-#if 0
-       assert(_silent_buffers->available() >= count);
-       _silent_buffers->set_count(count);
-
-       for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
-               for (size_t i= 0; i < count.get(*t); ++i) {
-                       _silent_buffers->get(*t, i).clear();
-               }
-       }
-
-       return *_silent_buffers;
-#endif
 }
 
 BufferSet&
 Session::get_scratch_buffers (ChanCount count)
 {
        return ProcessThread::get_scratch_buffers (count);
-#if 0
-       if (count != ChanCount::ZERO) {
-               assert(_scratch_buffers->available() >= count);
-               _scratch_buffers->set_count(count);
-       } else {
-               _scratch_buffers->set_count (_scratch_buffers->available());
-       }
-
-       return *_scratch_buffers;
-#endif
 }
 
 BufferSet&
 Session::get_mix_buffers (ChanCount count)
 {
        return ProcessThread::get_mix_buffers (count);
-#if 0
-       assert(_mix_buffers->available() >= count);
-       _mix_buffers->set_count(count);
-       return *_mix_buffers;
-#endif
 }
 
 uint32_t