## MIDI Setup

Setting Up A Linux Computer For MIDI Recordings

## Introduction

This document describes how to setup and use a linux computer to record MIDI data. This setup has been used along with a self-penned program (metro, courtesy of Martin Neubauer) to manipulate MIDI data like pitch, velocity and note on and off in real-time, as well as, along with yet another custom-made script (sendtrigger, courtesy of yours truly) to send note-on events as well as metronome clicks to an EEG amplifier via the parallel port.

## Requirements

• A linux computer (the setups presented here were tested on a Debian 9.3 (stretch) system)
• A MIDI interface if the computer does not come with one built in—the interface used here was a Midisport USB Uno; around €10 at your retailer
• A parallel port extension card if the computer doesn not have such a port—obviously only required if data are to be sent to the parallel port (see Parallel Port)
• A MIDI keyboard (a keyboard with an internal synthesizer is nice to have but not mandatory if the keyboard can [or must anyway] be used along with a soft synth like fluidsynth); a virtual MIDI keyboard can be useful (see Helper Programs)
• A software synthesizer; this manual assumes fluidsynth along with its GUI **qsynth* be installed
• Sound fonts—only needed if
• the MIDI keyboard does not have a built-in synthesizer
• MIDI events need to be manipulated (and played back to the player) as described in the Introduction
• A MIDI sequencer-like software—we use metro which is simple and quick, and allows for systematic manipulation of pitch, velocity (i.e. percieved loudness) as well as note onset and offset; this program is unfortunately neither open source nor in the public domain and we cannot share it with you. We will later on outline how such a simple MIDI sequencer might be coded in python though. As a testing tool we usually use a ‘normal’ MIDI sequencing software like MusE (http://muse-sequencer.org/) but rosegarden (http://rosegardenmusic.com/) or similar programs shold work as well

## Recording Scenarios

In this section a couple of scenarios are described under which MIDI data need to be acquired. We will start as simple as possible and increase complexity step by step. But first, we will list required and nice-to-have programs along with alternatives.

### Prerequisits

#### Required Software

On most (all?) modern linux systems, ALSA (Advanced Linux Sound Architecture, http://alsa-project.org/) is used as the sound subsystem, which is the software layer located between soundcard drivers and upper layer software. Your Debian system should come preinstalled with it as it is part of modern linux kernels.

##### The Sound Subsystem

A sound server is at the next hierarchical level atop the sound subsystem, and modern linuxes often (but not always, or not exclusively) use JACK (Jack Audio Connection Kit, http://jackaudio.org/) as sound server. It can be installed by

apt-get install jackd2

To be able to run JACK with real-time priority (essential when real-time manipulations are done to MIDI commands, or during live recording), it needs to be configured appropriately. This can be done during installation of jackd2 or by issuing

dpkg-reconfigure -p high jackd2 

Also, the user needs to be added to the audio group:

adduser <user-name> audio

The user must re-log in for this to work! To test whether real-time and memory-locking are allowed for the current user issue

ulimit -r -l

On a working system this yielded

real-time priority              (-r) 95
max locked memory       (kbytes, -l) unlimited

for me, and starting Qjackctl (see below) ran smoothly (as opposed to before reconfiguring jackd). If this does not work, please visit the config page of JACK!

To ease setting up and controlling JACK, we will also need Qjackctl (https://sourceforge.net/projects/qjackctl/files/):

apt-get install qjackctl
##### MIDI Sequencer

End-user applications like MIDI sequencers etc. operate on top of the sound server and make use of its connections setup in, e.g., qjackctl. To quickly check functionality of connections we install MusE:

apt-get install muse

To be able to run MusE with a high-frequency timer, their FAQ suggests to install a real-time kernel:

aptitude install linux-image-4.14.0-0.bpo.3-rt-amd64 

For the kernel to be used the computer needs to be rebooted! Unfortunately, this did not make the error message go away when starting MusE. Starting MusE from the console yielded, among other messages:

Trying RTC timer...
fatal error: open /dev/rtc failed: Keine Berechtigung
hint: check if 'rtc' kernel module is loaded, or used by something else
Trying ALSA timer...
AlsaTimer::initTimer(): best available ALSA timer: system timer
got timer = 72
Aquired timer frequency: 250

The above mentioned FAQ mentions possible problems with priviliges and suggests to add the line

echo 3072 >/sys/class/rtc/rtc0/max_user_freq

to the file /etc/rc.local directly above the “exit” line, followed by a reboot.1 Since this all happens on Debian stretch, on which /etc/rc.local has been deprecated, I decided to delete the file.] This did not solve things for me though. They go on to suggest the following:

1. Create a file: /etc/udev/rules.d/40-timer-permissions.rules
2. Put the following lines in it:
KERNEL=="rtc0", GROUP="audio"
KERNEL=="hpet", GROUP="audio"

On one computer (amd X2 Phenom), this killed MusE entirely:

Trying RTC timer...
got timer = 72
*** stack smashing detected ***: muse terminated
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x70bcb)[0x7ff56cb2ebcb]
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x37)[0x7ff56cbb7227]
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x0)[0x7ff56cbb71f0]
/usr/lib/muse/modules/libmuse_driver.so(+0x169bc)[0x7ff5723c49bc]
/usr/lib/muse/modules/libmuse_core.so(_ZN8MusECore7MidiSeq30checkAndReportTimingResolutionEv+0x29)[0x7ff573212389]
muse(main+0xab4)[0x56362057cbb4]
muse(_start+0x2a)[0x56362057e77a]
======= Memory map: ========
563620577000-563620581000 r-xp 00000000 08:02 9451563                    /usr/bin/muse
563620781000-563620782000 r--p 0000a000 08:02 9451563                    /usr/bin/muse
563620782000-563620783000 rw-p 0000b000 08:02 9451563                    /usr/bin/muse
56362111f000-563621de3000 rw-p 00000000 00:00 0                          [heap]
.
.
.

