Optimize automation-event process splitting
[ardour.git] / gtk2_ardour / export_channel_selector.cc
index 8df698e8539a4e244cc6ac06f49b404a123f2b54..09dab030407c99b9c244bcb037390aa4cdd78d66 100644 (file)
 
 */
 
-#include "export_channel_selector.h"
-
 #include <algorithm>
 
 #include "pbd/convert.h"
 
-#include "ardour/audio_port.h"
 #include "ardour/audio_track.h"
-#include "ardour/audioengine.h"
 #include "ardour/audioregion.h"
 #include "ardour/export_channel_configuration.h"
-#include "ardour/export_handler.h"
 #include "ardour/io.h"
 #include "ardour/route.h"
 #include "ardour/session.h"
 
 #include <sstream>
 
-#include "i18n.h"
+#include "export_channel_selector.h"
+#include "route_sorter.h"
+
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace Glib;
@@ -101,6 +99,11 @@ PortExportChannelSelector::sync_with_manager ()
        split_checkbox.set_active (state->config->get_split());
        channels_spinbutton.set_value (state->config->get_n_chans());
 
+       /* when loading presets, config is ready set here (shared ptr)
+        * fill_route_list () -> update_channel_count () -> set_channel_count () -> update_config()
+        * will call config->clear_channels(); and clear the config
+        */
+       channel_view.set_config (ChannelConfigPtr ());
        fill_route_list ();
        channel_view.set_config (state->config);
 }
