1 #define __STDC_FORMAT_MACROS 1
8 #include "midi++/parser.h"
10 #include "midi_tracer.h"
11 #include "gui_thread.h"
19 MidiTracer::MidiTracer (const std::string
& name
, Parser
& p
)
20 : ArdourDialog (string_compose (_("MIDI Trace %1"), name
))
22 , line_count_adjustment (200, 1, 2000, 1, 10)
23 , line_count_spinner (line_count_adjustment
)
24 , line_count_label (_("Store this many lines: "))
28 , update_queued (false)
30 , buffer_pool ("miditracer", buffer_size
, 1024) // 1024 256 byte buffers
31 , autoscroll_button (_("Auto-Scroll"))
32 , base_button (_("Decimal"))
33 , collect_button (_("Enabled"))
36 get_vbox()->set_border_width (12);
37 get_vbox()->pack_start (scroller
, true, true);
40 text
.set_name ("MidiTracerTextView");
42 scroller
.set_size_request (400, 400);
44 collect_button
.set_active (true);
45 base_button
.set_active (false);
46 autoscroll_button
.set_active (true);
48 line_count_box
.set_spacing (6);
49 line_count_box
.pack_start (line_count_label
, false, false);
50 line_count_box
.pack_start (line_count_spinner
, false, false);
52 line_count_spinner
.show ();
53 line_count_label
.show ();
54 line_count_box
.show ();
56 get_action_area()->add (line_count_box
);
58 HBox
* bbox
= manage (new HBox
);
59 bbox
->add (base_button
);
60 bbox
->add (collect_button
);
61 bbox
->add (autoscroll_button
);
64 get_action_area()->add (*bbox
);
66 base_button
.signal_toggled().connect (sigc::mem_fun (*this, &MidiTracer::base_toggle
));
67 collect_button
.signal_toggled().connect (sigc::mem_fun (*this, &MidiTracer::collect_toggle
));
68 autoscroll_button
.signal_toggled().connect (sigc::mem_fun (*this, &MidiTracer::autoscroll_toggle
));
71 collect_button
.show ();
72 autoscroll_button
.show ();
78 MidiTracer::~MidiTracer()
83 MidiTracer::connect ()
86 parser
.any
.connect_same_thread (connection
, boost::bind (&MidiTracer::tracer
, this, _1
, _2
, _3
));
90 MidiTracer::disconnect ()
92 connection
.disconnect ();
96 MidiTracer::tracer (Parser
&, byte
* msg
, size_t len
)
105 gettimeofday (&tv
, 0);
106 localtime_r (&tv
.tv_sec
, &now
);
108 buf
= (char *) buffer_pool
.alloc ();
109 bufsize
= buffer_size
;
111 s
= strftime (buf
, bufsize
, "%H:%M:%S", &now
);
113 s
+= snprintf (&buf
[s
], bufsize
, ".%06" PRId64
, (int64_t) tv
.tv_usec
);
116 switch ((eventType
) msg
[0]&0xf0) {
119 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %02x %02x\n", "NoteOff", (msg
[0]&0xf)+1, (int) msg
[1], (int) msg
[2]);
121 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %-3d %-3d\n", "NoteOff", (msg
[0]&0xf)+1, (int) msg
[1], (int) msg
[2]);
127 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %02x %02x\n", "NoteOn", (msg
[0]&0xf)+1, (int) msg
[1], (int) msg
[2]);
129 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %-3d %-3d\n", "NoteOn", (msg
[0]&0xf)+1, (int) msg
[1], (int) msg
[2]);
135 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %02x\n", "PolyPressure", (msg
[0]&0xf)+1, (int) msg
[1]);
137 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %-3d\n", "PolyPressure", (msg
[0]&0xf)+1, (int) msg
[1]);
141 case MIDI::controller
:
143 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %02x %02x\n", "Controller", (msg
[0]&0xf)+1, (int) msg
[1], (int) msg
[2]);
145 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %2d %-3d\n", "Controller", (msg
[0]&0xf)+1, (int) msg
[1], (int) msg
[2]);
151 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %02x\n", "Program Change", (msg
[0]&0xf)+1, (int) msg
[1]);
153 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %-3d\n", "Program Change", (msg
[0]&0xf)+1, (int) msg
[1]);
159 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %02x/%-3d\n", "Channel Pressure", (msg
[0]&0xf)+1, (int) msg
[1], (int) msg
[1]);
161 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %02x/%-3d\n", "Channel Pressure", (msg
[0]&0xf)+1, (int) msg
[1], (int) msg
[1]);
165 case MIDI::pitchbend
:
167 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %02x\n", "Pitch Bend", (msg
[0]&0xf)+1, (int) msg
[1]);
169 s
+= snprintf (&buf
[s
], bufsize
, "%16s chn %2d %-3d\n", "Pitch Bend", (msg
[0]&0xf)+1, (int) msg
[1]);
177 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Clock");
180 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Start");
183 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Continue");
186 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Stop");
189 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Active Sense");
192 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Reset");
195 s
+= snprintf (&buf
[s
], bufsize
, "%16s %02x\n", "Sysex", (int) msg
[1]);
201 s
+= snprintf (&buf
[s
], bufsize
, "%16s (%d) = [", "Sysex", (int) len
);
204 for (unsigned int i
= 0; i
< len
&& bufsize
> 3; ++i
) {
206 s
+= snprintf (&buf
[s
], bufsize
, " %02x", msg
[i
]);
208 s
+= snprintf (&buf
[s
], bufsize
, "%02x", msg
[i
]);
212 s
+= snprintf (&buf
[s
], bufsize
, "]\n");
217 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Song");
221 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Tune");
225 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "EOX");
229 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Timing");
233 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Start");
237 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Stop");
241 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Continue");
245 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Active Sense");
249 s
+= snprintf (&buf
[s
], bufsize
, "%16s\n", "Unknown");
253 // If you want to append more to the line, uncomment this first
256 fifo
.write (&buf
, 1);
258 if (!update_queued
) {
259 gui_context()->call_slot (invalidator (*this), boost::bind (&MidiTracer::update
, this));
260 update_queued
= true;
265 MidiTracer::update ()
267 bool updated
= false;
268 update_queued
= false;
270 RefPtr
<TextBuffer
> buf (text
.get_buffer());
272 int excess
= buf
->get_line_count() - line_count_adjustment
.get_value();
275 buf
->erase (buf
->begin(), buf
->get_iter_at_line (excess
));
280 while (fifo
.read (&str
, 1)) {
281 buf
->insert (buf
->end(), string (str
));
282 buffer_pool
.release (str
);
286 if (updated
&& autoscroll
) {
287 scroller
.get_vadjustment()->set_value (scroller
.get_vadjustment()->get_upper());
292 MidiTracer::base_toggle ()
294 show_hex
= !base_button
.get_active();
298 MidiTracer::collect_toggle ()
300 if (collect_button
.get_active ()) {
308 MidiTracer::autoscroll_toggle ()
310 autoscroll
= autoscroll_button
.get_active ();