PianoPCJ/A.F
Posts: 18
Joined: Thu Feb 15, 2018 10:20 pm

Using MIDI-events to run shell comands on a raspi

Sun Feb 18, 2018 11:11 am

Hello,
I want to build a Gaming PC(with Windows on it) witch goes on if I play a melody on a MDI-Piano(like a Password).
More accurate:
I wan t that if I play that melody the raspy turns same Relay to give power to the PC. The PC strats if he gets power and also the screens. Before I want to save a Melody and only if I play this Melody the relays should be turned.

I'm not a programmer so can anyone help me to programm it?
thanks a lot!
bye

mattmiller
Posts: 1834
Joined: Thu Feb 05, 2015 11:25 pm

Re: Using MIDI-events to run shell comands on a raspi

Sun Feb 18, 2018 12:30 pm

I need to start on interfacing a Pi to my midi piano so I'll try out a proof of concept

mattmiller
Posts: 1834
Joined: Thu Feb 05, 2015 11:25 pm

Re: Using MIDI-events to run shell comands on a raspi

Sun Feb 18, 2018 9:43 pm

Just had a quick play - I've got a midi keyboard and plugged it in and found this code to read the midi datastream

Code: Select all

import sys, pygame, pygame.midi
 
# set up pygame
pygame.init()
pygame.midi.init()

# list all midi devices
for x in range( 0, pygame.midi.get_count() ):
    print pygame.midi.get_device_info(x)
 
# open a specific midi device
inp = pygame.midi.Input(3)

# run the event loop
while True:
    if inp.poll():
        # no way to find number of messages in queue
        # so we just specify a high max value
        mdatain = inp.read(1000)[0][0]
        if mdatain[0] <> 248:
            print mdatain
 
    # wait 10ms - this is arbitrary, but wait(0) still resulted
    # in 100% cpu utilization
    pygame.time.wait(10)

Then if i start playing Sound of Music Do-Re-Me I get this output

Code: Select all

('ALSA', 'Midi Through Port-0', 0, 1, 0)
('ALSA', 'Midi Through Port-0', 1, 0, 0)
('ALSA', 'Digital Keyboard MIDI 1', 0, 1, 0)
('ALSA', 'Digital Keyboard MIDI 1', 1, 0, 0)
[144, 60, 31, 0]
[144, 60, 0, 0]
[144, 62, 42, 0]
[144, 62, 0, 0]
[144, 64, 44, 0]
[144, 64, 0, 0]
which shows it receiving note on - note off for C-D-E

So I think its should be fairly easy to recognise a note sequence as a password

To switch the power off/on - the best method is to use 433Mhz remote control sockets and to plug a 433 transmitter into the Pi that can be programmed to simulate the remote control and switch it on/off

PianoPCJ/A.F
Posts: 18
Joined: Thu Feb 15, 2018 10:20 pm

Re: Using MIDI-events to run shell comands on a raspi

Mon Feb 19, 2018 2:10 pm

mattmiller wrote:
Sun Feb 18, 2018 9:43 pm
Just had a quick play - I've got a midi keyboard and plugged it in and found this code to read the midi datastream

Code: Select all

import sys, pygame, pygame.midi
 
# set up pygame
pygame.init()
pygame.midi.init()

# list all midi devices
for x in range( 0, pygame.midi.get_count() ):
    print pygame.midi.get_device_info(x)
 
# open a specific midi device
inp = pygame.midi.Input(3)

# run the event loop
while True:
    if inp.poll():
        # no way to find number of messages in queue
        # so we just specify a high max value
        mdatain = inp.read(1000)[0][0]
        if mdatain[0] <> 248:
            print mdatain
 
    # wait 10ms - this is arbitrary, but wait(0) still resulted
    # in 100% cpu utilization
    pygame.time.wait(10)

Then if i start playing Sound of Music Do-Re-Me I get this output

Code: Select all

