Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 at 8:30
2  
Detecting the release of the key is a tricky problem. See this. –  Simon Woods Apr 30 at 9:16

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.