better error message if VST SDK is not installed and neeed
[ardour.git] / libs / ardour / session.cc
index 2cf86434d7cbcab1941dd70cf4494ab2b81124e7..987a8377efa7d7e0afa85d28b6e9497b25001ccf 100644 (file)
@@ -66,6 +66,7 @@
 #include <ardour/click.h>
 #include <ardour/data_type.h>
 #include <ardour/source_factory.h>
+#include <ardour/region_factory.h>
 
 #ifdef HAVE_LIBLO
 #include <ardour/osc.h>
@@ -277,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 ();
                }
@@ -333,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 ();
@@ -358,8 +360,8 @@ Session::Session (AudioEngine &eng,
                output_ac = AutoConnectOption (output_ac & ~AutoConnectMaster);
        }
 
-       input_auto_connect = input_ac;
-       output_auto_connect = output_ac;
+       Config->set_input_auto_connect (input_ac);
+       Config->set_output_auto_connect (output_ac);
 
        if (second_stage_init (new_session)) {
                throw failed_constructor ();
@@ -429,6 +431,10 @@ Session::~Session ()
        }
 
        AudioDiskstream::free_working_buffers();
+
+       /* this should cause deletion of the auditioner */
+
+       // auditioner.reset ();
        
 #undef TRACK_DESTRUCTION
 #ifdef TRACK_DESTRUCTION
@@ -462,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 ();
@@ -939,7 +952,7 @@ Session::reset_input_monitor_state ()
                for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
                        if ((*i)->record_enabled ()) {
                                //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
-                               (*i)->monitor_input (Config->get_use_hardware_monitoring() && !Config->get_auto_input());
+                               (*i)->monitor_input (Config->get_monitoring_model() == HardwareMonitoring && !Config->get_auto_input());
                        }
                }
        } else {
@@ -948,7 +961,7 @@ Session::reset_input_monitor_state ()
                for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
                        if ((*i)->record_enabled ()) {
                                //cerr << "switching to input = " << !Config->get_auto_input() << __FILE__ << __LINE__ << endl << endl;
-                               (*i)->monitor_input (Config->get_use_hardware_monitoring());
+                               (*i)->monitor_input (Config->get_monitoring_model() == HardwareMonitoring);
                        }
                }
        }
@@ -1149,7 +1162,7 @@ Session::enable_record ()
                _last_record_location = _transport_frame;
                send_mmc_in_another_thread (MIDI::MachineControl::cmdRecordStrobe);
 
