{"id":1625,"date":"2020-04-15T14:31:06","date_gmt":"2020-04-15T13:31:06","guid":{"rendered":"https:\/\/ofalcao.pt\/blog\/?p=1625"},"modified":"2020-04-16T10:00:06","modified_gmt":"2020-04-16T09:00:06","slug":"lego-ipmidi","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2020\/lego-ipmidi","title":{"rendered":"LEGO ipMIDI"},"content":{"rendered":"<div class=\"seriesmeta\">This post is part 1 of 6 of \u00a0<a href=\"https:\/\/ofalcao.pt\/blog\/series\/lego-ipmidi\" class=\"series-359\" title=\"LEGO ipMIDI\">LEGO ipMIDI<\/a><\/div>\n<p>3 weeks since COVID-19 lockdown.<\/p>\n\n\n\n<p>Bored.<\/p>\n\n\n\n<p>Let&#8217;s go back to MIDI and the never completed LEGO Laser Harp idea &#8211; instead of usinq MQTT to send codes from the EV3 to my laptop and converting there to MIDI&#8230; how can I send pure MIDI?<\/p>\n\n\n\n<p>More than 2 years have past. Python MIDI libraries are better. ev3dev is better. I am bored. Let&#8217;s search.<\/p>\n\n\n\n<p>TouchDAW supports two network MIDI protocols: multicast (ipMIDI) and RTP-MIDI. The first one is not exactly a MIDI standard although lots of products seem to support it. But RPT-MIDI is so I&#8217;ll try it first.<\/p>\n\n\n\n<p>Found David Moreno&#8217;s rtpmidi. It installs a daemon on my Ubuntu laptop. I can use TouchDAW to play music on my laptop through it. Very nice!<\/p>\n\n\n\n<p>But it is not available for ev3dev so I would have to build it&#8230; I&#8217;m always afraid of that.  So maybe there is a python library that can send midi notes through rptmidi? Found none \ud83d\ude41<\/p>\n\n\n\n<p>OK, ipMIDI then.<\/p>\n\n\n\n<p>&#8216;qmidinet&#8217; on my laptop works. Had to disable jack and sometimes notes get stuck (usually the last note) but it works.<\/p>\n\n\n\n<p>It also works on ev3dev (without GUI, of course):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">qmidinet -g -j off<\/pre>\n\n\n\n<p>And I can play a MIDI file directly to the laptop:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">aplaymidi --port 128:0 happy_birthday.midi<\/pre>\n\n\n\n<p>playing heavy MIDI files seemed to stress EV3 (or maybe just Wi-Fi) but small files worked very well &#8211; and a single MIDI instrument like my Laser Harp will generate just a few notes per second (at best).<\/p>\n\n\n\n<p>While I was fiddling with different MIDI files I dedided to try &#8216;multimidicast&#8217;, the linux father of &#8216;qmidinet&#8217;. And it also works, just  needed to compile from source (not as slow as I expected). Since it is command line only and doesn&#8217;t support jack, it uses fewer resources so I&#8217;ll use it instead of qmidinet.<\/p>\n\n\n\n<p>I can send a full MIDI file&#8230; but what I really need is to send notes, in real time. So I need to generate those notes in python.<\/p>\n\n\n\n<p>&#8216;python-rtmidi&#8217; seems to be the best choice and I remember trying it with the Harp when I was playing MIDI locally on the EV3 (running timidity as a soft synth). At that time, I managed to install &#8216;python-rtmidi&#8217; and also &#8216;mido&#8217; that uses it as a backend.<\/p>\n\n\n\n<p>But I couldn&#8217;t install it. <\/p>\n\n\n\n<p>There is a &#8216;python3-rtmidi&#8217; package for armel but not for stretcht. I tryed the buster package but requires python 3.7 (at this moment, ev3dev includes 3.5.3).<br>So I tried &#8216;pip&#8217;&#8230; and after a while I loose network connectivity.<br>Then i tried downloading the source code and install it&#8230; and after a while I also loose network. Even tried installing it without jack support to make it a bit lighter&#8230; same thing.<\/p>\n\n\n\n<p>Argh! <\/p>\n\n\n\n<p>So I needed a plan B: playing notes without a python library. And found a post on a Raspberry Pi forum where someone played notes from Ruby using system calls to &#8216;amidi&#8217;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>amidi -p hw:1,0 -S \"90 3C 7F\"\namidi -p hw:1,0 -S \"90 3C 00\"<\/code><\/pre>\n\n\n\n<p>This turns &#8216;C&#8217; on then off on MIDI channel #0. But it only works with sound cards, not with MIDI connections.<\/p>\n\n\n\n<p>So&#8230; there is a kernel module &#8216;snd-virmidi&#8217; that creates a virtual midi sound card&#8230; and it is available in ev3dev !!<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo modprobe snd-virmidi<\/pre>\n\n\n\n<p>This creates 4 MIDI clients:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">client 20: 'Virtual Raw MIDI 1-0' [type=kernel,card=1]\n    0 'VirMIDI 1-0     '\nclient 21: 'Virtual Raw MIDI 1-1' [type=kernel,card=1]\n    0 'VirMIDI 1-1     '\nclient 22: 'Virtual Raw MIDI 1-2' [type=kernel,card=1]\n    0 'VirMIDI 1-2     '\nclient 23: 'Virtual Raw MIDI 1-3' [type=kernel,card=1]\n    0 'VirMIDI 1-3     '<\/pre>\n\n\n\n<p>so I connect one of them to multimidicast:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">aconnect 20:0 128:0<\/pre>\n\n\n\n<p>and now amidi works!<\/p>\n\n\n\n<p>From python I can now play a &#8216;C&#8217;:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">os.system('amidi -p hw:1,0 -S \"90 3C 7F\"')\ntime.sleep(0.1)\nos.system('amidi -p hw:1,0 -S \"90 3C 00\"')<\/pre>\n\n\n\n<p>Even better: I can do it also from micropython because &#8216;os.system&#8217; is also available, including the new &#8216;pybricks-micropython&#8217; library.<\/p>\n\n\n\n<p>Now I need to learn a few hexadecimal MIDI codes and prepare a Raspberry Pi to work as an ipMIDI synth to free my laptop from that role.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"seriesmeta\">This post is part 1  of 6 of \u00a0<a href=\"https:\/\/ofalcao.pt\/blog\/series\/lego-ipmidi\" class=\"series-359\" title=\"LEGO ipMIDI\">LEGO ipMIDI<\/a><\/div><p>3 weeks since COVID-19 lockdown. Bored. Let&#8217;s go back to MIDI and the never completed LEGO Laser Harp idea &#8211; instead of usinq MQTT to send codes from the EV3 to my laptop and converting there to MIDI&#8230; how can I send pure MIDI? More than 2 years have past. Python MIDI libraries are better. &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2020\/lego-ipmidi\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;LEGO ipMIDI&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[358,279],"series":[359],"class_list":["post-1625","post","type-post","status-publish","format-standard","hentry","category-sem-categoria","tag-ipmidi","tag-midi","series-lego-ipmidi"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-qd","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/comments?post=1625"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1625\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=1625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=1625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=1625"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=1625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}