maxurf
Posts: 6
Joined: Fri Aug 19, 2016 4:34 pm

making a digital piano : is the rpi fast enough ?

Thu Oct 04, 2018 1:14 pm

Hi.
I recently broke my digital piano :lol: and I'm planning on making a new one from the keyboard (which is not broken) connected to a Raspberry Pi 3 Model B+.
I know how I'm going to do it but I'm not sure that the rpi will be fast enough for what I want to do. I'll have 8 digital outputs and 23 digital inputs. Here is the part of my program where I'm not sure that the rpi will be fast enough :

Code: Select all

//I dont know how to use gpios yet so I put comments instead
while  {
   for i from 1 to 8 {
      //write HIGH on output number i
      for j from 1 to 22 {
         //read input number j
      }
   }
   //read input number 23
}
  • I need that this while loop loops VERY quickly (at least 10,000 times per seconds). I assume that the CPU can handle it easily, but can the rpi switch inputs that fast and still give me the correct values ?
  • Moreover, I need to be 100% sure that when I read an input, the output has already ACTUALLY sent electricity because if there is some latency but the program keeps going before the output actually wrote, my piano won't work well. So does the rpi write fast enough ? Or if not, is it possible to pause the program until the output actually wrote ?
  • the rpi doesn't have enough gpios, I know that there are expansion boards but I guess it slows the writing/reading a little bit, so what's the "fastest" expansion board ? And does someone know if it will be fast enough for what I want to do ?
  • I didn't specified it in the code above but obviously, my program won't only do this (it will process the data from inputs and play piano accordingly). So, does someone know if such things can slow the program so much that it's not fast enough for what I want to do ?
  • Is it possible to use multithreading to read several pins through the expansion board at the same time ? And if so, is it worth it ?
Thank you in advance for your answers :D

pfletch101
Posts: 232
Joined: Sat Feb 24, 2018 4:09 am

Re: making a digital piano : is the rpi fast enough ?

Thu Oct 04, 2018 3:00 pm

It's not just a question of raw processing speed. It sounds as if you want to be able to rely on your program triggering and responding to external events in real time. Most Unix OSes, including Raspbian, are not suitable for applications that need this capability, since the OS may be servicing a request from another application when an event needs to be triggered and/or when an external event is detected, and will then take a significant time (at least milliseconds, rather than microseconds - occasionally tens or hundreds of milliseconds) to 'turn its attention back' to your program. Using a 'real time kernel' reduces, but does not eliminate, this issue. You can run 'bare metal', when your program is entirely in control of the processor, but this requires it to handle everything that the OS would normally handle for you, and is decidedly not for the faint-hearted. A pi may not be the ideal solution for your problem.

maxurf
Posts: 6
Joined: Fri Aug 19, 2016 4:34 pm

Re: making a digital piano : is the rpi fast enough ?

Thu Oct 04, 2018 3:09 pm

Thank you !
Do you know a more appropriate device for what I want to do ?
And if I was crazy enough to try the "bare metal" thing, do you think that it would be fast enough for my project ?

User avatar
Mortimer
Posts: 909
Joined: Sun Jun 10, 2012 3:57 pm

Re: making a digital piano : is the rpi fast enough ?

Thu Oct 04, 2018 3:21 pm

I would recommend taking a look at Julian Ilett's channel on Youtube.
He has done a series of videos on a penny organ project which might give you an ideal starting point to have a tinker. Doesn't use a RPi, but will give you a way of getting something going quite quickly and as you're understanding builds, you might spot a way of building a RPi into the project at some point.

https://www.youtube.com/user/julius256

Search Youtube for "julian ilett penny organ" will bring up a list of his videos on the project. It's wonderfully modular starting with showing the components, how they work etc. A building from single voice to polyphonic.

maxurf
Posts: 6
Joined: Fri Aug 19, 2016 4:34 pm

Re: making a digital piano : is the rpi fast enough ?

Thu Oct 04, 2018 3:30 pm

Thanks ! I'll take a look at this.

pfletch101
Posts: 232
Joined: Sat Feb 24, 2018 4:09 am

Re: making a digital piano : is the rpi fast enough ?

Thu Oct 04, 2018 3:49 pm

