[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] A bug in switch/case?



I changed my code and now this below, works:


       inputdic = IdentityDictionary [
           'white'         -> {WhiteNoise.ar(inmul, inadd);},
            'pink'         -> {PinkNoise.ar(inmul, inadd);},
             'brown'        -> {BrownNoise.ar(inmul, inadd);},
             'gray'        -> {GrayNoise.ar(inmul, inadd);},
             'clip'        -> {ClipNoise.ar(inmul, inadd);},
             'crackle'        -> {Crackle.ar(param, inmul, inadd);},
             'dust'        -> {Dust.ar(param, inmul, inadd);},
             'dust2'        -> {Dust2.ar(param, inmul, inadd);},
             'lfclip'        -> {LFClipNoise.ar(param, inmul, inadd);},
             'lfdclip'        -> {LFDClipNoise.ar(param, inmul, inadd);},
             'lfdn0'        -> {LFDNoise0.ar(param, inmul, inadd);},
             'lfdn1'        -> {LFDNoise1.ar(param, inmul, inadd);},
             'lfdn3'        -> {LFDNoise3.ar(param, inmul, inadd);},
             'lfn0'        -> {LFNoise0.ar(param, inmul, inadd);},
             'lfn1'        -> {LFNoise1.ar(param, inmul, inadd);},
             'lfn2'        -> {LFNoise2.ar(param, inmul, inadd);}
       ];

       in = inputdic[input].value;



Instead of putting the equality {in = ...} in the dictionary, I put it after. I think this is better. Anyway, it works... If IdentityDictionary is faster, I guess I'll use it instead of switch, then...

Thanks for your help.

Ph.




Alberto de Campo wrote:

Well, the simplest use is:

    // define your cases once
d = IdentityDictionary [
    'white'    -> {in = WhiteNoise.ar(inmul, inadd);},
    'pink'    -> {in = PinkNoise.ar(inmul, inadd);},
    'brown'    -> {in = BrownNoise.ar(inmul, inadd);},
    'gray'    -> {in = GrayNoise.ar(inmul, inadd);},
    'clip'    -> {in = ClipNoise.ar(inmul, inadd);},
    'crackle' -> {in = Crackle.ar(param, inmul, inadd);},
    'dust'    -> {in = Dust.ar(param, inmul, inadd);}
];
    // whatever other code
...

100.do {    // maybe inside a loop,

        // you generate your case key somehow.
        // can be as complicated as you want:
    input = ['white', 'pink', 'brown'].choose;

        // and look up your case.
    d[input].value;

};

I am not sure I understand what you want beyond that.

best, adc

Thanks Alberto, for your answer. But can we do anything with IdentityDictionary?

For example, the code below works, with switch:

       switch (input,
           "white",        {in = WhiteNoise.ar(inmul, inadd);},
           "pink",        {in = PinkNoise.ar(inmul, inadd);},
           "brown",        {in = BrownNoise.ar(inmul, inadd);},
           "gray",        {in = GrayNoise.ar(inmul, inadd);},
           "clip",        {in = ClipNoise.ar(inmul, inadd);},
           "crackle",    {in = Crackle.ar(param, inmul, inadd);},
           "dust",        {in = Dust.ar(param, inmul, inadd);}
       );

Then, I dug into the SC list, and found this code written by hjh:

c = IdentityDictionary[
 { x == 1 } -> { "case 1".postln },
 { x == 2 } -> { "case 2".postln },
 { x == 3 } -> { "case 3".postln }
];


functions as keys make little sense to me;
but I don't know the context this comes from.


I tried to write this:

       inputdic = IdentityDictionary [
{input == "white"} -> {in = WhiteNoise.ar(inmul, inadd);}, {input == "pink"} -> {in = PinkNoise.ar(inmul, inadd);}, {input == "brown"} -> {in = BrownNoise.ar(inmul, inadd);}, {input == "gray"} -> {in = GrayNoise.ar(inmul, inadd);}, {input == "clip"} -> {in = ClipNoise.ar(inmul, inadd);}, {input == "crackle"} -> {in = Crackle.ar(param, inmul, inadd);}, {input == "dust"} -> {in = Dust.ar(param, inmul, inadd);}
      ];

But, it doesn't seem to work. I'm just wondering whether my code is wrong or there is an impossibility of doing this with IdentityDictionary. I should add that input is not a variable but an argument of a proto-synthdef function.


the point is, if you do   dict.at(input);
you _are_ already comparing key == input to find the key,
so there is no need to wrap a function around it.