libmidi.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2015,2016 Felipe F. Tonello <eu@felipetonello.com>
  7. * Copyright (C) 2016 ROLI Ltd.
  8. *
  9. */
  10. #ifndef LIBMIDI_H
  11. #define LIBMIDI_H
  12. #include <stdint.h>
  13. #include <stdbool.h>
  14. #include <alsa/asoundlib.h>
  15. #define MIDI_UUID "03B80E5A-EDE8-4B33-A751-6CE34EC4C700"
  16. #define MIDI_IO_UUID "7772E5DB-3868-4112-A1A9-F2669D106BF3"
  17. #define MIDI_MAX_TIMESTAMP 8191
  18. #define MIDI_MSG_MAX_SIZE 12
  19. #define MIDI_SYSEX_MAX_SIZE (4 * 1024)
  20. struct midi_buffer {
  21. uint8_t *data;
  22. size_t len;
  23. };
  24. /* MIDI I/O Write parser */
  25. struct midi_write_parser {
  26. int64_t rtime; /* last writer's real time */
  27. snd_seq_event_type_t rstatus; /* running status event type */
  28. struct midi_buffer midi_stream; /* MIDI I/O byte stream */
  29. size_t stream_size; /* what is the maximum size of the midi_stream array */
  30. snd_midi_event_t *midi_ev; /* midi<->seq event */
  31. };
  32. int midi_write_init(struct midi_write_parser *parser, size_t buffer_size);
  33. static inline void midi_write_free(struct midi_write_parser *parser)
  34. {
  35. free(parser->midi_stream.data);
  36. snd_midi_event_free(parser->midi_ev);
  37. }
  38. static inline void midi_write_reset(struct midi_write_parser *parser)
  39. {
  40. parser->rstatus = SND_SEQ_EVENT_NONE;
  41. parser->midi_stream.len = 0;
  42. }
  43. static inline bool midi_write_has_data(const struct midi_write_parser *parser)
  44. {
  45. return parser->midi_stream.len > 0;
  46. }
  47. static inline const uint8_t * midi_write_data(const struct midi_write_parser *parser)
  48. {
  49. return parser->midi_stream.data;
  50. }
  51. static inline size_t midi_write_data_size(const struct midi_write_parser *parser)
  52. {
  53. return parser->midi_stream.len;
  54. }
  55. typedef void (*midi_read_ev_cb)(const struct midi_write_parser *parser, void *);
  56. /* It creates BLE-MIDI raw packets from the a sequencer event. If the packet
  57. is full, then it calls write_cb and resets its internal state as many times
  58. as necessary.
  59. */
  60. void midi_read_ev(struct midi_write_parser *parser, const snd_seq_event_t *ev,
  61. midi_read_ev_cb write_cb, void *user_data);
  62. /* MIDI I/O Read parser */
  63. struct midi_read_parser {
  64. uint8_t rstatus; /* running status byte */
  65. int64_t rtime; /* last reader's real time */
  66. int16_t timestamp; /* last MIDI-BLE timestamp */
  67. int8_t timestamp_low; /* MIDI-BLE timestampLow from the current packet */
  68. int8_t timestamp_high; /* MIDI-BLE timestampHigh from the current packet */
  69. struct midi_buffer sysex_stream; /* SysEx stream */
  70. snd_midi_event_t *midi_ev; /* midi<->seq event */
  71. };
  72. int midi_read_init(struct midi_read_parser *parser);
  73. static inline void midi_read_free(struct midi_read_parser *parser)
  74. {
  75. free(parser->sysex_stream.data);
  76. snd_midi_event_free(parser->midi_ev);
  77. }
  78. static inline void midi_read_reset(struct midi_read_parser *parser)
  79. {
  80. parser->rstatus = 0;
  81. parser->timestamp_low = 0;
  82. parser->timestamp_high = 0;
  83. }
  84. /* Parses raw BLE-MIDI messages and populates a sequencer event representing the
  85. current MIDI message. It returns how much raw data was processed.
  86. */
  87. size_t midi_read_raw(struct midi_read_parser *parser, const uint8_t *data,
  88. size_t size, snd_seq_event_t *ev /* OUT */);
  89. #endif /* LIBMIDI_H */