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