yankeecactus
Posts: 6
Joined: Tue Feb 13, 2018 2:35 am

pi player piano

Tue Feb 13, 2018 2:56 am

My goal is to build a rudimentary player piano for use in a music lesson. (for example: https://youtu.be/f2gVhBxwRqg) I want to use GPIO to trigger a specific pitch on a software synthesizer, such as zynaddsubfx. Notes will be triggered when a hole in a piece of paper passes over a contact. The longer the hole, the longer the note is sustained. This is why I don't think simply triggering mp3s would work.

Is what I'm asking to do even possible? I know there are midi synthesizers built on Pi, but they are WAY more complicated than what I'm trying to do. I just need 1 voice and maybe 6 notes, no FX.

Thank you.

B.Goode
Posts: 6356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: pi player piano

Tue Feb 13, 2018 5:08 pm

Maybe something based on Sonic Pi?

http://sonic-pi.net

Establishing pitch and duration by reading an external source and passing those to Sonic Pi to be played looks as if it might be quite simple?

And I suspect a little web searching for that as a target might reveal that it has been done.

PiGraham
Posts: 3391
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: pi player piano

Tue Feb 13, 2018 6:20 pm

yankeecactus wrote:
Tue Feb 13, 2018 2:56 am
Notes will be triggered when a hole in a piece of paper passes over a contact. The longer the hole, the longer the note is sustained.
Do these paper rolls with holes already exist and you want to make a devoce that can play them, or do you just like moving roll aesthetic?

I think it will be more work to make a mechanism of switches and paper feed that it would be to draw a graphic of a piano roll on screen and play music from a midi file. You would have lots of existing music files to use and the midi notes will specify pitch and duration just like holes in a roll would do.

If you really went to play a paper roll you could arrange a lot of photo sensors on a bar and put a strip light on the other side and read the photo sensors by gpio inputs. You could perhaps 3D print the bar (in sections to fit your printer) so that the sensor positions are acurately matched to the paper rolls.

You could point a Pi camera at the roll passing over a strip light and detect the holes that way using OpenCV or some basic pixel sampling code.
That is mehcanically simpler, but more complicated code.

yankeecactus
Posts: 6
Joined: Tue Feb 13, 2018 2:35 am

Re: pi player piano

Wed Feb 14, 2018 11:59 pm

I looked at using sonic pi, but I would have to install raspbian stretch (I think). I'm hoping to find a solution that doesn't involve such drastic measures.

As for the machine itself, I hope to ask my students to create compositions by cutting holes in prepared 8.5x11 paper (to give a tangible experience of what a composer using piano rolls would have had to hip through). For triggering notes, I'm thinking of using metal contacts on a flexible arm. When the paper is pulled across the bed, it will break the circuit until one of the holes comes by, which well let gravity complete the circuit. So if you run the machine with no paper, all notes would sound.

I've seen projects that use pi cameras, which would be cool, but I'm a teacher and this have no money for such things.

PiGraham
Posts: 3391
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: pi player piano

Thu Feb 15, 2018 12:57 pm

Are you teaching music or technology?
How will the students make the holes?
How will the punched sheets be fed?
I fear that doing it by hand will distract from the musical aspects.
A card punch that can make regular holes in registered positions would help a lot but that's a significant project in itself.
If sheets are pulled by hand it will be hard to realise a good rendition.

I'd be tempted to get an old dot matrix printer (eBay) and a box of tractor feed listing paper (still available to buy) and wire up the feed motor (stepper?) so that the Pi could control it.
That would give you nice regular feed and good registration.
Perforating the paper could be fiddly and maybe dangerous if hand cut with a craft knife, and not accessible to a normal hole punch.
It would be easier to make black marks on the paper through a stencil using a black marker pen. The stencil could locate on the tractor holes of the paper to ensure good alignment(you can buy registration pins for drawing boards that might help with that).

Use a simple photo sensor to detect the black marks. The paper is less likely to tear or catch on wires.
You can buy line following sensors for peanuts that would be easy to connect to a Pi.
Here's a four channel example for £1.49 https://www.ebay.co.uk/itm/4-Channel-In ... 2356451776

yankeecactus
Posts: 6
Joined: Tue Feb 13, 2018 2:35 am

Re: pi player piano

Thu Feb 15, 2018 7:31 pm

>>Are you teaching music or technology?
I teach music. The lesson I'm planning will cover Conlon Nancarrow, player pianos, music tech, and black midi.


>>How will the students make the holes?
>>How will the punched sheets be fed?
>>I fear that doing it by hand will distract from the musical aspects.
>>A card punch that can make regular holes in registered positions would help a lot but that's a significant project in itself.
>>If sheets are pulled by hand it will be hard to realise a good rendition.

My main goal is for students to connect with music history and technology. I hope that by creating their own piano roll-style compositions they will have better understanding of how player pianos work, experiment with pitch and duration, and anchor the history of Nancarrow to an authentic experience (rather than just watching youtube videos).


>>Use a simple photo sensor to detect the black marks. The paper is less likely to tear or catch on wires.
>>You can buy line following sensors for peanuts that would be easy to connect to a Pi.
>>Here's a four channel example for £1.49 https://www.ebay.co.uk/itm/4-Channel-In ... 2356451776

Ok, this is affordable. I'm a little bit in love with the idea of this elaborate looking machine with moving parts making crazy sounding music. I'm open to going this route though if I have to.

My main concern is figuring out how to get GPIO signal to trigger a synth. I'm slowly building my lab at home. I plan to start experimenting with buttons and code perhaps tomorrow night.

PiGraham
Posts: 3391
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: pi player piano

Thu Feb 15, 2018 7:41 pm

A printer mechanism feeding fanfold paper covered in dots under a row of photo sensors with blinking lights has some appeal, don't you think? With paper feed running it would even look like a player piano with paper naturally forming a cylindrical shape.
You haven't said anything about plans for how the paper would be cut or fed but it's not a trivial challenge and there's plenty of scope for paper to slip, tear or jam, all of which has been fairly well sorted out on printers.

For software my first thought is pygame.midi. It should be easy to fire note_on() when a gpio goes high and note_off() when it goes low (or vice versa). Note length direct from the mark/hole on the roll.

This looks helpful http://www.derickdeleon.com/2014/02/mid ... ry-pi.html

You could easily do a full octave with a couple of those opto sensor boards direct to gpios.
It wouldn't be hard to expand it for more notes.

yankeecactus
Posts: 6
Joined: Tue Feb 13, 2018 2:35 am

Re: pi player piano

Thu Feb 15, 2018 9:35 pm

I had no idea pygame had midi! That is exactly what I'm looking for! Thank you.

While you paint an attractive picture, the dot matrix setup does not really fit my situation. I will post my back of the napkin sketch of how this thing might work later tonight.'

Edit - Here it is
Image

I should quit my job and become a graphic artist I think. You know you want that diagram framed.

User avatar
Burngate
Posts: 5409
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK
Contact: Website

Re: pi player piano

Fri Feb 16, 2018 12:00 pm

yankeecactus wrote:
Thu Feb 15, 2018 7:31 pm
>>Are you teaching music or technology?
I teach music. The lesson I'm planning will cover Conlon Nancarrow, player pianos, music tech, and black midi.
Not helpful, but I find interesting - my maternal grandfather, James Bell Brow, was a Player Piano music arranger. He and his wife Emma Lillian spent a good few years skedaddling backwards and forwards across the pond, between NY & Liverpool, arround the beginning of the century. He died in Harrow in 1920 aged 45; she died in Brixham in 1923 aged 38, when my mother was 5

PiGraham
Posts: 3391
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: pi player piano

Sun Feb 18, 2018 6:06 pm

You can also play notes in Scratch.
http://foxbotindustries.com/build-easy-floor-piano/

yankeecactus
Posts: 6
Joined: Tue Feb 13, 2018 2:35 am

Re: pi player piano

Thu Feb 22, 2018 3:29 pm

Update: So I've got effectively nowhere on this because something was corrupt forcing me to reinstall raspbian. Then I screwed something else up, which ended up with me reinstalling raspbian again. Hopefully tonight or tomorrow I'll be able to actually play with some code.

PiGraham
Posts: 3391
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: pi player piano

Fri Feb 23, 2018 9:23 am

Possible alternative paper feed - a laminator. They can be cheap (from £15 / $20) and have nice slow smooth feed. You may have one you can use and no modifications are required. As with any friction feed the paper will wander and you can't guide it from the edge once it is gripped, but it should be OK for single sheets that are guided before the roller grips.

yankeecactus
Posts: 6
Joined: Tue Feb 13, 2018 2:35 am

Re: pi player piano

Tue Feb 27, 2018 3:43 am

So I'm stuck trying to get the "push a button get sound, release the button sound stops" effect.

(Im stuck typing this on my phone so case might be inaccurate and I'm going to abbreviate)

Code: Select all

If GPIO.input(18) == GPIO.LOW:
  Play note
 Elif GPIO == GPIO.HIGH
   stop note
This results with a repeating very fast attack, no sustain. Otherwise works.

Code: Select all

GPIO.wait_for_edge (falling)
Play
GPIO.wait_for_edge(rising)
Stop
This works awesome, but only for one button. I can't press and hold buttons and get 2 notes (yes I coded different pitches)

Code: Select all

Def note(channel):
Midioutput.note.on(60)
Sleep 1
Midioutput.note.off(60)
GPIO.add_event_detect(18,.gpio.falling, callback = note, bouncetime = 300)
Going this route, I can make multiple buttons work, but cannot seem to get the sustain function that I'm going for. I've tried to do event.detect gpio.rising but it throws a conflicting edge detection error.

I know there's got to be a simple solution that I'm just not finding. Thoughts?

Thanks!

PiGraham
Posts: 3391
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: pi player piano

Tue Feb 27, 2018 4:13 pm

You might try gpiozero to fire callbacks when the signal goes high and low
Assuming you define functions note_on_fn and note_off_fn to do the appropriate MIDI thing
Hack psuedocode: -

Code: Select all

from gpiozero import Button

piano_key_1=Button(pin=17, bounce_time=0.33)		#you probably need to deal with bounce 
												#especially from electrical contacts dropping into holes in paper.

piano_key_1.on_pressed=note_on_fn
piano_key_1.on_released=note_off_fn

#Just keep the program running on idle while the notes are played
while 1:
	time.sleep(1)
Some MIDI instruments will have defined decay envelopes (e.g. piano) that won't last however long between on and off. Others will be better for that, like an organ that can sustain notes indefinitely.

IanS
Posts: 190
Joined: Wed Jun 20, 2012 2:51 pm
Location: Southampton, England

Re: pi player piano

Thu Mar 01, 2018 10:39 am

The way I solved this problem in my Pi-powered organ was to maintain a state table of what keys are down. In each processing cycle, examine the note hardware to build a list of what notes should be playing. Then compare that with the previous state and send the appropriate midi on or off commands. It cycles around about 200 times a second, which is mainly limited by how fast I can query the state of the 61 notes. No need for callbacks and no problem with polyphony up to the processing limit of the synth. I start hitting processing power limitations (which lead to serious audio corruption) somewhere above 120 simultaneous notes.

Pseudocode:

Code: Select all

num_notes = 8
oldstate = "0" * numnotes
repeat
  newstate = queryhardware()
  for i in 0 to numnotes
    if oldstate[i] == "0" and newstate[i] == "1" then midi_on(i)
    if oldstate[i] == "1" and newstate[i] == "0" then midi_off(i)
    oldstate = newstate
  next
until neighbours_complain()
The real code is rather more complex these days, as there is one Pi per keyboard, and they need to send note on/off messages to each other if the keyboard couplers are active, so the hardware querying and per-keyboard state change analysis is done by one process per keyboard, then a note up/down list sent to a second process on one or more keyboards which maintains a more complex state machine integrating the organ stops and couplers, which talks to the synth.

It might already be being handled by the MCP23017 boards I am using, although I doubt it, or I might just have bounce resistant switches in the keyboard, but I found signal debouncing was unnecessary.

Return to “Graphics, sound and multimedia”

Who is online

Users browsing this forum: No registered users and 6 guests