('ALSA', 'Midi Through Port-0', 0, 1, 0)
('ALSA', 'Midi Through Port-0', 1, 0, 0)
('ALSA', 'Digital Keyboard MIDI 1', 0, 1, 0)
('ALSA', 'Digital Keyboard MIDI 1', 1, 0, 0)
[144, 60, 31, 0]
[144, 60, 0, 0]
[144, 62, 42, 0]
[144, 62, 0, 0]
[144, 64, 44, 0]
[144, 64, 0, 0]
which shows it receiving note on - note off for C-D-E

So I think its should be fairly easy to recognise a note sequence as a password

To switch the power off/on - the best method is to use 433Mhz remote control sockets and to plug a 433 transmitter into the Pi that can be programmed to simulate the remote control and switch it on/off
is the first code you sended a script?
if I put it into a script there comes an error:,,ImportError: No module named pygame"
notice: On my Raspi i already installed a tool named mididings. and if I open a script with this tool it shoes me midiin-/output. so i made a script withe your code and opened it. than came this error

mattmiller
Posts: 1834
Joined: Thu Feb 05, 2015 11:25 pm

Re: Using MIDI-events to run shell comands on a raspi

Mon Feb 19, 2018 4:07 pm

It is a python file. i saved it as ymidi.py

try running it as

Code: Select all

python ymidi.py
Interestingly - I can't get mididings to install - it gives me an error saying no package called jack :)

PianoPCJ/A.F
Posts: 18
Joined: Thu Feb 15, 2018 10:20 pm

Re: Using MIDI-events to run shell comands on a raspi

Mon Feb 19, 2018 8:49 pm

mattmiller wrote:
Mon Feb 19, 2018 4:07 pm
It is a python file. i saved it as ymidi.py

try running it as

Code: Select all

python ymidi.py
Interestingly - I can't get mididings to install - it gives me an error saying no package called jack :)
do I have anything to do before runing this script? Create a file? install a package?
and what is "pygame"?

mattmiller
Posts: 1834
Joined: Thu Feb 05, 2015 11:25 pm

Re: Using MIDI-events to run shell comands on a raspi

Mon Feb 19, 2018 10:09 pm

do I have anything to do before runing this script? Create a file? install a package?
Yes - you have to create a file - I suggest calling it ymidi.py

Then you just run it by

Code: Select all

