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.
21 #include "export_channel_selector.h"
25 #include <pbd/convert.h>
27 #include <ardour/audio_port.h>
28 #include <ardour/audio_track.h>
29 #include <ardour/audioengine.h>
30 #include <ardour/export_channel_configuration.h>
31 #include <ardour/export_handler.h>
32 #include <ardour/io.h>
33 #include <ardour/route.h>
34 #include <ardour/session.h>
40 using namespace ARDOUR;
43 PortExportChannelSelector::PortExportChannelSelector () :
44 channels_label (_("Channels:"), Gtk::ALIGN_LEFT),
45 split_checkbox (_("Split to mono files")),
47 channel_view (max_channels)
50 channels_hbox.pack_start (channels_label, false, false, 0);
51 channels_hbox.pack_end (channels_spinbutton, false, false, 0);
53 channels_vbox.pack_start (channels_hbox, false, false, 0);
54 channels_vbox.pack_start (split_checkbox, false, false, 6);
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);
62 pack_start (channels_vbox, false, false, 0);
63 pack_start (channel_alignment, true, true, 0);
65 /* Channels spinbutton */
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);
72 channels_spinbutton.signal_value_changed().connect (sigc::mem_fun (*this, &PortExportChannelSelector::update_channel_count));
76 split_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &PortExportChannelSelector::update_split_state));
77 channel_view.CriticalSelectionChanged.connect (CriticalSelectionChanged.make_slot());
85 PortExportChannelSelector::~PortExportChannelSelector ()
88 // session->add_instant_xml (get_state(), false);
93 PortExportChannelSelector::set_state (ARDOUR::ExportProfileManager::ChannelConfigStatePtr const state_, ARDOUR::Session * session_)
98 split_checkbox.set_active (state->config->get_split());
99 channels_spinbutton.set_value (state->config->get_n_chans());
102 channel_view.set_config (state->config);
106 PortExportChannelSelector::fill_route_list ()
108 channel_view.clear_routes ();
109 RouteList routes = *session->get_routes();
111 /* Add master bus and then everything else */
113 ARDOUR::IO * master = session->master_out().get();
114 channel_view.add_route (master);
116 for (RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
117 if (it->get() == master) {
120 channel_view.add_route (it->get());
123 update_channel_count ();
127 PortExportChannelSelector::update_channel_count ()
129 uint32_t chans = static_cast<uint32_t> (channels_spinbutton.get_value());
130 channel_view.set_channel_count (chans);
131 CriticalSelectionChanged();
135 PortExportChannelSelector::update_split_state ()
137 state->config->set_split (split_checkbox.get_active());
138 CriticalSelectionChanged();
142 PortExportChannelSelector::RouteCols::add_channels (uint32_t chans)
145 channels.push_back (Channel (*this));
151 PortExportChannelSelector::RouteCols::Channel &
152 PortExportChannelSelector::RouteCols::get_channel (uint32_t channel)
154 if (channel > n_channels) {
155 std::cout << "Invalid channel cout for get_channel!" << std::endl;
158 std::list<Channel>::iterator it = channels.begin();
160 while (channel > 1) { // Channel count starts from one!
168 PortExportChannelSelector::ChannelTreeView::ChannelTreeView (uint32_t max_channels) :
173 route_cols.add_channels (max_channels);
175 route_list = Gtk::ListStore::create(route_cols);
176 set_model (route_list);
178 /* Add column with toggle and text */
180 append_column_editable (_("Bus or Track"), route_cols.selected);
182 Gtk::CellRendererText* text_renderer = Gtk::manage (new Gtk::CellRendererText);
183 text_renderer->property_editable() = false;
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);
189 Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *>(get_column_cell_renderer (0));
190 toggle->signal_toggled().connect (mem_fun (*this, &PortExportChannelSelector::ChannelTreeView::update_toggle_selection));
192 static_columns = get_columns().size();
196 PortExportChannelSelector::ChannelTreeView::set_config (ChannelConfigPtr c)
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
203 if (config == c) { return; }
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) {
210 for (Gtk::ListStore::Children::iterator r_it = route_list->children().begin(); r_it != route_list->children().end(); ++r_it) {
212 ARDOUR::PortExportChannel * pec;
213 if (!(pec = dynamic_cast<ARDOUR::PortExportChannel *> (c_it->get()))) {
217 Glib::RefPtr<Gtk::ListStore> port_list = r_it->get_value (route_cols.port_list_col);
218 std::set<AudioPort *> route_ports;
219 std::set<AudioPort *> intersection;
220 std::map<AudioPort *, ustring> port_labels;
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 (std::pair<AudioPort*, ustring> ((*p_it)->get_value (route_cols.port_cols.port),
225 (*p_it)->get_value (route_cols.port_cols.label)));
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<AudioPort *> > (intersection, intersection.begin()));
232 intersection.erase (0); // Remove "none" selection
234 if (intersection.empty()) {
238 if (!r_it->get_value (route_cols.selected)) {
239 r_it->set_value (route_cols.selected, true);
241 /* Set previous channels (if any) to none */
243 for (uint32_t chn = 1; chn < i; ++chn) {
244 r_it->set_value (route_cols.get_channel (chn).port, (AudioPort *) 0);
245 r_it->set_value (route_cols.get_channel (chn).label, ustring ("(none)"));
249 AudioPort * port = *intersection.begin();
250 std::map<AudioPort *, ustring>::iterator label_it = port_labels.find (port);
251 ustring label = label_it != port_labels.end() ? label_it->second : "error";
253 r_it->set_value (route_cols.get_channel (i).port, port);
254 r_it->set_value (route_cols.get_channel (i).label, label);
262 PortExportChannelSelector::ChannelTreeView::add_route (ARDOUR::IO * route)
264 Gtk::TreeModel::iterator iter = route_list->append();
265 Gtk::TreeModel::Row row = *iter;
267 row[route_cols.selected] = false;
268 row[route_cols.name] = route->name();
269 row[route_cols.io] = route;
271 /* Initialize port list */
273 Glib::RefPtr<Gtk::ListStore> port_list = Gtk::ListStore::create (route_cols.port_cols);
274 row[route_cols.port_list_col] = port_list;
276 uint32_t outs = route->n_outputs().n_audio();
277 for (uint32_t i = 0; i < outs; ++i) {
278 iter = port_list->append();
281 row[route_cols.port_cols.selected] = false;
282 row[route_cols.port_cols.port] = route->audio_output (i);
284 std::ostringstream oss;
285 oss << "Out-" << (i + 1);
287 row[route_cols.port_cols.label] = oss.str();
290 iter = port_list->append();
293 row[route_cols.port_cols.selected] = false;
294 row[route_cols.port_cols.port] = 0;
295 row[route_cols.port_cols.label] = "(none)";
300 PortExportChannelSelector::ChannelTreeView::set_channel_count (uint32_t channels)
302 int offset = channels - n_channels;
307 std::ostringstream oss;
312 Gtk::TreeView::Column* column = Gtk::manage (new Gtk::TreeView::Column (oss.str()));
314 Gtk::CellRendererCombo* combo_renderer = Gtk::manage (new Gtk::CellRendererCombo);
315 combo_renderer->property_text_column() = 2;
316 column->pack_start (*combo_renderer);
318 append_column (*column);
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);
324 combo_renderer->signal_edited().connect (sigc::bind (sigc::mem_fun (*this, &PortExportChannelSelector::ChannelTreeView::update_selection_text), n_channels));
326 /* put data into view */
328 for (Gtk::ListStore::Children::iterator it = route_list->children().begin(); it != route_list->children().end(); ++it) {
329 Glib::ustring 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, (AudioPort *) 0);
334 /* set column width */
336 get_column (static_columns + n_channels - 1)->set_min_width (80);
344 remove_column (*get_column (n_channels + static_columns));
353 PortExportChannelSelector::ChannelTreeView::update_config ()
356 if (!config) { return; }
358 config->clear_channels();
360 for (uint32_t i = 1; i <= n_channels; ++i) {
362 ExportChannelPtr channel (new PortExportChannel ());
363 PortExportChannel * pec = static_cast<PortExportChannel *> (channel.get());
365 for (Gtk::ListStore::Children::iterator it = route_list->children().begin(); it != route_list->children().end(); ++it) {
366 Gtk::TreeModel::Row row = *it;
368 if (!row[route_cols.selected]) {
372 AudioPort * port = row[route_cols.get_channel (i).port];
374 pec->add_port (port);
378 config->register_channel (channel);
381 CriticalSelectionChanged ();
385 PortExportChannelSelector::ChannelTreeView::update_toggle_selection (Glib::ustring const & path)
387 Gtk::TreeModel::iterator iter = get_model ()->get_iter (path);
388 bool selected = iter->get_value (route_cols.selected);
390 for (uint32_t i = 1; i <= n_channels; ++i) {
393 iter->set_value (route_cols.get_channel (i).label, Glib::ustring (""));
397 iter->set_value (route_cols.get_channel (i).label, Glib::ustring("(none)"));
398 iter->set_value (route_cols.get_channel (i).port, (AudioPort *) 0);
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;
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, (Glib::ustring) (*port_it)->get_value (route_cols.port_cols.label));
407 iter->set_value (route_cols.get_channel (i).port, (AudioPort *) (*port_it)->get_value (route_cols.port_cols.port));
418 PortExportChannelSelector::ChannelTreeView::update_selection_text (Glib::ustring const & path, Glib::ustring const & new_text, uint32_t channel)
420 Gtk::TreeModel::iterator iter = get_model ()->get_iter (path);
421 iter->set_value (route_cols.get_channel (channel).label, new_text);
423 Glib::RefPtr<Gtk::ListStore> port_list = iter->get_value (route_cols.port_list_col);
424 Gtk::ListStore::Children::iterator port_it;
426 for (port_it = port_list->children().begin(); port_it != port_list->children().end(); ++port_it) {
427 Glib::ustring label = port_it->get_value (route_cols.port_cols.label);
428 if (label == new_text) {
429 iter->set_value (route_cols.get_channel (channel).port, (AudioPort *) (*port_it)[route_cols.port_cols.port]);
436 RegionExportChannelSelector::RegionExportChannelSelector (ARDOUR::AudioRegion const & region, ARDOUR::AudioTrack & track) :
440 region_chans (region.n_channels()),
441 track_chans (track.n_outputs().n_audio()),
443 raw_button (type_group),
444 fades_button (type_group),
445 processed_button (type_group)
449 raw_button.set_label (string_compose (_("Region contents without fades (channels: %1)"), region_chans));
450 raw_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
451 vbox.pack_start (raw_button);
453 fades_button.set_label (string_compose (_("Region contents with fades (channels: %1)"), region_chans));
454 fades_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
455 vbox.pack_start (fades_button);
457 processed_button.set_label (string_compose (_("Track output (channels: %1)"), track_chans));
458 processed_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
459 vbox.pack_start (processed_button);
461 vbox.show_all_children ();
462 show_all_children ();
466 RegionExportChannelSelector::set_state (ARDOUR::ExportProfileManager::ChannelConfigStatePtr const state_, ARDOUR::Session * session_)
475 RegionExportChannelSelector::handle_selection ()
481 state->config->clear_channels ();
483 if (raw_button.get_active ()) {
484 factory.reset (new RegionExportChannelFactory (session, region, track, RegionExportChannelFactory::Raw));
485 } else if (fades_button.get_active ()) {
486 factory.reset (new RegionExportChannelFactory (session, region, track, RegionExportChannelFactory::Fades));
487 } else if (processed_button.get_active ()) {
488 factory.reset (new RegionExportChannelFactory(session, region, track, RegionExportChannelFactory::Processed));
490 CriticalSelectionChanged ();
494 for (size_t chan = 0; chan < region_chans; ++chan) {
495 state->config->register_channel (factory->create (chan));
498 CriticalSelectionChanged ();