On a newer computer (kabylake i5), it resulted in (among others):

RtcTimer::setTimerFreq(): cannot set freq 1024 on /dev/rtc: Keine Berechtigung
precise timer not available, check file permissions and allowed RTC freq (/sys/class/rtc/rtc0/max_user_freq) 
ls -l /sys/class/rtc/rtc0/max_user_freq

returned

-rw-r--r-- 1 root root 4096 Mär  7 15:49 /sys/class/rtc/rtc0/max_user_freq

and

cat /sys/class/rtc/rtc0/max_user_freq 

returned

64

Manually changing this to values below 1024 did not result in a change in error messages. Changing it to values $$\geq$$ 1024 though resulted in a memory dump to the screen and the message

Unknown error...
terminate called after throwing an instance of 'Jack::JackTemporaryException'
what():  

As found on MusE’s github repository, this is a known issue in pre-3 versions of the program. So I got rid of the Debian packages:

aptitude purge muse

and followed their build instructions and compiled the latest git (the stable branch on sourceforge was down at the time).
After successful compilation and installation, MusE3 can be started by

muse3

Since Muse3 uses Pulse Audio as sound subsystem instead of JACK, go to Settings|Global Settings and change the default sound output on the Audio tab to JACK Audio.

##### Software Synthesizer

MIDI keyboards, especially the more expensive ones, often come with reasonably sounding, sometimes even sample-based instruments. If you only want to use those ‘internal’ sythesizers you are good to go. Should you instead want to sound a computer-generated metronome while recording MIDI messages from a keyboard, you need to install a software synthesizer. The same holds for the case where you want to manipulate MIDI messages in real-time.
A software synthesizer emulates a hardware-based synthesizer. Therefore it needs some kind of information about how real instruments sound. These come as digital files called sound fonts. There are quite some sound fonts freely available on the internet. I chose to go with the one that comes with fluidsynth, others suggested http://www.pianosounds.com/freesoundfont.htm and http://www.pianosounds.com/freesoundfont.htm. To me they sounded about the same, but who am I to judge. Here’s how install the soft synth along a control panel to simplify its configuration:

aptitude install qsynth

(Maybe we also need to install fluidsynth? Installing qsynth ‘only’ installs libfluidsynth1…)

The Linux Journal has a (rather old but nevertheless) nice article](http://www.linuxjournal.com/article/8354) on using jack along with fluidsynth.

#### Helper Programs

In case you have no access to a physical MIDI keyboard, the linux program vkeybd may come in handy. It can be installed by issuing

apt-get install vkeybd

See Scenario #1: Playback MIDI From Keyboard for details. Also check vkeybd’s man page.

### Scenario #1: Playback MIDI From Keyboard

Connect your MIDI keyboard to the computer using either a MIDI converter or a plain MIDI cable.

#### MIDI ports

The command

sudo modprobe snd_virmidi enable=1

adds one virtual MIDI port to the system. For me this was /dev/midi2, but it might be a different one on your system. The MIDI port /dev/midi1 was the ‘real’ MIDI port I connected the MIDI keyboard with.

#### Software Synthesizer

Start qsynth, click on [Configure…] and make sure everything looks as in Fig. 1.

Fig. 1. Configuration for fluidsynth in the control panel, qsynth.

#### Setting up Jack

(Have a look at /home/grossbach/files/customers/ioannou_christos/eeg-stress-experiment/MIDIwExternalMetronome2EEG.pdf and sendtrigger.pdf for details. The python scripts are in /home/grossbach/files/customers/ioannou_christos/eeg-stress-experiment/software.)

Start qjackctl, click on [Settings…] and make sure everyhing looks as in Fig. 2.

Fig. 2. Configuration of the sound subsystem, jack, in its control panel GUI, qjackctl.

The following needs to be done in the right order to work properly (mostly stolen from here):

1. Start the jack sound server GUI, qjackctl
2. Click the [Start] button in qjackctl
3. Disconnect all audio in qjackctl’s Connections window
4. Disconnect all midi in qjackctl’s Connections window
5. Click on [Patchbay]
6. Click on the [New] button
7. When asked, “Create patchbay definition as a snapshot of all actual client connections?”, you must answer “NO”!
8. Click on the [Add] button in the “Output Sockets” section on the left
9. Click the “ALSA-MIDI” radio button, and from the “Client” drop-down list choose “MIDI-Through” (this will enable grabbing of MIDI signals by the mapping program, see below); I was only able to click [OK] after I also had added a connection (“MIDI Through Port-0”)
1. Optional: For auditory control add a second output port click [Add] in the “Output Sockets” section on the left of the patchbay again
2. Click on “Audio” and from the “Client” drop-down list choose “system”
3. From the “plug” list choose “capture_1” and click [Add plug]
4. Click [OK]
10. Click the “Add” button in the right Input socket pane.
11. Click the “ALSA-MIDI” radio button
12. Choose “USB Uno MIDI Interface” from the dropdown list
13. Click the “Add Connection” button
14. Click “OK”
15. Click once on each of the two connections in the left and the right pane, respectively, so that they are selected
16. Click on the “Connect” buttun at the bottom of the window

Fig. 3 shows what the patch bay should look like after the above settings have been applied.

Fig. 3. The qjackctl patch bay settings for scenario #1.

1. On a newer computer this led to messages like MusE failed to find a Jack audio server.

Zuletzt bearbeitet: 09.03.2018

Zum Seitenanfang