-               if (Config->get_use_hardware_monitoring() && Config->get_auto_input()) {
+               if (Config->get_monitoring_model() == HardwareMonitoring && Config->get_auto_input()) {
                        boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
                        for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
                                if ((*i)->record_enabled ()) {
@@ -1179,7 +1192,7 @@ Session::disable_record (bool rt_context, bool force)
 
                send_mmc_in_another_thread (MIDI::MachineControl::cmdRecordExit);
 
-               if (Config->get_use_hardware_monitoring() && Config->get_auto_input()) {
+               if (Config->get_monitoring_model() == HardwareMonitoring && Config->get_auto_input()) {
                        boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
                        
                        for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
@@ -1202,7 +1215,7 @@ Session::step_back_from_record ()
 {
        g_atomic_int_set (&_record_status, Enabled);
 
-       if (Config->get_use_hardware_monitoring()) {
+       if (Config->get_monitoring_model() == HardwareMonitoring) {
                boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
                
                for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
@@ -1219,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);
@@ -1309,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();
 
@@ -1540,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";
        
@@ -1610,13 +1629,13 @@ Session::new_audio_track (int input_channels, int output_channels, TrackMode mod
                        
                } while (track_id < (UINT_MAX-1));
 
-               if (input_auto_connect & AutoConnectPhysical) {
+               if (Config->get_input_auto_connect() & AutoConnectPhysical) {
                        nphysical_in = min (n_physical_inputs, (uint32_t) physinputs.size());
                } else {
                        nphysical_in = 0;
                }
                
-               if (output_auto_connect & AutoConnectPhysical) {
+               if (Config->get_output_auto_connect() & AutoConnectPhysical) {
                        nphysical_out = min (n_physical_outputs, (uint32_t) physinputs.size());
                } else {
                        nphysical_out = 0;
@@ -1636,7 +1655,7 @@ Session::new_audio_track (int input_channels, int output_channels, TrackMode mod
                                        
                                        port = "";
                                        
-                                       if (input_auto_connect & AutoConnectPhysical) {
+                                       if (Config->get_input_auto_connect() & AutoConnectPhysical) {
                                                port = physinputs[(channels_used+x)%nphysical_in];
                                        } 
                                        
@@ -1650,9 +1669,9 @@ Session::new_audio_track (int input_channels, int output_channels, TrackMode mod
                                
                                port = "";
                                
-                               if (nphysical_out && (output_auto_connect & AutoConnectPhysical)) {
+                               if (nphysical_out && (Config->get_output_auto_connect() & AutoConnectPhysical)) {
                                        port = physoutputs[(channels_used+x)%nphysical_out];
-                               } else if (output_auto_connect & AutoConnectMaster) {
+                               } else if (Config->get_output_auto_connect() & AutoConnectMaster) {
                                        if (_master_out) {
                                                port = _master_out->input (x%_master_out->n_inputs())->name();
                                        }
@@ -1756,7 +1775,7 @@ Session::new_audio_route (int input_channels, int output_channels, uint32_t how_
                                
                                port = "";
                                
-                               if (input_auto_connect & AutoConnectPhysical) {
+                               if (Config->get_input_auto_connect() & AutoConnectPhysical) {
                                        port = physinputs[((n+x)%n_physical_inputs)];
                                } 
                                
@@ -1769,9 +1788,9 @@ Session::new_audio_route (int input_channels, int output_channels, uint32_t how_
                                
                                port = "";
                                
-                               if (output_auto_connect & AutoConnectPhysical) {
+                               if (Config->get_output_auto_connect() & AutoConnectPhysical) {
                                        port = physoutputs[((n+x)%n_physical_outputs)];
-                               } else if (output_auto_connect & AutoConnectMaster) {
+                               } else if (Config->get_output_auto_connect() & AutoConnectMaster) {
                                        if (_master_out) {
                                                port = _master_out->input (x%_master_out->n_inputs())->name();
                                        }
@@ -1825,7 +1844,10 @@ Session::add_routes (RouteList& new_routes, bool save)
        }
 
        for (RouteList::iterator x = new_routes.begin(); x != new_routes.end(); ++x) {
-               (*x)->solo_changed.connect (sigc::bind (mem_fun (*this, &Session::route_solo_changed), (*x)));
+               
+               boost::weak_ptr<Route> wpr (*x);
+
+               (*x)->solo_changed.connect (sigc::bind (mem_fun (*this, &Session::route_solo_changed), wpr));
                (*x)->mute_changed.connect (mem_fun (*this, &Session::route_mute_changed));
                (*x)->output_changed.connect (mem_fun (*this, &Session::set_worst_io_latencies_x));
                (*x)->redirects_changed.connect (mem_fun (*this, &Session::update_latency_compensation_proxy));
@@ -1875,19 +1897,20 @@ 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.
                */
 
                if (route == _master_out) {
-                       _master_out = shared_ptr<Route> ((Route*) 0);
+                       _master_out = shared_ptr<Route> ();
                }
 
                if (route == _control_out) {
-                       _control_out = shared_ptr<Route> ((Route*) 0);
+                       _control_out = shared_ptr<Route> ();
 
                        /* cancel control outs for all routes */
 
@@ -1924,14 +1947,26 @@ 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);
        
-       /* XXX should we disconnect from the Route's signals ? */
+       /* get rid of it from the dead wood collection in the route list manager */
+
+       /* XXX i think this is unsafe as it currently stands, but i am not sure. (pd, october 2nd, 2006) */
 
-       save_state (_current_snapshot_name);
+       routes.flush ();
 
        /* try to cause everyone to drop their references */
 
        route->drop_references ();
+
+       /* save the new state of the world */
+
+       if (save_state (_current_snapshot_name)) {
+               save_history (_current_snapshot_name);
+       }
 }      
 
 void
@@ -1941,7 +1976,7 @@ Session::route_mute_changed (void* src)
 }
 
 void
-Session::route_solo_changed (void* src, shared_ptr<Route> route)
+Session::route_solo_changed (void* src, boost::weak_ptr<Route> wpr)
 {      
        if (solo_update_disabled) {
                // We know already
@@ -1949,8 +1984,15 @@ Session::route_solo_changed (void* src, shared_ptr<Route> route)
        }
        
        bool is_track;
-       
-       is_track = (dynamic_cast<AudioTrack*>(route.get()) != 0);
+       boost::shared_ptr<Route> route = wpr.lock ();
+
+       if (!route) {
+               /* should not happen */
+               error << string_compose (_("programming error: %1"), X_("invalid route weak ptr passed to route_solo_changed")) << endmsg;
+               return;
+       }
+
+       is_track = (boost::dynamic_pointer_cast<AudioTrack>(route) != 0);
        
        shared_ptr<RouteList> r = routes.reader ();
 
@@ -2426,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);
@@ -2442,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);
 
@@ -2502,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
@@ -2623,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>
@@ -3256,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;
@@ -3634,7 +3692,12 @@ Session::write_one_audio_track (AudioTrack& track, nframes_t start, nframes_t le
                                afs->build_peaks ();
                        }
                }
-               
+
+               /* construct a region to represent the bounced material */
+
+               boost::shared_ptr<Region> aregion = RegionFactory::create (srcs, 0, srcs.front()->length(), 
+                                                                          region_name_from_path (srcs.front()->name()));
+
                ret = 0;
        }
                
@@ -3702,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;
 }
+