1. punchlist draft

    //Karst version 33-CONTACT
    //code still not finalized but getting cleaned up
    //things to do:
    //add envelope with gate to decrease clipping -DONE
    //setup MOTU inputs for wall scraping with exoskelton -DONE
    //more notes in the code to help with version tracking in the final 24 hours
    //add notes about site layout where pertinent -
    //test different sensor behaivor with exoskelton - DONE
    //transition to normalized audio files - DONE
    //add stops to sensor behaivor: CmdPeriod.run; and a-k.stop inside fork -DONE

    //SERVER
    (
    Server.default = s = Server.internal.boot;
    s.waitForBoot({
    });
    )

    //ARDUINO
    (
    SerialPort.listDevices;
    d = SerialPort.devices[2];
    f = SCPyduino.new(d);  //declares f an SCPyduino object
    )

    //KARST MAIN CODE BLOCK
    (
    //VARIABLES
    //synths
    var synth, synth2;
    //functions
    var func, pfunc, ffunc;
    //alpha &
    var omega = 1;
    //sample variables
    var transform, death, machine, noisefloor;
    //crap imma get rid of
    var syn1, syn2, syn3, syn4, syn5, syn6;
    var breath, chant;
    //more joshparametrical magic right here
    var flex1, flex2, controlflex1, controlflex2;
    var scrape;



    controlflex1 = CtkControl.play;
    controlflex2 = CtkControl.play;




    //SAMPLES
    //Source = Rilke: Orpheus Sonnet pt2 10 “The machine threatens all we have gained, so long as it dare become the tyrant of spirit, rather than its servant.”
    machine = CtkBuffer.playbuf(“sounds/Karst_Machine.aif”).load;

    //Source = Rilke: Orpheus Sonnet pt2 11 “Many quietly acceptable methods of death have come to pass since onward conquering man first laid claim to the hunter’s arts;….”
    death = CtkBuffer.playbuf(“sounds/Karst_Death.aif”).load;

    //Source = Rilke: Orpheus Sonnet pt2 12 “Aspire to transform, O enraptured be by the fire wherein something elusive flames with brazen tidings of change;….”
    transform = CtkBuffer.playbuf(“sounds/Karst_Transform.aif”).load;

    //Source = Site Noisefloor, May 29, 2010
    noisefloor = CtkBuffer.playbuf(“sounds/Karst_Noisefloor.aif”).load;

    //testing samples
    breath = CtkBuffer.playbuf(“sounds/breath.aif”).load;
    chant = CtkBuffer.playbuf(“sounds/chant.aif”).load;

    CmdPeriod.doOnce({
        machine.free;
        death.free;
        transform.free;
        noisefloor.free;
        breath.free;
        chant.free;
        a.stop;
    b.stop;
    c.stop;
    g.stop;
    h.stop;
    k.stop;
    Server.killAll;
    “I’m freeing the buffers!”.postln;
        });
       
    //SYNTHDEFS
    // trigger synth
    synth = CtkSynthDef(\synth, {arg imp, begin, out = 0, bufnum = 0, pan = 0, amp =0.5, deviation = 0, pch = 1;
        var trig, sig, sampstosecs, devAmt, outsig, env;
        env = EnvGen.kr(Env.new([0, 1,0], [0.1,0.1], \linear, 1));
        trig = Impulse.ar(imp);
        sampstosecs = (SampleRate.ir * BufRateScale.kr(bufnum));
        devAmt = (deviation * LFNoise1.kr( imp) * sampstosecs);
        sig = PlayBuf.ar(1, bufnum, BufRateScale.kr(bufnum) * pch, trig, (begin * sampstosecs) + devAmt, 0);
        outsig = Pan2.ar(sig*amp, pan);
        Out.ar(out, [outsig[0], DelayN.ar(outsig[1], 0.002, 0.002)]);
    });

    scrape = CtkSynthDef(\scrape, { arg bus;
        var signal;
        signal = SoundIn.ar(bus);
        Out.ar(bus,signal*1);
    });

    scrape.new(0,nil).bus_(0).play;
    scrape.new(0,nil).bus_(1).play;

    //FUNCTIONS

    //FUNC :: for use with impulse triggers
    func = {arg duration, amp, rhythms,  imps, begs, pans, dev;
    var start, now, index, dur, imp, beg, rhy, pan1;
    Task({
        start = Main.elapsedTime;
        now = 0;
        index = 0;
        imp = imps;
        while({
        now < duration;
        }, {
        now = Main.elapsedTime - start;
        rhy = rhythms[index%rhythms.size];
       
        imp = imps[index%imps.size];
        beg = begs[index%begs.size];
        pan1 =pans[index%pans.size];
        synth.new(0, 10).bufnum_(noisefloor.bufnum).imp_(4).begin_(beg).pan_(pan1).amp_(amp).deviation_(dev)
            .pch_(controlflex1).play;
        rhy.wait;
        index = index + 1;
        })
    }).play
    };

    //PFUNC :: make my func the pfunc
    pfunc = {arg duration, amp, rhythms, durs, imps, begs, pans, dev;
    var start, now, index, dur, imp, beg, rhy, pan1;
    Task({
        start = Main.elapsedTime;
        now = 0;
        index = 0;
        imp = imps;
        while({
        now < duration;
        }, {
        now = Main.elapsedTime - start;
        rhy = rhythms[index%rhythms.size];
        dur = durs[index%durs.size];
        imp = imps[index%imps.size];
        beg = begs[index%begs.size];
        pan1 =pans[index%pans.size];
        synth.new(0, 10).bufnum_(noisefloor.bufnum).imp_(4).begin_(beg).pan_(pan1).amp_(amp).deviation_(dev).play;
        (rhy + dur).wait;
        index = index + 1;
        })
    }).play
    };

    //FFUNC
    ffunc = {arg duration, amp, rhythms, durs, imps, begs, pans, dev;
    var start, now, index, dur, imp, beg, rhy, pan1;
    Task({
        start = Main.elapsedTime;
        now = 0;
        index = 0;
        imp = imps;
        while({
        now < duration;
        }, {
        now = Main.elapsedTime - start;
        rhy = rhythms[index%rhythms.size];
        dur = durs[index%durs.size];
        imp = imps[index%imps.size];
        beg = begs[index%begs.size];
        pan1 =pans[index%pans.size];
        synth.new(0, 10).bufnum_(machine.bufnum).imp_(4).begin_(beg).pan_(pan1).amp_(amp).deviation_(dev).play;
        (rhy + dur).wait;
        index = index + 1;
        })
    }).play
    };

    /*
    //synth instantiations :: aka crap imma get rid of
    syn1 = synth.new(0, nil).bufnum_(noisefloor.bufnum).imp_(4).begin_(0).pan_(1).amp_(4).deviation_(2).play;
    syn2 = synth.new(0, nil).bufnum_(noisefloor.bufnum).imp_(4).begin_(110).pan_(-1).amp_(4).deviation_(2).play;
    syn3 = synth.new(0, nil).bufnum_(noisefloor.bufnum).imp_(4).begin_(220).pan_(-1).amp_(4).deviation_(2).play;
    syn4 = synth.new(0, nil).bufnum_(noisefloor.bufnum).imp_(4).begin_(330).pan_(-1).amp_(4).deviation_(2).play;
    syn5 = synth.new(0, nil).bufnum_(noisefloor.bufnum).imp_(4).begin_(440).pan_(-1).amp_(4).deviation_(2).play;
    syn6 = synth.new(0, nil).bufnum_(noisefloor.bufnum).imp_(4).begin_(550).pan_(-1).amp_(4).deviation_(2).play;
    */

    //ARDUINO

    //Arduino: Sensors
    //Left Piezo     “a” “u” lop
    f.analog[0].active_(1);
    //Left Flex     “b” “v” lox
    f.analog[1].active_(1);
    //Left Force     “c” “w” lor
    f.analog[2].active_(1);

    //Right Piezo “g” “x” rop
    f.analog[3].active_(1);
    //Right Flex     “h” “y” rox
    f.analog[4].active_(1);
    //Right Force “k” “z” ror
    f.analog[5].active_(1);


    //j = CtkSynthDef(\grain, {arg freq, dur;
    //    Out.ar(0, SinOsc.ar(freq, 0, 0.1) * EnvGen.kr(Env([0, 1, 0], [0.5, 0.5], \sin), timeScale: dur, doneAction: 2))
    //    });


    j = synth2 = CtkSynthDef(\synth2, {arg freq, dur, imp, begin, out = 0, bufnum = 0, pan = 0, amp =0.5, deviation = 0;
        var trig, sig, sampstosecs, devAmt, outsig;
        trig = Impulse.ar(imp);
        sampstosecs = (SampleRate.ir * BufRateScale.kr(bufnum));
        devAmt = (deviation * LFNoise1.kr( imp) * sampstosecs);
        sig = PlayBuf.ar(1, bufnum, BufRateScale.kr(bufnum), trig, (begin * sampstosecs) + devAmt, 0);
        outsig = Pan2.ar(sig*amp, pan);
        Out.ar(out, [outsig[0], DelayN.ar(outsig[1], 0.002, 0.002)]);
    });

    l = CtkSynthDef(\sawgrain, {arg freq, dur;
        Out.ar(0, LFSaw.ar(freq, 0.05) * EnvGen.kr(Env([0, 1, 0], [0.5, 0.5], \sin), timeScale: dur, doneAction: 2))
        });
       
    k = {arg noteObj;
        Task({
            20.do({
                noteObj.new.freq_(1000 -  v).dur_(2.5).play;
                0.05.wait;
                })
            }).play;
        };
       
    /*   
    //Left Piezo
    a = fork{
            loop{
                var lop;
                f.iterate;
                u= f.analog[0].value;
                “u:”.post;
                u.postln;
                lop = (1000-u)/100;
                “lop: imp: “.post;
                lop.postln;
                syn1.imp_(lop);
                0.001.wait;
            }
    };   
    */   
       
       
    m = 0;
    //Left Flex
    //3030: this is working, starts inactive, is trigger at first bend and cycles through noisefloor functions, the first of which is pitch affected by sensor value
    b = fork{
        t = Main.elapsedTime;
            loop{
                var lox;
                f.iterate;
                v= f.analog[1].value;
                flex1 = v;
                //controlflex1.set((flex1 / 500) - 0.5);
                controlflex1.set((flex1 / 250) - 0.5);
                “v:”.post;
                v.postln;
                (v < 250).if({
                    (Main.elapsedTime > (t + 1)).if({
                        t = Main.elapsedTime;
                        m = m + 1;
                        m.postln;
                        [       
                       
                        //FUNC:::: duration, amp, rhythms, durs, imps, begs, pans, dev;
                        //duration = duration of the gesture, but may default to 10 seconds
                        //amp = amp
                        //rhythms = space between attacks in the gesture
                        //imps = how ofter the playbuff retriggers; low values = larger chunks o’ audio, inverse relationship means 0.1 = 10 seconds, 10 = .1 seconds
                        //begs = pickup spot
                        //pans = pan
                        //dev    = deviation or randomness on the location of the pickup spot   
                        //duration should be longer than rhytms for more than one note       
                            {func.value(5.5, 0.5, [3.25],[3],[4],[0, 1, -1],[2]).play},
                            {pfunc.value(20, 0.5, [3.25],[3],[4,5,6,7,8,9],[0, 1, -1],[2]).play},
                            //{ffunc.value(2, 0.5, [0.25],[3],[3],[4,5,6,7,8,9],[0, 1, -1],[2]).play},
                            //{pfunc.value(0.1, 0.5, [0.25],[3],[3],[4,5,6,7,8,9],[0, 1, -1],[2]).play}
                        ].wrapAt(m).value;
    //
    //
    //                    (m.odd).if({
    //                        func.value(0.5, 0.5, [0.25],[3],[3],[4],[0, 1, -1],[2]).play;
    //                        }, {
    //                        pfunc.value(2, 0.5, [0.25],[3],[3],[4,5,6,7,8,9],[0, 1, -1],[2]).play;
    //                        },
    //                        {
    //                        pfunc.value(0.1, 0.5, [0.25],[3],[3],[4,5,6,7,8,9],[0, 1, -1],[2]).play;
    //                        });
    //
    //                    Task({
    //                        20.do({
    //                            noteObj.new.freq_(1000 -  v).dur_(2.5).play;
    //                            0.05.wait;
    //                            })
    //                        }).play;
                        })
                    });
                   
                lox = 250-v;
    //            “lox: imp: “.post;
    //            lox.postln;
                //syn2.imp_(lox);
    //            if((v == 40) ,{arg inc = 0;
    //            inc = inc+1;
    //            func.value(0, 15, 0.5, [0.25],[3],[30],[4,5,6,7,8,9],[0, 1, -1],[4]).play;
    //                        },{
    //           
    //            }           
    //            );           
                0.001.wait;
            }
    };



    /*


    //Left Force
    c = fork{
            loop{
                var lor;
                f.iterate;
                w= f.analog[2].value;
                “w:”.post;
                w.postln;
                lor = 100-w;
                “lor: imp: “.post;
                lor.postln;
                syn3.imp_(lor);
                syn3.pan_(lor-80);
                syn3.imp_(lor+0.1);
                syn3.pan_(lor-90);
                0.001.wait;
           
            }
    };

    */

    //Right Piezo
    //Right Piezo has a yellow wire conencting to the breadboard in prototype layout
    g = fork{
            loop{
                var rop;
                f.iterate;
                x= f.analog[3].value;
                “x:”.post;
                x.postln;
                rop = x-350/100;
                “imp “.post;
                rop.postln;
                syn4.imp_(rop);
               
                0.001.wait;
            }
    };

    n = 0;
    //Right Flex
    h = fork{
        t = Main.elapsedTime;
            loop{
                var lox;
                f.iterate;
                y= f.analog[4].value;
                flex1 = y;
                controlflex2.set((flex1 / 1000) - 0.5);
                “y:”.post;
                y.postln;
                (y < 300).if({
                    (Main.elapsedTime > (t + 1)).if({
                        t = Main.elapsedTime;
                        n = n + 1;
                        n.postln;
                        [                       
                            {pfunc.value(0.5, 0.5, [0.25],[3],[3],[4],[0, 1, -1],[2]).play},   
                            {ffunc.value(2, 0.5, [0.25],[3],[3],[4,5,6,7,8,9],[0, 1, -1],[2]).play},   
                            {pfunc.value(0.5, 0.5, [0.25],[30],[3],[4],[0, 1, -1],[2]).play},   
                            {ffunc.value(2, 0.5, [0.25],[1],[5],[4,5,6,7,8,9],[0, 1, -1],[2]).play},   
                            {pfunc.value(0.5, 0.5, [0.25],[5],[1],[4],[0, 1, -1],[2]).play},   
                            {ffunc.value(2, 0.5, [0.25],[3],[3],[4,5,6,7,8,9],[0, 1, -1],[2]).play}
                        ].wrapAt(m).value;

                        })
                    });
                   
                lox = 250-y;
               
                0.001.wait;
            }
    };

    //Right Force
    k= fork{
            loop{
                var ror;
                f.iterate;
                z =0;
                z = f.analog[5].value;
                “z:”.post;
                z.postln;
                (z >350).if({
                CmdPeriod.run;
                });
                //ror = 100-z;
                //syn6.begin_(ror);
                “beg:”.post;
                //ror.postln;
                0.001.wait;
            }
    };

      )
    //cease reading values
    //(
    //a.stop;
    //b.stop;
    //c.stop;
    //g.stop;
    //h.stop;
    //k.stop;
    //)
    //close the serial port
    (
    f.close;
    )