python ymidi.py
I think if you have no programming knowledge at all then this project is going to be tricky :(

PianoPCJ/A.F
Posts: 18
Joined: Thu Feb 15, 2018 10:20 pm

Re: Using MIDI-events to run shell comands on a raspi

Tue Feb 20, 2018 2:05 pm

But there is still the error:"ImportError: No module named pygame".

mattmiller
Posts: 1834
Joined: Thu Feb 05, 2015 11:25 pm

Re: Using MIDI-events to run shell comands on a raspi

Tue Feb 20, 2018 3:21 pm

:(
From all that I've read - pygame is supposed to be installed by default

I'm getting a new SD card tomorrow - I'll try setting up from scratch and see what happens on my system

Can you remember the steps you did to get mididings to work for you?

PianoPCJ/A.F
Posts: 18
Joined: Thu Feb 15, 2018 10:20 pm

Re: Using MIDI-events to run shell comands on a raspi

Tue Feb 20, 2018 3:31 pm

mattmiller wrote:
Tue Feb 20, 2018 3:21 pm
:(
From all that I've read - pygame is supposed to be installed by default

I'm getting a new SD card tomorrow - I'll try setting up from scratch and see what happens on my system

Can you remember the steps you did to get mididings to work for you?
I only typed in

Code: Select all

apt-get update
and

Code: Select all

apt-get install mididings
notice i'm runing on Raspian Strech lite

mattmiller
Posts: 1834
Joined: Thu Feb 05, 2015 11:25 pm

Re: Using MIDI-events to run shell comands on a raspi

Tue Feb 20, 2018 10:01 pm

Ta - never thought of obvious :)

I was following the instructions on GitHub site which didn't work

mattmiller
Posts: 1834
Joined: Thu Feb 05, 2015 11:25 pm

Re: Using MIDI-events to run shell comands on a raspi

Tue Feb 20, 2018 10:06 pm

notice i'm runing on Raspian Strech lite
aah- that will be the problem with pygame not being installed

maybe try

Code: Select all

sudo apt-get install python-pygame

PianoPCJ/A.F
Posts: 18
Joined: Thu Feb 15, 2018 10:20 pm

Re: Using MIDI-events to run shell comands on a raspi

Wed Feb 21, 2018 2:05 pm

mattmiller wrote:
Tue Feb 20, 2018 10:06 pm
notice i'm runing on Raspian Strech lite
aah- that will be the problem with pygame not being installed

maybe try

Code: Select all

sudo apt-get install python-pygame
OK nice. that worked. but then when I run the file ther comes this:

Code: Select all

('ALSA', 'Midi Through Port-0', 0, 1, 0)
('ALSA', 'Midi Through Port-0', 1, 0, 0)
Traceback (most recent call last):
  File "ymidi.py", line 12, in <module>
    inp = pygame.midi.Input(3)
  File "/usr/lib/python2.7/dist-packages/pygame/midi.py", line 272, in __init__
    raise MidiException("Device id invalid, out of range.")
pygame.midi.MidiException: 'Device id invalid, out of range.
and not such a code at your try :cry:

mattmiller
Posts: 1834
Joined: Thu Feb 05, 2015 11:25 pm

Re: Using MIDI-events to run shell comands on a raspi

Wed Feb 21, 2018 2:30 pm

My guess is that your Pi isn't recognising your midi keyboard and is only finding the internal ones (which I believe but dont 100% know are sort of pretend internal midi devices)

Mine shows this

Code: Select all

python ymidiin.py 
('ALSA', 'Midi Through Port-0', 0, 1, 0)
('ALSA', 'Midi Through Port-0', 1, 0, 0)
('ALSA', 'Digital Keyboard MIDI 1', 0, 1, 0)
('ALSA', 'Digital Keyboard MIDI 1', 1, 0, 0)
....
Wht keyboard do you have - does it have its own usbmidi port or are you using an adaptor device of some sort?

PianoPCJ/A.F
Posts: 18
Joined: Thu Feb 15, 2018 10:20 pm

Re: Using MIDI-events to run shell comands on a raspi

Wed Feb 21, 2018 4:46 pm

mattmiller wrote:
Wed Feb 21, 2018 2:30 pm
Wht keyboard do you have - does it have its own usbmidi port or are you using an adaptor device of some sort?
I builded a "adapter" by a tutorial to build a Raspisynt
http://www.instructables.com/id/PiMiDi- ... earned-to/
but then I noticed that in the tutorial they used a Raspi 2 with an old OS so there were problems. I thought that the adapter witch i builded generaly works if I enable the serialport with

Code: Select all

sudo raspi-config

but...
is it possible to plug an USB piano to the raspi with this script?

mattmiller
Posts: 1834
Joined: Thu Feb 05, 2015 11:25 pm

Re: Using MIDI-events to run shell comands on a raspi

Wed Feb 21, 2018 5:00 pm

I've not got one of those serial adaptors :(

My piano uses USB for its midi interface so all I do is plug it into a usb port on my Pi

My brother says this type of one works for him on his Pi

https://www.amazon.co.uk/dp/B01GY5JDQQ/ ... 11_TE_item

PianoPCJ/A.F
Posts: 18
Joined: Thu Feb 15, 2018 10:20 pm

Re: Using MIDI-events to run shell comands on a raspi

Sun Mar 18, 2018 8:59 pm

mattmiller wrote:
Sun Feb 18, 2018 9:43 pm

Then if i start playing Sound of Music Do-Re-Me I get this output

Code: Select all

('ALSA', 'Midi Through Port-0', 0, 1, 0)
('ALSA', 'Midi Through Port-0', 1, 0, 0)
('ALSA', 'Digital Keyboard MIDI 1', 0, 1, 0)
('ALSA', 'Digital Keyboard MIDI 1', 1, 0, 0)
[144, 60, 31, 0]
[144, 60, 0, 0]
[144, 62, 42, 0]
[144, 62, 0, 0]
[144, 64, 44, 0]
[144, 64, 0, 0]
which shows it receiving note on - note off for C-D-E
Ok! Puh! Now I nearly get the same output.

Code: Select all

('ALSA', 'Midi Through Port-0', 0, 1, 0)
('ALSA', 'Midi Through Port-0', 1, 0, 0)
('ALSA', 'MIDISPORT 2x2 Anniv MIDI 1', 0, 1, 0)
('ALSA', 'MIDISPORT 2x2 Anniv MIDI 1', 1, 0, 0)
('ALSA', 'MIDISPORT 2x2 Anniv MIDI 2', 0, 1, 0)
('ALSA', 'MIDISPORT 2x2 Anniv MIDI 2', 1, 0, 0)
[128, 64, 71, 0]
[128, 63, 74, 0]

So now I would like to know what do the four different numbers mean.

mattmiller
Posts: 1834
Joined: Thu Feb 05, 2015 11:25 pm

Re: Using MIDI-events to run shell comands on a raspi

Mon Mar 19, 2018 7:47 am

128 is note off (made up of 1000 and 0000) - should really be a note on 144 (1001 and 0000)

64 is what note (60 = midlle C so 64 = E , four semi-tones higher than C)

71 is note velocity - e.g hard hard you pressed the key down

0 (I don't really know what this is for - my keyboard sends it as well but if I just send 3 byte messages - sounds play OK) I think it just gets ignored

Can you just check what you get if you just play C D E in sequence please (which is what I did)

PianoPCJ/A.F
Posts: 18
Joined: Thu Feb 15, 2018 10:20 pm

Re: Using MIDI-events to run shell comands on a raspi

Mon Mar 19, 2018 6:00 pm

mattmiller wrote:
Mon Mar 19, 2018 7:47 am
Can you just check what you get if you just play C D E in sequence please (which is what I did)
Of course. My output:

Code: Select all

[144, 60, 77, 0]
[128, 60, 46, 0]
[144, 62, 68, 0]
[128, 62, 47, 0]
[144, 64, 80, 0]
[128, 64, 80, 0]
now i played every tone separate.

mattmiller
Posts: 1834
Joined: Thu Feb 05, 2015 11:25 pm

Re: Using MIDI-events to run shell comands on a raspi

Mon Mar 19, 2018 9:07 pm

Excellent
Yours sends better information than mine (mine just sends a note on followed by another note on with no amplitude - yours is sending proper note on/note off for each note :)

PianoPCJ/A.F
Posts: 18
Joined: Thu Feb 15, 2018 10:20 pm

Re: Using MIDI-events to run shell comands on a raspi

Tue Mar 20, 2018 1:37 pm

Ok, but now I need a possibility to set a melody which works like a passwort.
How can i do this?

mattmiller
Posts: 1834
Joined: Thu Feb 05, 2015 11:25 pm

Re: Using MIDI-events to run shell comands on a raspi

Tue Mar 20, 2018 6:22 pm

I've had to disconnect keyboard as rooom full of other stuff for the moment but try this and see it if works

Code: Select all

import sys, pygame, pygame.midi
 
# set up pygame
pygame.init()
pygame.midi.init()

# list all midi devices
for x in range( 0, pygame.midi.get_count() ):
    print pygame.midi.get_device_info(x)
 
# open a specific midi device
inp = pygame.midi.Input(3)
pmatch = [0,0,0] # set initial data to 0
password = [60,62,64] # password = C,D,E
while True:
    if inp.poll():
        # no way to find number of messages in queue
        # so we just specify a high max value
        mdatain = inp.read(1000)[0][0]
        if mdatain[0] <> 248:
            #print mdatain
            if (mdatain[0] == 144):
                pmatch = [pmatch[1], pmatch[2], mdatain[1]] # update pmatch with new note
                print pmatch
    if password == pmatch:
        print "PASSWORD ACCEPTED"
    # wait 10ms - this is arbitrary, but wait(0) still resulted
    # in 100% cpu utilization
    pygame.time.wait(10)

PianoPCJ/A.F
Posts: 18
Joined: Thu Feb 15, 2018 10:20 pm

Re: Using MIDI-events to run shell comands on a raspi

Thu Mar 22, 2018 7:44 pm

mattmiller wrote:
Tue Mar 20, 2018 6:22 pm
I've had to disconnect keyboard as rooom full of other stuff for the moment but try this and see it if works

Code: Select all

import sys, pygame, pygame.midi
 
# set up pygame
pygame.init()
pygame.midi.init()

# list all midi devices
for x in range( 0, pygame.midi.get_count() ):
    print pygame.midi.get_device_info(x)
 
# open a specific midi device
inp = pygame.midi.Input(3)
pmatch = [0,0,0] # set initial data to 0
password = [60,62,64] # password = C,D,E
while True:
    if inp.poll():
        # no way to find number of messages in queue
        # so we just specify a high max value
        mdatain = inp.read(1000)[0][0]
        if mdatain[0] <> 248:
            #print mdatain
            if (mdatain[0] == 144):
                pmatch = [pmatch[1], pmatch[2], mdatain[1]] # update pmatch with new note
                print pmatch
    if password == pmatch:
        print "PASSWORD ACCEPTED"
    # wait 10ms - this is arbitrary, but wait(0) still resulted
    # in 100% cpu utilization
    pygame.time.wait(10)
YES! it works. Thanks. I tried to modify the script so if it should print "PASSWORD ACCEPTED", I want that an external LED turns on.
I thought I only have to put the commands, which I normal use to turn the LED on, at the place where you writed "print "PASSWORD ACCEPTED".
like so:

Code: Select all

import sys, pygame, pygame.midi
 
# set up pygame
pygame.init()
pygame.midi.init()

# list all midi devices
for x in range( 0, pygame.midi.get_count() ):
    print pygame.midi.get_device_info(x)
 
# open a specific midi device
inp = pygame.midi.Input(3)
pmatch = [0,0,0] # set initial data to 0
password = [60,62,64] # password = C,D,E
while True:
    if inp.poll():
        # no way to find number of messages in queue
        # so we just specify a high max value
        mdatain = inp.read(1000)[0][0]
        if mdatain[0] <> 248:
            #print mdatain
            if (mdatain[0] == 144):
                pmatch = [pmatch[1], pmatch[2], mdatain[1]] # update pmatch with new note
                print pmatch
    if password == pmatch:
        sudo echo "2" > /sys/class/gpio/export
        sudo echo "out" > /sys/class/gpio/gpio2/direction
        sudo echo "1" > /sys/class/gpio/gpio2/value
    # wait 10ms - this is arbitrary, but wait(0) still resulted
    # in 100% cpu utilization
    pygame.time.wait(10)
but it wont work. it says: "invalid syntax"

User avatar
Douglas6
Posts: 4346
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Using MIDI-events to run shell comands on a raspi

Thu Mar 22, 2018 8:14 pm

Because those are bash commands, not Python.

mattmiller
Posts: 1834
Joined: Thu Feb 05, 2015 11:25 pm

Re: Using MIDI-events to run shell comands on a raspi

Thu Mar 22, 2018 8:31 pm

In Python there are several gpio librariies that can be used
I use the RPi.GPIO one

So at beginning of program add this in

Code: Select all

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM) #uses broadcom pin numbering - same as bash numbering that you use
GPIO.setup(2, GPIO.OUT) # set GPIO2 as output
then when you want to make a pin go high use

Code: Select all

GPIO.output(2, 1)
and to make it go low use

Code: Select all

GPIO.output(2, 0)

Return to “Graphics, sound and multimedia”

Who is online

Users browsing this forum: No registered users and 10 guests