ardour
midi_tracer.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 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 <stdint.h>
21 
22 #include <sstream>
23 #include <sys/time.h>
24 #include <time.h>
25 
26 #include "pbd/localtime_r.h"
27 #include "pbd/timersub.h"
28 
29 #include "midi++/parser.h"
30 
31 #include "ardour/async_midi_port.h"
32 #include "ardour/midi_port.h"
33 #include "ardour/audioengine.h"
34 
35 #include "midi_tracer.h"
36 #include "gui_thread.h"
37 #include "i18n.h"
38 
39 using namespace Gtk;
40 using namespace std;
41 using namespace MIDI;
42 using namespace Glib;
43 
45  : ArdourWindow (_("MIDI Tracer"))
46  , line_count_adjustment (200, 1, 2000, 1, 10)
47  , line_count_spinner (line_count_adjustment)
48  , line_count_label (_("Line history: "))
49  , autoscroll (true)
50  , show_hex (true)
51  , show_delta_time (false)
52  , _update_queued (0)
53  , fifo (1024)
54  , buffer_pool ("miditracer", buffer_size, 1024) // 1024 256 byte buffers
55  , autoscroll_button (_("Auto-Scroll"))
56  , base_button (_("Decimal"))
57  , collect_button (_("Enabled"))
58  , delta_time_button (_("Delta times"))
59 {
61  (_manager_connection, invalidator (*this), boost::bind (&MidiTracer::ports_changed, this), gui_context());
62 
63  _last_receipt.tv_sec = 0;
64  _last_receipt.tv_usec = 0;
65 
66  VBox* vbox = manage (new VBox);
67  vbox->set_spacing (4);
68 
69  HBox* pbox = manage (new HBox);
70  pbox->set_spacing (6);
71  pbox->pack_start (*manage (new Label (_("Port:"))), false, false);
72 
73  _port_combo.signal_changed().connect (sigc::mem_fun (*this, &MidiTracer::port_changed));
74  pbox->pack_start (_port_combo);
75  pbox->show_all ();
76  vbox->pack_start (*pbox, false, false);
77 
78  scroller.add (text);
79  vbox->set_border_width (12);
80  vbox->pack_start (scroller, true, true);
81 
82  text.show ();
83  text.set_name ("MidiTracerTextView");
84  scroller.show ();
85  scroller.set_size_request (400, 400);
86 
87  collect_button.set_active (true);
88  base_button.set_active (false);
89  autoscroll_button.set_active (true);
90 
91  line_count_box.set_spacing (6);
92  line_count_box.pack_start (line_count_label, false, false);
93  line_count_box.pack_start (line_count_spinner, false, false);
94 
95  line_count_spinner.show ();
96  line_count_label.show ();
97  line_count_box.show ();
98 
99  HBox* bbox = manage (new HBox);
100  bbox->add (line_count_box);
101  bbox->add (delta_time_button);
102  bbox->add (base_button);
103  bbox->add (collect_button);
104  bbox->add (autoscroll_button);
105  bbox->show ();
106 
107  vbox->pack_start (*bbox, false, false);
108 
109  add (*vbox);
110 
111  base_button.signal_toggled().connect (sigc::mem_fun (*this, &MidiTracer::base_toggle));
112  collect_button.signal_toggled().connect (sigc::mem_fun (*this, &MidiTracer::collect_toggle));
113  autoscroll_button.signal_toggled().connect (sigc::mem_fun (*this, &MidiTracer::autoscroll_toggle));
114  delta_time_button.signal_toggled().connect (sigc::mem_fun (*this, &MidiTracer::delta_toggle));
115 
116  base_button.show ();
117  collect_button.show ();
118  autoscroll_button.show ();
119 
120  ports_changed ();
121  port_changed ();
122 }
123 
124 
126 {
127 }
128 
129 void
131 {
132  string const c = _port_combo.get_active_text ();
133  _port_combo.clear ();
134 
137 
138  if (pl.empty()) {
139  _port_combo.set_active_text ("");
140  return;
141  }
142 
143  for (ARDOUR::PortManager::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
144  _port_combo.append_text ((*i)->name());
145  }
146 
147  if (c.empty()) {
148  _port_combo.set_active_text (pl.front()->name());
149  } else {
150  _port_combo.set_active_text (c);
151  }
152 }
153 
154 void
156 {
157  using namespace ARDOUR;
158 
159  disconnect ();
160 
161  boost::shared_ptr<ARDOUR::Port> p = AudioEngine::instance()->get_port_by_name (_port_combo.get_active_text());
162 
163  if (!p) {
164  std::cerr << "port not found\n";
165  return;
166  }
167 
168  /* The inheritance heirarchy makes this messy. AsyncMIDIPort has two
169  * available MIDI::Parsers what we could connect to, ::self_parser()
170  * (from ARDOUR::MidiPort) and ::parser() from MIDI::Port. One day,
171  * this mess will all go away ...
172  */
173 
175 
176  if (!async) {
177 
179 
180  if (mp) {
181  mp->self_parser().any.connect_same_thread (_parser_connection, boost::bind (&MidiTracer::tracer, this, _1, _2, _3));
182  mp->set_trace_on (true);
183  traced_port = mp;
184  }
185 
186  } else {
187  async->parser()->any.connect_same_thread (_parser_connection, boost::bind (&MidiTracer::tracer, this, _1, _2, _3));
188  }
189 }
190 
191 void
193 {
195 
196  if (traced_port) {
197  traced_port->set_trace_on (false);
198  traced_port.reset ();
199  }
200 }
201 
202 void
203 MidiTracer::tracer (Parser&, byte* msg, size_t len)
204 {
205  stringstream ss;
206  struct timeval tv;
207  char* buf;
208  struct tm now;
209  size_t bufsize;
210  size_t s;
211 
212  gettimeofday (&tv, 0);
213 
214  buf = (char *) buffer_pool.alloc ();
215  bufsize = buffer_size;
216 
217  if (_last_receipt.tv_sec != 0 && show_delta_time) {
218  struct timeval delta;
219  timersub (&tv, &_last_receipt, &delta);
220  s = snprintf (buf, bufsize, "+%02" PRId64 ":%06" PRId64, (int64_t) delta.tv_sec, (int64_t) delta.tv_usec);
221  bufsize -= s;
222  } else {
223  localtime_r ((const time_t*)&tv.tv_sec, &now);
224  s = strftime (buf, bufsize, "%H:%M:%S", &now);
225  bufsize -= s;
226  s += snprintf (&buf[s], bufsize, ".%06" PRId64, (int64_t) tv.tv_usec);
227  bufsize -= s;
228  }
229 
230  _last_receipt = tv;
231 
232  switch ((eventType) msg[0]&0xf0) {
233  case off:
234  if (show_hex) {
235  s += snprintf (&buf[s], bufsize, "%16s chn %2d %02x %02x\n", "NoteOff", (msg[0]&0xf)+1, (int) msg[1], (int) msg[2]);
236  } else {
237  s += snprintf (&buf[s], bufsize, "%16s chn %2d %-3d %-3d\n", "NoteOff", (msg[0]&0xf)+1, (int) msg[1], (int) msg[2]);
238  }
239  break;
240 
241  case on:
242  if (show_hex) {
243  s += snprintf (&buf[s], bufsize, "%16s chn %2d %02x %02x\n", "NoteOn", (msg[0]&0xf)+1, (int) msg[1], (int) msg[2]);
244  } else {
245  s += snprintf (&buf[s], bufsize, "%16s chn %2d %-3d %-3d\n", "NoteOn", (msg[0]&0xf)+1, (int) msg[1], (int) msg[2]);
246  }
247  break;
248 
249  case polypress:
250  if (show_hex) {
251  s += snprintf (&buf[s], bufsize, "%16s chn %2d %02x\n", "PolyPressure", (msg[0]&0xf)+1, (int) msg[1]);
252  } else {
253  s += snprintf (&buf[s], bufsize, "%16s chn %2d %-3d\n", "PolyPressure", (msg[0]&0xf)+1, (int) msg[1]);
254  }
255  break;
256 
257  case MIDI::controller:
258  if (show_hex) {
259  s += snprintf (&buf[s], bufsize, "%16s chn %2d %02x %02x\n", "Controller", (msg[0]&0xf)+1, (int) msg[1], (int) msg[2]);
260  } else {
261  s += snprintf (&buf[s], bufsize, "%16s chn %2d %2d %-3d\n", "Controller", (msg[0]&0xf)+1, (int) msg[1], (int) msg[2]);
262  }
263  break;
264 
265  case program:
266  if (show_hex) {
267  s += snprintf (&buf[s], bufsize, "%16s chn %2d %02x\n", "Program Change", (msg[0]&0xf)+1, (int) msg[1]);
268  } else {
269  s += snprintf (&buf[s], bufsize, "%16s chn %2d %-3d\n", "Program Change", (msg[0]&0xf)+1, (int) msg[1]);
270  }
271  break;
272 
273  case chanpress:
274  if (show_hex) {
275  s += snprintf (&buf[s], bufsize, "%16s chn %2d %02x/%-3d\n", "Channel Pressure", (msg[0]&0xf)+1, (int) msg[1], (int) msg[1]);
276  } else {
277  s += snprintf (&buf[s], bufsize, "%16s chn %2d %02x/%-3d\n", "Channel Pressure", (msg[0]&0xf)+1, (int) msg[1], (int) msg[1]);
278  }
279  break;
280 
281  case MIDI::pitchbend:
282  if (show_hex) {
283  s += snprintf (&buf[s], bufsize, "%16s chn %2d %02x\n", "Pitch Bend", (msg[0]&0xf)+1, (int) msg[1]);
284  } else {
285  s += snprintf (&buf[s], bufsize, "%16s chn %2d %-3d\n", "Pitch Bend", (msg[0]&0xf)+1, (int) msg[1]);
286  }
287  break;
288 
289  case MIDI::sysex:
290  if (len == 1) {
291  switch (msg[0]) {
292  case 0xf8:
293  s += snprintf (&buf[s], bufsize, "%16s\n", "Clock");
294  break;
295  case 0xfa:
296  s += snprintf (&buf[s], bufsize, "%16s\n", "Start");
297  break;
298  case 0xfb:
299  s += snprintf (&buf[s], bufsize, "%16s\n", "Continue");
300  break;
301  case 0xfc:
302  s += snprintf (&buf[s], bufsize, "%16s\n", "Stop");
303  break;
304  case 0xfe:
305  s += snprintf (&buf[s], bufsize, "%16s\n", "Active Sense");
306  break;
307  case 0xff:
308  s += snprintf (&buf[s], bufsize, "%16s\n", "Reset");
309  break;
310  default:
311  s += snprintf (&buf[s], bufsize, "%16s %02x\n", "Sysex", (int) msg[1]);
312  break;
313  }
314 
315  } else if (len > 5 && msg[0] == 0xf0 && msg[1] == 0x7f && msg[3] == 0x06) {
316  /* MMC */
317  int cmd = msg[4];
318  if (cmd == 0x44 && msg[5] == 0x06 && msg[6] == 0x01) {
319  s += snprintf (
320  &buf[s], bufsize, " MMC locate to %02d:%02d:%02d:%02d.%02d\n",
321  msg[7], msg[8], msg[9], msg[10], msg[11]
322  );
323  } else {
324  std::string name;
325  if (cmd == 0x1) {
326  name = "STOP";
327  } else if (cmd == 0x3) {
328  name = "DEFERRED PLAY";
329  } else if (cmd == 0x6) {
330  name = "RECORD STROBE";
331  } else if (cmd == 0x7) {
332  name = "RECORD EXIT";
333  } else if (cmd == 0x8) {
334  name = "RECORD PAUSE";
335  }
336  if (!name.empty()) {
337  s += snprintf (&buf[s], bufsize, " MMC command %s\n", name.c_str());
338  } else {
339  s += snprintf (&buf[s], bufsize, " MMC command %02x\n", cmd);
340  }
341  }
342 
343  } else if (len == 10 && msg[0] == 0xf0 && msg[1] == 0x7f && msg[9] == 0xf7) {
344 
345  /* MTC full frame */
346  s += snprintf (
347  &buf[s], bufsize, " MTC full frame to %02d:%02d:%02d:%02d\n", msg[5] & 0x1f, msg[6], msg[7], msg[8]
348  );
349  } else if (len == 3 && msg[0] == MIDI::position) {
350 
351  /* MIDI Song Position */
352  int midi_beats = (msg[2] << 7) | msg[1];
353  s += snprintf (&buf[s], bufsize, "%16s %d\n", "Position", (int) midi_beats);
354  } else {
355 
356  /* other sys-ex */
357 
358  s += snprintf (&buf[s], bufsize, "%16s (%d) = [", "Sysex", (int) len);
359  bufsize -= s;
360 
361  for (unsigned int i = 0; i < len && bufsize > 3; ++i) {
362  if (i > 0) {
363  s += snprintf (&buf[s], bufsize, " %02x", msg[i]);
364  } else {
365  s += snprintf (&buf[s], bufsize, "%02x", msg[i]);
366  }
367  bufsize -= s;
368  }
369  s += snprintf (&buf[s], bufsize, "]\n");
370  }
371  break;
372 
373  case MIDI::song:
374  s += snprintf (&buf[s], bufsize, "%16s\n", "Song");
375  break;
376 
377  case MIDI::tune:
378  s += snprintf (&buf[s], bufsize, "%16s\n", "Tune");
379  break;
380 
381  case MIDI::eox:
382  s += snprintf (&buf[s], bufsize, "%16s\n", "EOX");
383  break;
384 
385  case MIDI::timing:
386  s += snprintf (&buf[s], bufsize, "%16s\n", "Timing");
387  break;
388 
389  case MIDI::start:
390  s += snprintf (&buf[s], bufsize, "%16s\n", "Start");
391  break;
392 
393  case MIDI::stop:
394  s += snprintf (&buf[s], bufsize, "%16s\n", "Stop");
395  break;
396 
397  case MIDI::contineu:
398  s += snprintf (&buf[s], bufsize, "%16s\n", "Continue");
399  break;
400 
401  case active:
402  s += snprintf (&buf[s], bufsize, "%16s\n", "Active Sense");
403  break;
404 
405  default:
406  s += snprintf (&buf[s], bufsize, "%16s\n", "Unknown");
407  break;
408  }
409 
410  // If you want to append more to the line, uncomment this first
411  // bufsize -= s;
412 
413  assert(s <= buffer_size); // clang dead-assignment
414 
415  fifo.write (&buf, 1);
416 
417  if (g_atomic_int_get (const_cast<gint*> (&_update_queued)) == 0) {
418  gui_context()->call_slot (invalidator (*this), boost::bind (&MidiTracer::update, this));
419  g_atomic_int_inc (const_cast<gint*> (&_update_queued));
420  }
421 }
422 
423 void
425 {
426  bool updated = false;
427  g_atomic_int_dec_and_test (const_cast<gint*> (&_update_queued));
428 
429  RefPtr<TextBuffer> buf (text.get_buffer());
430 
431  int excess = buf->get_line_count() - line_count_adjustment.get_value();
432 
433  if (excess > 0) {
434  buf->erase (buf->begin(), buf->get_iter_at_line (excess));
435  }
436 
437  char *str;
438 
439  while (fifo.read (&str, 1)) {
440  buf->insert (buf->end(), string (str));
441  buffer_pool.release (str);
442  updated = true;
443  }
444 
445  if (updated && autoscroll) {
446  scroller.get_vadjustment()->set_value (scroller.get_vadjustment()->get_upper());
447  }
448 }
449 
450 void
452 {
453  show_hex = !base_button.get_active();
454 }
455 
456 void
458 {
459  show_delta_time = delta_time_button.get_active();
460 }
461 
462 void
464 {
465  if (collect_button.get_active ()) {
466  port_changed ();
467  } else {
468  disconnect ();
469  }
470 }
471 
472 void
474 {
475  autoscroll = autoscroll_button.get_active ();
476 }
void disconnect()
Definition: midi_tracer.cc:192
Gtk::HBox line_count_box
Definition: midi_tracer.h:58
boost::shared_ptr< ARDOUR::MidiPort > traced_port
Definition: midi_tracer.h:95
bool show_delta_time
Definition: midi_tracer.h:63
Gtk::CheckButton collect_button
Definition: midi_tracer.h:80
void collect_toggle()
Definition: midi_tracer.cc:463
void ports_changed()
Definition: midi_tracer.cc:130
Gtk::SpinButton line_count_spinner
Definition: midi_tracer.h:56
struct tm * localtime_r(const time_t *const timep, struct tm *p_tm)
Definition: localtime_r.cc:17
void delta_toggle()
Definition: midi_tracer.cc:457
Definition: ardour_ui.h:130
shared_ptr< T > dynamic_pointer_cast(shared_ptr< U > const &r)
Definition: shared_ptr.hpp:396
struct timeval _last_receipt
Definition: midi_tracer.h:59
MIDI::Parser & self_parser()
Definition: midi_port.h:61
Definition: Beats.hpp:239
void base_toggle()
Definition: midi_tracer.cc:451
Gtk::CheckButton delta_time_button
Definition: midi_tracer.h:81
LIBARDOUR_API PBD::PropertyDescriptor< framepos_t > start
Definition: region.cc:63
static AudioEngine * instance()
Definition: audioengine.h:196
Gtk::CheckButton autoscroll_button
Definition: midi_tracer.h:78
guint read(T *dest, guint cnt)
Definition: ringbuffer.h:124
#define invalidator(x)
Definition: gui_thread.h:40
PBD::ScopedConnection _parser_connection
Definition: midi_tracer.h:92
#define _(Text)
Definition: i18n.h:11
virtual void release(void *)
Definition: pool.cc:82
std::list< boost::shared_ptr< Port > > PortList
Definition: port_manager.h:47
Gtk::TextView text
Definition: midi_tracer.h:53
RingBuffer< char * > fifo
Definition: midi_tracer.h:71
Definition: amp.h:29
PBD::ScopedConnection _manager_connection
Definition: midi_tracer.h:93
#define gui_context()
Definition: gui_thread.h:36
bool autoscroll
Definition: midi_tracer.h:61
guint write(T const *src, guint cnt)
Definition: ringbuffer.h:163
Pool buffer_pool
Definition: midi_tracer.h:72
static const size_t buffer_size
Definition: midi_tracer.h:73
Gtk::ScrolledWindow scroller
Definition: midi_tracer.h:54
LIBARDOUR_API PBD::PropertyDescriptor< bool > active
Definition: route_group.cc:43
volatile gint _update_queued
Definition: midi_tracer.h:69
const char * name
Gtk::ComboBoxText _port_combo
Definition: midi_tracer.h:82
Gtk::Label line_count_label
Definition: midi_tracer.h:57
LIBARDOUR_API PBD::PropertyDescriptor< framepos_t > position
Definition: region.cc:65
void set_trace_on(bool yn)
Definition: midi_port.cc:313
PBD::Signal0< void > PortRegisteredOrUnregistered
Definition: port_manager.h:129
void update()
Definition: midi_tracer.cc:424
bool show_hex
Definition: midi_tracer.h:62
Gtk::CheckButton base_button
Definition: midi_tracer.h:79
void port_changed()
Definition: midi_tracer.cc:155
void tracer(MIDI::Parser &, MIDI::byte *, size_t)
Definition: midi_tracer.cc:203
#define timersub(a, b, result)
Definition: timersub.h:22
void autoscroll_toggle()
Definition: midi_tracer.cc:473
Gtk::Adjustment line_count_adjustment
Definition: midi_tracer.h:55
int get_ports(const std::string &port_name_pattern, DataType type, PortFlags flags, std::vector< std::string > &)
virtual void * alloc()
Definition: pool.cc:67
Definition: ardour.h:41