Allow naming of new tracks/busses in the add route dialogue (#3376).
[ardour.git] / libs / ardour / session.cc
index 28822ef6f9a830821f868dcb539022aba5cf5e78..b7d04c379fefd477897fdfb9c8e3575c87226568 100644 (file)
@@ -323,7 +323,6 @@ Session::destroy ()
        playlists.reset ();
 
        delete _locations;
-        delete _speakers;
 
        DEBUG_TRACE (DEBUG::Destruction, "Session::destroy() done\n");
 
@@ -660,6 +659,7 @@ Session::when_engine_running ()
        BootMessage (_("Connect to engine"));
 
        _engine.set_session (this);
+        _engine.update_total_latencies ();
 }
 
 void
@@ -671,7 +671,6 @@ Session::hookup_io ()
 
        _state_of_the_state = StateOfTheState (_state_of_the_state | InitialConnecting);
 
-
        if (!auditioner) {
 
                /* we delay creating the auditioner till now because
@@ -679,13 +678,12 @@ Session::hookup_io ()
                */
 
                try {
-                       Auditioner* a = new Auditioner (*this);
+                       boost::shared_ptr<Auditioner> a (new Auditioner (*this));
                        if (a->init()) {
-                               delete a;
-                               throw failed_constructor();
+                               throw failed_constructor ();
                        }
                        a->use_new_diskstream ();
-                       auditioner.reset (a);
+                       auditioner = a;
                }
 
                catch (failed_constructor& err) {
@@ -727,10 +725,8 @@ Session::hookup_io ()
                                /* relax */
                                 
                        } else {
-                                
-                               (*x)->listen_via (_monitor_out,
-                                                 (Config->get_listen_position() == AfterFaderListen ? PostFader : PreFader),
-                                                 false, false);
+
+                               (*x)->listen_via_monitor ();
                        }
                }
        }
@@ -1255,43 +1251,6 @@ Session::set_block_size (pframes_t nframes)
        }
 }
 
