Merge branch 'patches' of https://github.com/jdekozak/ardour
[ardour.git] / gtk2_ardour / export_channel_selector.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
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.
9
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.
14
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.
18
19 */
20
21 #include "export_channel_selector.h"
22
23 #include <algorithm>
24
25 #include "pbd/convert.h"
26
27 #include "ardour/audio_track.h"
28 #include "ardour/audioregion.h"
29 #include "ardour/export_channel_configuration.h"
30 #include "ardour/io.h"
31 #include "ardour/route.h"
32 #include "ardour/session.h"
33
34 #include <sstream>
35
36 #include "i18n.h"
37
38 using namespace std;
39 using namespace Glib;
40 using namespace ARDOUR;
41 using namespace PBD;
42
43 PortExportChannelSelector::PortExportChannelSelector (ARDOUR::Session * session, ProfileManagerPtr manager) :
44   ExportChannelSelector (session, manager),
45   channels_label (_("Channels:"), Gtk::ALIGN_LEFT),
46   split_checkbox (_("Split to mono files")),
47   max_channels (20),
48   channel_view (max_channels)
49 {
50         channels_hbox.pack_start (channels_label, false, false, 0);
51         channels_hbox.pack_end (channels_spinbutton, false, false, 0);
52
53         channels_vbox.pack_start (channels_hbox, false, false, 0);
54         channels_vbox.pack_start (split_checkbox, false, false, 6);
55
56         channel_alignment.add (channel_scroller);
57         channel_alignment.set_padding (0, 0, 12, 0);
58         channel_scroller.add (channel_view);
59         channel_scroller.set_size_request (-1, 130);
60         channel_scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
61
62         pack_start (channels_vbox, false, false, 0);
63         pack_start (channel_alignment, true, true, 0);
64
65         /* Channels spinbutton */
66
67         channels_spinbutton.set_digits (0);
68         channels_spinbutton.set_increments (1, 2);
69         channels_spinbutton.set_range (1, max_channels);
70         channels_spinbutton.set_value (2);
71
72         channels_spinbutton.signal_value_changed().connect (sigc::mem_fun (*this, &PortExportChannelSelector::update_channel_count));
73
74         /* Other signals */
75
76         split_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &PortExportChannelSelector::update_split_state));
77         channel_view.CriticalSelectionChanged.connect (CriticalSelectionChanged.make_slot());
78
79         /* Finalize */
80
81         sync_with_manager();
82         show_all_children ();
83
84 }
85
86 PortExportChannelSelector::~PortExportChannelSelector ()
87 {
88 //      if (session) {
89 //              session->add_instant_xml (get_state(), false);
90 //      }
91 }
92
93 void
94 PortExportChannelSelector::sync_with_manager ()
95 {
96         state = manager->get_channel_configs().front();
97
98         split_checkbox.set_active (state->config->get_split());
99         channels_spinbutton.set_value (state->config->get_n_chans());
100
101         fill_route_list ();
102         channel_view.set_config (state->config);
103 }
104
105 void
106 PortExportChannelSelector::fill_route_list ()
107 {
108         channel_view.clear_routes ();
109         RouteList routes = *_session->get_routes();
110
111         /* Add master bus and then everything else */
112
113         ARDOUR::IO* master = _session->master_out()->output().get();
114         channel_view.add_route (master);
115
116         for (RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
117                 if ((*it)->is_master () || (*it)->is_monitor ()) {
118                         continue;
119                 }
120                 channel_view.add_route ((*it)->output().get());
121         }
122
123         update_channel_count ();
124 }
125
126 void
127 PortExportChannelSelector::update_channel_count ()
128 {
129         uint32_t chans = static_cast<uint32_t> (channels_spinbutton.get_value());
130         channel_view.set_channel_count (chans);
131         CriticalSelectionChanged();
132 }
133
134 void
135 PortExportChannelSelector::update_split_state ()
136 {
137         state->config->set_split (split_checkbox.get_active());
138         CriticalSelectionChanged();
139 }
140
141 void
142 PortExportChannelSelector::RouteCols::add_channels (uint32_t chans)
143 {
144         while (chans > 0) {
145                 channels.push_back (Channel (*this));
146                 ++n_channels;
147                 --chans;
148         }
149 }
150
151 PortExportChannelSelector::RouteCols::Channel &
152 PortExportChannelSelector::RouteCols::get_channel (uint32_t channel)
153 {
154         if (channel > n_channels) {
155                 std::cout << "Invalid channel cout for get_channel!" << std::endl;
156         }
157
158         std::list<Channel>::iterator it = channels.begin();
159
160         while (channel > 1) { // Channel count starts from one!
161                 ++it;
162                 --channel;
163         }
164
165         return *it;
166 }
167
168 PortExportChannelSelector::ChannelTreeView::ChannelTreeView (uint32_t max_channels) :
169   n_channels (0)
170 {
171         /* Main columns */
172
173         route_cols.add_channels (max_channels);
174
175         route_list = Gtk::ListStore::create(route_cols);
176         set_model (route_list);
177
178         /* Add column with toggle and text */
179
180         append_column_editable (_("Bus or Track"), route_cols.selected);
181
182         Gtk::CellRendererText* text_renderer = Gtk::manage (new Gtk::CellRendererText);
183         text_renderer->property_editable() = false;
184
185         Gtk::TreeView::Column* column = get_column (0);
186         column->pack_start (*text_renderer);
187         column->add_attribute (text_renderer->property_text(), route_cols.name);
188
189         Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *>(get_column_cell_renderer (0));
190         toggle->signal_toggled().connect (sigc::mem_fun (*this, &PortExportChannelSelector::ChannelTreeView::update_toggle_selection));
191
192         static_columns = get_columns().size();
193 }
194
195 void
196 PortExportChannelSelector::ChannelTreeView::set_config (ChannelConfigPtr c)
197 {
198         /* TODO Without the following line, the state might get reset.
199          * Pointing to the same address does not mean the state of the configuration hasn't changed.
200          * In the current code this is safe, but the actual cause of the problem would be good to fix
201          */
202
203         if (config == c) { return; }
204         config = c;
205
206         uint32_t i = 1;
207         ExportChannelConfiguration::ChannelList chan_list = config->get_channels();
208         for (ExportChannelConfiguration::ChannelList::iterator c_it = chan_list.begin(); c_it != chan_list.end(); ++c_it) {
209
210                 for (Gtk::ListStore::Children::iterator r_it = route_list->children().begin(); r_it != route_list->children().end(); ++r_it) {
211
212                         ARDOUR::PortExportChannel * pec;
213                         if (!(pec = dynamic_cast<ARDOUR::PortExportChannel *> (c_it->get()))) {
214                                 continue;
215                         }
216
217                         Glib::RefPtr<Gtk::ListStore> port_list = r_it->get_value (route_cols.port_list_col);
218                         std::set<boost::weak_ptr<AudioPort> > route_ports;
219                         std::set<boost::weak_ptr<AudioPort> > intersection;
220                         std::map<boost::weak_ptr<AudioPort>, string> port_labels;
221
222                         for (Gtk::ListStore::Children::const_iterator p_it = port_list->children().begin(); p_it != port_list->children().end(); ++p_it) {
223                                 route_ports.insert ((*p_it)->get_value (route_cols.port_cols.port));
224                                 port_labels.insert (make_pair ((*p_it)->get_value (route_cols.port_cols.port),
225                                                                (*p_it)->get_value (route_cols.port_cols.label)));
226                         }
227
228                         std::set_intersection (pec->get_ports().begin(), pec->get_ports().end(),
229                                                route_ports.begin(), route_ports.end(),
230                                                std::insert_iterator<std::set<boost::weak_ptr<AudioPort> > > (intersection, intersection.begin()));
231
232                         intersection.erase (boost::weak_ptr<AudioPort> ()); // Remove "none" selection
233
234                         if (intersection.empty()) {
235                                 continue;
236                         }
237
238                         if (!r_it->get_value (route_cols.selected)) {
239                                 r_it->set_value (route_cols.selected, true);
240
241                                 /* Set previous channels (if any) to none */
242
243                                 for (uint32_t chn = 1; chn < i; ++chn) {
244                                         r_it->set_value (route_cols.get_channel (chn).port, boost::weak_ptr<AudioPort> ());
245                                         r_it->set_value (route_cols.get_channel (chn).label, string ("(none)"));
246                                 }
247                         }
248
249                         boost::weak_ptr<AudioPort> port = *intersection.begin();
250                         std::map<boost::weak_ptr<AudioPort>, string>::iterator label_it = port_labels.find (port);
251                         string label = label_it != port_labels.end() ? label_it->second : "error";
252
253                         r_it->set_value (route_cols.get_channel (i).port, port);
254                         r_it->set_value (route_cols.get_channel (i).label, label);
255                 }
256
257                 ++i;
258         }
259 }
260
261 void
262 PortExportChannelSelector::ChannelTreeView::add_route (ARDOUR::IO * io)
263 {
264         Gtk::TreeModel::iterator iter = route_list->append();
265         Gtk::TreeModel::Row row = *iter;
266
267         row[route_cols.selected] = false;
268         row[route_cols.name] = io->name();
269         row[route_cols.io] = io;
270
271         /* Initialize port list */
272
273         Glib::RefPtr<Gtk::ListStore> port_list = Gtk::ListStore::create (route_cols.port_cols);
274         row[route_cols.port_list_col] = port_list;
275
276         uint32_t outs = io->n_ports().n_audio();
277         for (uint32_t i = 0; i < outs; ++i) {
278                 iter = port_list->append();
279                 row = *iter;
280
281                 row[route_cols.port_cols.selected] = false;
282                 row[route_cols.port_cols.port] = io->audio (i);
283
284                 std::ostringstream oss;
285                 oss << "Out-" << (i + 1);
286
287                 row[route_cols.port_cols.label] = oss.str();
288         }
289
290         iter = port_list->append();
291         row = *iter;
292
293         row[route_cols.port_cols.selected] = false;
294         row[route_cols.port_cols.port] = boost::weak_ptr<AudioPort> ();
295         row[route_cols.port_cols.label] = "(none)";
296
297 }
298
299 void
300 PortExportChannelSelector::ChannelTreeView::set_channel_count (uint32_t channels)
301 {
302         int offset = channels - n_channels;
303
304         while (offset > 0) {
305                 ++n_channels;
306
307                 std::ostringstream oss;
308                 oss << n_channels;
309
310                 /* New column */
311
312                 Gtk::TreeView::Column* column = Gtk::manage (new Gtk::TreeView::Column (oss.str()));
313
314                 Gtk::CellRendererCombo* combo_renderer = Gtk::manage (new Gtk::CellRendererCombo);
315                 combo_renderer->property_text_column() = 2;
316                 column->pack_start (*combo_renderer);
317
318                 append_column (*column);
319
320                 column->add_attribute (combo_renderer->property_text(), route_cols.get_channel(n_channels).label);
321                 column->add_attribute (combo_renderer->property_model(), route_cols.port_list_col);
322                 column->add_attribute (combo_renderer->property_editable(), route_cols.selected);
323
324                 combo_renderer->signal_edited().connect (sigc::bind (sigc::mem_fun (*this, &PortExportChannelSelector::ChannelTreeView::update_selection_text), n_channels));
325
326                 /* put data into view */
327
328                 for (Gtk::ListStore::Children::iterator it = route_list->children().begin(); it != route_list->children().end(); ++it) {
329                         std::string label = it->get_value(route_cols.selected) ? "(none)" : "";
330                         it->set_value (route_cols.get_channel (n_channels).label, label);
331                         it->set_value (route_cols.get_channel (n_channels).port, boost::weak_ptr<AudioPort> ());
332                 }
333
334                 /* set column width */
335
336                 get_column (static_columns + n_channels - 1)->set_min_width (80);
337
338                 --offset;
339         }
340
341         while (offset < 0) {
342                 --n_channels;
343
344                 remove_column (*get_column (n_channels + static_columns));
345
346                 ++offset;
347         }
348
349         update_config ();
350 }
351
352 void
353 PortExportChannelSelector::ChannelTreeView::update_config ()
354 {
355         if (!config) { return; }
356
357         config->clear_channels();
358
359         for (uint32_t i = 1; i <= n_channels; ++i) {
360
361                 ExportChannelPtr channel (new PortExportChannel ());
362                 PortExportChannel * pec = static_cast<PortExportChannel *> (channel.get());
363
364                 for (Gtk::ListStore::Children::iterator it = route_list->children().begin(); it != route_list->children().end(); ++it) {
365                         Gtk::TreeModel::Row row = *it;
366
367                         if (!row[route_cols.selected]) {
368                                 continue;
369                         }
370
371                         boost::weak_ptr<AudioPort> weak_port = row[route_cols.get_channel (i).port];
372                         boost::shared_ptr<AudioPort> port = weak_port.lock ();
373                         if (port) {
374                                 pec->add_port (port);
375                         }
376                 }
377
378                 config->register_channel (channel);
379         }
380
381         CriticalSelectionChanged ();
382 }
383
384 void
385 PortExportChannelSelector::ChannelTreeView::update_toggle_selection (std::string const & path)
386 {
387         Gtk::TreeModel::iterator iter = get_model ()->get_iter (path);
388         bool selected = iter->get_value (route_cols.selected);
389
390         for (uint32_t i = 1; i <= n_channels; ++i) {
391
392                 if (!selected) {
393                         iter->set_value (route_cols.get_channel (i).label, std::string (""));
394                         continue;
395                 }
396
397                 iter->set_value (route_cols.get_channel (i).label, std::string("(none)"));
398                 iter->set_value (route_cols.get_channel (i).port, boost::weak_ptr<AudioPort> ());
399
400                 Glib::RefPtr<Gtk::ListStore> port_list = iter->get_value (route_cols.port_list_col);
401                 Gtk::ListStore::Children::iterator port_it;
402                 uint32_t port_number = 1;
403
404                 for (port_it = port_list->children().begin(); port_it != port_list->children().end(); ++port_it) {
405                         if (port_number == i) {
406                                 iter->set_value (route_cols.get_channel (i).label, (std::string) (*port_it)->get_value (route_cols.port_cols.label));
407                                 iter->set_value (route_cols.get_channel (i).port, (*port_it)->get_value (route_cols.port_cols.port));
408                         }
409
410                         ++port_number;
411                 }
412         }
413
414         update_config ();
415 }
416
417 void
418 PortExportChannelSelector::ChannelTreeView::update_selection_text (std::string const & path, std::string const & new_text, uint32_t channel)
419 {
420         Gtk::TreeModel::iterator iter = get_model ()->get_iter (path);
421         iter->set_value (route_cols.get_channel (channel).label, new_text);
422
423         Glib::RefPtr<Gtk::ListStore> port_list = iter->get_value (route_cols.port_list_col);
424         Gtk::ListStore::Children::iterator port_it;
425
426         for (port_it = port_list->children().begin(); port_it != port_list->children().end(); ++port_it) {
427                 std::string label = port_it->get_value (route_cols.port_cols.label);
428                 if (label == new_text) {
429                         boost::weak_ptr<AudioPort> w = (*port_it)[route_cols.port_cols.port];
430                         iter->set_value (route_cols.get_channel (channel).port, w);
431                 }
432         }
433
434         update_config ();
435 }
436
437 RegionExportChannelSelector::RegionExportChannelSelector (ARDOUR::Session * _session,
438                                                           ProfileManagerPtr manager,
439                                                           ARDOUR::AudioRegion const & region,
440                                                           ARDOUR::AudioTrack & track) :
441   ExportChannelSelector (_session, manager),
442   region (region),
443   track (track),
444   region_chans (region.n_channels()),
445   track_chans (track.n_outputs().n_audio()),
446
447   raw_button (type_group),
448   fades_button (type_group),
449   processed_button (type_group)
450 {
451         pack_start (vbox);
452
453         /* make fades+region gain be the default */
454
455         fades_button.set_active ();
456
457         raw_button.set_label (string_compose (_("Region contents without fades nor region gain (channels: %1)"), region_chans));
458         raw_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
459         vbox.pack_start (raw_button);
460
461         fades_button.set_label (string_compose (_("Region contents with fades and region gain (channels: %1)"), region_chans));
462         fades_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
463         vbox.pack_start (fades_button);
464
465         processed_button.set_label (string_compose (_("Track output (channels: %1)"), track_chans));
466         processed_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
467         vbox.pack_start (processed_button);
468
469         sync_with_manager();
470         vbox.show_all_children ();
471         show_all_children ();
472 }
473
474 void
475 RegionExportChannelSelector::sync_with_manager ()
476 {
477         state = manager->get_channel_configs().front();
478
479         if (!state) { return; }
480
481         switch (state->config->region_processing_type()) {
482         case RegionExportChannelFactory::None:
483                 // Do nothing
484                 break;
485         case RegionExportChannelFactory::Raw:
486                 raw_button.set_active (true);
487                 break;
488         case RegionExportChannelFactory::Fades:
489                 fades_button.set_active (true);
490                 break;
491         case RegionExportChannelFactory::Processed:
492                 processed_button.set_active (true);
493                 break;
494         }
495
496         handle_selection ();
497 }
498
499 void
500 RegionExportChannelSelector::handle_selection ()
501 {
502         if (!state) {
503                 return;
504         }
505
506         state->config->clear_channels ();
507
508         RegionExportChannelFactory::Type type = RegionExportChannelFactory::None;
509         if (raw_button.get_active ()) {
510                 type = RegionExportChannelFactory::Raw;
511         } else if (fades_button.get_active ()) {
512                 type = RegionExportChannelFactory::Fades;
513         } else if (processed_button.get_active ()) {
514                 type = RegionExportChannelFactory::Processed;
515         } else {
516                 CriticalSelectionChanged ();
517                 return;
518         }
519
520         factory.reset (new RegionExportChannelFactory (_session, region, track, type));
521         state->config->set_region_processing_type (type);
522
523         for (size_t chan = 0; chan < region_chans; ++chan) {
524                 state->config->register_channel (factory->create (chan));
525         }
526
527         CriticalSelectionChanged ();
528 }
529
530 /* Track export channel selector */
531
532 TrackExportChannelSelector::TrackExportChannelSelector (ARDOUR::Session * session, ProfileManagerPtr manager)
533   : ExportChannelSelector(session, manager)
534   , region_contents_button(source_group, _("Export region contents"))
535   , track_output_button(source_group, _("Export track output"))
536 {
537         pack_start(main_layout);
538
539         // Options
540         options_box.pack_start(region_contents_button);
541         options_box.pack_start(track_output_button);
542         main_layout.pack_start(options_box);
543
544         // Track scroller
545         track_scroller.add (track_view);
546         track_scroller.set_size_request (-1, 130);
547         track_scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
548         main_layout.pack_start(track_scroller);
549
550         // Track list
551         track_list = Gtk::ListStore::create (track_cols);
552         track_view.set_model (track_list);
553         track_view.set_headers_visible (true);
554
555         track_view.append_column_editable (_("Track"), track_cols.selected);
556         Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *>(track_view.get_column_cell_renderer (0));
557         toggle->signal_toggled().connect (sigc::hide (sigc::mem_fun (*this, &TrackExportChannelSelector::update_config)));
558
559         Gtk::CellRendererText* text_renderer = Gtk::manage (new Gtk::CellRendererText);
560         text_renderer->property_editable() = false;
561
562         Gtk::TreeView::Column* column = track_view.get_column (0);
563         column->pack_start (*text_renderer);
564         column->add_attribute (text_renderer->property_text(), track_cols.label);
565
566         fill_list();
567
568         show_all_children ();
569 }
570
571 void
572 TrackExportChannelSelector::sync_with_manager ()
573 {
574         // TODO implement properly
575         update_config();
576 }
577
578 void
579 TrackExportChannelSelector::fill_list()
580 {
581         track_list->clear();
582         RouteList routes = *_session->get_routes();
583
584         for (RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
585                 if (!boost::dynamic_pointer_cast<Track>(*it)) {
586                         // not a track, must be a bus
587                         if ((*it)->is_master () || (*it)->is_monitor ()) {
588                                 continue;
589                         }
590                         // not monitor or master bus
591                         add_track (*it);
592                 }
593         }
594         for (RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
595                 if (boost::dynamic_pointer_cast<AudioTrack>(*it)) {
596                         add_track (*it);
597                 }
598         }
599 }
600
601 void
602 TrackExportChannelSelector::add_track (boost::shared_ptr<Route> route)
603 {
604         Gtk::TreeModel::iterator iter = track_list->append();
605         Gtk::TreeModel::Row row = *iter;
606
607         row[track_cols.selected] = true;
608         row[track_cols.label] = route->name();
609         row[track_cols.route] = route;
610 }
611
612 void
613 TrackExportChannelSelector::update_config()
614 {
615         manager->clear_channel_configs();
616
617         for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
618                 Gtk::TreeModel::Row row = *it;
619
620                 if (!row[track_cols.selected]) {
621                         continue;
622                 }
623
624                 ExportProfileManager::ChannelConfigStatePtr state = manager->add_channel_config();
625
626                 boost::shared_ptr<Route> route = row[track_cols.route];
627
628                 if (track_output_button.get_active()) {
629                         uint32_t outs = route->n_outputs().n_audio();
630                         for (uint32_t i = 0; i < outs; ++i) {
631                                 boost::shared_ptr<AudioPort> port = route->output()->audio (i);
632                                 if (port) {
633                                         ExportChannelPtr channel (new PortExportChannel ());
634                                         PortExportChannel * pec = static_cast<PortExportChannel *> (channel.get());
635                                         pec->add_port(port);
636                                         state->config->register_channel(channel);
637                                 }
638                         }
639                 } else {
640                         std::list<ExportChannelPtr> list;
641                         RouteExportChannel::create_from_route (list, route);
642                         state->config->register_channels (list);
643                 }
644
645                 state->config->set_name (route->name());
646         }
647
648         CriticalSelectionChanged ();
649 }