Saturday, November 12, 2022

Real-time MIDI in Python Using sched and rtmidi Introduction

Wouldn't it be great to send and receive MIDI in realtime in a really simple language like python?  I've written some snappy sequencers using the Sony PSP, and numerous microcontrollers (from 4 Mhz to 600 MHz!), but they can't easily leverage the internet to let composers and performances collaborate via long distance.  And using a convenient language like python usually means serious performance limitations.  But let's give this python realtime MIDI library a try!  It could give us the best of both worlds!

BTW, this post is building on the work of Uğur Güney, found on his blog post.  I've been trying out real-time music in python and wanted to share my work so others can benefit from it.

My github repo contains the results of my experiments toward building a Simultaneous Multi-User Tracker.  I am primarily working on my i9 laptop running Ubuntu20.  My goal is to target a Raspberry Pi 3+ as well. 

This is how you can install the real-time library pymidi for Python:

python3 -m pip install rtmidi

 

Now if you try Ugur's first example, you'll get an error like this, because I believe It the API to rtmidi has developed since then:

dsp@nvawter-tempo:~/SMUT/test$ python3
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rtmidi
>>> midi_out = rtmidi.MidiOut()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'rtmidi' has no attribute 'MidiOut'

 


I noticed the name for MidiOut()  is changed to RtMidiOut():

dsp@nvawter-tempo:~/SMUT/test$  python3

