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.
23 #include "pbd/convert.h"
25 #include "ardour/audio_track.h"
26 #include "ardour/audioregion.h"
27 #include "ardour/export_channel_configuration.h"
28 #include "ardour/io.h"
29 #include "ardour/route.h"
30 #include "ardour/session.h"
34 #include "export_channel_selector.h"
35 #include "route_sorter.h"
41 using namespace ARDOUR;
44 PortExportChannelSelector::PortExportChannelSelector (ARDOUR::Session * session, ProfileManagerPtr manager) :
45 ExportChannelSelector (session, manager),
46 channels_label (_("Channels:"), Gtk::ALIGN_LEFT),
47 split_checkbox (_("Split to mono files")),
49 channel_view (max_channels)
51 channels_hbox.pack_start (channels_label, false, false, 0);
52 channels_hbox.pack_end (channels_spinbutton, false, false, 0);
54 channels_vbox.pack_start (channels_hbox, false, false, 0);
55 channels_vbox.pack_start (split_checkbox, false, false, 6);
57 channel_alignment.add (channel_scroller);
58 channel_alignment.set_padding (0, 0, 12, 0);
59 channel_scroller.add (channel_view);
60 channel_scroller.set_size_request (-1, 130);
61 channel_scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
63 pack_start (channels_vbox, false, false, 0);
64 pack_start (channel_alignment, true, true, 0);
66 /* Channels spinbutton */
68 channels_spinbutton.set_digits (0);
69 channels_spinbutton.set_increments (1, 2);
70 channels_spinbutton.set_range (1, max_channels);
71 channels_spinbutton.set_value (2);
73 channels_spinbutton.signal_value_changed().connect (sigc::mem_fun (*this, &PortExportChannelSelector::update_channel_count));
77 split_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &PortExportChannelSelector::update_split_state));
78 channel_view.CriticalSelectionChanged.connect (CriticalSelectionChanged.make_slot());
87 PortExportChannelSelector::~PortExportChannelSelector ()
92 PortExportChannelSelector::sync_with_manager ()
94 state = manager->get_channel_configs().front();
96 split_checkbox.set_active (state->config->get_split());
97 channels_spinbutton.set_value (state->config->get_n_chans());
99 /* when loading presets, config is ready set here (shared ptr)
100 * fill_route_list () -> update_channel_count () -> set_channel_count () -> update_config()
101 * will call config->clear_channels(); and clear the config
103 channel_view.set_config (ChannelConfigPtr ());
105 channel_view.set_config (state->config);
109 PortExportChannelSelector::fill_route_list ()
111 channel_view.clear_routes ();
112 RouteList routes = _session->get_routelist();
114 /* Add master bus and then everything else */
116 if (_session->master_out()) {
117 ARDOUR::IO* master = _session->master_out()->output().get();
118 channel_view.add_route (master);
121 routes.sort (Stripable::Sorter ());
123 for (RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
124 if ((*it)->is_master () || (*it)->is_monitor ()) {
127 channel_view.add_route ((*it)->output().get());
130 update_channel_count ();
134 PortExportChannelSelector::update_channel_count ()
136 uint32_t chans = static_cast<uint32_t> (channels_spinbutton.get_value());
137 channel_view.set_channel_count (chans);
138 CriticalSelectionChanged();
142 PortExportChannelSelector::update_split_state ()
144 state->config->set_split (split_checkbox.get_active());
145 CriticalSelectionChanged();
149 PortExportChannelSelector::RouteCols::add_channels (uint32_t chans)
152 channels.push_back (Channel (*this));
158 PortExportChannelSelector::RouteCols::Channel &
159 PortExportChannelSelector::RouteCols::get_channel (uint32_t channel)
161 if (channel > n_channels) {
162 std::cout << "Invalid channel count for get_channel!" << std::endl;
165 std::list<Channel>::iterator it = channels.begin();
167 while (channel > 1) { // Channel count starts from one!
175 PortExportChannelSelector::ChannelTreeView::ChannelTreeView (uint32_t max_channels) :
180 route_cols.add_channels (max_channels);
182 route_list = Gtk::ListStore::create(route_cols);
183 set_model (route_list);
185 /* Add column with toggle and text */
187 append_column_editable (_("Export"), route_cols.selected);
189 Gtk::CellRendererText* text_renderer = Gtk::manage (new Gtk::CellRendererText);
190 text_renderer->property_editable() = false;
191 text_renderer->set_alignment (0.0, 0.5);
193 Gtk::TreeView::Column* column = Gtk::manage (new Gtk::TreeView::Column);
194 column->set_title (_("Bus or Track"));
195 column->pack_start (*text_renderer);
196 column->set_expand (true);
197 column->add_attribute (text_renderer->property_text(), route_cols.name);
198 append_column (*column);
200 Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *>(get_column_cell_renderer (0));
201 toggle->set_alignment (0.0, 0.5);
202 toggle->signal_toggled().connect (sigc::mem_fun (*this, &PortExportChannelSelector::ChannelTreeView::update_toggle_selection));
204 static_columns = get_columns().size();
208 PortExportChannelSelector::ChannelTreeView::set_config (ChannelConfigPtr c)
210 /* TODO Without the following line, the state might get reset.
211 * Pointing to the same address does not mean the state of the configuration hasn't changed.
212 * In the current code this is safe, but the actual cause of the problem would be good to fix
215 if (config == c) { return; }
217 if (!config) { return; }
220 ExportChannelConfiguration::ChannelList chan_list = config->get_channels();
221 for (ExportChannelConfiguration::ChannelList::iterator c_it = chan_list.begin(); c_it != chan_list.end(); ++c_it) {
223 for (Gtk::ListStore::Children::iterator r_it = route_list->children().begin(); r_it != route_list->children().end(); ++r_it) {
225 ARDOUR::PortExportChannel * pec;
226 if (!(pec = dynamic_cast<ARDOUR::PortExportChannel *> (c_it->get()))) {
230 Glib::RefPtr<Gtk::ListStore> port_list = r_it->get_value (route_cols.port_list_col);
231 std::set<boost::weak_ptr<AudioPort> > route_ports;
232 std::set<boost::weak_ptr<AudioPort> > intersection;
233 std::map<boost::weak_ptr<AudioPort>, string> port_labels;
235 for (Gtk::ListStore::Children::const_iterator p_it = port_list->children().begin(); p_it != port_list->children().end(); ++p_it) {
236 route_ports.insert ((*p_it)->get_value (route_cols.port_cols.port));
237 port_labels.insert (make_pair ((*p_it)->get_value (route_cols.port_cols.port),
238 (*p_it)->get_value (route_cols.port_cols.label)));
241 std::set_intersection (pec->get_ports().begin(), pec->get_ports().end(),
242 route_ports.begin(), route_ports.end(),
243 std::insert_iterator<std::set<boost::weak_ptr<AudioPort> > > (intersection, intersection.begin()));
245 intersection.erase (boost::weak_ptr<AudioPort> ()); // Remove "none" selection
247 if (intersection.empty()) {
251 if (!r_it->get_value (route_cols.selected)) {
252 r_it->set_value (route_cols.selected, true);
254 /* Set previous channels (if any) to none */
256 for (uint32_t chn = 1; chn < i; ++chn) {
257 r_it->set_value (route_cols.get_channel (chn).port, boost::weak_ptr<AudioPort> ());
258 r_it->set_value (route_cols.get_channel (chn).label, string ("(none)"));
262 boost::weak_ptr<AudioPort> port = *intersection.begin();
263 std::map<boost::weak_ptr<AudioPort>, string>::iterator label_it = port_labels.find (port);
264 string label = label_it != port_labels.end() ? label_it->second : "error";
266 r_it->set_value (route_cols.get_channel (i).port, port);
267 r_it->set_value (route_cols.get_channel (i).label, label);
275 PortExportChannelSelector::ChannelTreeView::add_route (ARDOUR::IO * io)
277 Gtk::TreeModel::iterator iter = route_list->append();
278 Gtk::TreeModel::Row row = *iter;
280 row[route_cols.selected] = false;
281 row[route_cols.name] = io->name();
282 row[route_cols.io] = io;
284 /* Initialize port list */
286 Glib::RefPtr<Gtk::ListStore> port_list = Gtk::ListStore::create (route_cols.port_cols);
287 row[route_cols.port_list_col] = port_list;
289 uint32_t outs = io->n_ports().n_audio();
290 for (uint32_t i = 0; i < outs; ++i) {
291 iter = port_list->append();
294 row[route_cols.port_cols.selected] = false;
295 row[route_cols.port_cols.port] = io->audio (i);
297 std::ostringstream oss;
298 oss << "Out-" << (i + 1);
300 row[route_cols.port_cols.label] = oss.str();
303 iter = port_list->append();
306 row[route_cols.port_cols.selected] = false;
307 row[route_cols.port_cols.port] = boost::weak_ptr<AudioPort> ();
308 row[route_cols.port_cols.label] = "(none)";
313 PortExportChannelSelector::ChannelTreeView::set_channel_count (uint32_t channels)
315 int offset = channels - n_channels;
320 std::ostringstream oss;
325 Gtk::TreeView::Column* column = Gtk::manage (new Gtk::TreeView::Column (oss.str()));
327 Gtk::CellRendererCombo* combo_renderer = Gtk::manage (new Gtk::CellRendererCombo);
328 combo_renderer->property_text_column() = 2;
329 combo_renderer->property_has_entry() = false;
330 column->pack_start (*combo_renderer);
332 append_column (*column);
334 column->add_attribute (combo_renderer->property_text(), route_cols.get_channel(n_channels).label);
335 column->add_attribute (combo_renderer->property_model(), route_cols.port_list_col);
336 column->add_attribute (combo_renderer->property_editable(), route_cols.selected);
338 combo_renderer->signal_edited().connect (sigc::bind (sigc::mem_fun (*this, &PortExportChannelSelector::ChannelTreeView::update_selection_text), n_channels));
340 /* put data into view */
342 for (Gtk::ListStore::Children::iterator it = route_list->children().begin(); it != route_list->children().end(); ++it) {
343 std::string label = it->get_value(route_cols.selected) ? "(none)" : "";
344 it->set_value (route_cols.get_channel (n_channels).label, label);
345 it->set_value (route_cols.get_channel (n_channels).port, boost::weak_ptr<AudioPort> ());
348 /* set column width */
350 get_column (static_columns + n_channels - 1)->set_min_width (80);
358 remove_column (*get_column (n_channels + static_columns));
367 PortExportChannelSelector::ChannelTreeView::update_config ()
369 if (!config) { return; }
371 config->clear_channels();
373 for (uint32_t i = 1; i <= n_channels; ++i) {
375 ExportChannelPtr channel (new PortExportChannel ());
376 PortExportChannel * pec = static_cast<PortExportChannel *> (channel.get());
378 for (Gtk::ListStore::Children::iterator it = route_list->children().begin(); it != route_list->children().end(); ++it) {
379 Gtk::TreeModel::Row row = *it;
381 if (!row[route_cols.selected]) {
385 boost::weak_ptr<AudioPort> weak_port = row[route_cols.get_channel (i).port];
386 boost::shared_ptr<AudioPort> port = weak_port.lock ();
388 pec->add_port (port);
392 config->register_channel (channel);
395 CriticalSelectionChanged ();
399 PortExportChannelSelector::ChannelTreeView::update_toggle_selection (std::string const & path)
401 Gtk::TreeModel::iterator iter = get_model ()->get_iter (path);
402 bool selected = iter->get_value (route_cols.selected);
404 for (uint32_t i = 1; i <= n_channels; ++i) {
407 iter->set_value (route_cols.get_channel (i).label, std::string (""));
411 iter->set_value (route_cols.get_channel (i).label, std::string("(none)"));
412 iter->set_value (route_cols.get_channel (i).port, boost::weak_ptr<AudioPort> ());
414 Glib::RefPtr<Gtk::ListStore> port_list = iter->get_value (route_cols.port_list_col);
415 Gtk::ListStore::Children::iterator port_it;
416 uint32_t port_number = 1;
418 for (port_it = port_list->children().begin(); port_it != port_list->children().end(); ++port_it) {
419 if (port_number == i) {
420 iter->set_value (route_cols.get_channel (i).label, (std::string) (*port_it)->get_value (route_cols.port_cols.label));
421 iter->set_value (route_cols.get_channel (i).port, (*port_it)->get_value (route_cols.port_cols.port));
432 PortExportChannelSelector::ChannelTreeView::update_selection_text (std::string const & path, std::string const & new_text, uint32_t channel)
434 Gtk::TreeModel::iterator iter = get_model ()->get_iter (path);
435 iter->set_value (route_cols.get_channel (channel).label, new_text);
437 Glib::RefPtr<Gtk::ListStore> port_list = iter->get_value (route_cols.port_list_col);
438 Gtk::ListStore::Children::iterator port_it;
440 for (port_it = port_list->children().begin(); port_it != port_list->children().end(); ++port_it) {
441 std::string label = port_it->get_value (route_cols.port_cols.label);
442 if (label == new_text) {
443 boost::weak_ptr<AudioPort> w = (*port_it)[route_cols.port_cols.port];
444 iter->set_value (route_cols.get_channel (channel).port, w);
451 RegionExportChannelSelector::RegionExportChannelSelector (ARDOUR::Session * _session,
452 ProfileManagerPtr manager,
453 ARDOUR::AudioRegion const & region,
454 ARDOUR::AudioTrack & track) :
455 ExportChannelSelector (_session, manager),
458 region_chans (region.n_channels()),
459 track_chans (track.n_outputs().n_audio()),
461 raw_button (type_group),
462 fades_button (type_group),
463 processed_button (type_group)
467 /* make fades+region gain be the default */
469 fades_button.set_active ();
471 raw_button.set_label (string_compose (_("Region contents without fades nor region gain (channels: %1)"), region_chans));
472 raw_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
473 vbox.pack_start (raw_button, false, false);
475 fades_button.set_label (string_compose (_("Region contents with fades and region gain (channels: %1)"), region_chans));
476 fades_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
477 vbox.pack_start (fades_button, false, false);
479 processed_button.set_label (string_compose (_("Track output (channels: %1)"), track_chans));
480 processed_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
481 vbox.pack_start (processed_button, false, false);
484 vbox.show_all_children ();
485 show_all_children ();
489 RegionExportChannelSelector::sync_with_manager ()
491 state = manager->get_channel_configs().front();
493 if (!state) { return; }
495 switch (state->config->region_processing_type()) {
496 case RegionExportChannelFactory::None:
499 case RegionExportChannelFactory::Raw:
500 raw_button.set_active (true);
502 case RegionExportChannelFactory::Fades:
503 fades_button.set_active (true);
505 case RegionExportChannelFactory::Processed:
506 processed_button.set_active (true);
514 RegionExportChannelSelector::handle_selection ()
520 state->config->clear_channels ();
522 RegionExportChannelFactory::Type type = RegionExportChannelFactory::None;
523 if (raw_button.get_active ()) {
524 type = RegionExportChannelFactory::Raw;
525 } else if (fades_button.get_active ()) {
526 type = RegionExportChannelFactory::Fades;
527 } else if (processed_button.get_active ()) {
528 type = RegionExportChannelFactory::Processed;
530 CriticalSelectionChanged ();
534 factory.reset (new RegionExportChannelFactory (_session, region, track, type));
535 state->config->set_region_processing_type (type);
537 const size_t cc = type == RegionExportChannelFactory::Processed ? track_chans : region_chans;
538 for (size_t chan = 0; chan < cc; ++chan) {
539 state->config->register_channel (factory->create (chan));
542 CriticalSelectionChanged ();
545 /* Track export channel selector */
547 TrackExportChannelSelector::TrackExportChannelSelector (ARDOUR::Session * session, ProfileManagerPtr manager)
548 : ExportChannelSelector(session, manager)
549 , track_output_button(_("Apply track/bus processing"))
550 , select_tracks_button (_("Select all tracks"))
551 , select_busses_button (_("Select all busses"))
552 , select_none_button (_("Deselect all"))
554 pack_start(main_layout);
557 options_box.pack_start(track_output_button);
558 options_box.pack_start (select_tracks_button);
559 options_box.pack_start (select_busses_button);
560 options_box.pack_start (select_none_button);
561 main_layout.pack_start(options_box, false, false);
564 track_scroller.add (track_view);
565 track_scroller.set_size_request (-1, 130);
566 track_scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
567 main_layout.pack_start(track_scroller);
570 track_list = Gtk::ListStore::create (track_cols);
571 track_list->set_sort_column (track_cols.order_key, Gtk::SORT_ASCENDING);
572 track_view.set_model (track_list);
573 track_view.set_headers_visible (true);
575 track_view.append_column_editable (_("Export"), track_cols.selected);
576 Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *>(track_view.get_column_cell_renderer (0));
577 toggle->set_alignment (0.0, 0.5);
579 toggle->signal_toggled().connect (sigc::hide (sigc::mem_fun (*this, &TrackExportChannelSelector::update_config)));
581 Gtk::CellRendererText* text_renderer = Gtk::manage (new Gtk::CellRendererText);
582 text_renderer->property_editable() = false;
583 text_renderer->set_alignment (0.0, 0.5);
585 Gtk::TreeView::Column* column = Gtk::manage (new Gtk::TreeView::Column);
586 column->set_title (_("Track name"));
588 track_view.append_column (*column);
589 column->pack_start (*text_renderer, false);
590 column->add_attribute (text_renderer->property_text(), track_cols.label);
592 select_tracks_button.signal_clicked().connect (sigc::mem_fun (*this, &TrackExportChannelSelector::select_tracks));
593 select_busses_button.signal_clicked().connect (sigc::mem_fun (*this, &TrackExportChannelSelector::select_busses));
594 select_none_button.signal_clicked().connect (sigc::mem_fun (*this, &TrackExportChannelSelector::select_none));
596 track_output_button.signal_clicked().connect (sigc::mem_fun (*this, &TrackExportChannelSelector::track_outputs_selected));
600 show_all_children ();
604 TrackExportChannelSelector::sync_with_manager ()
606 // TODO implement properly
611 TrackExportChannelSelector::select_tracks ()
613 for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
614 Gtk::TreeModel::Row row = *it;
615 boost::shared_ptr<Route> route = row[track_cols.route];
616 if (boost::dynamic_pointer_cast<Track> (route)) {
618 row[track_cols.selected] = true;
625 TrackExportChannelSelector::select_busses ()
627 for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
628 Gtk::TreeModel::Row row = *it;
629 boost::shared_ptr<Route> route = row[track_cols.route];
630 if (!boost::dynamic_pointer_cast<Track> (route)) {
631 // it's not a track, must be a bus
632 row[track_cols.selected] = true;
639 TrackExportChannelSelector::select_none ()
641 for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
642 Gtk::TreeModel::Row row = *it;
643 row[track_cols.selected] = false;
649 TrackExportChannelSelector::track_outputs_selected ()
655 TrackExportChannelSelector::fill_list()
658 RouteList routes = _session->get_routelist();
660 for (RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
661 if (!boost::dynamic_pointer_cast<Track>(*it)) {
662 // not a track, must be a bus
663 if ((*it)->is_master () || (*it)->is_monitor ()) {
666 if (!(*it)->active ()) {
667 // don't include inactive busses
671 // not monitor or master bus
675 for (RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
676 if (boost::dynamic_pointer_cast<AudioTrack>(*it)) {
677 if (!(*it)->active ()) {
678 // don't include inactive tracks
687 TrackExportChannelSelector::add_track (boost::shared_ptr<Route> route)
689 Gtk::TreeModel::iterator iter = track_list->append();
690 Gtk::TreeModel::Row row = *iter;
692 row[track_cols.selected] = false;
693 row[track_cols.label] = route->name();
694 row[track_cols.route] = route;
695 row[track_cols.order_key] = route->presentation_info().order();
699 TrackExportChannelSelector::update_config()
701 manager->clear_channel_configs();
703 for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
704 Gtk::TreeModel::Row row = *it;
706 if (!row[track_cols.selected]) {
710 ExportProfileManager::ChannelConfigStatePtr state;
712 boost::shared_ptr<Route> route = row[track_cols.route];
714 if (track_output_button.get_active()) {
715 uint32_t outs = route->n_outputs().n_audio();
716 for (uint32_t i = 0; i < outs; ++i) {
717 boost::shared_ptr<AudioPort> port = route->output()->audio (i);
719 ExportChannelPtr channel (new PortExportChannel ());
720 PortExportChannel * pec = static_cast<PortExportChannel *> (channel.get());
723 state = manager->add_channel_config();
725 state->config->register_channel(channel);
729 std::list<ExportChannelPtr> list;
730 RouteExportChannel::create_from_route (list, route);
731 if (list.size () == 0) {
734 state = manager->add_channel_config();
735 state->config->register_channels (list);
739 if (_session->config.get_track_name_number() && route->track_number() > 0) {
740 state->config->set_name (string_compose ("%1-%2", route->track_number(), route->name()));
742 state->config->set_name (route->name());
748 CriticalSelectionChanged ();