Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}]
share|improve this question
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.