# Is it possible to model the piano key attack?

My purpose is to model the attack of a piano key. As the key is pressed a sound must be emitted. When the key is released the sound must stop with decreasing volume.

I have a partial success when the time is fixed. For example, if time=2 then the following code simulates a key pressed for 2 seconds. But I don't know how to change it so that the sound stops (with a "natural feeling") immediately after the key is released!

Button[Graphics[{White, Rectangle[{0, 0}, {1, 2}]}],

EmitSound[SampledSoundList[{{0.}}, 8000], "Preemptive"]; EmitSound[

Play[(1 - E^(-30 t)) (1 - E^(-18 (time - t)))  Sin[
2 Pi 440  t], {t, 0, time}]
]
]

-
I can't test it now but have you tried to use EventHandler with it's events? I'm not sure if the transition will be smooth but it is worth to try. – Kuba Apr 30 '14 at 8:30
Detecting the release of the key is a tricky problem. See this. – Simon Woods Apr 30 '14 at 9:16