maxurf wrote:
Thu Oct 04, 2018 3:09 pm
Thank you !
Do you know a more appropriate device for what I want to do ?
Not my area, but the links that Mortimer has suggested look very relevant.
And if I was crazy enough to try the "bare metal" thing, do you think that it would be fast enough for my project ?
Almost certainly, yes, but you should ask this question in the Bare Metal forum here (https://www.raspberrypi.org/forums/viewforum.php?f=72)

maxurf
Posts: 6
Joined: Fri Aug 19, 2016 4:34 pm

Re: making a digital piano : is the rpi fast enough ?

Thu Oct 04, 2018 3:57 pm

Ok, thank you, I'll post something there.

hippy
Posts: 4031
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: making a digital piano : is the rpi fast enough ?

Thu Oct 04, 2018 5:42 pm

Code: Select all

   for i from 1 to 8 {
      for j from 1 to 22 {
You would be better of reading the whole set of inputs into a 32-bit word which will allow you to quickly tell which have been pressed or released doing bit-wise operations. And I would unroll the outer loop to reduce all overhead.

I would suggest starting with something which can scan the keyboard and generate MIDI serial output then add in what else you want to handle.

Eirikur
Posts: 3
Joined: Sun Sep 09, 2018 9:43 pm

Re: making a digital piano : is the rpi fast enough ?

Fri Oct 05, 2018 10:56 pm

I come from an electronic music background and I've built my own keyboard design. The Pi (especially 3B+) is plenty fast enough.
How do you want to make the piano sound? Physical models are pretty difficult to program. I use Linuxsampler (which I have to patch for ARM and build) and I find that it plays almost any of the many freely available sampled instruments on the net. There are several good multi-layer piano samples out there. Look at the SFZ and SF2 file formats. I would not roll my own sample-playback code. Linuxsampler streams directly from storage and works extremely well. It's an obscure and strange command-line program, but you can wrap it in a script. I wrap it in Python and put up a touch-screen GUI using Kivy. So, what you need is a MIDI controller, that is a piano keyboard that transmits MIDI note information. You then configure that using ALSA so that it acts as the performing input to Linuxsampler, which you have loaded with your chosen sample. I think this is easier than trying to do things with the GPIO! If you use MIDI as your data standard and Linuxsampler as your playback engine, you have all industry standard parts with no custom unreplaceable things except your script. I hated learning Linuxsampler's command interface, but it was worth it.

There are GUI programs like Fantasia to manage Linuxsampler and Carla to process your audio with reverb and so on. I just load the reverb and chorus plugins into Linuxsampler because it will do that and it keeps things simpler.

Eirikur

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

Re: making a digital piano : is the rpi fast enough ?

Thu Oct 11, 2018 4:02 pm

Yes an RPi is fast enough. I am part way through building an organ, which you can consider as being a stack of pianos, one of which is foot operated. There is then the complication of multiple stops (so one note press triggers multiple sounds), and couplers (where a note on one keyboard triggers the sounds according to the stops selected for another keyboard). 'Just' a piano is much simpler. I got acceptable performance out of a RPi v2 provided only a couple of stops were active, although obviously a 3 or 3+ is better - allowing many more simultaneous stops in the case of an organ.

Does your keyboard have one output per key? More than one? These would trigger at different press depths, so the time between gives the velocity. A switch matrix? These are more complex to read, but require fewer I/O connections. In any of these cases you are going to need some kind of GPIO expander. I use MCP23017 boards from AB Electronics.
So far I have two manuals (keyboards) with their associated stop boards working, and I am building the pedalboard. Each has its own RPi, with one of them acting as master, running an mqtt service to handle passing messages around. Latency is low enough that it is not noticeable while playing, even though everything is just in Python. The remaining work is to build and wire the pedalboard, and then build a console case to hold everything together.

Build details and code is available at https://github.com/Squeack/rpi_organ

Idahowalker
Posts: 403
Joined: Wed Jan 03, 2018 5:43 pm

Re: making a digital piano : is the rpi fast enough ?

Thu Oct 11, 2018 5:15 pm

If an Arduino can do it https://blog.arduino.cc/2018/10/08/casi ... d-fingers/, a Pi should be able to do it better.
Without knowing why you are deleting my postings, I will not know how...

User avatar
Imperf3kt
Posts: 1491
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: making a digital piano : is the rpi fast enough ?

Thu Oct 11, 2018 7:59 pm

A Pi should easily be fast enough.

If YouTube Piano works, which has the disadvantage of internet latency, then a Pi should.
Stop plugging your fan directly into the GPIO 5v
https://www.electronics-tutorials.ws/power/transient-suppression.html

lbdroidman
Posts: 13
Joined: Wed Oct 17, 2018 7:46 pm

Re: making a digital piano : is the rpi fast enough ?

Thu Oct 18, 2018 8:24 pm

maxurf wrote:
Thu Oct 04, 2018 1:14 pm
Here is the part of my program where I'm not sure that the rpi will be fast enough :

Code: Select all

//I dont know how to use gpios yet so I put comments instead
while  {
   for i from 1 to 8 {
      //write HIGH on output number i
      for j from 1 to 22 {
         //read input number j
      }
   }
   //read input number 23
}
It isn't entirely clear from the comments in this pseudocode what this code segment is actually intended to accomplish.
It looks like you're writing "high" on each output, then reading a whole bunch of inputs and doing NOTHING with those inputs.

There also doesn't seem to be any reason to nest those loops, since the inner loop does not seem to depend on the outer loop at all.

So you have 8 digital outputs and 23 digital inputs. I'll go out on a limb and guess that the digital inputs are the piano keys, but I'm at a complete loss trying to figure out just what in the heck the outputs are actually for and why you would want to write "1" to them in a loop.

As far as reading the keys go, have you considered using INTERRUPTS? Instead of sampling all of your keys (or for that matter, whatever these inputs happen to be connected to) a million times a second, you will simply write a routine (called an "Interrupt Service Routine" or "ISR"), which does something when the state of one of those keys *changes*. The action of running the ISR is triggered BY the change in one of those keys.

And also, without a bigger picture of what this project will eventually do, I'm thinking that the pi may be serious overkill. I had a digital piano way back in the 1980's, and even some cheap microcontroller that you can get for $1 can make a fool of any computer from the 80's.

User avatar
Mortimer
Posts: 909
Joined: Sun Jun 10, 2012 3:57 pm

Re: making a digital piano : is the rpi fast enough ?

Fri Oct 19, 2018 8:19 am

lbdroidman wrote:
Thu Oct 18, 2018 8:24 pm
So you have 8 digital outputs and 23 digital inputs. I'll go out on a limb and guess that the digital inputs are the piano keys, but I'm at a complete loss trying to figure out just what in the heck the outputs are actually for and why you would want to write "1" to them in a loop.
It's quite a common technique for multiplexing the reading of more buttons than you have inputs for. The 8 outputs will energise 8 banks of 22 keys, which will be detected by scanning through 22 input GPIO pins. The 23rd is for something else, which is the only bit I can't guess.

Return to “Other projects”