Could you improve my code? Swapping in algorithms from an array

Hi,
I’m building a sketch that uses different algorithms to generate audio. I have one knob swap in different algorithms. Ideally, I’d like to store dozens of them…
This snipped of code works fine, but seems a bit inelegant - it feels like I should be able to store a bunch of these lines in an array, and pull them in with a number:

if (knob == 0){
PORTD = t * ((t>>12|t>>8)&63&t>>4) ; // Algorithm here
t++; 
}
else if (knob == 1){
PORTD = t*((("38947237")[t>>13&3])^(("2414144")[t>>8&("5685")[t>>15&4]]))
; 
t++;
 } 
 
else if (knob == 2){
PORTD = ((((t%4)/10)+((t%3)/10)+((t%8)/10)+((t%9)/3)+((t%11)/10)+((t%13)/10)+((t%17)/10)+((t%23)/10)+((t%33)/10)+((t%12)/10))*5)+(((((t>>5)&((t>>((t&4)?(t%3):(t%2)))*(t>>5|t>>3|t>>7|t>>13)))>>(t<<((t&12)?t>>(t%4):t>>8)))%256)/2)
; 
t++;
} 
 
else if (knob == 3){
PORTD =  t*(((t>>11)^((t>>17)-1)^((t>>23)-3)^((t>>27)-7)^1)%13)
; 
t++;
}

… and so on.

PORTD = t*((("38947237")[t>>13&3])^(("2414144")[t>>8&("5685")[t>>15&4]]))

Please explain what you think this statement is doing. I'm amazed this compiled (if, indeed it did).

Looks like a good candidate for switch:case.

switch (knob){
case 0:
PORTD = .....
break;
case 1:
PORTD = ....
break;
:
:
case n:
PORTD = .....
break;
} // end switch(knob)

Will be more responsive as you will not have to evaluate all the if()s to get to the one at the end. As for using an array, wouldn't you have rewrite each PORTD = statement to all have the same # of shifts & and subtractions & (t%/x) things, then fill the array with the numbers to replace the variable parts, even if a lot of was 0 to so it didn't affect the actually used parts? For an example, looking at knob =2 vs knob =3. knob =2 would need more than 30 factors, while knob = 3 only uses 9 or 10, and the math operations being done between them aren't even the same. I think you're better off keeping what you have for PORTD = statements.

PaulS: PORTD = t*((("38947237")[t>>13&3])^(("2414144")[t>>8&("5685")[t>>15&4]]))

Please explain what you think this statement is doing. I'm amazed this compiled (if, indeed it did).

Hi, yes, it looks mad but does compile, although I haven't yet spent any time optimising/rationalising the numbers for this application.

When you output it to - in this case - an 8-bit DAC (or you can use a PWM pin), while stepping through values of t, it produces sound. Perhaps 'music' is stretching it. The rest of the code is an interrupt routine using Timer1 to trigger the portd calls.

You can play with the algorithms here: http://wurstcaptures.untergrund.net/music/

This video explains more:

http://www.youtube.com/watch?v=GtQdIYUtAHg

CrossRoads:
Looks like a good candidate for switch:case.

Thank you - that looks like a more sensible approach, and performance is important with this application. What I was thinking about was some way to persuade the code to execute the code in a string, but I can’t imagine that would work.