Skip to contentSkip to conversation

profiles/midi: Added MIDI over BLE profile implementation

PATCH Superseded Felipe F. Tonello 1 participant 1 message Nov 29, 2016

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 profiles/midi: Added MIDI over BLE profile implementation

This plugin implements the Central role of MIDI over Bluetooth Low-Energy (BLE-MIDI) 1.0 specification as published by MMA in November/2015.

It was implmemented as a bluetoothd plugin because of latency requirements of MIDI. There are still room for improvements on this regard.

Like previsouly mentioned, it only implements the Central role, but since all parsing and state-machine code is in libmidi.[hc] it should be simple to implement the Peripheral role as a GATT service as well.

I have also implemented several unit-tests that can be easily extended without adding any code, just use-cases.

Files added:
* profiles/midi/midi.c: Actual GATT plugin
* profiles/midi/libmidi.[ch]: MIDI parsers
* unit/test-midi.c: Unit-tests

Techinal notes
==============

This plugin doesn't require any new threads. It relies on notifications from a device to parse and render proper events that are queued in the kernel, causing no blocks at all. Even if an error occur, it will be handled and returned control to bluetoothd.

It also adds a new file descriptor to be read using struct io. That is necessary to read events from applications and render raw BLE packets to be sent to the device with a write without response command. It doesn't block as well.

ALSA dependency was added since there is no other way of doing it, sorry for it. :/ But this can be easily disabled with --disable-alsa configure flag.

Even though this introduces ALSA dependency, it is not an audio plugin. It is rather a MIDI plugin, which is a byte stream protocol with low throughput but requires low-latency.

Observations
============

I have tested on a normal laptop Arch-linux (x86_64) and a Raspberry Pi 2 (ARM A8) and it works very well. As I mentioned, the latency can always be improved.

I will still maintain a personal branch on my github[1] so others can contribute there and I can test before sending to bluez.

IMPORTAT: the timestamp support is incomplete since ALSA doesn't support the way MIDI over BLE expects (asign timestamp to an event without scheduling). We are working on ALSA to improve this.

Credits
=======

I would like to send kudos to ROLI Ltd. which allowed my to work on this as part of my full-time job.

[1] https://github.com/ftonello/bluez/

---
 Makefile.am             |  13 +-
 Makefile.plugins        |   8 +
 configure.ac            |  10 +
 profiles/midi/libmidi.c | 387 ++++++++++++++++++++++++++++++++++++++
 profiles/midi/libmidi.h | 140 ++++++++++++++
 profiles/midi/midi.c    | 489 ++++++++++++++++++++++++++++++++++++++++++++++++
 unit/test-midi.c        | 464 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 1510 insertions(+), 1 deletion(-)
 create mode 100644 profiles/midi/libmidi.c
 create mode 100644 profiles/midi/libmidi.h
 create mode 100644 profiles/midi/midi.c
 create mode 100644 unit/test-midi.c
Makefile.am +12 -1
index c469a6caf83a..c077b2568d5d 100644
@@ -147,6 +147,7 @@ gobex_sources = gobex/gobex.h gobex/gobex.c \
 builtin_modules =
 builtin_sources =
 builtin_nodist =
+builtin_ldadd =
 
 include Makefile.plugins
 
@@ -191,7 +192,8 @@ src_bluetoothd_SOURCES = $(builtin_sources) \
 src_bluetoothd_LDADD = lib/libbluetooth-internal.la \
 			gdbus/libgdbus-internal.la \
 			src/libshared-glib.la \
-			@BACKTRACE_LIBS@ @GLIB_LIBS@ @DBUS_LIBS@ -ldl -lrt
+			@BACKTRACE_LIBS@ @GLIB_LIBS@ @DBUS_LIBS@ -ldl -lrt \
+			$(builtin_ldadd)
 src_bluetoothd_LDFLAGS = $(AM_LDFLAGS) -Wl,--export-dynamic \
 				-Wl,--version-script=$(srcdir)/src/bluetooth.ver
 
@@ -421,6 +423,15 @@ unit_test_gattrib_LDADD = lib/libbluetooth-internal.la \
 			src/libshared-glib.la \
 			@GLIB_LIBS@ @DBUS_LIBS@ -ldl -lrt
 
+if ALSA
+unit_tests += unit/test-midi
+unit_test_midi_SOURCES = unit/test-midi.c \
+			profiles/midi/libmidi.h \
+			profiles/midi/libmidi.c
+unit_test_midi_LDADD = src/libshared-glib.la \
+			@GLIB_LIBS@ @ALSA_LIBS@
+endif
+
 if MAINTAINER_MODE
 noinst_PROGRAMS += $(unit_tests)
 endif
Makefile.plugins +8 -0
index 59342c0cb803..3600f113304c 100644
@@ -95,6 +95,14 @@ builtin_sources += profiles/scanparam/scan.c
 builtin_modules += deviceinfo
 builtin_sources += profiles/deviceinfo/deviceinfo.c
 
+if ALSA
+builtin_modules += midi
+builtin_sources += profiles/midi/midi.c \
+			profiles/midi/libmidi.h \
+			profiles/midi/libmidi.c
+builtin_ldadd += @ALSA_LIBS@
+endif
+
 if SIXAXIS
 plugin_LTLIBRARIES += plugins/sixaxis.la
 plugins_sixaxis_la_SOURCES = plugins/sixaxis.c
configure.ac +10 -0
index fe4103c19037..c84927fbcede 100644
@@ -210,8 +210,18 @@ AM_CONDITIONAL(HID2HCI, test "${enable_tools}" != "no" &&
 
 AC_ARG_ENABLE(cups, AC_HELP_STRING([--disable-cups],
                 [disable CUPS printer support]), [enable_cups=${enableval}])
+if (test "${enable_alsa}" != "no"); then
+	PKG_CHECK_MODULES(ALSA, alsa, dummy=yes,
+				AC_MSG_ERROR(ALSA lib is required))
+	AC_SUBST(ALSA_CFLAGS)
+	AC_SUBST(ALSA_LIBS)
+fi
 AM_CONDITIONAL(CUPS, test "${enable_cups}" != "no")
 
+AC_ARG_ENABLE(alsa, AC_HELP_STRING([--disable-alsa],
+                [disable ALSA MIDI support]), [enable_alsa=${enableval}])
+AM_CONDITIONAL(ALSA, test "${enable_alsa}" != "no")
+
 AC_ARG_ENABLE(obex, AC_HELP_STRING([--disable-obex],
 		[disable OBEX profile support]), [enable_obex=${enableval}])
 if (test "${enable_obex}" != "no"); then
profiles/midi/libmidi.c new file +387 -0

387 lines not on this page · raw message

profiles/midi/libmidi.h new file +140 -0

140 lines not on this page · raw message

profiles/midi/midi.c new file +489 -0

489 lines not on this page · raw message

unit/test-midi.c new file +464 -0

464 lines not on this page · raw message

Signature · 1 line
2.10.2