This blog has moved! All new posts will be on and all the current posts have migrated there too, so please leave comments there instead.

25 August 2008

Theremin to MIDI Control program in Pd

** Update - throughout 2008 and 2009 I used these techniques with the vibraphone in my piece "Duet for Vibraphone and Computer"

Over the weekend I wrote a simple program in Pd that analyses an audio input and then uses pitch and amplitude information to output a midi control value.

My idea was to use my little theremin as the audio input and send the midi data to an internal midi bus which is received by Ableton Live for controlling parameters of some effects. Since the theremin has such a simple sound the pitch and amplitude analysis works extremely well. To achieve this feat I use the excellent fiddle~ object (written by Miller Puckette) and... well not much else really! A few * and + objects adjust the output values so that they are sensibly distributed between 0-127.

Here's the code (or just copy from the picture!):

#N canvas 738 31 442 441 10;
#X obj 88 21 adc~ 1;
#X obj 88 44 *~ 1;
#X obj 29 91 *~;
#X obj 59 91 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 1
#X obj 29 114 dac~ 1;
#X text 9 146 Audio Output!;
#X obj 147 114 unpack;
#X obj 188 177 moses 1;
#X floatatom 219 214 5 0 0 0 Note - -;
#X floatatom 109 214 5 0 0 0 Amplitude - -;
#X obj 117 83 fiddle~ 1024;
#X obj 50 411 ctlout 14;
#X obj 195 411 ctlout 15;
#X obj 222 303 tgl 15 0 empty on empty 17 7 0 10 -262144 -1 -1 1 1
#X obj 192 303 *;
#X obj 54 245 *;
#X obj 54 268 +;
#X floatatom 91 245 5 0 100 0 - ampmult -;
#X floatatom 91 265 5 -127 127 0 - ampshift -;
#X obj 190 240 *;
#X obj 190 263 +;
#X floatatom 227 242 5 0 100 0 - pitchmult -;
#X floatatom 227 265 5 0 0 0 - pitchshift -;
#X obj 232 368 tgl 15 0 empty on empty 17 7 0 10 -262144 -1 -1 1 1
#X obj 201 368 *;
#X floatatom 212 328 5 0 0 0 - - -;
#X obj 84 303 tgl 15 0 empty on empty 17 7 0 10 -262144 -1 -1 1 1;
#X obj 54 303 *;
#X obj 94 368 tgl 15 0 empty on empty 17 7 0 10 -262144 -1 -1 1 1;
#X obj 63 368 *;
#X floatatom 74 328 5 0 0 0 - - -;
#X text 147 14 Theremin Controller;
#X obj 303 162 loadbang;
#X msg 302 184 \; pitchmult 2 \; pitchshift -80 \; ampmult 1 \; ampshift
20 \;;
#X obj 302 253 loadbang;
#X obj 303 275 s on;
#X text 147 34 Output midi controller data;
#X text 146 49 from pitch and amplitude input.;
#X text 292 402 Charles Martin;
#X text 293 414 25/08/08;
#X text 301 145 Default Settings;
#X connect 0 0 1 0;
#X connect 1 0 2 0;
#X connect 1 0 10 0;
#X connect 2 0 4 0;
#X connect 3 0 2 1;
#X connect 6 0 7 0;
#X connect 6 1 9 0;
#X connect 7 1 8 0;
#X connect 8 0 19 0;
#X connect 9 0 15 0;
#X connect 10 2 6 0;
#X connect 13 0 14 1;
#X connect 14 0 25 0;
#X connect 15 0 16 0;
#X connect 16 0 27 0;
#X connect 17 0 15 1;
#X connect 18 0 16 1;
#X connect 19 0 20 0;
#X connect 20 0 14 0;
#X connect 21 0 19 1;
#X connect 22 0 20 1;
#X connect 23 0 24 1;
#X connect 24 0 12 0;
#X connect 25 0 24 0;
#X connect 26 0 27 1;
#X connect 27 0 30 0;
#X connect 28 0 29 1;
#X connect 29 0 11 0;
#X connect 30 0 29 0;
#X connect 32 0 33 0;
#X connect 34 0 35 0;
To check it out, copy and paste the code into a text editor, save it as a .pd file, then open in Pd! Click the little toggle boxes to first to view the data and then to send it to the midi output. This way you can shut off output of one parameter while you have Live (for example) learn the assignment.

** Update - The program was a little bit wrong. Fixed now! Hello internet!


Chrissy said...

So clev!

Factran said...

I gotta try that on the koassilator... RIGHT NOW !!!!

Factran said...

I think there is (still !) an error on the code : you put 2 ## at the beginning on the file, instead of one. (if i don't make a mistake)
Bye !

sofie said...

This is a great patch here, i made something similar using Pd, but i am unsure how this patch works with Live. You mentioned sending it to an internal midi bus. Can you clarify?

Charles said...

I did this on Mac OS X where there is a special inter-application midi device called IAC. You have to enable the IAC driver (in Audio MIDI Setup) and then you can select it as the output midi device for Pd and as an input in Live. I wrote a new post about this.