Python 3.8.10 (default, Jun 22 2022, 20:18:18)[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.                                  
>>> import rtmidi
>>> rtmidi.
rtmidi.Collector(     rtmidi.RandomOut(     rtmidi.randomout                                            
rtmidi.CollectorBin(  rtmidi.RtMidiIn(      rtmidi.rtmidi                                               
rtmidi.DEBUG          rtmidi.RtMidiOut(     rtmidi.threading                                            
rtmidi.Error(         rtmidi.collector      rtmidi.time                                                 
rtmidi.MidiMessage(   rtmidi.random                                                                   
>>>x=rtmidi.RtMidiOut()                                                                               

 

So far that's an improvement, but I still got this error when RtMidiOut() runs:
ALSA lib seq_hw.c:466:(snd_seq_hw_open) open /dev/snd/seq failed: Permission denied
MidiOutAlsa::initialize: error creating ALSA sequencer client object.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>                                                                 
rtmidi.Error: MidiOutAlsa::initialize: error creating ALSA sequencer client object.                     
                                                                                                        
                                                                                                        
This turns out to be a permissions issue with /dev/snd/seq.  It's easy to fix.  Just add yourself to the "audio" group on your computer, as found in this thread.  On my computer, I typed this.  Note that my username is dsp and the group is audio:

dsp@nvawter-tempo:$sudo usermod -a -G audio dsp

And remember to log out and then back in again!   It will not work if you don't.  This an security feature of Linux/GNU/Ubuntu.


This is the next line of Ugur's post:

vailable_ports = midi_out.get_ports()

However, I found that gave an error message, too:

AttributeError: 'midi.RtMidiOut' object has no attribute 'get_ports'

I see two similar calls from RtMidiOut(), but not sure how to use them, so I went looking for the manual.  It seems Gary's website is down at the moment?  But thankfully there's an archive.  Anyway, that's just for the C++ class, and doesn't mention the python.  Wrt python, the docs on PyPi are outdated, too.   They point to this github repo, but the docs look like an old version, too.  Let's just try some calls!

>>> x.getPortCount()
1
>>> x.getPortName(0)
'Midi Through 14:0'

Ok, that looks reasonable!  It probably refers to the internal MIDI soundcard on my laptop.  I don't know why it only lists 15 channels... Normally any MIDI connection has 16, but this is a good start!

I'll continue this topic soon!  Thanks for getting it started, Gary and Ugur!



Thursday, April 16, 2020

How Do You Know When a New Music Instrument Design is Done?

Reviewing the Pyramid Eel and reflecting on other instruments, I set my foot down in the sand and label the first Pyramid Eel "done." Watch the video to hear the full details!



Monday, April 6, 2020

Adding Sensitivity and Expression Using Optical Encoders on Electric Eel Generators

I recently upgrade the generators on my Electric Eel instruments to include optical encoders.  Check out this video to see how it works.

This means I'll be able to measure the speed and acceleration of the bow, leading to much more sensitive and expressive instruments.  I've been wanting to do this for YEARS and now it's finally happening...  


Over the years of playing my generator-based Electric Eel instruments, I have been enjoying how pushing them hard increases the sound volume.  It makes for sound so much more expressive than even my best MIDI keyboards with aftertouch.  It's slowly becoming apparent to me, though, that if I had a better idea how fast the player is pushing/sliding/bowing the generator, the sound could be even more sensitive and expressive.

Early on, I tried to do this by measuring the voltage on the temporary capacitors.   That approach works a little bit, but it doesn't change fast enough and different sound outputs consume the voltage at different rates, so it's way too entangled with the output sound, rather, than focused on what the player is doing.  Therefore, I decided to go WHOLE HOG and use an optical encoder.

This wasn't an easy decision.  These are more expensive and the algorithms get much more complex.  However, I am working again and don't have children (my instruments are my children!), so I went for it.  And actually I had a nice surprise---- over the last ten years, prices have come waaaaay down for these parts.  What used to cost about $300, can now be for only $40.  Yes, it's hard to believe.  It must be through the mass quantities of these parts that get used in factories.  Anyway, I picked up two of them and I've been wiring them up and writing code for them.  Soon I'll be putting them right into the instruments.  I can't wait.  It's going to be a whole new era.  In the meantime, please my newest video so you can share some the excitement that I'm going through!

Wednesday, September 4, 2019

Can all of these analog synthesizer knob PCBs be soldered within just one hour???

Enjoy the time-lapse vid and 80s synth-shread montage music and find out

New Synth Knob Multiplexer - 64 Analogs in one! Part 3

So you know I'm exactly halfway in the middle of my 64-in-1 analog knob multiplexer project, right? To keep it exciting, I did a "Soldering Challenge." Enjoy the time-lapse video!

My bigger goal is to demonstrate all 64 knobs at once, but that's so many circuit boards... and I just got back from a sublimely relaxing vacation... so I decided to challenge myself to build them all in one hour. If I succeeded, I motivated myself with a relaxing swim at Walden Pond.

Tuesday, July 30, 2019

New Synth Knob Multiplexer - 64 Analogs in one! Part 3

You've seen it run with 5 synthesizer knobs, but now the stakes are higher!  I'm going to daisy-chain three of my circuit boards together.  Then I'm going to run them at top speed.


And if that wasn't enough, I've been preparing a special treat, so watch through the second half of the video :)

Tuesday, July 16, 2019

All Things Bright And Beautiful


I recorded this gorgeous, classic hymn on three different Electric Eels! Lowest two parts are played on Bowed Eel. Violin 2 is played on Blue Guitar Eel. Violin 1 is played on Red Saxophone Eel.




I'm looking for more players who want to play classical music on DIY/DSP Instruments!

Electric Eels return the nuance of movement to electronic instruments. I free synthesizers from the desktop/laptop paradigm. I make them more like acoustic instruments. They use no batteries, only the energy generated by player movement backdriving stepper motors. So you can take them anywhere and play all night. You don't have to recharge them or wait for their software to upgrade. They do not connect to a cell phone I developed them while at the MIT Media Lab in the Computing Culture research group. 

First part was recorded into a phone and iPad. Subsequent parts recorded into phone while listening on iPad through headphones. Four parts were synced up with an inexpensive, buggy video editor. Recording took place over a six hour period on Saturday, July 13th. Cathartic language has been deleted.

I invented these instruments, the "Electric Eels," while at MIT's Media Lab. My inspiration was that I loved the sounds of electronic timbres, but hated standing still at a laptop or MIDI sequencer. So I made instruments that require you to move to make electronic sound, just like acoustic ones... but I didn't "fake" it with sensors. The Electric Eels have small electrical generators in them that require you to move to generate that electricity to run the synthesizer and CPU in short bursts.
I really enjoy the sounds of harmony and I'm looking for more players to make these recordings with on Electric Eels or other DIY/DSP instruments.

Friday, June 28, 2019

New Synth Knob Multiplexer - 64 Analogs in one! Part 1


New! Fresh in the mail. A circuit board I designed. Check out the edgy and powerful design!




It muxes 64 analog signals, especially synthesizer knobs, into 1. It lets you read a wholllle lot of knobs with just a few pins.

You can use three pins from an Arduino or other microcontroller and read all 64 daisy-chain knobs!

You need this board to make professional synthesizers with lots of knobs.