-void
-Session::set_default_fade (float /*steepness*/, float /*fade_msecs*/)
-{
-#if 0
-       framecnt_t fade_frames;
-
-       /* Don't allow fade of less 1 frame */
-
-       if (fade_msecs < (1000.0 * (1.0/_current_frame_rate))) {
-
-               fade_msecs = 0;
-               fade_frames = 0;
-
-       } else {
-
-               fade_frames = (framecnt_t) floor (fade_msecs * _current_frame_rate * 0.001);
-
-       }
-
-       default_fade_msecs = fade_msecs;
-       default_fade_steepness = steepness;
-
-       {
-               // jlc, WTF is this!
-               Glib::RWLock::ReaderLock lm (route_lock);
-               AudioRegion::set_default_fade (steepness, fade_frames);
-       }
-
-       set_dirty();
-
-       /* XXX have to do this at some point */
-       /* foreach region using default fade, reset, then
-          refill_all_diskstream_buffers ();
-       */
-#endif
-}
-
 struct RouteSorter {
     /** @return true to run r1 before r2, otherwise false */
     bool operator() (boost::shared_ptr<Route> r1, boost::shared_ptr<Route> r2) {
@@ -1458,20 +1417,29 @@ Session::resort_routes_using (boost::shared_ptr<RouteList> r)
 
 }
 
-/** Find the route name starting with \a base with the lowest \a id.
+/** Find a route name starting with \a base, maybe followed by the
+ *  lowest \a id.  \a id will always be added if \a definitely_add_number
+ *  is true on entry; otherwise it will only be added if required
+ *  to make the name unique.
  *
- * Names are constructed like e.g. "Audio 3" for base="Audio" and id=3.
- * The available route name with the lowest ID will be used, and \a id
- * will be set to the ID.
+ *  Names are constructed like e.g. "Audio 3" for base="Audio" and id=3.
+ *  The available route name with the lowest ID will be used, and \a id
+ *  will be set to the ID.
  *
- * \return false if a route name could not be found, and \a track_name
- * and \a id do not reflect a free route name.
+ *  \return false if a route name could not be found, and \a track_name
+ *  and \a id do not reflect a free route name.
  */
 bool
-Session::find_route_name (const char* base, uint32_t& id, char* name, size_t name_len)
+Session::find_route_name (string const & base, uint32_t& id, char* name, size_t name_len, bool definitely_add_number)
 {
+       if (!definitely_add_number && route_by_name (base) == 0) {
+               /* juse use the base */
+               snprintf (name, name_len, "%s", base.c_str());
+               return true;
+       }
+               
        do {
-               snprintf (name, name_len, "%s %" PRIu32, base, id);
+               snprintf (name, name_len, "%s %" PRIu32, base.c_str(), id);
 
                if (route_by_name (name) == 0) {
                        return true;
@@ -1499,9 +1467,11 @@ Session::count_existing_route_channels (ChanCount& in, ChanCount& out)
        }
 }
 
-/** Caller must not hold process lock */
+/** Caller must not hold process lock
+ *  @param name_template string to use for the start of the name, or "" to use "Midi".
+ */
 list<boost::shared_ptr<MidiTrack> >
-Session::new_midi_track (TrackMode mode, RouteGroup* route_group, uint32_t how_many)
+Session::new_midi_track (TrackMode mode, RouteGroup* route_group, uint32_t how_many, string name_template)
 {
        char track_name[32];
        uint32_t track_id = 0;
@@ -1517,7 +1487,7 @@ Session::new_midi_track (TrackMode mode, RouteGroup* route_group, uint32_t how_m
        control_id = ntracks() + nbusses();
 
        while (how_many) {
-               if (!find_route_name ("Midi", ++track_id, track_name, sizeof(track_name))) {
+               if (!find_route_name (name_template.empty() ? _("Midi") : name_template, ++track_id, track_name, sizeof(track_name), false)) {
                        error << "cannot find name for new midi track" << endmsg;
                        goto failed;
                }
@@ -1525,20 +1495,17 @@ Session::new_midi_track (TrackMode mode, RouteGroup* route_group, uint32_t how_m
                 boost::shared_ptr<MidiTrack> track;
                 
                try {
-                       MidiTrack* mt = new MidiTrack (*this, track_name, Route::Flag (0), mode);
+                       track.reset (new MidiTrack (*this, track_name, Route::Flag (0), mode));
 
-                       if (mt->init ()) {
-                               delete mt;
+                       if (track->init ()) {
                                goto failed;
                        }
 
-                       mt->use_new_diskstream();
+                       track->use_new_diskstream();
 
 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
-                       boost_debug_shared_ptr_mark_interesting (mt, "Track");
+                       boost_debug_shared_ptr_mark_interesting (track.get(), "Track");
 #endif
-                       track = boost::shared_ptr<MidiTrack>(mt);
-
                        {
                                Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
                                if (track->input()->ensure_io (ChanCount(DataType::MIDI, 1), false, this)) {
@@ -1670,9 +1637,13 @@ Session::auto_connect_route (
        existing_outputs += route->n_outputs();
 }
 
-/** Caller must not hold process lock */
+/** Caller must not hold process lock
+ *  @param name_template string to use for the start of the name, or "" to use "Audio".
+ */
 list< boost::shared_ptr<AudioTrack> >
-Session::new_audio_track (int input_channels, int output_channels, TrackMode mode, RouteGroup* route_group, uint32_t how_many)
+Session::new_audio_track (
+       int input_channels, int output_channels, TrackMode mode, RouteGroup* route_group, uint32_t how_many, string name_template
+       )
 {
        char track_name[32];
        uint32_t track_id = 0;
@@ -1688,7 +1659,7 @@ Session::new_audio_track (int input_channels, int output_channels, TrackMode mod
        control_id = ntracks() + nbusses() + 1;
 
        while (how_many) {
-               if (!find_route_name ("Audio", ++track_id, track_name, sizeof(track_name))) {
+               if (!find_route_name (name_template.empty() ? _("Audio") : name_template, ++track_id, track_name, sizeof(track_name), false)) {
                        error << "cannot find name for new audio track" << endmsg;
                        goto failed;
                }
@@ -1696,20 +1667,17 @@ Session::new_audio_track (int input_channels, int output_channels, TrackMode mod
                 boost::shared_ptr<AudioTrack> track;
                 
                try {
-                       AudioTrack* at = new AudioTrack (*this, track_name, Route::Flag (0), mode);
+                       track.reset (new AudioTrack (*this, track_name, Route::Flag (0), mode));
 
-                       if (at->init ()) {
-                               delete at;
+                       if (track->init ()) {
                                goto failed;
                        }
 
-                       at->use_new_diskstream();
+                       track->use_new_diskstream();
 
 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
-                       boost_debug_shared_ptr_mark_interesting (at, "Track");
+                       boost_debug_shared_ptr_mark_interesting (track.get(), "Track");
 #endif
-                       track = boost::shared_ptr<AudioTrack>(at);
-
                        {
                                Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
 
@@ -1795,9 +1763,11 @@ Session::set_remote_control_ids ()
        }
 }
 
-/** Caller must not hold process lock */
+/** Caller must not hold process lock.
+ *  @param name_template string to use for the start of the name, or "" to use "Bus".
+ */
 RouteList
-Session::new_audio_route (int input_channels, int output_channels, RouteGroup* route_group, uint32_t how_many)
+Session::new_audio_route (int input_channels, int output_channels, RouteGroup* route_group, uint32_t how_many, string name_template)
 {
        char bus_name[32];
        uint32_t bus_id = 0;
@@ -1812,24 +1782,21 @@ Session::new_audio_route (int input_channels, int output_channels, RouteGroup* r
        control_id = ntracks() + nbusses() + 1;
 
        while (how_many) {
-               if (!find_route_name ("Bus", ++bus_id, bus_name, sizeof(bus_name))) {
+               if (!find_route_name (name_template.empty () ? _("Bus") : name_template, ++bus_id, bus_name, sizeof(bus_name), false)) {
                        error << "cannot find name for new audio bus" << endmsg;
                        goto failure;
                }
 
                try {
-                       Route* rt = new Route (*this, bus_name, Route::Flag(0), DataType::AUDIO);
+                       boost::shared_ptr<Route> bus (new Route (*this, bus_name, Route::Flag(0), DataType::AUDIO));
 
-                       if (rt->init ()) {
-                               delete rt;
+                       if (bus->init ()) {
                                goto failure;
                        }
 
 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
-                       boost_debug_shared_ptr_mark_interesting (rt, "Route");
+                       boost_debug_shared_ptr_mark_interesting (bus.get(), "Route");
 #endif
-                        boost::shared_ptr<Route> bus (rt);
-
                        {
                                Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
 
@@ -1910,7 +1877,7 @@ Session::new_route_from_template (uint32_t how_many, const std::string& template
                std::string node_name = IO::name_from_state (*node_copy.children().front());
 
                /* generate a new name by adding a number to the end of the template name */
-               if (!find_route_name (node_name.c_str(), ++number, name, sizeof(name))) {
+               if (!find_route_name (node_name.c_str(), ++number, name, sizeof(name), true)) {
                        fatal << _("Session: UINT_MAX routes? impossible!") << endmsg;
                        /*NOTREACHED*/
                }
@@ -2037,9 +2004,7 @@ Session::add_routes (RouteList& new_routes, bool save)
                        } else if ((*x)->is_master()) {
                                /* relax */
                        } else {
-                               (*x)->listen_via (_monitor_out,
-                                                 (Config->get_listen_position() == AfterFaderListen ? PostFader : PreFader),
-                                                 false, false);
+                               (*x)->listen_via_monitor ();
                        }
                }
 
@@ -2128,7 +2093,7 @@ Session::add_internal_sends (boost::shared_ptr<Route> dest, Placement p, boost::
                        continue;
                }
 
-               (*i)->listen_via (dest, p, true, true);
+               (*i)->listen_via (dest, p);
        }
 
        graph_reordered ();
@@ -2244,7 +2209,7 @@ Session::route_listen_changed (void* /*src*/, boost::weak_ptr<Route> wpr)
                return;
        }
 
-       if (route->listening()) {
+       if (route->listening_via_monitor ()) {
 
                if (Config->get_exclusive_solo()) {
                        /* new listen: disable all other listen */
@@ -2263,6 +2228,8 @@ Session::route_listen_changed (void* /*src*/, boost::weak_ptr<Route> wpr)
 
                _listen_cnt--;
        }
+
+       update_route_solo_state ();
 }
 void
 Session::route_solo_isolated_changed (void* /*src*/, boost::weak_ptr<Route> wpr)
@@ -2400,7 +2367,7 @@ Session::update_route_solo_state (boost::shared_ptr<RouteList> r)
                        something_soloed = true;
                }
 
-               if (!(*i)->is_hidden() && (*i)->listening()) {
+               if (!(*i)->is_hidden() && (*i)->listening_via_monitor()) {
                        if (Config->get_solo_control_is_listen_control()) {
                                listeners++;
                        } else {
@@ -3929,22 +3896,10 @@ Session::update_have_rec_enabled_track ()
 void
 Session::listen_position_changed ()
 {
-       Placement p;
-
-       switch (Config->get_listen_position()) {
-       case AfterFaderListen:
-               p = PostFader;
-               break;
-
-       case PreFaderListen:
-               p = PreFader;
-               break;
-       }
-
        boost::shared_ptr<RouteList> r = routes.reader ();
 
        for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
-               (*i)->put_monitor_send_at (p);
+               (*i)->listen_position_changed ();
        }
 }
 
@@ -4211,10 +4166,10 @@ Session::ensure_search_path_includes (const string& path, DataType type)
        }
 }
 
-Speakers&
+boost::shared_ptr<Speakers>
 Session::get_speakers() 
 {
-        return *_speakers;
+        return _speakers;
 }
 
 list<string>
@@ -4239,5 +4194,18 @@ void
 Session::update_latency (bool playback)
 {
         DEBUG_TRACE (DEBUG::Latency, "JACK latency callback\n");
+
+       boost::shared_ptr<RouteList> r = routes.reader ();
+
+        if (playback) {
+                /* reverse the list so that we work backwards from the last route to run to the first */
+                reverse (r->begin(), r->end());
+        }
+
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+                DEBUG_TRACE (DEBUG::Latency, string_compose ("------------- Working on latency for %1\n", (*i)->name()));
+                (*i)->set_latency_ranges (playback);
+                DEBUG_TRACE (DEBUG::Latency, string_compose ("------------- Done working on latency for %1\n\n", (*i)->name()));
+        }
 }
 #endif