2 Copyright (C) 2008 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <gtkmm/menu.h>
26 #include "pbd/convert.h"
28 #include "ardour/audio_track.h"
29 #include "ardour/audioregion.h"
30 #include "ardour/export_channel_configuration.h"
31 #include "ardour/io.h"
32 #include "ardour/route.h"
33 #include "ardour/session.h"
35 #include "export_channel_selector.h"
36 #include "route_sorter.h"
42 using namespace ARDOUR;
45 PortExportChannelSelector::PortExportChannelSelector (ARDOUR::Session * session, ProfileManagerPtr manager) :
46 ExportChannelSelector (session, manager),
47 channels_label (_("Channels:"), Gtk::ALIGN_LEFT),
48 split_checkbox (_("Split to mono files")),
50 channel_view (max_channels)
52 channels_hbox.pack_start (channels_label, false, false, 0);
53 channels_hbox.pack_end (channels_spinbutton, false, false, 0);
55 channels_vbox.pack_start (channels_hbox, false, false, 0);
56 channels_vbox.pack_start (split_checkbox, false, false, 6);
58 channel_alignment.add (channel_scroller);
59 channel_alignment.set_padding (0, 0, 12, 0);
60 channel_scroller.add (channel_view);
61 channel_scroller.set_size_request (-1, 130);
62 channel_scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
64 pack_start (channels_vbox, false, false, 0);
65 pack_start (channel_alignment, true, true, 0);
67 /* Channels spinbutton */
69 channels_spinbutton.set_digits (0);
70 channels_spinbutton.set_increments (1, 2);
71 channels_spinbutton.set_range (1, max_channels);
72 channels_spinbutton.set_value (2);
74 channels_spinbutton.signal_value_changed().connect (sigc::mem_fun (*this, &PortExportChannelSelector::update_channel_count));
78 split_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &PortExportChannelSelector::update_split_state));
79 channel_view.CriticalSelectionChanged.connect (CriticalSelectionChanged.make_slot());
88 PortExportChannelSelector::~PortExportChannelSelector ()
93 PortExportChannelSelector::sync_with_manager ()
95 state = manager->get_channel_configs().front();
97 split_checkbox.set_active (state->config->get_split());
98 channels_spinbutton.set_value (state->config->get_n_chans());
100 /* when loading presets, config is ready set here (shared ptr)
101 * fill_route_list () -> update_channel_count () -> set_channel_count () -> update_config()
102 * will call config->clear_channels(); and clear the config
104 channel_view.set_config (ChannelConfigPtr ());
106 channel_view.set_config (state->config);
110 PortExportChannelSelector::fill_route_list ()
112 channel_view.clear_routes ();
113 RouteList routes = _session->get_routelist();
115 /* Add master bus and then everything else */
117 if (_session->master_out()) {
118 ARDOUR::IO* master = _session->master_out()->output().get();
119 channel_view.add_route (master);
122 routes.sort (Stripable::Sorter ());
124 for (RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
125 if ((*it)->is_master () || (*it)->is_monitor ()) {
128 channel_view.add_route ((*it)->output().get());
131 update_channel_count ();
135 PortExportChannelSelector::update_channel_count ()
137 uint32_t chans = static_cast<uint32_t> (channels_spinbutton.get_value());
138 channel_view.set_channel_count (chans);
139 CriticalSelectionChanged();
143 PortExportChannelSelector::update_split_state ()
145 state->config->set_split (split_checkbox.get_active());
146 CriticalSelectionChanged();
150 PortExportChannelSelector::RouteCols::add_channels (uint32_t chans)
153 channels.push_back (Channel (*this));
159 PortExportChannelSelector::RouteCols::Channel &
160 PortExportChannelSelector::RouteCols::get_channel (uint32_t channel)
162 if (channel > n_channels) {
163 std::cout << "Invalid channel count for get_channel!" << std::endl;
166 std::list<Channel>::iterator it = channels.begin();
168 while (channel > 1) { // Channel count starts from one!
176 PortExportChannelSelector::ChannelTreeView::ChannelTreeView (uint32_t max_channels) :
181 route_cols.add_channels (max_channels);
183 route_list = Gtk::ListStore::create(route_cols);
184 set_model (route_list);
186 /* Add column with toggle and text */
188 append_column_editable (_("Export"), route_cols.selected);
190 Gtk::CellRendererText* text_renderer = Gtk::manage (new Gtk::CellRendererText);
191 text_renderer->property_editable() = false;
192 text_renderer->set_alignment (0.0, 0.5);
194 Gtk::TreeView::Column* column = Gtk::manage (new Gtk::TreeView::Column);
195 column->set_title (_("Bus or Track"));
196 column->pack_start (*text_renderer);
197 column->set_expand (true);
198 column->add_attribute (text_renderer->property_text(), route_cols.name);
199 append_column (*column);
201 Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *>(get_column_cell_renderer (0));
202 toggle->set_alignment (0.0, 0.5);
203 toggle->signal_toggled().connect (sigc::mem_fun (*this, &PortExportChannelSelector::ChannelTreeView::update_toggle_selection));
205 static_columns = get_columns().size();
209 PortExportChannelSelector::ChannelTreeView::set_config (ChannelConfigPtr c)
211 /* TODO Without the following line, the state might get reset.
212 * Pointing to the same address does not mean the state of the configuration hasn't changed.
213 * In the current code this is safe, but the actual cause of the problem would be good to fix
216 if (config == c) { return; }
218 if (!config) { return; }
221 ExportChannelConfiguration::ChannelList chan_list = config->get_channels();
222 for (ExportChannelConfiguration::ChannelList::iterator c_it = chan_list.begin(); c_it != chan_list.end(); ++c_it) {
224 for (Gtk::ListStore::Children::iterator r_it = route_list->children().begin(); r_it != route_list->children().end(); ++r_it) {
226 ARDOUR::PortExportChannel * pec;
227 if (!(pec = dynamic_cast<ARDOUR::PortExportChannel *> (c_it->get()))) {
231 Glib::RefPtr<Gtk::ListStore> port_list = r_it->get_value (route_cols.port_list_col);
232 std::set<boost::weak_ptr<AudioPort> > route_ports;
233 std::set<boost::weak_ptr<AudioPort> > intersection;
234 std::map<boost::weak_ptr<AudioPort>, string> port_labels;
236 for (Gtk::ListStore::Children::const_iterator p_it = port_list->children().begin(); p_it != port_list->children().end(); ++p_it) {
237 route_ports.insert ((*p_it)->get_value (route_cols.port_cols.port));
238 port_labels.insert (make_pair ((*p_it)->get_value (route_cols.port_cols.port),
239 (*p_it)->get_value (route_cols.port_cols.label)));
242 std::set_intersection (pec->get_ports().begin(), pec->get_ports().end(),
243 route_ports.begin(), route_ports.end(),
244 std::insert_iterator<std::set<boost::weak_ptr<AudioPort> > > (intersection, intersection.begin()));
246 intersection.erase (boost::weak_ptr<AudioPort> ()); // Remove "none" selection
248 if (intersection.empty()) {
252 if (!r_it->get_value (route_cols.selected)) {
253 r_it->set_value (route_cols.selected, true);
255 /* Set previous channels (if any) to none */
257 for (uint32_t chn = 1; chn < i; ++chn) {
258 r_it->set_value (route_cols.get_channel (chn).port, boost::weak_ptr<AudioPort> ());
259 r_it->set_value (route_cols.get_channel (chn).label, string ("(none)"));
263 boost::weak_ptr<AudioPort> port = *intersection.begin();
264 std::map<boost::weak_ptr<AudioPort>, string>::iterator label_it = port_labels.find (port);
265 string label = label_it != port_labels.end() ? label_it->second : "error";
267 r_it->set_value (route_cols.get_channel (i).port, port);
268 r_it->set_value (route_cols.get_channel (i).label, label);
276 PortExportChannelSelector::ChannelTreeView::add_route (ARDOUR::IO * io)
278 Gtk::TreeModel::iterator iter = route_list->append();
279 Gtk::TreeModel::Row row = *iter;
281 row[route_cols.selected] = false;
282 row[route_cols.name] = io->name();
283 row[route_cols.io] = io;
285 /* Initialize port list */
287 Glib::RefPtr<Gtk::ListStore> port_list = Gtk::ListStore::create (route_cols.port_cols);
288 row[route_cols.port_list_col] = port_list;
290 uint32_t outs = io->n_ports().n_audio();
291 for (uint32_t i = 0; i < outs; ++i) {
292 iter = port_list->append();
295 row[route_cols.port_cols.selected] = false;
296 row[route_cols.port_cols.port] = io->audio (i);
298 std::ostringstream oss;
299 oss << "Out-" << (i + 1);
301 row[route_cols.port_cols.label] = oss.str();
304 iter = port_list->append();
307 row[route_cols.port_cols.selected] = false;
308 row[route_cols.port_cols.port] = boost::weak_ptr<AudioPort> ();
309 row[route_cols.port_cols.label] = "(none)";
314 PortExportChannelSelector::ChannelTreeView::set_channel_count (uint32_t channels)
316 int offset = channels - n_channels;
321 std::ostringstream oss;
326 Gtk::TreeView::Column* column = Gtk::manage (new Gtk::TreeView::Column (oss.str()));
328 Gtk::CellRendererCombo* combo_renderer = Gtk::manage (new Gtk::CellRendererCombo);
329 combo_renderer->property_text_column() = 2;
330 combo_renderer->property_has_entry() = false;
331 column->pack_start (*combo_renderer);
333 append_column (*column);
335 column->add_attribute (combo_renderer->property_text(), route_cols.get_channel(n_channels).label);
336 column->add_attribute (combo_renderer->property_model(), route_cols.port_list_col);
337 column->add_attribute (combo_renderer->property_editable(), route_cols.selected);
339 combo_renderer->signal_edited().connect (sigc::bind (sigc::mem_fun (*this, &PortExportChannelSelector::ChannelTreeView::update_selection_text), n_channels));
341 /* put data into view */
343 for (Gtk::ListStore::Children::iterator it = route_list->children().begin(); it != route_list->children().end(); ++it) {
344 std::string label = it->get_value(route_cols.selected) ? "(none)" : "";
345 it->set_value (route_cols.get_channel (n_channels).label, label);
346 it->set_value (route_cols.get_channel (n_channels).port, boost::weak_ptr<AudioPort> ());
349 /* set column width */
351 get_column (static_columns + n_channels - 1)->set_min_width (80);
359 remove_column (*get_column (n_channels + static_columns));
368 PortExportChannelSelector::ChannelTreeView::update_config ()
370 if (!config) { return; }
372 config->clear_channels();
374 for (uint32_t i = 1; i <= n_channels; ++i) {
376 ExportChannelPtr channel (new PortExportChannel ());
377 PortExportChannel * pec = static_cast<PortExportChannel *> (channel.get());
379 for (Gtk::ListStore::Children::iterator it = route_list->children().begin(); it != route_list->children().end(); ++it) {
380 Gtk::TreeModel::Row row = *it;
382 if (!row[route_cols.selected]) {
386 boost::weak_ptr<AudioPort> weak_port = row[route_cols.get_channel (i).port];
387 boost::shared_ptr<AudioPort> port = weak_port.lock ();
389 pec->add_port (port);
393 config->register_channel (channel);
396 CriticalSelectionChanged ();
400 PortExportChannelSelector::ChannelTreeView::update_toggle_selection (std::string const & path)
402 Gtk::TreeModel::iterator iter = get_model ()->get_iter (path);
403 bool selected = iter->get_value (route_cols.selected);
405 for (uint32_t i = 1; i <= n_channels; ++i) {
408 iter->set_value (route_cols.get_channel (i).label, std::string (""));
412 iter->set_value (route_cols.get_channel (i).label, std::string("(none)"));
413 iter->set_value (route_cols.get_channel (i).port, boost::weak_ptr<AudioPort> ());
415 Glib::RefPtr<Gtk::ListStore> port_list = iter->get_value (route_cols.port_list_col);
416 Gtk::ListStore::Children::iterator port_it;
417 uint32_t port_number = 1;
419 for (port_it = port_list->children().begin(); port_it != port_list->children().end(); ++port_it) {
420 if (port_number == i) {
421 iter->set_value (route_cols.get_channel (i).label, (std::string) (*port_it)->get_value (route_cols.port_cols.label));
422 iter->set_value (route_cols.get_channel (i).port, (*port_it)->get_value (route_cols.port_cols.port));
433 PortExportChannelSelector::ChannelTreeView::update_selection_text (std::string const & path, std::string const & new_text, uint32_t channel)
435 Gtk::TreeModel::iterator iter = get_model ()->get_iter (path);
436 iter->set_value (route_cols.get_channel (channel).label, new_text);
438 Glib::RefPtr<Gtk::ListStore> port_list = iter->get_value (route_cols.port_list_col);
439 Gtk::ListStore::Children::iterator port_it;
441 for (port_it = port_list->children().begin(); port_it != port_list->children().end(); ++port_it) {
442 std::string label = port_it->get_value (route_cols.port_cols.label);
443 if (label == new_text) {
444 boost::weak_ptr<AudioPort> w = (*port_it)[route_cols.port_cols.port];
445 iter->set_value (route_cols.get_channel (channel).port, w);
452 RegionExportChannelSelector::RegionExportChannelSelector (ARDOUR::Session * _session,
453 ProfileManagerPtr manager,
454 ARDOUR::AudioRegion const & region,
455 ARDOUR::AudioTrack & track) :
456 ExportChannelSelector (_session, manager),
459 region_chans (region.n_channels()),
460 track_chans (track.n_outputs().n_audio()),
462 raw_button (type_group),
463 fades_button (type_group),
464 processed_button (type_group)
468 /* make fades+region gain be the default */
470 fades_button.set_active ();
472 raw_button.set_label (string_compose (_("Region contents without fades nor region gain (channels: %1)"), region_chans));
473 raw_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
474 vbox.pack_start (raw_button, false, false);
476 fades_button.set_label (string_compose (_("Region contents with fades and region gain (channels: %1)"), region_chans));
477 fades_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
478 vbox.pack_start (fades_button, false, false);
480 processed_button.set_label (string_compose (_("Track output (channels: %1)"), track_chans));
481 processed_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
482 vbox.pack_start (processed_button, false, false);
485 vbox.show_all_children ();
486 show_all_children ();
490 RegionExportChannelSelector::sync_with_manager ()
492 state = manager->get_channel_configs().front();
494 if (!state) { return; }
496 switch (state->config->region_processing_type()) {
497 case RegionExportChannelFactory::None:
500 case RegionExportChannelFactory::Raw:
501 raw_button.set_active (true);
503 case RegionExportChannelFactory::Fades:
504 fades_button.set_active (true);
506 case RegionExportChannelFactory::Processed:
507 processed_button.set_active (true);
515 RegionExportChannelSelector::handle_selection ()
521 state->config->clear_channels ();
523 RegionExportChannelFactory::Type type = RegionExportChannelFactory::None;
524 if (raw_button.get_active ()) {
525 type = RegionExportChannelFactory::Raw;
526 } else if (fades_button.get_active ()) {
527 type = RegionExportChannelFactory::Fades;
528 } else if (processed_button.get_active ()) {
529 type = RegionExportChannelFactory::Processed;
531 CriticalSelectionChanged ();
535 factory.reset (new RegionExportChannelFactory (_session, region, track, type));
536 state->config->set_region_processing_type (type);
538 const size_t cc = type == RegionExportChannelFactory::Processed ? track_chans : region_chans;
539 for (size_t chan = 0; chan < cc; ++chan) {
540 state->config->register_channel (factory->create (chan));
543 CriticalSelectionChanged ();
546 /* Track export channel selector */
548 TrackExportChannelSelector::TrackExportChannelSelector (ARDOUR::Session * session, ProfileManagerPtr manager)
549 : ExportChannelSelector(session, manager)
550 , track_output_button(_("Apply track/bus processing"))
552 pack_start(main_layout);
554 // Populate Selection Menu
556 using namespace Gtk::Menu_Helpers;
558 select_menu.set_text (_("Selection Actions"));
559 select_menu.disable_scrolling ();
561 select_menu.AddMenuElem (MenuElem (_("Select tracks"), sigc::mem_fun (*this, &TrackExportChannelSelector::select_tracks)));
562 select_menu.AddMenuElem (MenuElem (_("Select busses"), sigc::mem_fun (*this, &TrackExportChannelSelector::select_busses)));
563 select_menu.AddMenuElem (MenuElem (_("Deselect all"), sigc::mem_fun (*this, &TrackExportChannelSelector::select_none)));
564 select_menu.AddMenuElem (SeparatorElem ());
566 exclude_hidden = new Gtk::CheckMenuItem (_("Exclude Hidden"));
567 exclude_hidden->set_active (false);
568 exclude_hidden->show();
569 select_menu.AddMenuElem (*exclude_hidden);
571 exclude_muted = new Gtk::CheckMenuItem (_("Exclude Muted"));
572 exclude_muted->set_active (true);
573 exclude_muted->show();
574 select_menu.AddMenuElem (*exclude_muted);
578 options_box.set_spacing (8);
579 options_box.pack_start (track_output_button, false, false);
580 options_box.pack_start (select_menu, false, false);
581 main_layout.pack_start (options_box, false, false);
584 track_scroller.add (track_view);
585 track_scroller.set_size_request (-1, 130);
586 track_scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
587 main_layout.pack_start(track_scroller);
590 track_list = Gtk::ListStore::create (track_cols);
591 track_list->set_sort_column (track_cols.order_key, Gtk::SORT_ASCENDING);
592 track_view.set_model (track_list);
593 track_view.set_headers_visible (true);
595 track_view.append_column_editable (_("Export"), track_cols.selected);
596 Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *>(track_view.get_column_cell_renderer (0));
597 toggle->set_alignment (0.0, 0.5);
599 toggle->signal_toggled().connect (sigc::hide (sigc::mem_fun (*this, &TrackExportChannelSelector::update_config)));
601 Gtk::CellRendererText* text_renderer = Gtk::manage (new Gtk::CellRendererText);
602 text_renderer->property_editable() = false;
603 text_renderer->set_alignment (0.0, 0.5);
605 Gtk::TreeView::Column* column = Gtk::manage (new Gtk::TreeView::Column);
606 column->set_title (_("Track name"));
608 track_view.append_column (*column);
609 column->pack_start (*text_renderer, false);
610 column->add_attribute (text_renderer->property_text(), track_cols.label);
612 track_output_button.signal_clicked().connect (sigc::mem_fun (*this, &TrackExportChannelSelector::track_outputs_selected));
616 show_all_children ();
619 TrackExportChannelSelector::~TrackExportChannelSelector ()
621 delete exclude_hidden;
622 delete exclude_muted;
626 TrackExportChannelSelector::sync_with_manager ()
628 // TODO implement properly
633 TrackExportChannelSelector::select_tracks ()
635 bool excl_hidden = exclude_hidden->get_active ();
636 bool excl_muted = exclude_muted->get_active ();
638 for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
639 Gtk::TreeModel::Row row = *it;
640 boost::shared_ptr<Route> route = row[track_cols.route];
641 if (boost::dynamic_pointer_cast<Track> (route)) {
642 if (excl_muted && route->muted ()) {
645 if (excl_hidden && route->is_hidden ()) {
648 row[track_cols.selected] = true;
655 TrackExportChannelSelector::select_busses ()
657 bool excl_hidden = exclude_hidden->get_active ();
658 bool excl_muted = exclude_muted->get_active ();
660 for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
661 Gtk::TreeModel::Row row = *it;
662 boost::shared_ptr<Route> route = row[track_cols.route];
663 if (!boost::dynamic_pointer_cast<Track> (route)) {
664 if (excl_muted && route->muted ()) {
667 if (excl_hidden && route->is_hidden ()) {
670 row[track_cols.selected] = true;
677 TrackExportChannelSelector::select_none ()
679 for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
680 Gtk::TreeModel::Row row = *it;
681 row[track_cols.selected] = false;
687 TrackExportChannelSelector::track_outputs_selected ()
693 TrackExportChannelSelector::fill_list()
696 RouteList routes = _session->get_routelist();
698 for (RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
699 if (!boost::dynamic_pointer_cast<Track>(*it)) {
700 // not a track, must be a bus
701 if ((*it)->is_master () || (*it)->is_monitor ()) {
704 if (!(*it)->active ()) {
705 // don't include inactive busses
709 // not monitor or master bus
713 for (RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
714 if (boost::dynamic_pointer_cast<AudioTrack>(*it)) {
715 if (!(*it)->active ()) {
716 // don't include inactive tracks
725 TrackExportChannelSelector::add_track (boost::shared_ptr<Route> route)
727 Gtk::TreeModel::iterator iter = track_list->append();
728 Gtk::TreeModel::Row row = *iter;
730 row[track_cols.selected] = false;
731 row[track_cols.label] = route->name();
732 row[track_cols.route] = route;
733 row[track_cols.order_key] = route->presentation_info().order();
737 TrackExportChannelSelector::update_config()
739 manager->clear_channel_configs();
741 for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
742 Gtk::TreeModel::Row row = *it;
744 if (!row[track_cols.selected]) {
748 ExportProfileManager::ChannelConfigStatePtr state;
750 boost::shared_ptr<Route> route = row[track_cols.route];
752 if (track_output_button.get_active()) {
753 uint32_t outs = route->n_outputs().n_audio();
754 for (uint32_t i = 0; i < outs; ++i) {
755 boost::shared_ptr<AudioPort> port = route->output()->audio (i);
757 ExportChannelPtr channel (new PortExportChannel ());
758 PortExportChannel * pec = static_cast<PortExportChannel *> (channel.get());
761 state = manager->add_channel_config();
763 state->config->register_channel(channel);
767 std::list<ExportChannelPtr> list;
768 RouteExportChannel::create_from_route (list, route);
769 if (list.size () == 0) {
772 state = manager->add_channel_config();
773 state->config->register_channels (list);
777 if (_session->config.get_track_name_number() && route->track_number() > 0) {
778 state->config->set_name (string_compose ("%1-%2", route->track_number(), route->name()));
780 state->config->set_name (route->name());
786 CriticalSelectionChanged ();