Ferrence Beeb said...

Hi I'm trying to do the same but using a violin as the source.

I'm on PC and have downloaded MIDI Yoke which is working. I've loaded PD and Live up and set the ins and outs accordingly, but it doesn't seem to work.

When I go to 'test audio and midi' in PD and highlight the MIDI OUT box, a pulse of MIDI comes to Live - so the two programs are definitely communicating.

I'm getting levels in all the boxes, there's just no MIDI info being sent for some reason.

I've been looking for something like this for ages, so I'd be really grateful if you could help me get it going.



Ferrence Beeb said...

OK I've got some MIDI coming through, but it's not musical notes, it's MIDI Channel 1, CCs number 14 and 15. It just flickers between those two values.

Do I have to change something in the code like factran said? I don't understand what factran said -
" you put 2 ## at the beginning on the file, instead of one. (if i don't make a mistake".

Charles said...

Hey Ferrence,

Sounds like the program is working correctly. This program is does not output musical notes, it outputs control data on control numbers 14 and 15.

CC#14 is connected to the volume of the theremin and CC#15 to the pitch. So I have these two control numbers set to control two parameters in effects in Ableton Live.

You can see at the bottom of the patch that the data ends up being sent to [ctlout 14] and [ctlout 15] which send out control data. It's possible to change the patch to send out Midi notes but I'm not in a position to figure out how to do this right now.

I suggest that you go through the Pd tutorials (there are links in other posts in my blog) and then you can modify the patch to suit your needs!

BTW I fixed the error that factran suggested, this was a typo, if the patch is working I think you have the fixed version.

Factran said...

Hi !
I managed to do it with the Kaossilator ! That's great, I'm driving a filter with it. I just had to change a little the value, I also changed the order of the multiplication and addition, I don't know if it's far better but that was more easy for me to calibrate. It was a little bit flickering, so I add a VST plugin (in my host) that "smooth" the midi signal a little. I think this is doable in PD, but that was already done in the plugin ;)

So : here is my video !

Thanks again for your patch !

Rich's Blog said...

If anyone is able to get this program to output musical notes I would really be appreciative! This is what I'm looking for but I want to output musical notes.

Grant Nestor said...
This comment has been removed by the author.
Grant Nestor said...

im trying to recreate this patch using Max. can you tell me what the [moses] object does? everything else is working just fine!

Charles said...

the [moses x] object does this:

if (inlet value) < x then send it to left outlet
else if send it to right outlet

I'm sure you can replicate this easily in max.

This comment has been removed by the author.
Ben said...

first off, thanks for the code, Charles. without this post I wouldn't have found PD which is a very neat program. Factran, I was wondering what program you used to smooth out the notes? I seem to get alot of repeated notes when I use the Noteout function.

Greig (conquerearth) said...

I used this in combination with a bidule to convert theremin -> midi notes. It works great. I dont get the exact same note as the theremin is producing, but because the pitch is relative it still lets me play midi notes in realtime. Check the video here (the midi part is near the beginning, using piano):

Factran said...

@Ben : It's a kind midi treatment in Fruityloops, whose name is "XY controller" It smooth the flickering of the CC.

By the way, I put the scale of the kaossilator "OFF" (not major, minor, just glissando) so the notes are smooth already.

Scattered said...

Great work charles :)

Anyone made a patch to make it output notes yet? Or is there any chance anyone who's done it in bidule could give more details or post the .bidule file?

Scattered said...

Oh yea if anyone is wondering how to map midi between programs (eg pd and ableton) on windows, the program Midi Yoke works well.

ajcooke said...

Hi - I would love to get this to work. I'm a bit of a newbie but have downloaded pd-extended (version 0.40.3-extended) from the puredata website which seems to work fine with the test tones etc. When I put the control program in I get this:

error: \{rtf1ansiansicpg1252cocoartf949cocoasubrtf430: no such object
error: \}: no such object
error: red255green255blue255: no such object
error: red51green51blue51: no such object
error: \}: no such object
#X: no such object
#X: no such object
#X: no such object

What am I doing wrong. Hope someone can help as I'd love to use the theremin as a midi controller. Thanks.

Oliver said...

Hi! I made a simple patch to get midi notes. but there is still something to do with the velocity. The patch is here:

compagnie la valise said...

hi, did anyone do it with max masp??

Jan Paul said...

Hi, this post actually introduced me to Pd as I was looking for ways to transform audio into midi. Going to set up an art installation with multiple theremins. Working my way through tutorials I was just wondering: would it be possible to run this patch for different audio-inputs (turned into different MIDI-channels) or would I have to use a stand-alone Pd-version running for each individual input?
Hoping someone can help out...
Thnx, JP

Charles Martin said...

there would be no problem taking multiple audio inputs to different outputs, if you have more inputs, the adc~ object would have more outlets and you can make lots of fiddle~ objects and lots of midi outputs. Not too hard!

aurbyt said...

Thanks for this it's opened up a whole new area for me. Here's a video of it in action: