0
$\begingroup$

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
  bChannel=1;
  value=100-value;
  aChannel=value/45;
}else
{
 // fading nothing
 aChannel=1;
 bChannel=1;
}

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.

  • 0
    Shouldn't the statement be value>55 in the second if?2012-03-27
  • 0
    yep. that was a type-o. fixed now2012-03-27

1 Answers 1

1

How about:

assert( 0 >= value && value <=100 ) ;
bChannel = min( 1, value/45 ) ;
aChannel = min( 1, (100-value)/45 ) ;
  • 0
    The way he defined it in his OP would require min rather than max.2012-03-27
  • 0
    yes. that is right. made the edit.2012-03-27
  • 0
    thats a much tidier, nicer expression. Thanks2012-03-27