Skip to contentSkip to conversation

MIDI over BLE implementation

PATCH Applied Felipe F. Tonello 2 participants 4 messages Jan 3, 2017 – Jan 4, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v5 cover MIDI over BLE implementation

This patchset implements the MIDI over BLE spec, more information on patch #1.

Patch #2 is some unit-tests to make sure the MIDI parser works properly.

Changes from v4:
* Fixed valgrind's memory corruption warning
* Added some asserts where needed
* Minor improvements on libmidi API
* Fixed potential memory leak in case of initialization errors
* More unit-test checks

Changes from v3:
* Splited patch in two (main profile and unit-test)
* Some refactor on duplicated and complex code
* Removed usage of GLib types

Changes from v2:
* Removed _GNU_SOURCE (not used)
* Appended " Bluetooth" to the ALSA-Seq port name
* Changed configuration from alsa to midi (--disable-midi)

Changes from v1:
* Fixed typos in commit message
* Minor improves on the SysEx parser

Felipe F. Tonello (2):
profiles/midi: Added MIDI over BLE profile implementation unit/test-midi: Added libmidi use-case tests

 Makefile.am             |  14 +-
 Makefile.plugins        |   8 +
 configure.ac            |  11 +
 profiles/midi/libmidi.c | 459 +++++++++++++++++++++++++++++++++++
 profiles/midi/libmidi.h | 122 ++++++++++
 profiles/midi/midi.c    | 490 +++++++++++++++++++++++++++++++++++++
 unit/test-midi.c        | 628 ++++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 1731 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
Signature · 1 line
2.11.0
#1 lore raw

PATCH v5 1/2 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.

Files added:
* profiles/midi/midi.c: Actual GATT plugin
* profiles/midi/libmidi.[ch]: MIDI parsers

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.

This patch introduces ALSA as dependency. But this feature is disabled by default. To enable it, pass --enable-midi to the configure script.

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 Cortex-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 support 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             |   4 +-
 Makefile.plugins        |   8 +
 configure.ac            |  11 ++
 profiles/midi/libmidi.c | 459 +++++++++++++++++++++++++++++++++++++++++++++
 profiles/midi/libmidi.h | 122 ++++++++++++
 profiles/midi/midi.c    | 490 ++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 1093 insertions(+), 1 deletion(-)
 create mode 100644 profiles/midi/libmidi.c
 create mode 100644 profiles/midi/libmidi.h
 create mode 100644 profiles/midi/midi.c
Makefile.am +3 -1
index c469a6caf83a..7a758fbde713 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
 
Makefile.plugins +8 -0
index 59342c0cb803..3a9e27c653dd 100644
@@ -95,6 +95,14 @@ builtin_sources += profiles/scanparam/scan.c
 builtin_modules += deviceinfo
 builtin_sources += profiles/deviceinfo/deviceinfo.c
 
+if MIDI
+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 +11 -0
index fe4103c19037..2b0363c5af94 100644
@@ -212,6 +212,17 @@ AC_ARG_ENABLE(cups, AC_HELP_STRING([--disable-cups],
                 [disable CUPS printer support]), [enable_cups=${enableval}])
 AM_CONDITIONAL(CUPS, test "${enable_cups}" != "no")
 
+AC_ARG_ENABLE(midi, AC_HELP_STRING([--enable-midi],
+                [enable MIDI support]), [enable_midi=${enableval}])
+AM_CONDITIONAL(MIDI, test "${enable_midi}" = "yes")
+
+if (test "${enable_midi}" = "yes"); then
+	PKG_CHECK_MODULES(ALSA, alsa, dummy=yes,
+				AC_MSG_ERROR(ALSA lib is required for MIDI support))
+	AC_SUBST(ALSA_CFLAGS)
+	AC_SUBST(ALSA_LIBS)
+fi
+
 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 +459 -0

459 lines not on this page · raw message

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

122 lines not on this page · raw message

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

490 lines not on this page · raw message

Signature · 1 line
2.11.0
#2 lore raw

PATCH v5 2/2 unit/test-midi: Added libmidi use-case tests

In reply to Felipe F. Tonello

I tried to test all basic and most common use-case scenarios here and some more weird as well. It tests cases where the parser needs to handle problematic MIDI messages as well as ALSA Sequencer events.

It is really simple to add new tests, so others are welcome to do so.

---
 Makefile.am      |  10 +
 unit/test-midi.c | 628 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 638 insertions(+)
 create mode 100644 unit/test-midi.c
Makefile.am +10 -0
index 7a758fbde713..f038cb2b5206 100644
@@ -423,6 +423,16 @@ unit_test_gattrib_LDADD = lib/libbluetooth-internal.la \
 			src/libshared-glib.la \
 			@GLIB_LIBS@ @DBUS_LIBS@ -ldl -lrt
 
+if MIDI
+unit_tests += unit/test-midi
+unit_test_midi_CFLAGS = $(AM_CFLAGS) @ALSA_CFLAGS@ -DMIDI_TEST
+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
unit/test-midi.c new file +628 -0

628 lines not on this page · raw message

Signature · 1 line
2.11.0
maintainer Applied
#3 lore raw
In reply to Felipe F. Tonello

Hi Felipe,

Quoted 42 lines · Felipe F. Tonello

This patchset implements the MIDI over BLE spec, more information on patch #1.

Patch #2 is some unit-tests to make sure the MIDI parser works properly.

Changes from v4:
* Fixed valgrind's memory corruption warning
* Added some asserts where needed
* Minor improvements on libmidi API
* Fixed potential memory leak in case of initialization errors
* More unit-test checks

Changes from v3:
* Splited patch in two (main profile and unit-test)
* Some refactor on duplicated and complex code
* Removed usage of GLib types

Changes from v2:
* Removed _GNU_SOURCE (not used)
* Appended " Bluetooth" to the ALSA-Seq port name
* Changed configuration from alsa to midi (--disable-midi)

Changes from v1:
* Fixed typos in commit message
* Minor improves on the SysEx parser

Felipe F. Tonello (2):
profiles/midi: Added MIDI over BLE profile implementation unit/test-midi: Added libmidi use-case tests

 Makefile.am             |  14 +-
 Makefile.plugins        |   8 +
 configure.ac            |  11 +
 profiles/midi/libmidi.c | 459 +++++++++++++++++++++++++++++++++++
 profiles/midi/libmidi.h | 122 ++++++++++
 profiles/midi/midi.c    | 490 +++++++++++++++++++++++++++++++++++++
 unit/test-midi.c        | 628 ++++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 1731 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
Quoted 2 lines

--
2.11.0

Applied, thanks.

Signature · 1 line
Luiz Augusto von Dentz