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}]
]
]
EventHandler
with it's events? I'm not sure if the transition will be smooth but it is worth to try. $\endgroup$