Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've written some code to implement simple cross fading between 2 channels.

It works fine but the code is ugly and riddled with if else statements so I'd like to try and express the function a more cleanly. I'm curious to discover a mathematical function to do this.

My slider goes from 0 -100. It has a "dead" range of 45 - 55 where no changes happen, so if the slider is anywhere from 45 - 55 both channels have a volume of 1.

This means that each channel has a range of 45.

Here is my code to illustrate futher.

if(value <45)

  // fading out b
  aChannel= 1;
  bChannel =value/45;

}else if(value>55)
  // fading out a
 // fading nothing

I'm not entirely sure what family of mathemathics this problem would belong to so I've added algebra as a tag. If this is innappropiate please suggest a better tag.

share|cite|improve this question
Shouldn't the statement be value>55 in the second if? – Raskolnikov Mar 27 '12 at 10:21
yep. that was a type-o. fixed now – dubbeat Mar 27 '12 at 10:24
up vote 1 down vote accepted

How about:

assert( 0 >= value && value <=100 ) ;
bChannel = min( 1, value/45 ) ;
aChannel = min( 1, (100-value)/45 ) ;
share|cite|improve this answer
The way he defined it in his OP would require min rather than max. – Raskolnikov Mar 27 '12 at 10:35
yes. that is right. made the edit. – Tpofofn Mar 27 '12 at 10:36
thats a much tidier, nicer expression. Thanks – dubbeat Mar 27 '12 at 10:44

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.