(

Server.default = s = Server.internal;

s.waitForBoot{


q = ();

q.directory = "/localvol/sound/share/audiosamples/koelnKalk/ampel";

q.directory = "/Users/till/Documents/koelnKalk/ampel";

q.filenames = ["2010-ampel-klack.aif", "2010-ampel-mechanic-loop.aif"];

q.buffers = q.filenames.collect{|name|

Buffer.readChannel(s, "%/%".format(q.directory, name), channels: [0, 1], action:{"file % read.\n".postf(name) })

};

q.blockChannels = (

yellow: 6,

lightGreen: 1,

blue: 2,

orange: 3,

red: 4

//green: 5

);


SynthDef(\ampel, {|klackBufnum = 0, mechaBufnum = 1, out = 0, freq = 0.5, inter = 4, mechaPitch = 1, mechaMaxDur = 1.1, isGreen = 0, greenAmp = 0.0125, greenPitch = 1, amp = 1, masterAmp = 1, mute = 0, masterMute = 0|

var klack, mecha, greenSignal, trig;

trig = Impulse.ar(freq);

//trig = DelayN.ar(trig, 1, freq.reciprocal * 0.99, -1); // add gate closing

klack = BufRd.ar(

2,

klackBufnum, 

EnvGen.ar(Env([0, BufFrames.ir(klackBufnum), 0], [BufSampleRate.kr(klackBufnum).reciprocal * (BufFrames.ir(klackBufnum)), 0]), gate: trig), 

0, // no loop 

inter

).sum;

klack = trig;

mecha = BufRd.ar(

2,

mechaBufnum, 

Phasor.ar(trig, (BufRateScale.kr(mechaBufnum)) * mechaPitch, 1000, BufFrames.kr(mechaBufnum)),

1, // loop 

inter

);

mecha = (mecha * EnvGen.ar(Env.linen(0, max(0, min(mechaMaxDur, freq.reciprocal - 0.05)), 0.01), gate: trig)).sum;

greenSignal = greenAmp * (SinOsc.ar([1200, 2400] * greenPitch, 0, [3, 5]).tanh * [0.5, 1] * LFPulse.ar(8, 0, 0.25).lag(0.0125)).sum * isGreen;

Out.ar(out, [klack, mecha * 0.1, greenSignal].sum  * amp * masterAmp * (1 - masterMute) * (1-mute));

}).memStore

}

)



(

{

q.ampeln = q.blockChannels.collect{|channel|

1.0.rand.wait;

Synth(\ampel, [

out: channel, 

klackBufnum: q.buffers.first, 

mechaBufnum: q.buffers.last, 

freq: rrand(0.995, 1.05)*0.5,

mechaPitch: 1, //rrand(0.9, 1.1), 

\mechaMaxDur, rrand(0.8, 0.9)

]);

}

}.fork;


Tdef(\greenLight, {

var greenAmpeln;

loop{

rrand(20, 30.0).wait;

greenAmpeln = q.ampeln.asArray.scramble[0..1];

greenAmpeln.do{|ampel|

0.2.rand.wait;

ampel.set(\isGreen, 1);

};

rrand(5, 10).wait;

greenAmpeln.do{|ampel|

0.2.rand.wait;

ampel.set(\isGreen, 0);

};

}}).play

)


/////////////////////////////////////////////


// q.buffers.last.play


x = Synth(\ampel, [klackBufnum: q.buffers.first, mechaBufnum: q.buffers.last, freq: 0.25])


x.set(\freq, 1.1)


x.set(\isGreen, 1);

x.set(\isGreen, 0);

x.set(\greenPitch, 3);

x.set(\greenPitch, 1.01);



x.set(\mechaPitch, 4, \mechaMaxDur, 0.2)

x.set(\mechaPitch, 0.5, \mechaMaxDur, 0.2)

x.set(\mechaPitch, 1, \mechaMaxDur, 1.1)

x.set(\mechaPitch, 0.9, \mechaMaxDur, 0.2)


x.free




///////////////////////////////////////

// Midi Mixer


MIDIClient.init;

MIDIIn.connect;


(

MIDIIn.control = {|port, chan, ctlNum, val| 

[chan, ctlNum, val].postln;

(ctlNum == 7).if{ // fader

(chan == 8).if{ // master (right-most)

q.ampeln.do{|ampel|

ampel.set(\masterAmp, val.linlin(0, 127, 0, 1))

}

};


(chan < 8).if{ // red (left-most)

[chan, val].postln;

q.ampeln[q.blockChannels.findKeyForValue(chan)].set(\amp, val.linlin(0, 127, 0, 1))

};

};

(ctlNum == 17).if{ // lower button -> mute

(chan == 8).if{ // master (right-most)

q.ampeln.do{|ampeln|

ampeln.set(\masterMute, val.linlin(0, 127, 0, 1))

}

};


(chan < 8).if{ // red (1- rightmost)

q.ampeln[q.blockChannels.findKeyForValue(chan)].set(\mute, val.linlin(0, 127, 0, 1))

};

};

}

)