use new TransportMaster::speed_and_position() API
[ardour.git] / gtk2_ardour / transport_masters_dialog.cc
1 /*
2     Copyright (C) 2018 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "pbd/enumwriter.h"
21
22 #include "temporal/time.h"
23
24 #include "ardour/audioengine.h"
25 #include "ardour/session.h"
26 #include "ardour/transport_master.h"
27 #include "ardour/transport_master_manager.h"
28
29 #include "widgets/tooltips.h"
30
31 #include "gtkmm2ext/utils.h"
32 #include "gtkmm2ext/gui_thread.h"
33
34 #include "ardour_ui.h"
35 #include "transport_masters_dialog.h"
36
37 #include "pbd/i18n.h"
38
39 using namespace std;
40 using namespace Gtk;
41 using namespace Gtkmm2ext;
42 using namespace ARDOUR;
43 using namespace PBD;
44 using namespace ArdourWidgets;
45
46 TransportMastersWidget::TransportMastersWidget ()
47         : table (4, 13)
48 {
49         pack_start (table, PACK_EXPAND_WIDGET, 12);
50
51         col_title[0].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Use")));
52         col_title[1].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Name")));
53         col_title[2].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Type")));
54         col_title[3].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Format/\nBPM")));
55         col_title[4].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Current")));
56         col_title[5].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Last")));
57         col_title[6].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Timestamp")));
58         col_title[7].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Delta")));
59         col_title[8].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Collect")));
60         col_title[9].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Data Source")));
61         col_title[10].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Active\nCommands")));
62         col_title[11].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Clock\nSynced")));
63         col_title[12].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("29.97/30")));
64
65 #if 0
66         set_tooltip (col_title[12], _("<b>When enabled</b> the external timecode source is assumed to use 29.97 fps instead of 30000/1001.\n"
67                                       "SMPTE 12M-1999 specifies 29.97df as 30000/1001. The spec further mentions that "
68                                       "drop-sample timecode has an accumulated error of -86ms over a 24-hour period.\n"
69                                       "Drop-sample timecode would compensate exactly for a NTSC color frame rate of 30 * 0.9990 (ie 29.970000). "
70                                       "That is not the actual rate. However, some vendors use that rate - despite it being against the specs - "
71                                       "because the variant of using exactly 29.97 fps has zero timecode drift.\n"
72                              ));
73
74         set_tooltip (col_title[11], string_compose (_("<b>When enabled</b> the external timecode source is assumed to be sample-clock synced to the audio interface\n"
75                                                       "being used by %1."), PROGRAM_NAME));
76 #endif
77
78         table.set_spacings (6);
79
80         TransportMasterManager::instance().CurrentChanged.connect (current_connection, invalidator (*this), boost::bind (&TransportMastersWidget::current_changed, this, _1, _2), gui_context());
81
82         rebuild ();
83 }
84
85 TransportMastersWidget::~TransportMastersWidget ()
86 {
87         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
88                 delete *r;
89         }
90 }
91
92 void
93 TransportMastersWidget::current_changed (boost::shared_ptr<TransportMaster> old_master, boost::shared_ptr<TransportMaster> new_master)
94 {
95         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
96                 if ((*r)->tm == new_master) {
97                         (*r)->use_button.set_active (true);
98                         break; /* there can only be one */
99                 }
100         }
101 }
102
103 void
104 TransportMastersWidget::rebuild ()
105 {
106         TransportMasterManager::TransportMasters const & masters (TransportMasterManager::instance().transport_masters());
107
108         container_clear (table);
109
110         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
111                 delete *r;
112         }
113
114         rows.clear ();
115         table.resize (masters.size()+1, 13);
116
117         for (size_t col = 0; col < sizeof (col_title) / sizeof (col_title[0]); ++col) {
118                 table.attach (col_title[col], col, col+1, 0, 1);
119         }
120
121         uint32_t n = 1;
122
123         for (TransportMasterManager::TransportMasters::const_iterator m = masters.begin(); m != masters.end(); ++m, ++n) {
124
125                 Row* r = new Row;
126                 rows.push_back (r);
127
128                 r->tm = *m;
129                 r->label.set_text ((*m)->name());
130                 r->type.set_text (enum_2_string  ((*m)->type()));
131
132                 r->use_button.set_group (use_button_group);
133
134                 if (TransportMasterManager::instance().current() == r->tm) {
135                         r->use_button.set_active (true);
136                 }
137
138                 int col = 0;
139
140                 table.attach (r->use_button, col, col+1, n, n+1); ++col;
141                 table.attach (r->type, col, col+1, n, n+1); ++col;
142                 table.attach (r->label, col, col+1, n, n+1); ++col;
143                 table.attach (r->format, col, col+1, n, n+1); ++col;
144                 table.attach (r->current, col, col+1, n, n+1); ++col;
145                 table.attach (r->last, col, col+1, n, n+1); ++col;
146                 table.attach (r->timestamp, col, col+1, n, n+1); ++col;
147                 table.attach (r->delta, col, col+1, n, n+1); ++col;
148                 table.attach (r->collect_button, col, col+1, n, n+1); ++col;
149                 table.attach (r->port_combo, col, col+1, n, n+1); ++col;
150                 table.attach (r->request_options, col, col+1, n, n+1); ++col;
151
152                 boost::shared_ptr<TimecodeTransportMaster> ttm (boost::dynamic_pointer_cast<TimecodeTransportMaster> (r->tm));
153
154                 if (ttm) {
155                         table.attach (r->sclock_synced_button, col, col+1, n, n+1); ++col;
156                         table.attach (r->fr2997_button, col, col+1, n, n+1); ++col;
157                         r->fr2997_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::fr2997_button_toggled));
158                 }
159
160                 r->port_combo.signal_changed().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::port_choice_changed));
161                 r->use_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::use_button_toggled));
162                 r->collect_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::collect_button_toggled));
163                 r->request_options.signal_button_press_event().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::request_option_press), false);
164
165                 if (ttm) {
166                         r->sclock_synced_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::sync_button_toggled));
167                 }
168
169                 r->tm->PropertyChanged.connect (r->property_change_connection, invalidator (*this), boost::bind (&TransportMastersWidget::Row::prop_change, r, _1), gui_context());
170
171                 PropertyChange all_change;
172                 all_change.add (Properties::locked);
173                 all_change.add (Properties::collect);
174                 all_change.add (Properties::connected);
175
176                 if (ttm) {
177                         all_change.add (Properties::fr2997);
178                         all_change.add (Properties::sclock_synced);
179                 }
180
181                 r->prop_change (all_change);
182         }
183 }
184
185 TransportMastersWidget::Row::Row ()
186         : request_option_menu (0)
187         , ignore_active_change (false)
188 {
189 }
190
191 void
192 TransportMastersWidget::Row::prop_change (PropertyChange what_changed)
193 {
194         if (what_changed.contains (Properties::locked)) {
195         }
196
197         if (what_changed.contains (Properties::fr2997)) {
198                 fr2997_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->fr2997());
199         }
200
201         if (what_changed.contains (Properties::sclock_synced)) {
202                 sclock_synced_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->sample_clock_synced());
203         }
204
205         if (what_changed.contains (Properties::collect)) {
206                 collect_button.set_active (tm->collect());
207         }
208
209         if (what_changed.contains (Properties::connected)) {
210                 populate_port_combo ();
211         }
212
213         if (what_changed.contains (Properties::name)) {
214                 label.set_text (tm->name());
215         }
216 }
217
218 void
219 TransportMastersWidget::Row::use_button_toggled ()
220 {
221         if (use_button.get_active()) {
222                 Config->set_sync_source (tm->type());
223         }
224 }
225
226 void
227 TransportMastersWidget::Row::fr2997_button_toggled ()
228 {
229         boost::dynamic_pointer_cast<TimecodeTransportMaster>(tm)->set_fr2997 (fr2997_button.get_active());
230 }
231
232 void
233 TransportMastersWidget::Row::collect_button_toggled ()
234 {
235         tm->set_collect (collect_button.get_active());
236 }
237
238 void
239 TransportMastersWidget::Row::sync_button_toggled ()
240 {
241         tm->set_sample_clock_synced (sclock_synced_button.get_active());
242 }
243
244 bool
245 TransportMastersWidget::Row::request_option_press (GdkEventButton* ev)
246 {
247         if (ev->button == 1) {
248                 if (!request_option_menu) {
249                         build_request_options ();
250                 }
251                 request_option_menu->popup (1, ev->time);
252                 return true;
253         }
254         return false;
255 }
256
257 void
258 TransportMastersWidget::Row::build_request_options ()
259 {
260         using namespace Gtk::Menu_Helpers;
261
262         request_option_menu = manage (new Menu);
263
264         MenuList& items (request_option_menu->items());
265
266         items.push_back (CheckMenuElem (_("Accept speed-changing commands (start/stop)")));
267         Gtk::CheckMenuItem* i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
268         i->set_active (tm->request_mask() & TR_Speed);
269         items.push_back (CheckMenuElem (_("Accept locate commands")));
270         i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
271         i->set_active (tm->request_mask() & TR_Locate);
272 }
273
274 Glib::RefPtr<Gtk::ListStore>
275 TransportMastersWidget::Row::build_port_list (vector<string> const & ports)
276 {
277         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (port_columns);
278         TreeModel::Row row;
279
280         row = *store->append ();
281         row[port_columns.full_name] = string();
282         row[port_columns.short_name] = _("Disconnected");
283
284         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
285
286                 if (AudioEngine::instance()->port_is_mine (*p)) {
287                         continue;
288                 }
289
290                 row = *store->append ();
291                 row[port_columns.full_name] = *p;
292
293                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
294                 if (pn.empty ()) {
295                         pn = (*p).substr ((*p).find (':') + 1);
296                 }
297                 row[port_columns.short_name] = pn;
298         }
299
300         return store;
301 }
302
303 void
304 TransportMastersWidget::Row::populate_port_combo ()
305 {
306         if (!tm->port()) {
307                 port_combo.hide ();
308                 return;
309         } else {
310                 port_combo.show ();
311         }
312
313         vector<string> inputs;
314
315         if (tm->port()->type() == DataType::MIDI) {
316                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
317         } else {
318                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::AUDIO, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
319         }
320
321         Glib::RefPtr<Gtk::ListStore> input = build_port_list (inputs);
322         bool input_found = false;
323         int n;
324
325         port_combo.set_model (input);
326
327         Gtk::TreeModel::Children children = input->children();
328         Gtk::TreeModel::Children::iterator i;
329         i = children.begin();
330         ++i; /* skip "Disconnected" */
331
332
333         for (n = 1;  i != children.end(); ++i, ++n) {
334                 string port_name = (*i)[port_columns.full_name];
335                 if (tm->port()->connected_to (port_name)) {
336                         port_combo.set_active (n);
337                         input_found = true;
338                         break;
339                 }
340         }
341
342         if (!input_found) {
343                 port_combo.set_active (0); /* disconnected */
344         }
345 }
346
347 void
348 TransportMastersWidget::Row::port_choice_changed ()
349 {
350         if (ignore_active_change) {
351                 return;
352         }
353
354         TreeModel::iterator active = port_combo.get_active ();
355         string new_port = (*active)[port_columns.full_name];
356
357         if (new_port.empty()) {
358                 tm->port()->disconnect_all ();
359                 return;
360         }
361
362         if (!tm->port()->connected_to (new_port)) {
363                 tm->port()->disconnect_all ();
364                 tm->port()->connect (new_port);
365         }
366 }
367
368 void
369 TransportMastersWidget::Row::update (Session* s, samplepos_t now)
370 {
371         using namespace Timecode;
372
373         samplepos_t pos;
374         double speed;
375         samplepos_t last;
376         samplepos_t when;
377         stringstream ss;
378         Time t;
379         boost::shared_ptr<TimecodeTransportMaster> ttm;
380         boost::shared_ptr<MIDIClock_TransportMaster> mtm;
381
382         if (s) {
383
384                 if (tm->speed_and_position (speed, pos, last, when, now)) {
385
386                         sample_to_timecode (pos, t, false, false, 25, false, AudioEngine::instance()->sample_rate(), 100, false, 0);
387
388                         if ((ttm = boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm))) {
389                                 format.set_text (timecode_format_name (ttm->apparent_timecode_format()));
390                         } else if ((mtm = boost::dynamic_pointer_cast<MIDIClock_TransportMaster> (tm))) {
391                                 char buf[8];
392                                 snprintf (buf, sizeof (buf), "%.1f", mtm->bpm());
393                                 format.set_text (buf);
394                         } else {
395                                 format.set_text ("");
396                         }
397                         current.set_text (Timecode::timecode_format_time (t));
398                         timestamp.set_markup (tm->position_string());
399                         delta.set_markup (tm->delta_string ());
400
401                 }
402         }
403 }
404
405 void
406 TransportMastersWidget::update (samplepos_t audible)
407 {
408         samplepos_t now = AudioEngine::instance()->sample_time ();
409
410         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
411                 (*r)->update (_session, now);
412         }
413 }
414
415 void
416 TransportMastersWidget::on_map ()
417 {
418         update_connection = ARDOUR_UI::Clock.connect (sigc::mem_fun (*this, &TransportMastersWidget::update));
419         Gtk::VBox::on_map ();
420 }
421
422 void
423 TransportMastersWidget::on_unmap ()
424 {
425         update_connection.disconnect ();
426         Gtk::VBox::on_unmap ();
427 }
428
429 TransportMastersWindow::TransportMastersWindow ()
430         : ArdourWindow (_("Transport Masters"))
431 {
432         add (w);
433         w.show ();
434 }
435
436 void
437 TransportMastersWindow::on_realize ()
438 {
439         ArdourWindow::on_realize ();
440         /* (try to) ensure that resizing is possible and the window can be moved (and closed) */
441         get_window()->set_decorations (Gdk::DECOR_BORDER | Gdk::DECOR_RESIZEH | Gdk::DECOR_TITLE | Gdk::DECOR_MENU);
442 }
443
444
445
446 void
447 TransportMastersWindow::set_session (ARDOUR::Session* s)
448 {
449         ArdourWindow::set_session (s);
450         w.set_session (s);
451 }