Region export dialog: Make export channel and -selector polymorphic, add the region...
[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/audioengine.h>
28 #include <ardour/export_channel_configuration.h>
29 #include <ardour/export_handler.h>
30 #include <ardour/io.h>
31 #include <ardour/route.h>
32 #include <ardour/audio_port.h>
33 #include <ardour/session.h>
34
35 #include <sstream>
36
37 #include "i18n.h"
38
39 using namespace ARDOUR;
40 using namespace PBD;
41
42 PortExportChannelSelector::PortExportChannelSelector () :
43   channels_label (_("Channels:"), Gtk::ALIGN_LEFT),
44   split_checkbox (_("Split to mono files")),
45   max_channels (20),
46   channel_view (max_channels)
47 {
48
49         channels_hbox.pack_start (channels_label, false, false, 0);
50         channels_hbox.pack_end (channels_spinbutton, false, false, 0);
51         
52         channels_vbox.pack_start (channels_hbox, false, false, 0);
53         channels_vbox.pack_start (split_checkbox, false, false, 6);
54         
55         channel_alignment.add (channel_scroller);
56         channel_alignment.set_padding (0, 0, 12, 0);
57         channel_scroller.add (channel_view);
58         channel_scroller.set_size_request (-1, 130);
59         channel_scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
60         
61         pack_start (channels_vbox, false, false, 0);
62         pack_start (channel_alignment, true, true, 0);
63         
64         /* Channels spinbutton */
65         
66         channels_spinbutton.set_digits (0);
67         channels_spinbutton.set_increments (1, 2);
68         channels_spinbutton.set_range (1, max_channels);
69         channels_spinbutton.set_value (2);
70         
71         channels_spinbutton.signal_value_changed().connect (sigc::mem_fun (*this, &PortExportChannelSelector::update_channel_count));
72         
73         /* Other signals */
74         
75         split_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &PortExportChannelSelector::update_split_state));
76         channel_view.CriticalSelectionChanged.connect (CriticalSelectionChanged.make_slot());
77         
78         /* Finalize */
79         
80         show_all_children ();
81         
82 }
83
84 PortExportChannelSelector::~PortExportChannelSelector ()
85 {
86 //      if (session) {
87 //              session->add_instant_xml (get_state(), false);
88 //      }
89 }
90
91 void
92 PortExportChannelSelector::set_state (ARDOUR::ExportProfileManager::ChannelConfigStatePtr const state_, ARDOUR::Session * session_)
93 {
94         state = state_;
95         session = session_;
96         
97         split_checkbox.set_active (state->config->get_split());
98         channels_spinbutton.set_value (state->config->get_n_chans());
99         
100         fill_route_list ();
101         channel_view.set_config (state->config);
102 }
103
104 void
105 PortExportChannelSelector::fill_route_list ()
106 {
107         channel_view.clear_routes ();
108         Session::RouteList routes = *session->get_routes();
109
110         /* Add master bus and then everything else */
111         
112         ARDOUR::IO * master = session->master_out().get();
113         channel_view.add_route (master);
114         
115         for (Session::RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
116                 if (it->get() == master) {
117                         continue;
118                 }
119                 channel_view.add_route (it->get());
120         }
121         
122         update_channel_count ();
123 }
124
125 void
126 PortExportChannelSelector::update_channel_count ()
127 {
128         uint32_t chans = static_cast<uint32_t> (channels_spinbutton.get_value());
129         channel_view.set_channel_count (chans);
130         CriticalSelectionChanged();
131 }
132
133 void
134 PortExportChannelSelector::update_split_state ()
135 {
136         state->config->set_split (split_checkbox.get_active());
137         CriticalSelectionChanged();
138 }
139
140 void
141 PortExportChannelSelector::RouteCols::add_channels (uint32_t chans)
142 {
143         while (chans > 0) {
144                 channels.push_back (Channel (*this));
145                 ++n_channels;
146                 --chans;
147         }
148 }
149
150 PortExportChannelSelector::RouteCols::Channel &
151 PortExportChannelSelector::RouteCols::get_channel (uint32_t channel)
152 {
153         if (channel > n_channels) {
154                 std::cout << "Invalid channel cout for get_channel!" << std::endl;
155         }
156
157         std::list<Channel>::iterator it = channels.begin();
158         
159         while (channel > 1) { // Channel count starts from one!
160                 ++it;
161                 --channel;
162         }
163         
164         return *it;
165 }
166
167 PortExportChannelSelector::ChannelTreeView::ChannelTreeView (uint32_t max_channels) :
168   n_channels (0)
169 {
170         /* Main columns */
171         
172         route_cols.add_channels (max_channels);
173         
174         route_list = Gtk::ListStore::create(route_cols);
175         set_model (route_list);
176         
177         /* Add column with toggle and text */
178         
179         append_column_editable (_("Bus or Track"), route_cols.selected);
180         
181         Gtk::CellRendererText* text_renderer = Gtk::manage (new Gtk::CellRendererText);
182         text_renderer->property_editable() = false;
183         
184         Gtk::TreeView::Column* column = get_column (0);
185         column->pack_start (*text_renderer);
186         column->add_attribute (text_renderer->property_text(), route_cols.name);
187         
188         Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *>(get_column_cell_renderer (0));
189         toggle->signal_toggled().connect (mem_fun (*this, &PortExportChannelSelector::ChannelTreeView::update_toggle_selection));
190         
191         static_columns = get_columns().size();
192 }
193
194 void
195 PortExportChannelSelector::ChannelTreeView::set_config (ChannelConfigPtr c)
196 {
197         /* TODO Without the following line, the state might get reset.
198          * Pointing to the same address does not mean the state of the configuration hasn't changed.
199          * In the current code this is safe, but the actual cause of the problem would be good to fix
200          */
201
202         if (config == c) { return; }
203         config = c;
204
205         uint32_t i = 1;
206         ExportChannelConfiguration::ChannelList chan_list = config->get_channels();
207         for (ExportChannelConfiguration::ChannelList::iterator c_it = chan_list.begin(); c_it != chan_list.end(); ++c_it) {
208         
209                 for (Gtk::ListStore::Children::iterator r_it = route_list->children().begin(); r_it != route_list->children().end(); ++r_it) {
210                         
211                         ARDOUR::PortExportChannel * pec;
212                         if (!(pec = dynamic_cast<ARDOUR::PortExportChannel *> (c_it->get()))) {
213                                 continue;
214                         }
215                         
216                         Glib::RefPtr<Gtk::ListStore> port_list = r_it->get_value (route_cols.port_list_col);
217                         std::set<AudioPort *> route_ports;
218                         std::set<AudioPort *> intersection;
219                         std::map<AudioPort *, ustring> port_labels;
220                         
221                         for (Gtk::ListStore::Children::const_iterator p_it = port_list->children().begin(); p_it != port_list->children().end(); ++p_it) {
222                                 route_ports.insert ((*p_it)->get_value (route_cols.port_cols.port));
223                                 port_labels.insert (std::pair<AudioPort*, ustring> ((*p_it)->get_value (route_cols.port_cols.port),
224                                                                                     (*p_it)->get_value (route_cols.port_cols.label)));
225                         }
226                         
227                         std::set_intersection (pec->get_ports().begin(), pec->get_ports().end(),
228                                                route_ports.begin(), route_ports.end(),
229                                                std::insert_iterator<std::set<AudioPort *> > (intersection, intersection.begin()));
230                         
231                         intersection.erase (0); // Remove "none" selection
232                         
233                         if (intersection.empty()) {
234                                 continue;
235                         }
236                         
237                         if (!r_it->get_value (route_cols.selected)) {
238                                 r_it->set_value (route_cols.selected, true);
239                                 
240                                 /* Set previous channels (if any) to none */
241                                 
242                                 for (uint32_t chn = 1; chn < i; ++chn) {
243                                         r_it->set_value (route_cols.get_channel (chn).port, (AudioPort *) 0);
244                                         r_it->set_value (route_cols.get_channel (chn).label, ustring ("(none)"));
245                                 }
246                         }
247                         
248                         AudioPort * port = *intersection.begin();
249                         std::map<AudioPort *, ustring>::iterator label_it = port_labels.find (port);
250                         ustring label = label_it != port_labels.end() ? label_it->second : "error";
251                         
252                         r_it->set_value (route_cols.get_channel (i).port, port);
253                         r_it->set_value (route_cols.get_channel (i).label, label);
254                 }
255                 
256                 ++i;
257         }
258 }
259
260 void
261 PortExportChannelSelector::ChannelTreeView::add_route (ARDOUR::IO * route)
262 {
263         Gtk::TreeModel::iterator iter = route_list->append();
264         Gtk::TreeModel::Row row = *iter;
265
266         row[route_cols.selected] = false;
267         row[route_cols.name] = route->name();
268         row[route_cols.io] = route;
269         
270         /* Initialize port list */
271         
272         Glib::RefPtr<Gtk::ListStore> port_list = Gtk::ListStore::create (route_cols.port_cols);
273         row[route_cols.port_list_col] = port_list;
274         
275         uint32_t outs = route->n_outputs().n_audio();
276         for (uint32_t i = 0; i < outs; ++i) {
277                 iter = port_list->append();
278                 row = *iter;
279                 
280                 row[route_cols.port_cols.selected] = false;
281                 row[route_cols.port_cols.port] = route->audio_output (i);
282                 
283                 std::ostringstream oss;
284                 oss << "Out-" << (i + 1);
285                 
286                 row[route_cols.port_cols.label] = oss.str();
287         }
288         
289         iter = port_list->append();
290         row = *iter;
291         
292         row[route_cols.port_cols.selected] = false;
293         row[route_cols.port_cols.port] = 0;
294         row[route_cols.port_cols.label] = "(none)";
295         
296 }
297
298 void
299 PortExportChannelSelector::ChannelTreeView::set_channel_count (uint32_t channels)
300 {
301         int offset = channels - n_channels;
302         
303         while (offset > 0) {
304                 ++n_channels;
305         
306                 std::ostringstream oss;
307                 oss << n_channels;
308                 
309                 /* New column */
310                 
311                 Gtk::TreeView::Column* column = Gtk::manage (new Gtk::TreeView::Column (oss.str())); 
312                 
313                 Gtk::CellRendererCombo* combo_renderer = Gtk::manage (new Gtk::CellRendererCombo);
314                 combo_renderer->property_text_column() = 2; 
315                 column->pack_start (*combo_renderer);
316                 
317                 append_column (*column);
318                 
319                 column->add_attribute (combo_renderer->property_text(), route_cols.get_channel(n_channels).label);
320                 column->add_attribute (combo_renderer->property_model(), route_cols.port_list_col);
321                 column->add_attribute (combo_renderer->property_editable(), route_cols.selected);
322                 
323                 combo_renderer->signal_edited().connect (sigc::bind (sigc::mem_fun (*this, &PortExportChannelSelector::ChannelTreeView::update_selection_text), n_channels));
324                 
325                 /* put data into view */
326                 
327                 for (Gtk::ListStore::Children::iterator it = route_list->children().begin(); it != route_list->children().end(); ++it) {
328                         Glib::ustring label = it->get_value(route_cols.selected) ? "(none)" : "";
329                         it->set_value (route_cols.get_channel (n_channels).label, label);
330                         it->set_value (route_cols.get_channel (n_channels).port, (AudioPort *) 0);
331                 }
332                 
333                 /* set column width */
334                 
335                 get_column (static_columns + n_channels - 1)->set_min_width (80);
336                 
337                 --offset;
338         }
339         
340         while (offset < 0) {
341                 --n_channels;
342                 
343                 remove_column (*get_column (n_channels + static_columns));
344                 
345                 ++offset;
346         }
347         
348         update_config ();
349 }
350
351 void
352 PortExportChannelSelector::ChannelTreeView::update_config ()
353 {
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                         AudioPort * port = row[route_cols.get_channel (i).port];
372                         if (port) {
373                                 pec->add_port (port);
374                         }
375                 }
376                 
377                 config->register_channel (channel);
378         }
379         
380         CriticalSelectionChanged ();
381 }
382
383 void
384 PortExportChannelSelector::ChannelTreeView::update_toggle_selection (Glib::ustring const & path)
385 {
386         Gtk::TreeModel::iterator iter = get_model ()->get_iter (path);
387         bool selected = iter->get_value (route_cols.selected);
388         
389         for (uint32_t i = 1; i <= n_channels; ++i) {
390         
391                 if (!selected) {
392                         iter->set_value (route_cols.get_channel (i).label, Glib::ustring (""));
393                         continue;
394                 }
395         
396                 iter->set_value (route_cols.get_channel (i).label, Glib::ustring("(none)"));
397                 iter->set_value (route_cols.get_channel (i).port, (AudioPort *) 0);
398                         
399                 Glib::RefPtr<Gtk::ListStore> port_list = iter->get_value (route_cols.port_list_col);
400                 Gtk::ListStore::Children::iterator port_it;
401                 uint32_t port_number = 1;
402                 
403                 for (port_it = port_list->children().begin(); port_it != port_list->children().end(); ++port_it) {
404                         if (port_number == i) {
405                                 iter->set_value (route_cols.get_channel (i).label, (Glib::ustring) (*port_it)->get_value (route_cols.port_cols.label));
406                                 iter->set_value (route_cols.get_channel (i).port, (AudioPort *) (*port_it)->get_value (route_cols.port_cols.port));
407                         }
408                         
409                         ++port_number;
410                 }
411         }
412         
413         update_config ();
414 }
415
416 void
417 PortExportChannelSelector::ChannelTreeView::update_selection_text (Glib::ustring const & path, Glib::ustring const & new_text, uint32_t channel)
418 {
419         Gtk::TreeModel::iterator iter = get_model ()->get_iter (path);
420         iter->set_value (route_cols.get_channel (channel).label, new_text);
421         
422         Glib::RefPtr<Gtk::ListStore> port_list = iter->get_value (route_cols.port_list_col);
423         Gtk::ListStore::Children::iterator port_it;
424         
425         for (port_it = port_list->children().begin(); port_it != port_list->children().end(); ++port_it) {
426                 Glib::ustring label = port_it->get_value (route_cols.port_cols.label);
427                 if (label == new_text) {
428                         iter->set_value (route_cols.get_channel (channel).port, (AudioPort *) (*port_it)[route_cols.port_cols.port]);
429                 }
430         }
431         
432         update_config ();
433 }
434
435 RegionExportChannelSelector::RegionExportChannelSelector (ARDOUR::AudioRegion const & region, ARDOUR::AudioTrack & track) :
436   session (0),
437   region (region),
438   track (track),
439   region_chans (region.n_channels()),
440   track_chans (track.n_outputs().n_audio()),
441
442   raw_button (type_group),
443   processed_button (type_group)
444 {
445         pack_start (vbox);
446
447         raw_button.set_label (string_compose (_("Raw region export, no fades or plugins (%1 channels)"), region_chans));
448         raw_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
449         vbox.pack_start (raw_button);
450         
451         processed_button.set_label (string_compose (_("Processed region export with fades and plugins applied (%1 channels)"), track_chans));
452         processed_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
453         vbox.pack_start (processed_button);
454         
455         vbox.show_all_children ();
456         show_all_children ();
457 }
458
459 void
460 RegionExportChannelSelector::set_state (ARDOUR::ExportProfileManager::ChannelConfigStatePtr const state_, ARDOUR::Session * session_)
461 {
462         state = state_;
463         session = session_;
464         
465         handle_selection ();
466 }
467
468 void
469 RegionExportChannelSelector::handle_selection ()
470 {
471         if (!state) {
472                 return;
473         }
474
475         state->config->clear_channels ();
476         
477         if (raw_button.get_active ()) {
478         
479                 factory.reset (new RegionExportChannelFactory (session, region, track, RegionExportChannelFactory::Raw));
480                 
481                 for (size_t chan = 0; chan < region_chans; ++chan) {
482                         state->config->register_channel (factory->create (chan));
483                 }
484                 
485         } else if (processed_button.get_active ()) {
486         
487                 factory.reset (new RegionExportChannelFactory(session, region, track, RegionExportChannelFactory::Processed));
488                 
489                 for (size_t chan = 0; chan < region_chans; ++chan) {
490                         state->config->register_channel (factory->create (chan));
491                 }
492                 
493         }
494         
495         CriticalSelectionChanged ();
496 }