2 Copyright (C) 2010 Paul Davis
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.
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.
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.
26 #include "midi++/parser.h"
27 #include "midi++/manager.h"
29 #include "midi_tracer.h"
30 #include "gui_thread.h"
38 MidiTracer::MidiTracer ()
39 : ArdourDialog (_("MIDI Tracer"))
41 , line_count_adjustment (200, 1, 2000, 1, 10)
42 , line_count_spinner (line_count_adjustment
)
43 , line_count_label (_("Store this many lines: "))
49 , buffer_pool ("miditracer", buffer_size
, 1024) // 1024 256 byte buffers
50 , autoscroll_button (_("Auto-Scroll"))
51 , base_button (_("Decimal"))
52 , collect_button (_("Enabled"))
54 get_vbox()->set_spacing (4);
56 Manager::instance()->PortsChanged
.connect (_manager_connection
, invalidator (*this), boost::bind (&MidiTracer::ports_changed
, this), gui_context());
58 HBox
* pbox
= manage (new HBox
);
59 pbox
->pack_start (*manage (new Label (_("Port:"))), false, false);
61 _port_combo
.signal_changed().connect (sigc::mem_fun (*this, &MidiTracer::port_changed
));
62 pbox
->pack_start (_port_combo
);
64 get_vbox()->pack_start (*pbox
, false, false);
67 get_vbox()->set_border_width (12);
68 get_vbox()->pack_start (scroller
, true, true);
71 text
.set_name ("MidiTracerTextView");
73 scroller
.set_size_request (400, 400);
75 collect_button
.set_active (true);
76 base_button
.set_active (false);
77 autoscroll_button
.set_active (true);
79 line_count_box
.set_spacing (6);
80 line_count_box
.pack_start (line_count_label
, false, false);
81 line_count_box
.pack_start (line_count_spinner
, false, false);
83 line_count_spinner
.show ();
84 line_count_label
.show ();
85 line_count_box
.show ();
87 get_action_area()->add (line_count_box
);
89 HBox
* bbox
= manage (new HBox
);
90 bbox
->add (base_button
);
91 bbox
->add (collect_button
);
92 bbox
->add (autoscroll_button
);
95 get_action_area()->add (*bbox
);
97 base_button
.signal_toggled().connect (sigc::mem_fun (*this, &MidiTracer::base_toggle
));
98 collect_button
.signal_toggled().connect (sigc::mem_fun (*this, &MidiTracer::collect_toggle
));
99 autoscroll_button
.signal_toggled().connect (sigc::mem_fun (*this, &MidiTracer::autoscroll_toggle
));
102 collect_button
.show ();
103 autoscroll_button
.show ();
110 MidiTracer::~MidiTracer()
115 MidiTracer::ports_changed ()
117 string
const c
= _port_combo
.get_active_text ();
118 _port_combo
.clear ();
120 Manager::PortList
const & p
= Manager::instance()->get_midi_ports ();
121 for (Manager::PortList::const_iterator i
= p
.begin(); i
!= p
.end(); ++i
) {
122 _port_combo
.append_text ((*i
)->name());
125 _port_combo
.set_active_text (c
);
129 MidiTracer::port_changed ()
133 Port
* p
= Manager::instance()->port (_port_combo
.get_active_text());
136 p
->parser()->any
.connect_same_thread (_parser_connection
, boost::bind (&MidiTracer::tracer
, this, _1
, _2
, _3
));
141 MidiTracer::disconnect ()
143 _parser_connection
.disconnect ();
147 MidiTracer::tracer (Parser
&, byte
* msg
, size_t len
)
156 gettimeofday (&tv
, 0);
157 localtime_r (&tv
.tv_sec
, &now
);
159 buf
= (char *) buffer_pool
.alloc ();
160 bufsize
= buffer_size
;
162 s
= strftime (buf
, bufsize
, "%H:%M:%S", &now
);
164 s
+= snprintf (&buf
[s
], bufsize
, ".%06" PRId64
, (int64_t) tv
.tv_usec
);
167 switch ((eventType
) msg
[0]&0xf0) {
170 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %02x %02x\n", "NoteOff", (msg
[0]&0xf)+1, (int) msg
[1], (int) msg
[2]);
172 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %-3d %-3d\n", "NoteOff", (msg
[0]&0xf)+1, (int) msg
[1], (int) msg
[2]);
178 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %02x %02x\n", "NoteOn", (msg
[0]&0xf)+1, (int) msg
[1], (int) msg
[2]);
180 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %-3d %-3d\n", "NoteOn", (msg
[0]&0xf)+1, (int) msg
[1], (int) msg
[2]);
186 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %02x\n", "PolyPressure", (msg
[0]&0xf)+1, (int) msg
[1]);
188 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %-3d\n", "PolyPressure", (msg
[0]&0xf)+1, (int) msg
[1]);
192 case MIDI::controller
:
194 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %02x %02x\n", "Controller", (msg
[0]&0xf)+1, (int) msg
[1], (int) msg
[2]);
196 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %2d %-3d\n", "Controller", (msg
[0]&0xf)+1, (int) msg
[1], (int) msg
[2]);
202 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %02x\n", "Program Change", (msg
[0]&0xf)+1, (int) msg
[1]);
204 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %-3d\n", "Program Change", (msg
[0]&0xf)+1, (int) msg
[1]);
210 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %02x/%-3d\n", "Channel Pressure", (msg
[0]&0xf)+1, (int) msg
[1], (int) msg
[1]);
212 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %02x/%-3d\n", "Channel Pressure", (msg
[0]&0xf)+1, (int) msg
[1], (int) msg
[1]);
216 case MIDI::pitchbend
:
218 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %02x\n", "Pitch Bend", (msg
[0]&0xf)+1, (int) msg
[1]);
220 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %-3d\n", "Pitch Bend", (msg
[0]&0xf)+1, (int) msg
[1]);
228 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Clock");
231 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Start");
234 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Continue");
237 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Stop");
240 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Active Sense");
243 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Reset");
246 s
+= snprintf (&buf
[s
], bufsize
, "%16s %02x\n", "Sysex", (int) msg
[1]);
250 } else if (len
> 5 && msg
[0] == 0xf0 && msg
[1] == 0x7f && msg
[3] == 0x06) {
253 if (cmd
== 0x44 && msg
[5] == 0x06 && msg
[6] == 0x01) {
255 &buf
[s
], bufsize
, " MMC locate to %02d:%02d:%02d:%02d.%02d\n",
256 msg
[7], msg
[8], msg
[9], msg
[10], msg
[11]
262 } else if (cmd
== 0x3) {
263 name
= "DEFERRED PLAY";
264 } else if (cmd
== 0x6) {
265 name
= "RECORD STROBE";
266 } else if (cmd
== 0x7) {
267 name
= "RECORD EXIT";
268 } else if (cmd
== 0x8) {
269 name
= "RECORD PAUSE";
272 s
+= snprintf (&buf
[s
], bufsize
, " MMC command %s\n", name
.c_str());
274 s
+= snprintf (&buf
[s
], bufsize
, " MMC command %02x\n", cmd
);
278 } else if (len
== 10 && msg
[0] == 0xf0 && msg
[1] == 0x7f && msg
[9] == 0xf7) {
282 &buf
[s
], bufsize
, " MTC full frame to %02d:%02d:%02d:%02d\n", msg
[5] & 0x1f, msg
[6], msg
[7], msg
[8]
289 s
+= snprintf (&buf
[s
], bufsize
, "%16s (%d) = [", "Sysex", (int) len
);
292 for (unsigned int i
= 0; i
< len
&& bufsize
> 3; ++i
) {
294 s
+= snprintf (&buf
[s
], bufsize
, " %02x", msg
[i
]);
296 s
+= snprintf (&buf
[s
], bufsize
, "%02x", msg
[i
]);
300 s
+= snprintf (&buf
[s
], bufsize
, "]\n");
305 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Song");
309 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Tune");
313 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "EOX");
317 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Timing");
321 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Start");
325 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Stop");
329 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Continue");
333 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Active Sense");
337 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Unknown");
341 // If you want to append more to the line, uncomment this first
344 fifo
.write (&buf
, 1);
346 if (g_atomic_int_get (&_update_queued
) == 0) {
347 gui_context()->call_slot (invalidator (*this), boost::bind (&MidiTracer::update
, this));
348 g_atomic_int_inc (&_update_queued
);
353 MidiTracer::update ()
355 bool updated
= false;
356 g_atomic_int_dec_and_test (&_update_queued
);
358 RefPtr
<TextBuffer
> buf (text
.get_buffer());
360 int excess
= buf
->get_line_count() - line_count_adjustment
.get_value();
363 buf
->erase (buf
->begin(), buf
->get_iter_at_line (excess
));
368 while (fifo
.read (&str
, 1)) {
369 buf
->insert (buf
->end(), string (str
));
370 buffer_pool
.release (str
);
374 if (updated
&& autoscroll
) {
375 scroller
.get_vadjustment()->set_value (scroller
.get_vadjustment()->get_upper());
380 MidiTracer::base_toggle ()
382 show_hex
= !base_button
.get_active();
386 MidiTracer::collect_toggle ()
388 if (collect_button
.get_active ()) {
396 MidiTracer::autoscroll_toggle ()
398 autoscroll
= autoscroll_button
.get_active ();