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}]
  • $\begingroup$ 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. $\endgroup$
    – Kuba
    Apr 30, 2014 at 8:30
  • 2
    $\begingroup$ Detecting the release of the key is a tricky problem. See this. $\endgroup$ Apr 30, 2014 at 9:16


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.