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.

Wednesday, May 27, 2015

Javascript Drum Sound Synthesis with WebAudio

This is a great article with lots of pictures and explanation:
https://dev.opera.com/articles/drum-sounds-webaudio/

He makes a kick drum and a hit-hat!

If you would like to hear how it sounds, check out:
https://chrislo.github.io/drum_synthesis/


Monday, February 23, 2015

Chordify.net Adds User-Editing of Extracted Chords

1. I was excited about SoundSlice - it let you enter tabs
2. I was excited about Chordify.net - It auto-extracts chords from youtube
3. I was excited about Youtab.me - It extracts chords better than Chordify.net (betting timing)
4. Now I'm excited about Chordify.net again. - It lets users improve chords.

This is the email they sent me earlier this evening:


NEWSLETTER Groningen, 18 Feb 2015      

Chord edits feature released today!

Achieving 100% accuracy together

Today Chordify is proud to present the new and exciting, game changing ‘chord edits’ feature!

For over two years now, millions of people have been using Chordify as their main source of chords for every song they wish to learn to play. The Chordify library currently consists of over two million songs chordified by you. The ‘ears’ of the machine aren’t always pitch perfect, nevertheless, the service offers a stunning 75-90% accuracy rate, depending on the style and sound quality of the song. Until now!
Thanks to our innovative and interactive chord editing software, you can now manually improve chord sequences extracted by Chordify, enabling you to play along to your favourite songs with 100% accurate chords. In so doing, you will be working together with us to enhance the technology behind automatic chord recognition.

So lend us your ears and start editing!

Monday, February 9, 2015

Online program to generate chords from youtube videos

Seriously the future is here... Another program to generate chords from youtube videos!!!
Example:
https://www.youtab.me/…/nMTJ_0spI4v/jets-to-brazil/chinatown


 
This is even smoother than http://chordify.net!

 It's faster and has better division of the chords (they're not all lined up on the beginning of a measure). Wow!

(beware it's burning hot new and crashed on a few songs I tried!)

Wednesday, February 4, 2015

Concept for DIY Musicians: House Concerts

If you get the most excitement out of building and designing your own instruments, a DIY-performance scene could be a really fun part of it!  I found Shannon's short chat full of amazingly level-headed and professional tips.  Burn two minutes of time: you'll only hear advice this good once in a long while.




By the way, if you're curious about Shannon Curtis, preview her albums:


                              

Monday, January 5, 2015

Free Book on DSP in Python

Think DSP- Digital Signal Processing in Python

This book looks great - it's a work in progress by Allen Downey, a professor at Olin College.  Every example includes Python code to perform the calculations as well as read and write waveforms.  Many examples use music so that should be valuable to readers of this blog and people who like to DIYDSP.  So go ahead and download the free PDF or read it in HTML.

Tuesday, December 30, 2014

Axoloti - Embed A Digital Modular Synth In Your Instrument



This is very special to me.  This is exactly what I have been looking to do with the digital music instruments I have been designing and building - Give them a modular synthesizer editor.

This is a software and a hardware project.  It lets you graphically edit a patch for a DSP-based music instrument, then upload it into a small circuit board.  That means you can put that instrument into a box with sensors and controls, make a digital guitar, tune it to sound just the way you like it and even put all kinds of digital effects on it!  It's a very difficult and important project and I am sure it will lead to very many interesting kinds of new instruments, songs, performances and possibly even genres of art!

Here are the specs:
  • Two 1/4" jacks
  • Stereo headphone output
  • MIDI Input
  • STM32F4 processor (168 MHz w/ FPU)
  • USB
  • Modular synth editor on Desktop
  • Cross-Platform support (Lin/Win/OSX)
  • Approximately $100 incl. shipping

I highly recommend supporting Axiloti on Indiegogo.  I look forward to working with it a lot in 2015!

Saturday, December 27, 2014

Binaural Sound with the Javascript Web Audio API


Live Demo of Binaural Sound with the Web Audio API

What is Binaural Sound?

Binaural sound is a way to make 3D sound!  Binaural means "Two Ears."

How does it work?  It starts with any sound you like, such as a dog barking.

Then it convolves that dog bark sound with a special signal.

The result: it sounds like the sound is coming from in front of you, or to your right or left!

Try out the real-time demo above!

 


Wednesday, December 17, 2014

Javascript audio feature extraction library designed for and implemented in the Web Audio API

I've posted hugh's demo on DIYDSP.com for you to try out.

This is a Javascript library for "Feature Extraction."

If you don't know what "Feature Extraction" is, let me tell you:

It's a term used in Machine Learning.  A "Feature" is quite simply a "calculation."  For example, if you examine a stream of sound or a file, you may want to calculate the average loudness of the stream every 50 milliseconds.  In that case, you are said to be calculating the "Feature" of loudness.  Also, you may wish to calculate the loudest part of the spectrum.  In that case, you would extract the feature called "Spectral Centroid."

So now you learned the jargon of what it means when everyone says they are "computing features."  What is it good for?

The primary use of features is in "trying to figure out what a piece of audio means."  For example, I once wrote a script that automatically downloaded 2-hour FM radio shows from WMBR.  I wanted to know which parts of the shows were the DJ talking and which parts were the music.  So, to do that, I did some research and discovered it's possible to compute "features" on the audio and compare them to determine whether the audio is speech or music.

Unfortunately the code for that is on a computer in another country at this moment, but if enough people request it, when I get back to that computer, I'll post the code and show how it works.

Good luck with this library and try out the demo I posted at Meyda Demo on DIYDSP.com.

Tuesday, December 16, 2014

New Playlist For Electric Eel Christmas Carols




 My new playlist: Electric Eel Christmas Carols

Subscribe to find out when new songs are added on my upcoming trip to Germany!

This is me playing traditional Christmas Carols like Silent Night, Joy to the World, etc., on an instrument I designed while at the MIT Media Lab.  It's called the Electric Eel.  I designed it because I was stuck between the two worlds of electronic and acoustic music.  I like the sounds of electronic music, but I also like the spontaneous, collaborative, nature of acoustic music.  So I designed this instrument which responds to the intensity of sliding the belt up and down by generating more power to emphasize notes.  If you're really curious, check out my wiki Exertion Music where I show how to design and build your own.

Wednesday, November 19, 2014

Volca Sample SDK - A Sample and Sequence Encoding Library for Volca Sample


Did you know that you can upload samples and sequences into the Korg Volca?

Yes, it's possible and now it's much easier with this software from Korg, including the source code. Wow!

This program takes your sequences and samples and turns them into an audio file that you play into the Korg Volca Sample to record the data into its memory!  Wow!

It even comes with a Visual Studio project and a GNU makefile! 

This is seriously cool, thank you Korg!