Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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.

share|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
add comment

1 Answer 1

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|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
add comment

Your Answer

 
discard

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.