better error message if VST SDK is not installed and neeed
[ardour.git] / libs / ardour / session.cc
index 3bcaaf3aca5af26500b3ae2c6bca110f677293e6..987a8377efa7d7e0afa85d28b6e9497b25001ccf 100644 (file)
@@ -278,7 +278,7 @@ Session::Session (AudioEngine &eng,
        
        new_session = !g_file_test (_path.c_str(), GFileTest (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR));
        if (new_session) {
-               if (create (new_session, mix_template, _engine.frame_rate() * 60 * 5)) {
+               if (create (new_session, mix_template, compute_initial_length())) {
                        cerr << "create failed\n";
                        throw failed_constructor ();
                }
@@ -334,6 +334,7 @@ Session::Session (AudioEngine &eng,
        first_stage_init (fullpath, snapshot_name);
 
        new_session = !g_file_test (_path.c_str(), GFileTest (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR));
+
        if (new_session) {
                if (create (new_session, 0, initial_length)) {
                        throw failed_constructor ();
@@ -430,6 +431,10 @@ Session::~Session ()
        }
 
        AudioDiskstream::free_working_buffers();
+
+       /* this should cause deletion of the auditioner */
+
+       // auditioner.reset ();
        
 #undef TRACK_DESTRUCTION
 #ifdef TRACK_DESTRUCTION
@@ -463,8 +468,15 @@ Session::~Session ()
        cerr << "delete audio regions\n";
 #endif /* TRACK_DESTRUCTION */
        
-       for (AudioRegionList::iterator i = audio_regions.begin(); i != audio_regions.end(); ++i) {
+       for (AudioRegionList::iterator i = audio_regions.begin(); i != audio_regions.end(); ) {
+               AudioRegionList::iterator tmp;
+
+               tmp = i;
+               ++tmp;
+
                i->second->drop_references ();
+
+               i = tmp;
        }
 
        audio_regions.clear ();
@@ -1220,8 +1232,8 @@ Session::maybe_enable_record ()
 {
        g_atomic_int_set (&_record_status, Enabled);
 
-       /* XXX this save should really happen in another thread. its needed so that
-          pending capture state can be recovered if we crash.
+       /* this function is currently called from somewhere other than an RT thread.
+          this save_state() call therefore doesn't impact anything.
        */
 
        save_state ("", true);
@@ -1310,10 +1322,10 @@ Session::set_frame_rate (nframes_t frames_per_second)
 
        sync_time_vars();
 
-       Route::set_automation_interval ((nframes_t) ceil ((double) frames_per_second * 0.25));
+       Route::set_automation_interval ((jack_nframes_t) ceil ((double) frames_per_second * 0.25));
 
        // XXX we need some equivalent to this, somehow
-       // DestructiveFileSource::setup_standard_crossfades (frames_per_second);
+       // SndFileSource::setup_standard_crossfades (frames_per_second);
 
        set_dirty();
 
@@ -1541,11 +1553,17 @@ Session::resort_routes_using (shared_ptr<RouteList> r)
        
        for (i = r->begin(); i != r->end(); ++i) {
                trace_terminal (*i, *i);
-       }
-       
+       }       
+
        RouteSorter cmp;
        r->sort (cmp);
        
+       /* don't leave dangling references to routes in Route::fed_by */
+
+       for (i = r->begin(); i != r->end(); ++i) {
+               (*i)->fed_by.clear ();
+       }
+
 #if 0
        cerr << "finished route resort\n";
        
@@ -1879,8 +1897,9 @@ Session::remove_route (shared_ptr<Route> route)
        {       
                RCUWriter<RouteList> writer (routes);
                shared_ptr<RouteList> rs = writer.get_copy ();
-               rs->remove (route);
                
+               rs->remove (route);
+
                /* deleting the master out seems like a dumb
                   idea, but its more of a UI policy issue
                   than our concern.
@@ -1928,6 +1947,10 @@ Session::remove_route (shared_ptr<Route> route)
        
        update_latency_compensation (false, false);
        set_dirty();
+
+       // We need to disconnect the routes inputs and outputs 
+       route->disconnect_inputs(NULL);
+       route->disconnect_outputs(NULL);
        
        /* get rid of it from the dead wood collection in the route list manager */
 
@@ -2445,15 +2468,21 @@ Session::add_region (boost::shared_ptr<Region> region)
        set_dirty();
        
        if (added) {
-               region->GoingAway.connect (sigc::bind (mem_fun (*this, &Session::remove_region), region));
-               region->StateChanged.connect (sigc::bind (mem_fun (*this, &Session::region_changed), region));
+               region->GoingAway.connect (sigc::bind (mem_fun (*this, &Session::remove_region), boost::weak_ptr<Region>(region)));
+               region->StateChanged.connect (sigc::bind (mem_fun (*this, &Session::region_changed), boost::weak_ptr<Region>(region)));
                AudioRegionAdded (ar); /* EMIT SIGNAL */
        }
 }
 
 void
-Session::region_changed (Change what_changed, boost::shared_ptr<Region> region)
+Session::region_changed (Change what_changed, boost::weak_ptr<Region> weak_region)
 {
+       boost::shared_ptr<Region> region (weak_region.lock ());
+
+       if (!region) {
+               return;
+       }
+
        if (what_changed & Region::HiddenChanged) {
                /* relay hidden changes */
                RegionHiddenChange (region);
@@ -2461,18 +2490,18 @@ Session::region_changed (Change what_changed, boost::shared_ptr<Region> region)
 }
 
 void
-Session::region_renamed (boost::shared_ptr<Region> region)
-{
-       add_region (region);
-}
-
-void
-Session::remove_region (boost::shared_ptr<Region> region)
+Session::remove_region (boost::weak_ptr<Region> weak_region)
 {
        AudioRegionList::iterator i;
+       boost::shared_ptr<Region> region (weak_region.lock ());
+
+       if (!region) {
+               return;
+       }
+
        boost::shared_ptr<AudioRegion> ar;
        bool removed = false;
-       
+
        { 
                Glib::Mutex::Lock lm (region_lock);
 
@@ -2521,7 +2550,7 @@ Session::find_whole_file_parent (boost::shared_ptr<AudioRegion> child)
                }
        } 
 
-       return boost::shared_ptr<AudioRegion> ((AudioRegion*) 0);
+       return boost::shared_ptr<AudioRegion> ();
 }      
 
 void
@@ -2642,29 +2671,27 @@ Session::remove_source (boost::weak_ptr<Source> src)
        boost::shared_ptr<Source> source = src.lock();
 
        if (!source) {
-               cerr << "removing a source DEAD\n";
-       } else {
-               cerr << "removing a source " << source->name () << endl;
+               return;
+       } 
+
+       { 
+               Glib::Mutex::Lock lm (audio_source_lock);
                
-               { 
-                       Glib::Mutex::Lock lm (audio_source_lock);
-                       
-                       if ((i = audio_sources.find (source->id())) != audio_sources.end()) {
-                               audio_sources.erase (i);
-                       } 
-               }
+               if ((i = audio_sources.find (source->id())) != audio_sources.end()) {
+                       audio_sources.erase (i);
+               } 
+       }
+       
+       if (!_state_of_the_state & InCleanup) {
                
-               if (!_state_of_the_state & InCleanup) {
-                       
-                       /* save state so we don't end up with a session file
-                          referring to non-existent sources.
-                       */
-                       
-                       save_state (_current_snapshot_name);
-               }
+               /* save state so we don't end up with a session file
+                  referring to non-existent sources.
+               */
                
-               SourceRemoved(source); /* EMIT SIGNAL */
+               save_state (_current_snapshot_name);
        }
+       
+       SourceRemoved(source); /* EMIT SIGNAL */
 }
 
 boost::shared_ptr<Source>
@@ -3275,7 +3302,19 @@ Session::remove_redirect (Redirect* redirect)
 nframes_t
 Session::available_capture_duration ()
 {
-       const double scale = 4096.0 / sizeof (Sample);
+       float sample_bytes_on_disk;
+
+       switch (Config->get_native_file_data_format()) {
+       case FormatFloat:
+               sample_bytes_on_disk = 4;
+               break;
+
+       case FormatInt24:
+               sample_bytes_on_disk = 3;
+               break;
+       }
+
+       double scale = 4096.0 / sample_bytes_on_disk;
 
        if (_total_free_4k_blocks * scale > (double) max_frames) {
                return max_frames;
@@ -3726,13 +3765,14 @@ Session::nbusses () const
 }
 
 void
-Session::add_curve(Curve *curve)
+Session::add_automation_list(AutomationList *al)
 {
-    curves[curve->id()] = curve;
+       automation_lists[al->id()] = al;
 }
 
-void
-Session::add_automation_list(AutomationList *al)
+nframes_t
+Session::compute_initial_length ()
 {
-    automation_lists[al->id()] = al;
+       return _engine.frame_rate() * 60 * 5;
 }
+