Read MIDI output from keyboard

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

My children recently had a Electronic Keyboard given to them from their
grandmother.  Not that anyone cares.  However, I thought I'd play in an
area I'm not familure with and see what I can do.  However, I'm a
little lost as to where to begin so I'm asking for help.

I would like to program an interface basic in design that would read
the notes played on our keyboard.  I'm not sure how to open the Serial
Port to read the MIDI signal or then again even where to start learning
the MIDI interface.

Does anyone have a good idea where I could go to read a quick overview
or how to begin on my learning project.  Sorry to bug some of the
experienced programmers with simple questions. This is just a personal
project I thought would be educational and good to know.

You all have given me good advice in the past.


Re: Read MIDI output from keyboard

Quoted text here. Click to load it

You havn't said whether your computer uses Linux or Windows, but when
it comes to MIDI, the OS matters.

I use Linux, and here are some tips and code.

First, its best to get a USB midi connector cable(the standard), rather
than the serial port connector or a soundcard  connector (which is
harder to use).  They cost about $50 new, but cheaper ones can be
had on ebay.

You say you want to "read the notes on the keyboard", but what do
you mean?  Do you want to record in a text form, the keypresses from
the keyboard? Do you want to record the keypresses direct to a midi
file? Or do you want to play the sounds thru the computer soundcard?

If you are on Linux and have the alsa sound system going with OSS
sequencer support(it's usually setup that way), you can look at
"amidi -h" to get an idea of what you can do. You need to be
concerned what port you are using.

If you don't have have a sequencer in your soundcard, you can
use the "software synthesizer" timidity to play midis.
You can google for timidity server setup, but the basic idea is:

#start the server
 timidity -iA -B2,8 -Os  #  (if this doesn't work try -Od)

Then when you list ports with amidi ( or some other app) you
will see something like:
#    * Timidity port 0 128:0
#    * Timidity port 1 128:0

Look at the alsa-utils package. It has some useful
midi utilities.
aconnect  -- to connect to ports
arecordmidi -- to record midis played on the keyboard
aplaymidi -- to play midis

Getting back to Perl, the here is how to use the Realtime
Module on linux. It is setup to play from the console keyboard
just to demonstrate it.  On my system, if I set "midi_device=>5",
it will play from my midi keyboard.

use warnings;
use strict;
use MIDI::Realtime;
use Term::ReadKey;


#this works on linux with an SBlive, Alsa , kernel 2.6.10
# the usb-hotplug blacklist must be setip right, or you  
#will get buggy results, but that is a setup problem

my $midi = MIDI::Realtime->new(dev=>'/dev/sequencer',
                               midi_device=> 1);  
                                 # 1 is your 101-key standard keyboard
                                 #5 for external keyboard thru USB UM-1

my $char;
if (defined ($char = ReadKey(0)) ) {
        print ord($char),"\n";    # input was waiting and it was $char
      $midi->patch(ord($char)); #change instrument,
                          #127 gives "exploding keyboard" :-)
                          #char 't' gives "monster footsteps"
  #play note #50 is about middle C, go lower or higher
      $midi->note(40,2,127);  #add a second channel for harmony

#attmpt to limit note length

} else {
          # no input was waiting

ReadMode('normal');        # restore normal tty settings


If you are on Windows, you might find this useful:
 module can be found at:

I'm not really a human, but I play one on earth.

Re: Read MIDI output from keyboard

Quoted text here. Click to load it

Presumably you want to do it with Perl, otherwise you wouldn't have asked in
this group.

Perhaps one of the CPAN MIDI modules will help you out.


Site Timeline