Categorieën bekijken

MIDI device via Bluetooth (linux)

In this example we are using a M-VAVE SMC-PAD Pocket and connect it with bluetooth (and make music/playback it on with a bluetooth speaker)

dmesg

[   59.596815] usb 5-1: new full-speed USB device number 2 using ohci-platform
[   59.825852] usb 5-1: New USB device found, idVendor=4353, idProduct=4b4d, bcdDevice= 1.00
[   59.825896] usb 5-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[   59.825917] usb 5-1: Product: SINCO
[   59.825933] usb 5-1: Manufacturer: Jieli Technology
[   59.825949] usb 5-1: SerialNumber: 433039343238332E
[   59.971929] usbcore: registered new interface driver snd-usb-audio

Connect via Bluetooth #

Please NOTE: Bluetooth manual basics

To create a connection with the built-in utils, you can follow this slightly more manual process using bluetoothctl.

hcitool scan  # to get the MAC address of your device
bluetoothctl
power on  # in case the bluez controller power is off 
agent on
scan on  # wait for your device's address to show up here
scan off
trust MAC_ADDRESS
pair MAC_ADDRRESS
connect MAC_ADDRESS

So let’s connect

To connect via bluetooth enter the bluetooth commandline:

bluetoothctl

Start the scan:

[bluetooth]# power on
[bluetooth]# agent on
[bluetooth]# scan on
Discovery started

Power on the MIDI Pad, eventuelly it will be discovered and you will see something like:

[NEW] Device 25:4E:5A:66:94:BC SMC-PAD Pocket

Turn scan off:

[bluetooth]# scan off

Trust the device by entering it’s MAC address:

[bluetooth]# trust 25:4E:5A:66:94:BC

Output:

[CHG] Device 25:4E:5A:66:94:BC Trusted: yes
[CHG] Device 25:4E:5A:66:94:BC ManufacturerData Key: 0x6973
[CHG] Device 25:4E:5A:66:94:BC ManufacturerData Value:
  6e 63 6f                                         nco
[CHG] Device 25:4E:5A:66:94:BC ManufacturerData Key: 0x05d6
[CHG] Device 25:4E:5A:66:94:BC ManufacturerData Value:
  08 00 4a 4c 41 49 53 44 4b                       ..JLAISDK

Pair with the MIDI Pad:

[bluetooth]# pair 25:4E:5A:66:94:BC

Output:

Attempting to pair with 25:4E:5A:66:94:BC
[CHG] Device 25:4E:5A:66:94:BC Connected: yes
[CHG] Device 25:4E:5A:66:94:BC Bonded: yes
[CHG] Device 25:4E:5A:66:94:BC UUIDs: 00001800-0000-1000-8000-00805f9b34fb
[CHG] Device 25:4E:5A:66:94:BC UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Device 25:4E:5A:66:94:BC UUIDs: 0000180a-0000-1000-8000-00805f9b34fb
[CHG] Device 25:4E:5A:66:94:BC UUIDs: 0000180f-0000-1000-8000-00805f9b34fb
[CHG] Device 25:4E:5A:66:94:BC UUIDs: 0000ae00-0000-1000-8000-00805f9b34fb
[CHG] Device 25:4E:5A:66:94:BC UUIDs: 0000ae40-0000-1000-8000-00805f9b34fb
[CHG] Device 25:4E:5A:66:94:BC UUIDs: 03b80e5a-ede8-4b33-a751-6ce34ec4c700
[CHG] Device 25:4E:5A:66:94:BC ServicesResolved: yes
[CHG] Device 25:4E:5A:66:94:BC Paired: yes
[NEW] Primary Service (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service0008
        00001801-0000-1000-8000-00805f9b34fb
        Generic Attribute Profile
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service0008/char0009
        00002a05-0000-1000-8000-00805f9b34fb
        Service Changed
[NEW] Descriptor (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service0008/char0009/desc000b
        00002902-0000-1000-8000-00805f9b34fb
        Client Characteristic Configuration
[NEW] Primary Service (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service000c
        0000180a-0000-1000-8000-00805f9b34fb
        Device Information
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service000c/char000d
        00002a29-0000-1000-8000-00805f9b34fb
        Manufacturer Name String
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service000c/char000f
        00002a24-0000-1000-8000-00805f9b34fb
        Model Number String
[NEW] Primary Service (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service001f
        0000180f-0000-1000-8000-00805f9b34fb
        Battery Service
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service001f/char0020
        00002a19-0000-1000-8000-00805f9b34fb
        Battery Level
[NEW] Descriptor (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service001f/char0020/desc0022
        00002902-0000-1000-8000-00805f9b34fb
        Client Characteristic Configuration
[NEW] Primary Service (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service0060
        0000ae40-0000-1000-8000-00805f9b34fb
        Unknown
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service0060/char0061
        0000ae41-0000-1000-8000-00805f9b34fb
        Unknown
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service0060/char0063
        0000ae42-0000-1000-8000-00805f9b34fb
        Unknown
[NEW] Descriptor (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service0060/char0063/desc0065
        00002902-0000-1000-8000-00805f9b34fb
        Client Characteristic Configuration
[NEW] Primary Service (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service0080
        0000ae00-0000-1000-8000-00805f9b34fb
        Unknown
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service0080/char0081
        0000ae01-0000-1000-8000-00805f9b34fb
        Unknown
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service0080/char0083
        0000ae02-0000-1000-8000-00805f9b34fb
        Unknown
[NEW] Descriptor (Handle 0x0000)
        /org/bluez/hci0/dev_25_4E_5A_66_94_BC/service0080/char0083/desc0085
        00002902-0000-1000-8000-00805f9b34fb
        Client Characteristic Configuration
Pairing successful
[CHG] Device 25:4E:5A:66:94:BC Appearance: 0x0080
[CHG] Device 25:4E:5A:66:94:BC Icon: computer

The device is now paired with the system, you can exit the bluetooth terminal.

aseqdump

sudo aseqdump -l

 Port    Client name                      Port name
  0:0    System                           Timer
  0:1    System                           Announce
129:0    RtMidi Output Client             from Node-RED
130:0    RtMidi Output Client             RtMidi Output
132:0    RtMidi Output Client             RtMidi Output
134:0    RtMidi Output Client             from Node-RED
136:0    RtMidi Output Client             RtMidi Output
138:0    RtMidi Output Client             from Node-RED
140:0    RtMidi Output Client             RtMidi Output
142:0    SMC-PAD Pocket                   SMC-PAD Pocket Bluetooth
143:0    RtMidi Output Client             from Node-RED
145:0    RtMidi Output Client             RtMidi Output
147:0    RtMidi Output Client             from Node-RED

You can see the port number: 142

Test MIDI input (note view) #

aseqdump

sudo aseqdump -p 142

Waiting for data. Press Ctrl+C to end.
Source  Event                  Ch  Data
142:0   Note on                 9, note 36, velocity 127
142:0   Note off                9, note 36, velocity 64
142:0   Note on                 9, note 37, velocity 105
142:0   Note off                9, note 37, velocity 64
142:0   Note on                 9, note 38, velocity 90
142:0   Note off                9, note 38, velocity 64
142:0   Note on                 9, note 39, velocity 90
142:0   Note off                9, note 39, velocity 64
142:0   Note on                 9, note 42, velocity 123
142:0   Channel aftertouch      9, value 127
142:0   Channel aftertouch      9, value 127
142:0   Channel aftertouch      9, value 126
142:0   Channel aftertouch      9, value 127
142:0   Channel aftertouch      9, value 125
142:0   Channel aftertouch      9, value 119
142:0   Channel aftertouch      9, value 107
142:0   Channel aftertouch      9, value 82
142:0   Channel aftertouch      9, value 8
142:0   Channel aftertouch      9, value 5
142:0   Channel aftertouch      9, value 42
142:0   Note on                 9, note 43, velocity 88
142:0   Note off                9, note 43, velocity 64
142:0   Note on                 9, note 43, velocity 79
142:0   Note off                9, note 43, velocity 64

Test MIDI input (RAW) #

amidi (does not list bluetooth devices)

sudo amidi -l

Dir Device    Name

When installed the MIDI palette you can select the bluetooth connection:

The great part is that you can still use Node-Red and also use aseqdump without any “port is busy” conflicts!


Install MIDI synthesizer #

Fluidsynth, Alsa en Pulse audio

sudo apt install -y alsa-utils pulseaudio-module-bluetooth pavucontrol fluidsynth fluid-soundfont-gm

To pair with a bluetooth audio output device you need to start Pulseaudio with this command, otherwise pairing will fail:

Failed to connect: org.bluez.Error.Failed br-connection-profile-unavailable

Start pulseaudio before connecting:

pulseaudio --start

Connect bluetooth speaker #

For example we use a S10 bluetooth speaker to connect to and play audio

To connect via bluetooth enter the bluetooth commandline:

bluetoothctl

Start the scan:

[bluetooth]# power on
[bluetooth]# agent on
[bluetooth]# scan on
Discovery started

Power on the MIDI Pad, eventuelly it will be discovered and you will see something like:

[NEW] Device B4:3A:3F:F6:09:CC S10 Bluetooth Speaker

Turn scan off:

[bluetooth]# scan off

Trust the device by entering it’s MAC address:

[bluetooth]# trust B4:3A:3F:F6:09:CC

Output:

[CHG] Device B4:3A:3F:F6:09:CC Trusted: yes
Changing B4:3A:3F:F6:09:CC trust succeeded

Pair with the Bluetooth speaker:

[bluetooth]# pair <a href="B4:3A:3F:F6:09:CC">25:4E:5A:66:94:BC</a>

Output:

Attempting to pair with B4:3A:3F:F6:09:CC
[CHG] Device B4:3A:3F:F6:09:CC Connected: yes
[CHG] Device B4:3A:3F:F6:09:CC Alias: B4-3A-3F-F6-09-CC
[CHG] Device B4:3A:3F:F6:09:CC Name is nil
[CHG] Device B4:3A:3F:F6:09:CC Bonded: yes
[CHG] Device B4:3A:3F:F6:09:CC Paired: yes
Pairing successful
[CHG] Device B4:3A:3F:F6:09:CC Connected: no

Connect to the bluetooth speaker:

[bluetooth]# connect B4:3A:3F:F6:09:CC

Output:

Attempting to connect to B4:3A:3F:F6:09:CC
[CHG] Device B4:3A:3F:F6:09:CC Connected: yes
[NEW] Endpoint /org/bluez/hci0/dev_B4_3A_3F_F6_09_CC/sep1
[NEW] Endpoint /org/bluez/hci0/dev_B4_3A_3F_F6_09_CC/sep2
[NEW] Transport /org/bluez/hci0/dev_B4_3A_3F_F6_09_CC/sep1/fd0
Connection successful
[CHG] Transport /org/bluez/hci0/dev_B4_3A_3F_F6_09_CC/sep1/fd0 State: active
[CHG] Device B4:3A:3F:F6:09:CC ServicesResolved: yes

The device is now connected to the system, you can exit the bluetooth terminal.

Set default audio output to bluetooth device #

To play trough the bluetooth device you need to set it to default output first:

List devices:

pactl list short sinks

1       bluez_sink.B4_3A_3F_F6_09_CC.a2dp_sink  module-bluez5-device.c  s16le 2ch 44100Hz       SUSPENDED

Set device to default audio output:

pactl set-default-sink bluez_sink.B4_3A_3F_F6_09_CC.a2dp_sink

MIDI testing #

Please note: Bluetooth speakers are mostly using pulseaudio

Test NOTE

test notes on fluidsynth

fluidsynth -a pulseaudio -m alsa_seq /usr/share/sounds/sf2/FluidR3_GM.sf2

in the console of fluidsynth type

noteon 1 60 100

You should here a note play

Test MIDI file

Download a midi file

wget https://files.khinsider.com/midifiles/snes/super-mario-world/overworld.mid -O test.mid

Check if the file is a midi file:

file test.mid

test.mid: Standard MIDI data (format 1) using 19 tracks at 1/120

Play the midi file:

fluidsynth -a pulseaudio -m alsa_seq -i /usr/share/sounds/sf2/default-GM.sf2 test.mid

or

fluidsynth -a pulseaudio -m alsa_seq -i /usr/share/sounds/sf2/FluidR3_GM.sf2 test.mid

Connect and route the midi keyboard #

So if you look at the “audio” devices with:

sudo aconnect -l

You will see something like:

client 0: 'System' [type=kernel]
    0 'Timer           '
    1 'Announce        '
client 142: 'SMC-PAD Pocket' [type=user,pid=901]
    0 'SMC-PAD Pocket Bluetooth'

Where device 28 is the midi keyboard

Now to open fluidsynth and to prevent multiple SSH sessions, because fluidsynth need to be running, we can run fluidsynth in the background with this command:

nohup sudo fluidsynth -a pulseaudio -m alsa_seq --server /usr/share/sounds/sf2/FluidR3_GM.sf2 > fluidsynth.log 2>&1 &

And if you list the devices with aconnect you will now see:

sudo aconnect -l

client 0: 'System' [type=kernel]
    0 'Timer           '
    1 'Announce        '
client 128: 'FLUID Synth (4557)' [type=user,pid=4557]
    0 'Synth input port (4557:0)'
client 142: 'SMC-PAD Pocket' [type=user,pid=901]
    0 'SMC-PAD Pocket Bluetooth'

Where device 128 is fluidsynth.

Now we have to redirect the input from the keyboard (device 142) to the synthesizer (device 128), with this command:

sudo aconnect 142:0 128:0

Now the devices are connected, you can check it again:

sudo aconnect -l

client 0: 'System' [type=kernel]
    0 'Timer           '
    1 'Announce        '
client 128: 'FLUID Synth (4557)' [type=user,pid=4557]
    0 'Synth input port (4557:0)'
        Connected From: 142:0
client 142: 'SMC-PAD Pocket' [type=user,pid=901]
    0 'SMC-PAD Pocket Bluetooth'
        Connecting To: 128:0

You can see the “connecting to” and “connected from”

If you now press keys on your keyboard you will hear the sound!, enjoy!