@@ -109,15 +112,19 @@ void
 PortExportChannelSelector::fill_route_list ()
 {
        channel_view.clear_routes ();
-       RouteList routes = *_session->get_routes();
+       RouteList routes = _session->get_routelist();
 
        /* Add master bus and then everything else */
 
-       ARDOUR::IO* master = _session->master_out()->output().get();
-       channel_view.add_route (master);
+       if (_session->master_out()) {
+               ARDOUR::IO* master = _session->master_out()->output().get();
+               channel_view.add_route (master);
+       }
+
+       routes.sort (Stripable::Sorter ());
 
        for (RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
-               if ((*it)->output().get() == master) {
+               if ((*it)->is_master () || (*it)->is_monitor ()) {
                        continue;
                }
                channel_view.add_route ((*it)->output().get());
@@ -155,7 +162,7 @@ PortExportChannelSelector::RouteCols::Channel &
 PortExportChannelSelector::RouteCols::get_channel (uint32_t channel)
 {
        if (channel > n_channels) {
-               std::cout << "Invalid channel cout for get_channel!" << std::endl;
+               std::cout << "Invalid channel count for get_channel!" << std::endl;
        }
 
        std::list<Channel>::iterator it = channels.begin();
@@ -180,16 +187,21 @@ PortExportChannelSelector::ChannelTreeView::ChannelTreeView (uint32_t max_channe
 
        /* Add column with toggle and text */
 
-       append_column_editable (_("Bus or Track"), route_cols.selected);
+       append_column_editable (_("Export"), route_cols.selected);
 
        Gtk::CellRendererText* text_renderer = Gtk::manage (new Gtk::CellRendererText);
        text_renderer->property_editable() = false;
+       text_renderer->set_alignment (0.0, 0.5);
 
-       Gtk::TreeView::Column* column = get_column (0);
+       Gtk::TreeView::Column* column = Gtk::manage (new Gtk::TreeView::Column);
+       column->set_title (_("Bus or Track"));
        column->pack_start (*text_renderer);
+       column->set_expand (true);
        column->add_attribute (text_renderer->property_text(), route_cols.name);
+       append_column  (*column);
 
        Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *>(get_column_cell_renderer (0));
+       toggle->set_alignment (0.0, 0.5);
        toggle->signal_toggled().connect (sigc::mem_fun (*this, &PortExportChannelSelector::ChannelTreeView::update_toggle_selection));
 
        static_columns = get_columns().size();
@@ -205,6 +217,7 @@ PortExportChannelSelector::ChannelTreeView::set_config (ChannelConfigPtr c)
 
        if (config == c) { return; }
        config = c;
+       if (!config) { return; }
 
        uint32_t i = 1;
        ExportChannelConfiguration::ChannelList chan_list = config->get_channels();
@@ -218,21 +231,21 @@ PortExportChannelSelector::ChannelTreeView::set_config (ChannelConfigPtr c)
                        }
 
                        Glib::RefPtr<Gtk::ListStore> port_list = r_it->get_value (route_cols.port_list_col);
-                       std::set<AudioPort *> route_ports;
-                       std::set<AudioPort *> intersection;
-                       std::map<AudioPort *, string> port_labels;
+                       std::set<boost::weak_ptr<AudioPort> > route_ports;
+                       std::set<boost::weak_ptr<AudioPort> > intersection;
+                       std::map<boost::weak_ptr<AudioPort>, string> port_labels;
 
                        for (Gtk::ListStore::Children::const_iterator p_it = port_list->children().begin(); p_it != port_list->children().end(); ++p_it) {
                                route_ports.insert ((*p_it)->get_value (route_cols.port_cols.port));
-                               port_labels.insert (std::pair<AudioPort*, string> ((*p_it)->get_value (route_cols.port_cols.port),
-                                                                                   (*p_it)->get_value (route_cols.port_cols.label)));
+                               port_labels.insert (make_pair ((*p_it)->get_value (route_cols.port_cols.port),
+                                                              (*p_it)->get_value (route_cols.port_cols.label)));
                        }
 
                        std::set_intersection (pec->get_ports().begin(), pec->get_ports().end(),
                                               route_ports.begin(), route_ports.end(),
-                                              std::insert_iterator<std::set<AudioPort *> > (intersection, intersection.begin()));
+                                              std::insert_iterator<std::set<boost::weak_ptr<AudioPort> > > (intersection, intersection.begin()));
 
-                       intersection.erase (0); // Remove "none" selection
+                       intersection.erase (boost::weak_ptr<AudioPort> ()); // Remove "none" selection
 
                        if (intersection.empty()) {
                                continue;
@@ -244,13 +257,13 @@ PortExportChannelSelector::ChannelTreeView::set_config (ChannelConfigPtr c)
                                /* Set previous channels (if any) to none */
 
                                for (uint32_t chn = 1; chn < i; ++chn) {
-                                       r_it->set_value (route_cols.get_channel (chn).port, (AudioPort *) 0);
+                                       r_it->set_value (route_cols.get_channel (chn).port, boost::weak_ptr<AudioPort> ());
                                        r_it->set_value (route_cols.get_channel (chn).label, string ("(none)"));
                                }
                        }
 
-                       AudioPort * port = *intersection.begin();
-                       std::map<AudioPort *, string>::iterator label_it = port_labels.find (port);
+                       boost::weak_ptr<AudioPort> port = *intersection.begin();
+                       std::map<boost::weak_ptr<AudioPort>, string>::iterator label_it = port_labels.find (port);
                        string label = label_it != port_labels.end() ? label_it->second : "error";
 
                        r_it->set_value (route_cols.get_channel (i).port, port);
@@ -294,7 +307,7 @@ PortExportChannelSelector::ChannelTreeView::add_route (ARDOUR::IO * io)
        row = *iter;
 
        row[route_cols.port_cols.selected] = false;
-       row[route_cols.port_cols.port] = 0;
+       row[route_cols.port_cols.port] = boost::weak_ptr<AudioPort> ();
        row[route_cols.port_cols.label] = "(none)";
 
 }
@@ -316,6 +329,7 @@ PortExportChannelSelector::ChannelTreeView::set_channel_count (uint32_t channels
 
                Gtk::CellRendererCombo* combo_renderer = Gtk::manage (new Gtk::CellRendererCombo);
                combo_renderer->property_text_column() = 2;
+               combo_renderer->property_has_entry() = false;
                column->pack_start (*combo_renderer);
 
                append_column (*column);
@@ -331,7 +345,7 @@ PortExportChannelSelector::ChannelTreeView::set_channel_count (uint32_t channels
                for (Gtk::ListStore::Children::iterator it = route_list->children().begin(); it != route_list->children().end(); ++it) {
                        std::string label = it->get_value(route_cols.selected) ? "(none)" : "";
                        it->set_value (route_cols.get_channel (n_channels).label, label);
-                       it->set_value (route_cols.get_channel (n_channels).port, (AudioPort *) 0);
+                       it->set_value (route_cols.get_channel (n_channels).port, boost::weak_ptr<AudioPort> ());
                }
 
                /* set column width */
@@ -355,7 +369,6 @@ PortExportChannelSelector::ChannelTreeView::set_channel_count (uint32_t channels
 void
 PortExportChannelSelector::ChannelTreeView::update_config ()
 {
-
        if (!config) { return; }
 
        config->clear_channels();
@@ -372,7 +385,8 @@ PortExportChannelSelector::ChannelTreeView::update_config ()
                                continue;
                        }
 
-                       AudioPort * port = row[route_cols.get_channel (i).port];
+                       boost::weak_ptr<AudioPort> weak_port = row[route_cols.get_channel (i).port];
+                       boost::shared_ptr<AudioPort> port = weak_port.lock ();
                        if (port) {
                                pec->add_port (port);
                        }
@@ -398,7 +412,7 @@ PortExportChannelSelector::ChannelTreeView::update_toggle_selection (std::string
                }
 
                iter->set_value (route_cols.get_channel (i).label, std::string("(none)"));
-               iter->set_value (route_cols.get_channel (i).port, (AudioPort *) 0);
+               iter->set_value (route_cols.get_channel (i).port, boost::weak_ptr<AudioPort> ());
 
                Glib::RefPtr<Gtk::ListStore> port_list = iter->get_value (route_cols.port_list_col);
                Gtk::ListStore::Children::iterator port_it;
@@ -407,7 +421,7 @@ PortExportChannelSelector::ChannelTreeView::update_toggle_selection (std::string
                for (port_it = port_list->children().begin(); port_it != port_list->children().end(); ++port_it) {
                        if (port_number == i) {
                                iter->set_value (route_cols.get_channel (i).label, (std::string) (*port_it)->get_value (route_cols.port_cols.label));
-                               iter->set_value (route_cols.get_channel (i).port, (AudioPort *) (*port_it)->get_value (route_cols.port_cols.port));
+                               iter->set_value (route_cols.get_channel (i).port, (*port_it)->get_value (route_cols.port_cols.port));
                        }
 
                        ++port_number;
@@ -429,7 +443,8 @@ PortExportChannelSelector::ChannelTreeView::update_selection_text (std::string c
        for (port_it = port_list->children().begin(); port_it != port_list->children().end(); ++port_it) {
                std::string label = port_it->get_value (route_cols.port_cols.label);
                if (label == new_text) {
-                       iter->set_value (route_cols.get_channel (channel).port, (AudioPort *) (*port_it)[route_cols.port_cols.port]);
+                       boost::weak_ptr<AudioPort> w = (*port_it)[route_cols.port_cols.port];
+                       iter->set_value (route_cols.get_channel (channel).port, w);
                }
        }
 
@@ -440,29 +455,33 @@ RegionExportChannelSelector::RegionExportChannelSelector (ARDOUR::Session * _ses
                                                           ProfileManagerPtr manager,
                                                           ARDOUR::AudioRegion const & region,
                                                           ARDOUR::AudioTrack & track) :
-  ExportChannelSelector (_session, manager),
-  region (region),
-  track (track),
-  region_chans (region.n_channels()),
-  track_chans (track.n_outputs().n_audio()),
-
-  raw_button (type_group),
-  fades_button (type_group),
-  processed_button (type_group)
+       ExportChannelSelector (_session, manager),
+       region (region),
+       track (track),
+       region_chans (region.n_channels()),
+       track_chans (track.n_outputs().n_audio()),
+
+       raw_button (type_group),
+       fades_button (type_group),
+       processed_button (type_group)
 {
        pack_start (vbox);
 
+       /* make fades+region gain be the default */
+
+       fades_button.set_active ();
+
        raw_button.set_label (string_compose (_("Region contents without fades nor region gain (channels: %1)"), region_chans));
        raw_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
-       vbox.pack_start (raw_button);
+       vbox.pack_start (raw_button, false, false);
 
        fades_button.set_label (string_compose (_("Region contents with fades and region gain (channels: %1)"), region_chans));
        fades_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
-       vbox.pack_start (fades_button);
+       vbox.pack_start (fades_button, false, false);
 
        processed_button.set_label (string_compose (_("Track output (channels: %1)"), track_chans));
        processed_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
-       vbox.pack_start (processed_button);
+       vbox.pack_start (processed_button, false, false);
 
        sync_with_manager();
        vbox.show_all_children ();
@@ -473,6 +492,24 @@ void
 RegionExportChannelSelector::sync_with_manager ()
 {
        state = manager->get_channel_configs().front();
+
+       if (!state) { return; }
+
+       switch (state->config->region_processing_type()) {
+       case RegionExportChannelFactory::None:
+               // Do nothing
+               break;
+       case RegionExportChannelFactory::Raw:
+               raw_button.set_active (true);
+               break;
+       case RegionExportChannelFactory::Fades:
+               fades_button.set_active (true);
+               break;
+       case RegionExportChannelFactory::Processed:
+               processed_button.set_active (true);
+               break;
+       }
+
        handle_selection ();
 }
 
@@ -485,20 +522,231 @@ RegionExportChannelSelector::handle_selection ()
 
        state->config->clear_channels ();
 
+       RegionExportChannelFactory::Type type = RegionExportChannelFactory::None;
        if (raw_button.get_active ()) {
-               factory.reset (new RegionExportChannelFactory (_session, region, track, RegionExportChannelFactory::Raw));
+               type = RegionExportChannelFactory::Raw;
        } else if (fades_button.get_active ()) {
-               factory.reset (new RegionExportChannelFactory (_session, region, track, RegionExportChannelFactory::Fades));
+               type = RegionExportChannelFactory::Fades;
        } else if (processed_button.get_active ()) {
-               factory.reset (new RegionExportChannelFactory(_session, region, track, RegionExportChannelFactory::Processed));
+               type = RegionExportChannelFactory::Processed;
        } else {
                CriticalSelectionChanged ();
                return;
        }
 
-       for (size_t chan = 0; chan < region_chans; ++chan) {
+       factory.reset (new RegionExportChannelFactory (_session, region, track, type));
+       state->config->set_region_processing_type (type);
+
+       const size_t cc = type == RegionExportChannelFactory::Processed ? track_chans : region_chans;
+       for (size_t chan = 0; chan < cc; ++chan) {
                state->config->register_channel (factory->create (chan));
        }
 
        CriticalSelectionChanged ();
 }
+
+/* Track export channel selector */
+
+TrackExportChannelSelector::TrackExportChannelSelector (ARDOUR::Session * session, ProfileManagerPtr manager)
+  : ExportChannelSelector(session, manager)
+  , track_output_button(_("Apply track/bus processing"))
+  , select_tracks_button (_("Select all tracks"))
+  , select_busses_button (_("Select all busses"))
+  , select_none_button (_("Deselect all"))
+{
+       pack_start(main_layout);
+
+       // Options
+       options_box.pack_start(track_output_button);
+       options_box.pack_start (select_tracks_button);
+       options_box.pack_start (select_busses_button);
+       options_box.pack_start (select_none_button);
+       main_layout.pack_start(options_box, false, false);
+
+       // Track scroller
+       track_scroller.add (track_view);
+       track_scroller.set_size_request (-1, 130);
+       track_scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
+       main_layout.pack_start(track_scroller);
+
+       // Track list
+       track_list = Gtk::ListStore::create (track_cols);
+       track_list->set_sort_column (track_cols.order_key, Gtk::SORT_ASCENDING);
+       track_view.set_model (track_list);
+       track_view.set_headers_visible (true);
+
+       track_view.append_column_editable (_("Export"), track_cols.selected);
+       Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *>(track_view.get_column_cell_renderer (0));
+       toggle->set_alignment (0.0, 0.5);
+
+       toggle->signal_toggled().connect (sigc::hide (sigc::mem_fun (*this, &TrackExportChannelSelector::update_config)));
+
+       Gtk::CellRendererText* text_renderer = Gtk::manage (new Gtk::CellRendererText);
+       text_renderer->property_editable() = false;
+       text_renderer->set_alignment (0.0, 0.5);
+
+       Gtk::TreeView::Column* column = Gtk::manage (new Gtk::TreeView::Column);
+       column->set_title (_("Track name"));
+
+       track_view.append_column  (*column);
+       column->pack_start (*text_renderer, false);
+       column->add_attribute (text_renderer->property_text(), track_cols.label);
+
+       select_tracks_button.signal_clicked().connect (sigc::mem_fun (*this, &TrackExportChannelSelector::select_tracks));
+       select_busses_button.signal_clicked().connect (sigc::mem_fun (*this, &TrackExportChannelSelector::select_busses));
+       select_none_button.signal_clicked().connect (sigc::mem_fun (*this, &TrackExportChannelSelector::select_none));
+
+       track_output_button.signal_clicked().connect (sigc::mem_fun (*this, &TrackExportChannelSelector::track_outputs_selected));
+
+       fill_list();
+
+       show_all_children ();
+}
+
+void
+TrackExportChannelSelector::sync_with_manager ()
+{
+       // TODO implement properly
+       update_config();
+}
+
+void
+TrackExportChannelSelector::select_tracks ()
+{
+       for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
+               Gtk::TreeModel::Row row = *it;
+               boost::shared_ptr<Route> route = row[track_cols.route];
+               if (boost::dynamic_pointer_cast<Track> (route)) {
+                       // it's a track
+                       row[track_cols.selected] = true;
+               }
+       }
+       update_config();
+}
+
+void
+TrackExportChannelSelector::select_busses ()
+{
+       for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
+               Gtk::TreeModel::Row row = *it;
+               boost::shared_ptr<Route> route = row[track_cols.route];
+               if (!boost::dynamic_pointer_cast<Track> (route)) {
+                       // it's not a track, must be a bus
+                       row[track_cols.selected] = true;
+               }
+       }
+       update_config();
+}
+
+void
+TrackExportChannelSelector::select_none ()
+{
+       for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
+               Gtk::TreeModel::Row row = *it;
+               row[track_cols.selected] = false;
+       }
+       update_config();
+}
+
+void
+TrackExportChannelSelector::track_outputs_selected ()
+{
+       update_config();
+}
+
+void
+TrackExportChannelSelector::fill_list()
+{
+       track_list->clear();
+       RouteList routes = _session->get_routelist();
+
+       for (RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
+               if (!boost::dynamic_pointer_cast<Track>(*it)) {
+                       // not a track, must be a bus
+                       if ((*it)->is_master () || (*it)->is_monitor ()) {
+                               continue;
+                       }
+                       if (!(*it)->active ()) {
+                               // don't include inactive busses
+                               continue;
+                       }
+
+                       // not monitor or master bus
+                       add_track (*it);
+               }
+       }
+       for (RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
+               if (boost::dynamic_pointer_cast<AudioTrack>(*it)) {
+                       if (!(*it)->active ()) {
+                               // don't include inactive tracks
+                               continue;
+                       }
+                       add_track (*it);
+               }
+       }
+}
+
+void
+TrackExportChannelSelector::add_track (boost::shared_ptr<Route> route)
+{
+       Gtk::TreeModel::iterator iter = track_list->append();
+       Gtk::TreeModel::Row row = *iter;
+
+       row[track_cols.selected] = false;
+       row[track_cols.label] = route->name();
+       row[track_cols.route] = route;
+       row[track_cols.order_key] = route->presentation_info().order();
+}
+
+void
+TrackExportChannelSelector::update_config()
+{
+       manager->clear_channel_configs();
+
+       for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
+               Gtk::TreeModel::Row row = *it;
+
+               if (!row[track_cols.selected]) {
+                       continue;
+               }
+
+               ExportProfileManager::ChannelConfigStatePtr state;
+
+               boost::shared_ptr<Route> route = row[track_cols.route];
+
+               if (track_output_button.get_active()) {
+                       uint32_t outs = route->n_outputs().n_audio();
+                       for (uint32_t i = 0; i < outs; ++i) {
+                               boost::shared_ptr<AudioPort> port = route->output()->audio (i);
+                               if (port) {
+                                       ExportChannelPtr channel (new PortExportChannel ());
+                                       PortExportChannel * pec = static_cast<PortExportChannel *> (channel.get());
+                                       pec->add_port(port);
+                                       if (!state) {
+                                               state = manager->add_channel_config();
+                                       }
+                                       state->config->register_channel(channel);
+                               }
+                       }
+               } else {
+                       std::list<ExportChannelPtr> list;
+                       RouteExportChannel::create_from_route (list, route);
+                       if (list.size () == 0) {
+                               continue;
+                       }
+                       state = manager->add_channel_config();
+                       state->config->register_channels (list);
+               }
+
+               if (state) {
+                       if (_session->config.get_track_name_number() && route->track_number() > 0) {
+                               state->config->set_name (string_compose ("%1-%2", route->track_number(), route->name()));
+                       } else {
+                               state->config->set_name (route->name());
+                       }
+               }
+
+       }
+
+       CriticalSelectionChanged ();
+}