On this site you can read the story about how I started to build my own optical piano sensor. Just in case you have no idea what this is about watch this:
This is how it looks right now:
The story behind my optical piano sensor:
It all started when I had bought a cheap digital piano to revive my piano playing, which i have not actively done for many years. When it came I was a little disappointed about the quality of the keyboard, but accepted it because of the price. I was fine with the mechanic noise being louder than usual, but it had a reduced dynamic midi range(less than 1-127) and no sensitivity setting that suited my needs(but it was working quite okay using the integrated synthesizer, which is not the best piano sound). The other problem I had with it was, that you couldn't really control the velocity of a fast repeated note, since the keys where bumping back from their upper stops. The first quite easy step I did to enhance it, was to put rubber bands on the key mechanic to decrease the bumping and get a harder attack. That was a big improvement, but didn't really solve the issue. By coincidence I stumbled upon a commercial solution for real pianos using optical sensors. I was quite impressed about that simple idea and a little saddened by the royal price. So I started thinking if one could build such a system with simple-to-solder cheap AVR microcontrollers, being initially convinced that it should be impossible. But anyway I was curious about how fast a piano key could be stroked by a usual player. So I build a simple testing circuit consisting of a red LED and a common phototransistor(the circuit is like in the schematics of project). I used my DSO to have a look at the strokes. From this experiment I learned, that I had to accomplish a sample rate of at least 1000 Hz to measure even the fastest attacks. So I looked up the possible sampling rates for my favourite AVR - the Atmega16 – and saw that it was possible. Hence I ordered a bunch of CNY70 sensors for testing and started to code a simple measuring program to test the sensors. The first thing I did, when I got the obgligatory noise issues solved(I first used an evaluation board that was unsuitable for the ADC to attain the desired precision) and implemented my MIDI out feature was to use the sensor to directly control the pitch of the notes(a little like a Theremin), while it was sending 25 notes per second to the synthesizer. The resulting sound can be listened to at here
When I finished living out my play instinct on that Theremin, I started to think about a way how one could physically model the mechanics of the keys and hammers in a piano. The problem doing this was, recalling that I want to achieve at least 1000Hz I decided to use the highest possible sampling rate of the AVR, when running at the maximum clock of 16Mhz, which is 1202 Hz per channel assuming that I use all 8 ADC channels. That means that I have 1664 8bit instructions to calculate with one sample of one channel. As if the physics wheren't enough, the sample data also needs to be filtered in order to get the desired dynamic range. So there was a lot of math to be done and it ended in a very simplified physical model, where I used lookup tables to speed up expensive arithmetic operations. With the current model one can adjust the strike point of the hammer, the point, where the dampers go up and down, the hammer mass and the hardness of the spring(I think I'll use gravity instead of a spring for the future*) that holds back the hammer. The impact speed of the hammer is then mapped to the range of 1-127 for the MIDI output. The velocity curve of that mapping can be freely adjusted to get the desired behaviour.
One day ago I mounted my setup to one key of my piano. I just attached a piece of paper on the key to be able to measure its position from outside, thus avoiding to disassemble the whole piano. I played on that key comparing the old two-switch electronic of my piano to the new optical sensor and the result was totally amazing. Now I had control over the full dynamic range of my synthesizer and I could repeat attacks without letting the key go up so far, that the virtual dampers would be enabled. Even attacks where you don't press the keys down to the felt stop are recognized arcordingly: You have a free choice at which point you move the key fast enough for the hammer to hit the string, which gets easier next to the felt stop.
*just tested it. Definitely better :)
Future plans: when I get the 8 channels on my single controller working together the next step will be building 11 of those 8 channeled devices connected via TWI(I2C). Finally I will add a master controller which speaks the midi protocol and provides the user interface, where you can setup all parameters. At the end you will be able to tweak each single key individually according to your needs. Of course I will use these sensors at the pedals, too. When I manage to get all this finished I could finally add an aftertouch feature for those who like synthesizer effects. But this all is really far away ;)