Virtual MIDI programming under Linux, Hello world - nodesign/libstk GitHub Wiki

This is great tutorial about using virtual Midi ports under Linux :

http://tldp.org/HOWTO/MIDI-HOWTO-10.html


Here is shortcut to go straight to the midi programming hello world

Create virtual midi device under Linux :

modprobe snd-virmidi snd_index=1

Here /dev/midi1 was created. After that test if interface is present using tools provided by stk :

root@minibian:~/stk/projects/examples# ./midiprobe

Here is what I got

root@minibian:~/stk/projects/examples# ./midiprobe

There are 5 MIDI input sources available.
  Input Port #1: Midi Through 14:0
  Input Port #2: Virtual Raw MIDI 1-0 20:0
  Input Port #3: Virtual Raw MIDI 1-1 21:0
  Input Port #4: Virtual Raw MIDI 1-2 22:0
  Input Port #5: Virtual Raw MIDI 1-3 23:0

There are 5 MIDI output ports available.
  Output Port #1: Midi Through 14:0
  Output Port #2: Virtual Raw MIDI 1-0 20:0
  Output Port #3: Virtual Raw MIDI 1-1 21:0
  Output Port #4: Virtual Raw MIDI 1-2 22:0
  Output Port #5: Virtual Raw MIDI 1-3 23:0

So expect Virtual Midi on port 2

Start eguitar example and listen to port 2

root@minibian:~/stk/projects/eguitar# ./eguitar -or -im 2

Write Midi Hello World program :

#include <linux/soundcard.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

int main(void) {
   char* device =  "/dev/midi1" ;
   unsigned char data[3] = {0x90, 60, 127};

   // step 1: open the OSS device for writing
   int fd = open(device, O_WRONLY, 0);
   if (fd < 0) {
      printf("Error: cannot open %s\n", device);
      return 1;
   }

   // step 2: write the MIDI information to the OSS device
   write(fd, data, sizeof(data));

   // step 3: (optional) close the OSS device
   close(fd);

   return 0;
}

This program that I slightly modified comes from this excellent site https://ccrma.stanford.edu/~craig/articles/linuxmidi/output/section1.html

Notice that path is /dev/midi1

compile

root@minibian:~# gcc main.c -o hello

run program

root@minibian:~# ./hello

Normally you will hear a sound

⚠️ **GitHub.com Fallback** ⚠️