# CrossFader for a mixer

**URL:** <https://forum.juce.com/t/crossfader-for-a-mixer/6587>\
**Category:** General JUCE discussion\
**Created:** [March 18, 2011, 12:36am UTC](https://forum.juce.com/t/crossfader-for-a-mixer/6587 "2011-03-18T00:36:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sharky101](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@Sharky101](https://forum.juce.com/u/Sharky101)\
**Post date:** [March 18, 2011, 12:36am UTC](https://forum.juce.com/t/crossfader-for-a-mixer/6587/1 "2011-03-18T00:36:13Z")

</div>

Hi all,

I am trying to use a horizontal slider as a cross fader for 2 file players.  
The end result i’m hoping for is a basic mixer.  
I have pitch control and indipendent gains but wanted to cross fade between them.

The slider range is 0 - 2 at 0.1 increments.  
From Right to left is no problem as when the slider passes the middle it goes from 1 to 0.  
How can I make it so from left to right when the slider passes the middle (1) and gets to the end (2) it actually goes from 1 - 0???  
Here is the code I have at the moment

```
	if(crossFader->getValue()<0.99)
	{
		
		std::cout<< "slider to the left" << "\n";
		leftPlayer->changeGain(1); // set gain at 1 (playing)
		rightPlayer->changeGain(crossFader->getValue()); // attenuate to 0
		
	}
	else if (crossFader->getValue()>1)
	{

		std::cout<< "slider to the right" << "\n";
		
		rightPlayer->changeGain(1); // set gain at 1 (playing)
	//	leftPlayer->changeGain(crossFader->getValue());
		
	}
	else
	{
		leftPlayer->changeGain(1);
		rightPlayer->changeGain(1);
	}
```

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [March 18, 2011, 1:20am UTC](https://forum.juce.com/t/crossfader-for-a-mixer/6587/2 "2011-03-18T01:20:38Z")

</div>

Why don’t you just set your slider range to 0-1 ( and probably have a smaller increment size or some smoothing, preferably both to avoid clicks) then just multiply the right player by the value and the left by 1 minus the value?

Any curve shaping you want to do can then be just applied to the slider value before you set any gains.

---

<div class="post-metadata">

**Author:** ![billythekid](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/billythekid/32/88_2.png) [@billythekid](https://forum.juce.com/u/billythekid)\
**Post date:** [March 18, 2011, 1:52pm UTC](https://forum.juce.com/t/crossfader-for-a-mixer/6587/3 "2011-03-18T13:52:27Z")

</div>

> [@dave96](#):
>
> …then just multiply the right player by the value and the left by 1 minus the value?

…or

source1_pow(value,2)  
source2_sqrt(1 - pow(value,2))  
output = source1+source2

to maintain uniform power across the fade for uncorrelated sources :idea:

---

<div class="post-metadata">

**Author:** ![Sharky101](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@Sharky101](https://forum.juce.com/u/Sharky101)\
**Post date:** [March 18, 2011, 2:07pm UTC](https://forum.juce.com/t/crossfader-for-a-mixer/6587/4 "2011-03-18T14:07:59Z")

</div>

awesome thanks guys,  
I have a linear fade at the moment just to get it working. Will use your advice to refine it 🙂

---

<div class="post-metadata">

**Author:** ![billythekid](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/billythekid/32/88_2.png) [@billythekid](https://forum.juce.com/u/billythekid)\
**Post date:** [March 21, 2011, 1:34pm UTC](https://forum.juce.com/t/crossfader-for-a-mixer/6587/5 "2011-03-21T13:34:32Z")

</div>

Sorry, I made a subtle balls-up. The following line . . .

[quote=“billythekid”]  
source1\*pow(value,2)  
:[/quote]

… should simply be:

```auto

```
