40 #ifdef PLATFORM_WINDOWS
43 #include <arpa/inet.h>
49 #define BUFFER_SIZE 1024
119 char *buf, *extracted;
123 g_critical(
"smf_event_decode_textual: malloc failed.");
128 if (extracted == NULL) {
133 snprintf(buf + off,
BUFFER_SIZE - off,
"%s: %s", name, extracted);
141 int off = 0, mspqn, flats, isminor;
144 static const char *
const major_keys[] = {
"Fb",
"Cb",
"Gb",
"Db",
"Ab",
145 "Eb",
"Bb",
"F",
"C",
"G",
"D",
"A",
"E",
"B",
"F#",
"C#",
"G#"};
147 static const char *
const minor_keys[] = {
"Dbm",
"Abm",
"Ebm",
"Bbm",
"Fm",
148 "Cm",
"Gm",
"Dm",
"Am",
"Em",
"Bm",
"F#m",
"C#m",
"G#m",
"D#m",
"A#m",
"E#m"};
186 g_critical(
"smf_event_decode_metadata: malloc failed.");
192 off += snprintf(buf + off,
BUFFER_SIZE - off,
"Sequence number");
198 g_critical(
"smf_event_decode_metadata: truncated MIDI message.");
207 g_critical(
"smf_event_decode_metadata: truncated MIDI message.");
215 off += snprintf(buf + off,
BUFFER_SIZE - off,
"End Of Track");
220 g_critical(
"smf_event_decode_metadata: truncated MIDI message.");
224 mspqn = (
event->midi_buffer[3] << 16) + (event->
midi_buffer[4] << 8) +
event->midi_buffer[5];
226 off += snprintf(buf + off,
BUFFER_SIZE - off,
"Tempo: %d microseconds per quarter note, %.2f BPM",
227 mspqn, 60000000.0 / (
double)mspqn);
231 off += snprintf(buf + off,
BUFFER_SIZE - off,
"SMPTE Offset");
236 g_critical(
"smf_event_decode_metadata: truncated MIDI message.");
241 "Time Signature: %d/%d, %d clocks per click, %d notated 32nd notes per quarter note",
248 g_critical(
"smf_event_decode_metadata: truncated MIDI message.");
252 flats =
event->midi_buffer[3];
253 isminor =
event->midi_buffer[4];
255 if (isminor != 0 && isminor != 1) {
256 g_critical(
"smf_event_decode_metadata: last byte of the Key Signature event has invalid value %d.", isminor);
260 off += snprintf(buf + off,
BUFFER_SIZE - off,
"Key Signature: ");
262 if (flats > 8 && flats < 248) {
263 off += snprintf(buf + off,
BUFFER_SIZE - off,
"%d %s, %s key", abs((int8_t)flats),
264 flats > 127 ?
"flats" :
"sharps", isminor ?
"minor" :
"major");
266 int i = (flats - 248) & 255;
268 assert(i >= 0 && (
size_t)i <
sizeof(minor_keys) /
sizeof(*minor_keys));
269 assert(i >= 0 && (
size_t)i <
sizeof(major_keys) /
sizeof(*major_keys));
272 off += snprintf(buf + off,
BUFFER_SIZE - off,
"%s", minor_keys[i]);
274 off += snprintf(buf + off,
BUFFER_SIZE - off,
"%s", major_keys[i]);
280 off += snprintf(buf + off,
BUFFER_SIZE - off,
"Proprietary (aka Sequencer) Event, length %zu",
306 g_critical(
"smf_event_decode_system_realtime: event length is not 1.");
312 g_critical(
"smf_event_decode_system_realtime: malloc failed.");
318 off += snprintf(buf + off,
BUFFER_SIZE - off,
"MIDI Clock (realtime)");
322 off += snprintf(buf + off,
BUFFER_SIZE - off,
"Tick (realtime)");
326 off += snprintf(buf + off,
BUFFER_SIZE - off,
"MIDI Start (realtime)");
330 off += snprintf(buf + off,
BUFFER_SIZE - off,
"MIDI Continue (realtime)");
334 off += snprintf(buf + off,
BUFFER_SIZE - off,
"MIDI Stop (realtime)");
338 off += snprintf(buf + off,
BUFFER_SIZE - off,
"Active Sense (realtime)");
354 char *buf, manufacturer, subid, subid2;
359 g_critical(
"smf_event_decode_sysex: truncated MIDI message.");
365 g_critical(
"smf_event_decode_sysex: malloc failed.");
369 manufacturer =
event->midi_buffer[1];
371 if (manufacturer == 0x7F) {
373 }
else if (manufacturer == 0x7E) {
376 off += snprintf(buf + off,
BUFFER_SIZE - off,
"SysEx, manufacturer 0x%x", manufacturer);
382 subid =
event->midi_buffer[3];
383 subid2 =
event->midi_buffer[4];
386 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Sample Dump Header");
388 else if (subid == 0x02)
389 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Sample Dump Data Packet");
391 else if (subid == 0x03)
392 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Sample Dump Request");
394 else if (subid == 0x04 && subid2 == 0x01)
395 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Master Volume");
397 else if (subid == 0x05 && subid2 == 0x01)
398 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Sample Dump Loop Point Retransmit");
400 else if (subid == 0x05 && subid2 == 0x02)
401 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Sample Dump Loop Point Request");
403 else if (subid == 0x06 && subid2 == 0x01)
404 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Identity Request");
406 else if (subid == 0x06 && subid2 == 0x02)
407 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Identity Reply");
409 else if (subid == 0x08 && subid2 == 0x00)
410 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Bulk Tuning Dump Request");
412 else if (subid == 0x08 && subid2 == 0x01)
413 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Bulk Tuning Dump");
415 else if (subid == 0x08 && subid2 == 0x02)
416 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Single Note Tuning Change");
418 else if (subid == 0x08 && subid2 == 0x03)
419 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Bulk Tuning Dump Request (Bank)");
421 else if (subid == 0x08 && subid2 == 0x04)
422 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Key Based Tuning Dump");
424 else if (subid == 0x08 && subid2 == 0x05)
425 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Scale/Octave Tuning Dump, 1 byte format");
427 else if (subid == 0x08 && subid2 == 0x06)
428 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Scale/Octave Tuning Dump, 2 byte format");
430 else if (subid == 0x08 && subid2 == 0x07)
431 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Single Note Tuning Change (Bank)");
433 else if (subid == 0x09)
434 off += snprintf(buf + off,
BUFFER_SIZE - off,
", General MIDI %s", subid2 == 0 ?
"disable" :
"enable");
436 else if (subid == 0x7C)
437 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Sample Dump Wait");
439 else if (subid == 0x7D)
440 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Sample Dump Cancel");
442 else if (subid == 0x7E)
443 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Sample Dump NAK");
445 else if (subid == 0x7F)
446 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Sample Dump ACK");
449 off += snprintf(buf + off,
BUFFER_SIZE - off,
", Unknown");
468 g_critical(
"smf_event_decode_system_realtime: malloc failed.");
474 off += snprintf(buf + off,
BUFFER_SIZE - off,
"MTC Quarter Frame");
478 off += snprintf(buf + off,
BUFFER_SIZE - off,
"Song Position Pointer");
482 off += snprintf(buf + off,
BUFFER_SIZE - off,
"Song Select");
486 off += snprintf(buf + off,
BUFFER_SIZE - off,
"Tune Request");
502 const char *names[] = {
"C",
"C#",
"D",
"D#",
"E",
"F",
"F#",
"G",
"G#",
"A",
"A#",
"B"};
504 octave = note_number / 12 - 1;
505 note = note_number % 12;
507 sprintf(buf,
"%s%d", names[note], octave);
521 int off = 0, channel;
534 g_critical(
"smf_event_decode: incorrect MIDI message length.");
540 g_critical(
"smf_event_decode: malloc failed.");
545 channel = (
event->midi_buffer[0] & 0x0F) + 1;
550 off += snprintf(buf + off,
BUFFER_SIZE - off,
"Note Off, channel %d, note %s, velocity %d",
556 off += snprintf(buf + off,
BUFFER_SIZE - off,
"Note On, channel %d, note %s, velocity %d",
562 off += snprintf(buf + off,
BUFFER_SIZE - off,
"Aftertouch, channel %d, note %s, pressure %d",
567 off += snprintf(buf + off,
BUFFER_SIZE - off,
"Controller, channel %d, controller %d, value %d",
572 off += snprintf(buf + off,
BUFFER_SIZE - off,
"Program Change, channel %d, controller %d",
577 off += snprintf(buf + off,
BUFFER_SIZE - off,
"Channel Pressure, channel %d, pressure %d",
582 off += snprintf(buf + off,
BUFFER_SIZE - off,
"Pitch Wheel, channel %d, value %d",
612 g_critical(
"smf_event_decode: malloc failed.");
620 off += snprintf(buf + off,
BUFFER_SIZE - off,
"(single track)");
624 off += snprintf(buf + off,
BUFFER_SIZE - off,
"(several simultaneous tracks)");
628 off += snprintf(buf + off,
BUFFER_SIZE - off,
"(several independent tracks)");
632 off += snprintf(buf + off,
BUFFER_SIZE - off,
"(INVALID FORMAT)");
639 off += snprintf(buf + off,
BUFFER_SIZE - off,
"; division: %d PPQN", smf->
ppqn);
char * smf_decode(const smf_t *smf)
int smf_event_is_system_common(const smf_event_t *event)
size_t midi_buffer_length
LIBPBD_API Transmitter error
int smf_event_is_sysex(const smf_event_t *event)
int smf_event_is_system_realtime(const smf_event_t *event)
int smf_event_length_is_valid(const smf_event_t *event)
static void note_from_int(char *buf, int note_number)
static char * smf_event_decode_textual(const smf_event_t *event, const char *name)
char * smf_event_extract_text(const smf_event_t *event) WARN_UNUSED_RESULT
int smf_event_is_metadata(const smf_event_t *event)
char * smf_event_decode(const smf_event_t *event)
static char * smf_event_decode_sysex(const smf_event_t *event)
static char * smf_event_decode_system_realtime(const smf_event_t *event)
static char * smf_event_decode_metadata(const smf_event_t *event)
static char * smf_event_decode_system_common(const smf_event_t *event)