# Fast Dynamic Audio Ramp

**URL:** <https://psychtoolbox.discourse.group/t/fast-dynamic-audio-ramp/379>\
**Category:** Uncategorized\
**Created:** [December 20, 2012, 1:19am UTC](https://psychtoolbox.discourse.group/t/fast-dynamic-audio-ramp/379 "2012-12-20T01:19:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [December 20, 2012, 1:19am UTC](https://psychtoolbox.discourse.group/t/fast-dynamic-audio-ramp/379/1 "2012-12-20T01:19:05Z")

</div>

Hello there,  
  
I've been struggling with this for a while now. I want to be able to   
play a tone and let the user decide when to shut the tone off by   
pressing a key without any clicks. Obviously, a click occurs if I just   
interrupt playback with a PsychPortAudio('Stop') command after a keypress.  
  
The closest I've come is by using something similar to the fade-out   
method at the end of the BasicAMAndMixScheduleDemo file. My code is:  
  
tic  
while toc \< .2  
ramp = 1 - (toc\*5);  
PsychPortAudio('Volume', pahandle, ramp);  
end  
PsychPortAudio('Stop', pahandle);  
  
Unfortunately, because this ramps so quickly (200 milliseconds is about   
as slow as I can afford), I get easily audible noise artifacts as it   
ramps. How else can I achieve my goal of being able to stop a tone in   
real time without a click?  
  
I'm on Windows 7 using Asio4all with crummy default laptop audio audio   
drivers.  
  
Thank you for any help,  
  
Andy

---

<div class="post-metadata">

**Author:** ![andreaswidmann](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@andreaswidmann](https://psychtoolbox.discourse.group/u/andreaswidmann)\
**Post date:** [December 20, 2012, 2:56pm UTC](https://psychtoolbox.discourse.group/t/fast-dynamic-audio-ramp/379/2 "2012-12-20T14:56:56Z")

</div>

Hi,  
  

> \> I've been struggling with this for a while now. I want to be able to   
> \> play a tone and let the user decide when to shut the tone off by   
> \> pressing a key without any clicks. Obviously, a click occurs if I just   
> \> interrupt playback with a PsychPortAudio('Stop') command after a keypress.  
> \>   
> \> The closest I've come is by using something similar to the fade-out   
> \> method at the end of the BasicAMAndMixScheduleDemo file. My code is:

So, you've already come quite close: You need to use the Master+Slave+AM-(amplitude-modulation)-Slave device approach. The audio signal goes into the first slave device. Your envelope/ramp goes in to AM-Slave device (type 32) including a sufficient number of zeros at the end. You can start both slave devices independently. The AM-slave is started immediately after your keypress. The audio signal is then multiplied by the AM envelope. When the envelope reaches the time range with zero amplitude you can safely stop the playback without hearing clicks.  
  
To my experience your approach changing the volume will introduce clicks with most sound cards.  
  

> \> tic  
> \> while toc \< .2  
> \> ramp = 1 - (toc\*5);  
> \> PsychPortAudio('Volume', pahandle, ramp);  
> \> end  
> \> PsychPortAudio('Stop', pahandle);  
> \>   
> \> Unfortunately, because this ramps so quickly (200 milliseconds is about   
> \> as slow as I can afford), I get easily audible noise artifacts as it   
> \> ramps. How else can I achieve my goal of being able to stop a tone in   
> \> real time without a click?  
> \>   
> \> I'm on Windows 7 using Asio4all with crummy default laptop audio audio   
> \> drivers.

I would expect very bad timing behavior with this combination.  
  
Hope this helps,  
Andreas  
  
  

> \> Thank you for any help,  
> \>   
> \> Andy  
> \>   
> \>   
> \> ------------------------------------  
> \>   
> \> Post your message to: [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com)  
> \> Please indicate OS9, OSX, or WIN version, and include your full name.  
> \> Denis Pelli, David Brainard, and Allen Ingling.  
> \> [http://psychtoolbox.org](http://psychtoolbox.org)  
> \> Yahoo! Groups Links  
> \>   
> \>   
> \>

---

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [December 21, 2012, 1:15am UTC](https://psychtoolbox.discourse.group/t/fast-dynamic-audio-ramp/379/3 "2012-12-21T01:15:21Z")

</div>

Thank you very much! This solved it and it works perfectly.  
  

> On 12/20/2012 6:56 AM, Andreas Widmann wrote:  
> \> Hi,  
> \>  
> \>\> I've been struggling with this for a while now. I want to be able to  
> \>\> play a tone and let the user decide when to shut the tone off by  
> \>\> pressing a key without any clicks. Obviously, a click occurs if I just  
> \>\> interrupt playback with a PsychPortAudio('Stop') command after a keypress.  
> \>\>  
> \>\> The closest I've come is by using something similar to the fade-out  
> \>\> method at the end of the BasicAMAndMixScheduleDemo file. My code is:  
> \> So, you've already come quite close: You need to use the Master+Slave+AM-(amplitude-modulation)-Slave device approach. The audio signal goes into the first slave device. Your envelope/ramp goes in to AM-Slave device (type 32) including a sufficient number of zeros at the end. You can start both slave devices independently. The AM-slave is started immediately after your keypress. The audio signal is then multiplied by the AM envelope. When the envelope reaches the time range with zero amplitude you can safely stop the playback without hearing clicks.  
> \>  
> \> To my experience your approach changing the volume will introduce clicks with most sound cards.  
> \>  
> \>\> tic  
> \>\> while toc \< .2  
> \>\> ramp = 1 - (toc\*5);  
> \>\> PsychPortAudio('Volume', pahandle, ramp);  
> \>\> end  
> \>\> PsychPortAudio('Stop', pahandle);  
> \>\>  
> \>\> Unfortunately, because this ramps so quickly (200 milliseconds is about  
> \>\> as slow as I can afford), I get easily audible noise artifacts as it  
> \>\> ramps. How else can I achieve my goal of being able to stop a tone in  
> \>\> real time without a click?  
> \>\>  
> \>\> I'm on Windows 7 using Asio4all with crummy default laptop audio audio  
> \>\> drivers.  
> \> I would expect very bad timing behavior with this combination.  
> \>  
> \> Hope this helps,  
> \> Andreas  
> \>  
> \>  
> \>\> Thank you for any help,  
> \>\>  
> \>\> Andy  
> \>\>  
> \>\>  
> \>\> ------------------------------------  
> \>\>  
> \>\> Post your message to: [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com)  
> \>\> Please indicate OS9, OSX, or WIN version, and include your full name.  
> \>\> Denis Pelli, David Brainard, and Allen Ingling.  
> \>\> [http://psychtoolbox.org](http://psychtoolbox.org)  
> \>\> Yahoo! Groups Links  
> \>\>  
> \>\>  
> \>\>
