# How do I fade in and fade out a channel (with stop)

**URL:** https://qa.fmod.com/t/how-do-i-fade-in-and-fade-out-a-channel-with-stop/11738
**Category:** FMOD Engine
**Created:** [November 30, 2014, 11:14pm UTC](https://qa.fmod.com/t/how-do-i-fade-in-and-fade-out-a-channel-with-stop/11738 "2014-11-30T23:14:44Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![brett](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/brett/32/261_2.png) [@brett](https://qa.fmod.com/u/brett)
#### Post date: [November 30, 2014, 11:14pm UTC](https://qa.fmod.com/t/how-do-i-fade-in-and-fade-out-a-channel-with-stop/11738/1 "2014-11-30T23:14:44Z")

</div>

How can I fade in a sound when it starts playing, and fade out a sound when I want it to stop?

---

<div class="post-metadata">

### Author: ![brett](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/brett/32/261_2.png) [@brett](https://qa.fmod.com/u/brett)
#### Post date: [November 30, 2014, 11:22pm UTC](https://qa.fmod.com/t/how-do-i-fade-in-and-fade-out-a-channel-with-stop/11738/2 "2014-11-30T23:22:23Z")

</div>

To do a 5 second fade in

After a playSound command, use the following code. For best results start the sound paused.

```
unsigned long long dspclock;
FMOD::System *sys;
int rate;

result = chan->getSystemObject(&sys); // OPTIONAL : Get System object
ERRCHECK(result);
result = sys->getSoftwareFormat(&rate, 0, 0); // Get mixer rate
ERRCHECK(result);

result = chan->getDSPClock(0, &dspclock); // Get the reference clock, which is the parent channel group
ERRCHECK(result);
result = chan->addFadePoint(dspclock, 0.0f); // Add a fade point at 'now' with full volume.
ERRCHECK(result);
result = chan->addFadePoint(dspclock + (rate * 5), 1.0f); // Add a fade point 5 seconds later at 0 volume.
ERRCHECK(result);

```

Now unpause the sound with chan-\>setPaused(false);

To do a 5 second fade out (and stop)

```
unsigned long long dspclock;
FMOD::System *sys;
int rate;

result = chan->getSystemObject(&sys); // OPTIONAL : Get System object
ERRCHECK(result);
result = sys->getSoftwareFormat(&rate, 0, 0); // Get mixer rate
ERRCHECK(result);

result = chan->getDSPClock(0, &dspclock); // Get the reference clock, which is the parent channel group
ERRCHECK(result);
result = chan->addFadePoint(dspclock, 1.0f); // Add a fade point at 'now' with full volume.
ERRCHECK(result);
result = chan->addFadePoint(dspclock + (rate * 5), 0.0f); // Add a fade point 5 seconds later at 0 volume.
ERRCHECK(result);
result = chan->setDelay(0, dspclock + (rate * 5), true); // Add a delayed stop command at 5 seconds ('stopchannels = true')
ERRCHECK(result);
```

---

<div class="post-metadata">

### Author: ![punker761](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/punker761/32/19_2.png) [@punker761](https://qa.fmod.com/u/punker761)
#### Post date: [December 1, 2014, 7:07pm UTC](https://qa.fmod.com/t/how-do-i-fade-in-and-fade-out-a-channel-with-stop/11738/3 "2014-12-01T19:07:49Z")

</div>

> [@](#):
>
> To do a 5 second fade in
> 
> After a playSound command, use the following code. For best results start the sound paused.
> 
> ```
> unsigned long long dspclock;
> FMOD::System *sys;
> int rate;
> 
> result = chan->getSystemObject(&sys); // OPTIONAL : Get System object
> ERRCHECK(result);
> result = sys->getSoftwareFormat(&rate, 0, 0); // Get mixer rate
> ERRCHECK(result);
> 
> result = chan->getDSPClock(0, &dspclock); // Get the reference clock, which is the parent channel group
> ERRCHECK(result);
> result = chan->addFadePoint(dspclock, 0.0f); // Add a fade point at 'now' with full volume.
> ERRCHECK(result);
> result = chan->addFadePoint(dspclock + (rate * 5), 1.0f); // Add a fade point 5 seconds later at 0 volume.
> ERRCHECK(result);
> 
> ```
> 
> Now unpause the sound with chan-\>setPaused(false);
> 
> To do a 5 second fade out (and stop)
> 
> ```
> unsigned long long dspclock;
> FMOD::System *sys;
> int rate;
> 
> result = chan->getSystemObject(&sys); // OPTIONAL : Get System object
> ERRCHECK(result);
> result = sys->getSoftwareFormat(&rate, 0, 0); // Get mixer rate
> ERRCHECK(result);
> 
> result = chan->getDSPClock(0, &dspclock); // Get the reference clock, which is the parent channel group
> ERRCHECK(result);
> result = chan->addFadePoint(dspclock, 1.0f); // Add a fade point at 'now' with full volume.
> ERRCHECK(result);
> result = chan->addFadePoint(dspclock + (rate * 5), 0.0f); // Add a fade point 5 seconds later at 0 volume.
> ERRCHECK(result);
> result = chan->setDelay(0, dspclock + (rate * 5), true); // Add a delayed stop command at 5 seconds ('stopchannels = true')
> ERRCHECK(result);
> 
> ```

how can i set the fade out at the end of the sound file?

---

<div class="post-metadata">

### Author: ![brett](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/brett/32/261_2.png) [@brett](https://qa.fmod.com/u/brett)
#### Post date: [December 3, 2014, 12:54am UTC](https://qa.fmod.com/t/how-do-i-fade-in-and-fade-out-a-channel-with-stop/11738/4 "2014-12-03T00:54:08Z")

</div>

You use the above fade out code, the only difference is, you have to work out in DSP clock time when your sound will end. You do that by getting the start time as above, add the length in samples of the sound divided by the default sample rate of the sound (ie Sound::getFormat), then multiplied by the output rate (System::getSoftwareFormat), subtract the rate\*delay\_in\_seconds in this case to start the fadeout early for the first fade point.  
You wouldn’t need the setDelay in this case as it will be stopping by itself.
