/* -- trans.fail / XENO TOPIC . NETWORK  -- */

s.boot
s.plotTree;
s.meter;

{PinkNoise.ar!2}.play

// init > + load all libs/ & smp/
this.executeFile(PathName.new(thisProcess.nowExecutingPath).pathOnly +/+ "xinit.scd")

// (re)launch busses
~launchBusses.()

// test video plate
~sOsc.("testPictureToggle", 1);
~sOsc.("testPictureToggle", 0);


( // INTRO TITLES
fork {
	// strobe warning
	"... strobe warning title".postln;
	~oscTitles0a.value;
	8.wait;

	~sOsc.("drawPlatesToggle", 0);
	2.wait;

	// title
	"... title: trans.fail".postln;
//
	~oscTitles0b.value;
	6.wait;

	~sOsc.("drawPlatesToggle", 0);
	3.wait;

	// ZERO TITLE1
	~oscTitles1.value;
	5.wait;

	~sOsc.("drawPlatesToggle", 0);

	// init for zero
	~sOsc.("drawPlatesimgAbank", 3);
	~sOsc.("drawPlatesimgBbank", 3);
	~sOsc.("drawPlatesimgCbank", 3);
	~sOsc.("drawPlatesimgBblendMode", 2);
	~sOsc.("drawPlatesimgCblendMode", 3);
	~sOsc.("drawImageBlenderToggle", 0);

	~platesSpeedRandRange = 0;

	"--- ready for next.".postln;
}
)

// ZERO /////////////////////////////////////////////////////////////////////////////////////
(
~qebsts = [(11..17),(23..36)].lace.copyRange(0,11) * 0.01;

Pbindef(\playBufP, *[ instrument: \playBuf,
	//Pbindef(\playBufP).clear
	bufnum:	~getSmp.("qebrus_machines_snip1"),
	dur: Pn(Pshuf([1/2, 1/2, 1/4, Pn(1/8,2), 1/2],8) * 1/2, inf),
	legato:[0.9,0.1,0.2] * Pwhite(0.9,1),
	amp: Pwhite(0.5,0.6) * 0.6,
	rate: Pwhite(0.99,1),
	start: Pseq([
		Pn(0.1, 48*2),
		Pdup(24, Pseq(~qebsts)),
		Pn(0.1, 48*2),
		Pn(Pshuf( ~qebsts, 4),2),
		Pdup(12 * 4 * 2, Pseq(
			[3.1, 3.5, 1.2, 2.5, 1, 3.3]) )
		* Pn(Pshuf( ~qebsts, 4), 2 * 6),
		Pn(0.91, 48),
		Pn(Pshuf((0..11)+5*0.01+0.78,4),1),
		Pn(0.92, 48),
		Pn(Pshuf((0..11)+5*0.01+0.8,4),1),
		Pn(0.93, 48),
		Pdup(24, Pseq(~qebsts)),
		Pn(0.1, 48*2)
	], 1),

	speedrange: Pseq([Pseg([0,0,50,50,0], [64,120,120,60]),Pn(0)]),
	osc: Pfunc { |e|
		~sOsc.("drawPlatesToggle", 1);
		~sOsc.("drawPlatesimgAid", 45.rand);
		~sOsc.("drawPlatesimgBid", 45.rand);
		~sOsc.("drawPlatesimgCid", 45.rand);
		~sOsc.("drawPlatesTexAspeed", e.speedrange.rand);
		~sOsc.("drawPlatesTexBspeed",  e.speedrange.rand);
		~sOsc.("drawPlatesTexCspeed",  e.speedrange.rand);
		~sOsc.("drawPlatesTexAdirection", 127.rand);
		~sOsc.("drawPlatesTexBdirection", 127.rand);
		~sOsc.("drawPlatesTexCdirection", 127.rand);
	};
]).play(quant:4);


Pbindef(\playBufBleep, *[ instrument: \playBuf,
	bufnum: ~getSmp.("Bleeps007"),
	legato:1,
	dur:Pseq([
		Rest(64),
		Pseq([1/4,Pn(3,7)],inf)
	]),
	amp:0.3,
	rate: 1
]).play(quant:4);

Pbindef(\playBufBleep2, *[ instrument: \playBuf,
	bufnum: ~getSmp.("Bleeps015"),
	legato:Pwhite(0.01,0.1),
	dur:Pseq([
		Rest(64*2),
		Pseq([1/4,Pn(1.5,7)],inf) * 1/2
	]),
	amp:0.5,
	rate: 0.25
]).play(quant:4);

Pbindef(\playBufHat, *[ instrument: \playBuf,
	bufnum: ~getSmp.("Hats076"),
	legato:1,
	dur: Pseq([
		Rest(32*3),
		Pn(1/4),
	]),
	amp: 2 * Pseq([1, 1/5],inf) * Pwhite(0.3,1),
	rate: Pwhite(0.95,1.01),
	pan: 0
]).play(quant:4);


// kick
Pbindef(\playBufKickP, *[ instrument: \playBuf,
	bufnum:	[
		~getSmp.("Kicks062"),
		~getSmp.("Kicks026"),
	],
	sustain: 2,
	amp: Pwhite(0.8,1) * [0.8,Pwhite(0.1,0.3)],
	rate: [1,0.8],
	start: 0,
	pan: Pseq([-1,1],inf) / 4,
	dur:
	Pseq([
		Rest(32),
		Pxrand([
			Pshuf([ 1/2, 1/4, 1/8, 1/8],8),
			Pshuf([ 1,1/2, 1/4, 1/8, 1/8],4),
			Pshuf([ 1/4,1/4,1/4,1/8,1/8],8),
			Pshuf([ 1,1/4,1/4,1/4,1/8,1/8],4),
			Pshuf([ 1/2,1/4,1/4], 8),
			Pshuf([ 1,1/2,1/4,1/4], 4),
		],inf)
	])
]).play(quant:4);


Pbindef(\granPlayPzero, *[
	instrument: \granPlay,
	bufnum:	~getSmp.("qebrus_machines_snip1"),
	dur: 		16,
	legato: 	1,
	atk:		8,
	rls:		8,
	amp: 		Pseq([ Pseg([0,1],128,\sqr), Pn(1)]) * 0.18,
	width:		1,
	pitch: 		1, //[0.5,1],
	autoPosN: 	0.08,
	autoPosS: 	0.02,
	outBus: 	[~lpfFXBus1,~revFXbus1],
	//outBus: 	0,
	gpos: 		0.12, //Pdup(3, Pxrand([0.4,0.45,0.6,0.2],inf)),
]).play(quant:4);
);

//
// granulator? pad?

/*
Pbindef(\playBufBleep).play(quant:4);
Pbindef(\playBufBleep2).play(quant:4);
Pbindef(\playBufHat).play(quant:4);

~platesSpeedRandRange = 3;
~platesSpeedRandRange = 9;
~platesSpeedRandRange = 30;
~platesSpeedRandRange = 40;
~platesSpeedRandRange = 70;

Pbindef(\playBufP).stop;

(
// clear ZERO
Pbindef(\playBufBleep).clear;
Pbindef(\playBufBleep2).clear;
Pbindef(\playBufHat).clear;
Pbindef(\playBufKickP).clear;
Pbindef(\playBufP).clear;
)
*/
(
// stop ZERO
Pbindef(\playBufBleep).stop;
Pbindef(\playBufBleep2).stop;
Pbindef(\playBufHat).stop;
Pbindef(\playBufKickP).stop;
Pbindef(\granPlayPzero).stop
)

/////////////////////////////////////////////////////////////////////////////////////////
// INTERRUPT!
(
fork {
	~sOsc.("drawPlatesToggle", 0); // turns of ZERO video plate
	4.wait;
	~oscTitles2.value;
	5.wait;
	~sOsc.("drawPlatesToggle", 0);

    ~sOsc.("drawImageBlenderBank", 0 );
    ~sOsc.("drawImageBlenderID", 1);
    ~sOsc.("iblNum", 10); //{ iblNum = int(oscVal2) ; } // 6
    ~sOsc.("iblX", 74 ); //{ iblX = ((oscVal2-64) / 64.0) * width/2 ; } // 7  int( ((oscVal2-64) / 64.0) * width/2 ); }
    ~sOsc.("iblY", 84 ); //{ iblY = ((oscVal2-64) / 64.0) * width/2 ; } // 8
    ~sOsc.("iblWidth", 87); //{ iblWidth = oscVal2 * oscVal2 * 0.2 * norm(width, 0, width) ; } // 9  int(oscVal2 * oscVal2 * 0.05 * norm(width, 0, width));
    ~sOsc.("iblHeight", 70 ); //{ iblHeight = oscVal2 * oscVal2 * 0.1 * norm(height, 0, height) ; } // 10

    ~sOsc.("iblRot", 0 ); //{ iblRot =  radians(oscVal2 * 360 / 120) ; } // 11
    ~sOsc.("iblTexWidth", 64 ); //{ iblTexWidth = (oscVal2 - 64) * oscVal2 ; } // 18
    ~sOsc.("iblTexHeight", 64 ); //{ iblTexHeight = (oscVal2 - 64) * oscVal2 ; } // 19

    ~sOsc.("iblTexSpeedXfactor", 70 ); //{ iblTexSpeedXfactor = ((oscVal2-64) * abs(oscVal2-64) * 0.6) ; } // 22
    ~sOsc.("iblTexSpeedYfactor", 64 ); //{ iblTexSpeedYfactor = ((oscVal2-64) * abs(oscVal2-64) * 0.6) ; } // 23

    ~sOsc.("iblH", 64 ); //{ iblH = int(oscVal2) ; } // 24
    ~sOsc.("iblS", 127); //{ iblS = int(oscVal2) ; } // 25
    ~sOsc.("iblB", 127 ); //{ iblB = int(oscVal2) ; } // 26
    ~sOsc.("iblA", 40 ); //{ iblA = int(oscVal2) ; } // 27
    ~sOsc.("iblBflicker", 0 ); //{ iblBflicker = int(oscVal2) ; } // 28

    ~sOsc.("iblItX", 94 ); //{ iblItX = ((oscVal2-64) * abs(oscVal2-64) * 0.1) ; } // 29
    ~sOsc.("iblItY", 64 ); //{ iblItY = ((oscVal2-64) * abs(oscVal2-64) * 0.1) ; } // 30
    ~sOsc.("iblItTexX", 9); //{ iblItTexX = oscVal2 * 4 ; } // 31
    ~sOsc.("iblItTexY", 10 ); //{ iblItTexY = oscVal2 * 4; } // 32
    ~sOsc.("iblItRot", 20); //{ iblItRot = radians(oscVal2) ; } // 33

};

// start interrup granulation
Pbindef(\granPlayP, *[
	instrument: \granPlay,
	dur: 		16,
	legato: 	1,
	amp: 		0.6,
	pitch: 		[0.5,1],
	autoPosN: 	0.03,
	autoPosS: 	0.01,
	outBus: 	~lpfFXBus1,
	gpos: 		Pdup(3, Pxrand([0.4,0.45,0.6,0.2],inf)),
	bufnum: 	Prand([
		~getSmp.("somanymen_1"),
		~getSmp.("somanymen_2")
	],inf)
]).play(quant:4);

// prep other patterns
Pbindef(\playBufErr1, *[ instrument: \playBuf,
	bufnum: Pdup(3,
		Prand([
			~getSmp.("Misc034"),
			~getSmp.("Misc106"),
			~getSmp.("Misc188"),
			~getSmp.("RawGlitch_259"),
			~getSmp.("RawGlitch_412"),
			~getSmp.("RawGlitch_336")
		],inf)
	),
	start: 0,
	legato:1,
	dur:12,
	amp:0.7,
	rate:1,
	osc: Pfunc { |e|
		~sOsc.("drawImageBlenderToggle", 1);
	}
]);

Pbindef(\playBufErr2, *[ instrument: \playBuf,
	bufnum: ~getSmp.("RawGlitch_335"),
	start: 0,
	legato:1,
	dur:8,
	amp:0.7,
	rate:1,
	osc: Pfunc { |e|
	    ~sOsc.("drawImageBlenderID", 29.rand);
		~sOsc.("iblTexSpeedXfactor", 20.rand+50 ); //{ iblTexSpeedXfactor = ((oscVal2-64) * abs(oscVal2-64) * 0.6) ; } // 22
		~sOsc.("iblTexSpeedYfactor", 20.rand+50 ); //{ iblTexSpeedYfactor = ((oscVal2-64) * abs(oscVal2-64) * 0.6) ; } // 23
	}
]);

Pbindef(\harpsiPingP, *[
	instrument: \beep_sus,
	dur: Pseq([1,Pn(6)]),
	amp: 0.5,
	octave: 9,
	degree:Pseq([Rest(),Pn(-1)]),
	legato:0.01,
	outBus: ~revFXbus1,
	osc: Pfunc { |e|
    ~sOsc.("iblItRot", 10.rand+10); //{ iblItRot = radians(oscVal2) ; } // 33
	}
]);
)

// ---------------------------------------------------------
Pbindef(\playBufErr1).play(quant:4); // first noise, image on,
( // fast enought start harpPing and bassnoise!
Pbindef(\harpsiPingP).play(quant:4);
Pbindef(\playBufErr2).play(quant:4);
)

// take time.

// -----------------------------------------------------------
( // stop INTERRUPT, start TRAP sound
Pbindef(\granPlayP).stop;
Pbindef(\playBufErr1).stop;
Pbindef(\playBufErr2).stop;
Pbindef(\harpsiPingP).stop;

Pbindef(\akjDisChordp, *[
	instrument: \akjDisChord,
	freq:300,
	dur: 16,
	legato: 1.3,
	amp: 0.8,
	out: 0
]).play(quant:4);
)

( // --------------------------------------------

fork {
	~sOsc.("drawImageBlenderToggle", 0);
	2.wait;
	~oscTitles3.value;
	5.wait;
	~sOsc.("drawPlatesToggle", 0);

	// init TRAP video
    ~sOsc.("drawImageBlenderBank", 2 );
    ~sOsc.("drawImageBlenderID", 18);
    ~sOsc.("iblNum", 20); //{ iblNum = int(oscVal2) ; } // 6
    ~sOsc.("iblX", 64 ); //{ iblX = ((oscVal2-64) / 64.0) * width/2 ; } // 7  int( ((oscVal2-64) / 64.0) * width/2 ); }
    ~sOsc.("iblY", 24 ); //{ iblY = ((oscVal2-64) / 64.0) * width/2 ; } // 8
    ~sOsc.("iblWidth", 77); //{ iblWidth = oscVal2 * oscVal2 * 0.2 * norm(width, 0, width) ; } // 9  int(oscVal2 * oscVal2 * 0.05 * norm(width, 0, width));
    ~sOsc.("iblHeight", 20 ); //{ iblHeight = oscVal2 * oscVal2 * 0.1 * norm(height, 0, height) ; } // 10

    ~sOsc.("iblRot", 0 ); //{ iblRot =  radians(oscVal2 * 360 / 120) ; } // 11
    ~sOsc.("iblTexWidth", 64 ); //{ iblTexWidth = (oscVal2 - 64) * oscVal2 ; } // 18
    ~sOsc.("iblTexHeight", 64 ); //{ iblTexHeight = (oscVal2 - 64) * oscVal2 ; } // 19

    ~sOsc.("iblTexSpeedXfactor", 69 ); //{ iblTexSpeedXfactor = ((oscVal2-64) * abs(oscVal2-64) * 0.6) ; } // 22
    ~sOsc.("iblTexSpeedYfactor", 59 ); //{ iblTexSpeedYfactor = ((oscVal2-64) * abs(oscVal2-64) * 0.6) ; } // 23

    ~sOsc.("iblH", 70 ); //{ iblH = int(oscVal2) ; } // 24
    ~sOsc.("iblS", 127); //{ iblS = int(oscVal2) ; } // 25
    ~sOsc.("iblB", 127 ); //{ iblB = int(oscVal2) ; } // 26
    ~sOsc.("iblA", 127 ); //{ iblA = int(oscVal2) ; } // 27
    ~sOsc.("iblBflicker", 0 ); //{ iblBflicker = int(oscVal2) ; } // 28

    ~sOsc.("iblItX", 64 ); //{ iblItX = ((oscVal2-64) * abs(oscVal2-64) * 0.1) ; } // 29
    ~sOsc.("iblItY", 89 ); //{ iblItY = ((oscVal2-64) * abs(oscVal2-64) * 0.1) ; } // 30
    ~sOsc.("iblItTexX", 40); //{ iblItTexX = oscVal2 * 4 ; } // 31
    ~sOsc.("iblItTexY", 127 ); //{ iblItTexY = oscVal2 * 4; } // 32
    ~sOsc.("iblItRot", 0); //{ iblItRot = radians(oscVal2) ; } // 33
}
)

// TRAP //

( // start glitches and highhats ------------------------------------
Pbindef(\playBufBd1, *[ instrument: \playBuf,
	bufnum: Prand([
		~getSmp.("loop9"),
		~getSmp.("loop7"),
		~getSmp.("loop33"),
		~getSmp.("loop37")
		], inf),
	dur: Prand([4,2,1,1/2,Pn(1/4,2),Pn(1/8,4),Pseq([3/8,1/8]),Pseq([3/4,1/4])],inf),
	legato:0.1,
	start: Pxrand([0,1,2,3,4,5,6,7],inf) * 1/16,
	amp:1,
	osc: Pfunc { |e|
		~sOsc.("drawImageBlenderToggle", 1);
	    ~sOsc.("drawImageBlenderID", 19.rand);
		~sOsc.("iblTexSpeedXfactor", 30.rand+50 ); //{ iblTexSpeedXfactor = ((oscVal2-64) * abs(oscVal2-64) * 0.6) ; } // 22
		~sOsc.("iblTexSpeedYfactor", 20.rand+54 ); //{ iblTexSpeedYfactor = ((oscVal2-64) * abs(oscVal2-64) * 0.6) ; } // 23
	}
]).play(quant:4);

Pbindef(\akjClickp, *[
	instrument: \akjClick,
	dur: Pseq([
		Pn(1/8, 7), Rest(1/8), // 1
		Rest(1), // 1
		Pn(1/4, 3), Rest(3/8), Pn(1/8, 1),
		Pn(1/4, 3), Rest(3/8), Pn(1/8, 1),
		Pn(1/4, 3), Rest(4/8),
		Pn(1/8, 7),	Rest(1/8),
		Prand([ Rest(1), Pn(1/6,6) ]),
		Pn(1/8, 3),	Rest(1/8),
		Prand([ Rest(1.25), Pn(1/4, 5) ]),
		Prand([ Pn(1/8, 3), Pn(1/16, 6)	]), Rest(1/8),
	],inf),
	amp: 8/20,
	releaseTime: 0.3,
	freq:1,
	out: 0
]).play(quant:4);
)

( // add bass -------------------------------- /
Pbindef(\akjBazp, *[
	instrument: \akjBaz,
	freq: 300,
	dur: 10,
	amp: 0.3,
	legato: 0.20,

	out: 0,
	osc: Pfunc { |e|
	    ~sOsc.("iblH", 20.rand+54 );
		~sOsc.("iblItTexY", 127.rand );
	}
]).play(quant:4);
)

( // start granulated noises ------------------------------- /
Pbindef(\granGlitcho, *[
	instrument: \granPlay,
	dur: 		4,
	legato: 	1,
	amp: 		0.5,
	pitch: 		[0.5,1],
	autoPosN: 	0.1,
	autoPosS: 	0.1,
	outBus: 	0,
	gpos: 		[0.6,0.7,0.8,0.2],
	width:		1,
	bufnum: 	Pdup(16, Pseq([~getSmp.("loop99"), ~getSmp.("loop97")],inf))
]).play(quant:4);
)

// take time.

// stop psycho background chord
Pbindef(\akjDisChordp).stop


( // start PARITY title, stop noises,click and bass ------------------- /
fork {

	// stop granulated noises
	Pbindef(\granGlitcho).stop

	// image off, stop noises and bass.
	Pbindef(\playBufBd1).stop;
	Pbindef(\akjClickp).stop;
	Pbindef(\akjBazp).stop;

	~sOsc.("drawImageBlenderToggle", 0);
	2.wait;
	// title4 PARITY
	~oscTitles4.value;

	Pbindef(\granPlayPMARO, *[
		instrument: \granPlay,
		dur: 		16,
		legato: 	1,
		amp: 		0.9,
		pitch: 		1,
		autoPosN: 	0.02,
		autoPosS: 	0.01,
		outBus: 	~lpfFXBus1,
		gpos: 		Pdup(2, Pseq([ 0.9, 0.8, 0.7, 0.65, 0.2, 0.1 ],inf)),
		width:		0.4,
		bufnum:		~getSmp.("saudade_CUT1")
	]).play;

	5.wait;
	~sOsc.("drawPlatesToggle", 0);

	// init VIDEO player
	~sOsc.value("playVideoToggle", 0);
	~sOsc.value("playVideoBank", 0 );//) {  playVideoBank = int(oscVal2); } // 44
	~sOsc.value("playVideoID", 0 );//) {  playVideoID = int(oscVal2); } // 45
	~sOsc.value("playVideoPausePlay", 0 );  //) {  playVideoPausePlay = boolean(int(oscVal2)); } // 47
	~sOsc.value("playVideoHue", 79 ); //) {  playVideoHue = int(oscVal2); } // 44
	~sOsc.value("playVideoSaturation", 0 ); //) {  playVideoSaturation = int(oscVal2); } // 44
	~sOsc.value("playVideoBrightness", 127 ); //) {  playVideoBrightness = int(oscVal2); } // 44
	~sOsc.value("playVideoAlpha", 127 );//) {  playVideoAlpha = int(oscVal2); } // 44
	~sOsc.value("playVideoSpeed", 100);  //) {  playVideoPausePlay = boolean(int(oscVal2)); } // 47
	~sOsc.value("playVideoJump", 0);


}
)

( // start noise and clicks + video cuts ------------------------------------//
Pbindef(\noise, *[
	instrument: \noise,
	dur:10,
	legato:0.2,
	amp:0.08,
	videojump: Pseq([10,15,20,25,30,35,40,45,50,55],inf),
	osc: Pfunc { |e|
		~sOsc.value("playVideoToggle", 1);
		~sOsc.value("playVideoPausePlay", 1 );
		~sOsc.value("playVideoJump", e.videojump);
	}

]).play(quant:4);

Pbindef(\playBufHatM, *[ instrument: \playBuf,
	bufnum: [~getSmp.("Hats108"),~getSmp.("Hats085")],
	legato:1,
	//dur:Pseq([Pn(1/4,6),Pn(1/8,3)],inf),
	dur:8,
	amp: [2,0.3] * 1.2,
	rate: Pwhite(0.95,1.01),
	osc: Pfunc { |e|
		~sOsc.value("playVideoToggle", 1);
		~sOsc.value("playVideoHue", 127.rand ); //) {  playVideoHue = int(oscVal2); } // 44
		~sOsc.value("playVideoSaturation", 64+64.rand ); //) {  playVideoSaturation = int(oscVal2); } // 44
	}
]).play(quant:4);

Ndef(\subwob2, {
	var snd = SinOsc.ar([43,42]* 0.5);
	snd = (snd*2).tanh;
	snd = (snd*2).clip2;
	snd = RLPF.ar(snd,
		SinOsc.ar(LFNoise1.kr(1/5).range(0.5,6)).range(50,LFNoise1.kr(1/5).range(60,150)));
	Rotate2.ar(snd[0],DelayN.ar(snd[1],0.1,0.04),LFNoise1.kr(1/5).range(-1,1)) * 0.5 * 1;
});

Ndef(\subwob2).fadeTime = 10;

// start deep sub --------------------------------------------------//
(
Ndef(\subwob2).fadeTime = 10;

)

( // stop noises, granulation and sub ----------------------------------/
Ndef(\subwob2).release;
Pbindef(\playBufHatM).stop;
Pbindef(\noise).stop;
Pbindef(\granPlayPMARO).stop;
)



(//  stop video  ----------------------------------------------/
~sOsc.value("playVideoPausePlay", 0 );
// cut to black
~sOsc.value("playVideoToggle", 0);
)


// ADJUST ---
( // on release of granulator ------------------------------------------/
//  stop video
~sOsc.value("playVideoPausePlay", 0 );
~sOsc.value("playVideoToggle", 0);
fork {
	3.wait;
	~oscTitles5.value;
	5.wait;
	~sOsc.("drawPlatesToggle", 0);

	~sOsc.("drawImageBlenderBank", 4 );
    ~sOsc.("drawImageBlenderID", 4);
    ~sOsc.("iblNum", 6); //{ iblNum = int(oscVal2) ; } // 6
    ~sOsc.("iblX", 64 ); //{ iblX = ((oscVal2-64) / 64.0) * width/2 ; } // 7  int( ((oscVal2-64) / 64.0) * width/2 ); }
    ~sOsc.("iblY", 64 ); //{ iblY = ((oscVal2-64) / 64.0) * width/2 ; } // 8
    ~sOsc.("iblWidth", 88); //{ iblWidth = oscVal2 * oscVal2 * 0.2 * norm(width, 0, width) ; } // 9  int(oscVal2 * oscVal2 * 0.05 * norm(width, 0, width));
    ~sOsc.("iblHeight", 77 ); //{ iblHeight = oscVal2 * oscVal2 * 0.1 * norm(height, 0, height) ; } // 10

    ~sOsc.("iblRot", 0 ); //{ iblRot =  radians(oscVal2 * 360 / 120) ; } // 11
    ~sOsc.("iblTexWidth", 64 ); //{ iblTexWidth = (oscVal2 - 64) * oscVal2 ; } // 18
    ~sOsc.("iblTexHeight", 64 ); //{ iblTexHeight = (oscVal2 - 64) * oscVal2 ; } // 19

    ~sOsc.("iblTexSpeedXfactor", 70 ); //{ iblTexSpeedXfactor = ((oscVal2-64) * abs(oscVal2-64) * 0.6) ; } // 22
    ~sOsc.("iblTexSpeedYfactor", 69 ); //{ iblTexSpeedYfactor = ((oscVal2-64) * abs(oscVal2-64) * 0.6) ; } // 23

    ~sOsc.("iblH", 64 ); //{ iblH = int(oscVal2) ; } // 24
    ~sOsc.("iblS", 127); //{ iblS = int(oscVal2) ; } // 25
    ~sOsc.("iblB", 127 ); //{ iblB = int(oscVal2) ; } // 26
    ~sOsc.("iblA", 40 ); //{ iblA = int(oscVal2) ; } // 27
    ~sOsc.("iblBflicker", 0 ); //{ iblBflicker = int(oscVal2) ; } // 28

    ~sOsc.("iblItX", 94 ); //{ iblItX = ((oscVal2-64) * abs(oscVal2-64) * 0.1) ; } // 29
    ~sOsc.("iblItY", 64 ); //{ iblItY = ((oscVal2-64) * abs(oscVal2-64) * 0.1) ; } // 30
    ~sOsc.("iblItTexX", 9); //{ iblItTexX = oscVal2 * 4 ; } // 31
    ~sOsc.("iblItTexY", 10 ); //{ iblItTexY = oscVal2 * 4; } // 32
    ~sOsc.("iblItRot", 128); //{ iblItRot = radians(oscVal2) ; } // 33

};
)

// ADJUST //
( // start noise ----- LOUD! -------------------------------//
Pbindef(\lcmnoip,
	\instrument, \lcmnoise,
	\dur, 8,
	\amp, 0.3,
	\amp1, 0.1,
	\amp2, 3,
	\legato, 1,
	\outBus, ~satBus1,
	\osc, Pfunc { |e|
		~sOsc.("drawImageBlenderToggle", 1);
	    ~sOsc.("drawImageBlenderID", 29.rand);
		~sOsc.("iblTexSpeedXfactor", 30.rand+30 );
		~sOsc.("iblTexSpeedYfactor", 30.rand+30 );
		~sOsc.("iblItRot", 128.rand);
		//
	}
).play(quant:4)
)

( // add second layer of noise -------------------------------------------//
Pbindef(\playBufGlitch1,
	*[
		instrument: \playBuf,
		bufnum:	Prand([
			~getSmp.("qebrus_machines_snip5"),
			~getSmp.("qebrus_machines_snip2"),
			~getSmp.("qebrus_machines_snip1")
		],inf),
		legato:1,
		//dur:Pseq([Pn(1/4,6),Pn(1/8,3)],inf),
		dur:8,
		start:Pwhite(0.0,0.4),
		rate: [0.5,0.7,1],
		amp: 0.4,
		sdel: 0.01,
		//outBus: 0,
		outBus: Prand([0, ~satBus1],inf),
	]).play(quant:4);
)

// stop second layer of noise ---------------------------------------------------- /
Pbindef(\playBufGlitch1).stop;

( // stop first layer of noise, start sawtooth pad ----------------------------------/
Pbindef(\lcmnoip).stop;

Pbindef(\sawp0,
	\instrument, \softSaw,
	\dur, 10,
	\attackTime, 5,
	\releaseTime,5,
	\legato,1,
	\degree, Pseq([
		[-2,2,12,25],
		[-2,1,9,19],
		[-2,3,11,20],
		[-2,6,8,22],
		[-2,3,12,18],
		[-2,5,7,21],
	],inf),
	\octave, 3,
	\amp, [1,0.5,0.2,0.1] * 1,
	\out, 0
).play(quant:4);
)

(
// off image plate
~sOsc.("drawImageBlenderToggle", 0);

// CARRY
fork {
	4.wait;
	~oscTitles6.value;
	5.wait;
	~sOsc.("drawPlatesToggle", 0);

    ~sOsc.("drawImageBlenderBank", 1 );
    ~sOsc.("drawImageBlenderID", 4);
    ~sOsc.("iblNum", 60); //{ iblNum = int(oscVal2) ; } // 6
    ~sOsc.("iblX", 0 ); //{ iblX = ((oscVal2-64) / 64.0) * width/2 ; } // 7  int( ((oscVal2-64) / 64.0) * width/2 ); }
    ~sOsc.("iblY", 64 ); //{ iblY = ((oscVal2-64) / 64.0) * width/2 ; } // 8
    ~sOsc.("iblWidth", 17); //{ iblWidth = oscVal2 * oscVal2 * 0.2 * norm(width, 0, width) ; } // 9  int(oscVal2 * oscVal2 * 0.05 * norm(width, 0, width));
    ~sOsc.("iblHeight", 128 ); //{ iblHeight = oscVal2 * oscVal2 * 0.1 * norm(height, 0, height) ; } // 10

    ~sOsc.("iblRot", 60 ); //{ iblRot =  radians(oscVal2 * 360 / 120) ; } // 11
    ~sOsc.("iblTexWidth", 64 ); //{ iblTexWidth = (oscVal2 - 64) * oscVal2 ; } // 18
    ~sOsc.("iblTexHeight", 64 ); //{ iblTexHeight = (oscVal2 - 64) * oscVal2 ; } // 19

    ~sOsc.("iblTexSpeedXfactor", 70 ); //{ iblTexSpeedXfactor = ((oscVal2-64) * abs(oscVal2-64) * 0.6) ; } // 22
    ~sOsc.("iblTexSpeedYfactor", 69 ); //{ iblTexSpeedYfactor = ((oscVal2-64) * abs(oscVal2-64) * 0.6) ; } // 23

    ~sOsc.("iblH", 64 ); //{ iblH = int(oscVal2) ; } // 24
    ~sOsc.("iblS", 127); //{ iblS = int(oscVal2) ; } // 25
    ~sOsc.("iblB", 127 ); //{ iblB = int(oscVal2) ; } // 26
    ~sOsc.("iblA", 40 ); //{ iblA = int(oscVal2) ; } // 27
    ~sOsc.("iblBflicker", 0 ); //{ iblBflicker = int(oscVal2) ; } // 28

    ~sOsc.("iblItX", 84 ); //{ iblItX = ((oscVal2-64) * abs(oscVal2-64) * 0.1) ; } // 29
    ~sOsc.("iblItY", 64 ); //{ iblItY = ((oscVal2-64) * abs(oscVal2-64) * 0.1) ; } // 30
    ~sOsc.("iblItTexX", 9); //{ iblItTexX = oscVal2 * 4 ; } // 31
    ~sOsc.("iblItTexY", 50 ); //{ iblItTexY = oscVal2 * 4; } // 32
    ~sOsc.("iblItRot", 0); //{ iblItRot = radians(oscVal2) ; } // 33
};
)



( // images and bleeps --------------------------------------------------------//
Pbindef(\playBufBleep2, *[ instrument: \playBuf,
	bufnum: ~getSmp.("Bleeps007"),
	legato:1,
	dur:Pseq([1/4,Pn(3,7)],inf),
	amp:0.2,
	rate: 1,
	osc: Pfunc { |e|
		~sOsc.("drawImageBlenderToggle", 1);
	    ~sOsc.("drawImageBlenderID", 41.rand);
		~sOsc.("iblTexSpeedXfactor", 30.rand+55 ); //{ iblTexSpeedXfactor = ((oscVal2-64) * abs(oscVal2-64) * 0.6) ; } // 22
		~sOsc.("iblItTexX", 20.rand+60); //{ iblItTexX = oscVal2 * 4 ; } // 31
		~sOsc.("iblItTexY", 20.rand+60 ); //{ iblItTexY = oscVal2 * 4; } // 32
	}
]).play(quant:4);
)

( // hihat -------------------------------------------------------------------//
Pbindef(\playBufHat2, *[ instrument: \playBuf,
	bufnum: ~getSmp.("Hats076"),
	legato:1,
	dur:Pseq([Pn(1/4,6),Pn(1/8,3)],inf),
	dur:1/4,
	amp: 0.6 * Pseq([1,1/5],inf) * Pwhite(0.3,1),
	rate: Pwhite(0.95,1.01),
	pan: Pwhite(0,10) - 5 * 0.2
]).play(quant:4);
);


( // -add 3/4 beeps + slight changes in direction --------------------------//
//Pbindef(\playBufBleep3).clear
Pbindef(\playBufBleep3, *[ instrument: \playBuf,
	bufnum: ~getSmp.("Bleeps015"),
	legato:Pwhite(0.01,0.1),
	dur:Pseq([1/4,Pn(1.5,7)],inf) * 1/2,
	amp: Pseg([0.7,1,0.7], 44, \sqr, inf) * 0.2,
	rate: 1,
	pan: Pseg([0,10], 10, \lin, inf) - 5 * 0.1,
	yspeed: 64 + (Prand((0..3), inf) * Pseq([-1,1],inf)),
	osc: Pfunc { |e| ~sOsc.("iblTexSpeedYfactor", e.yspeed ); }
]).play(quant:4);
)

( // glitchy-noise, changing rotation image  ---------------------------------------//
Pbindef(\playBufGlitch4, *[ instrument: \playBuf,
	bufnum: ~getSmp.("Misc106"),
	legato:0.3,
	dur:16,
	amp:0.4,
	rate: 1,
	rotation: Pseq([0,1], inf),
	osc: Pfunc { |e|
		~sOsc.("iblItRot", e.rotation);
		~sOsc.("iblRot", 15.rand + 53 );
	}
]).play(quant:4);
)





( // stop verything, start OVERFLOW title, init images

// stop the sawPAD ------------------------------------------------//
Pbindef(\sawp0).stop;

Pbindef(\playBufHat2).stop;
Pbindef(\playBufBleep2).stop;
Pbindef(\playBufBleep3).stop;
Pbindef(\playBufGlitch4).stop;
~sOsc.("drawImageBlenderToggle", 0);

fork {
	4.wait;
	// title7 OVERFLOW
	~oscTitles7.value;
	5.wait;
	~sOsc.("drawPlatesToggle", 0);

	~sOsc.("drawPlatesToggle", 0);
	~sOsc.("drawPlatesimgAbank", 5);
	~sOsc.("drawPlatesimgBbank", 5);
	~sOsc.("drawPlatesimgCbank", 5);
	~sOsc.("drawPlatesimgBblendMode", 2);
	~sOsc.("drawPlatesimgCblendMode", 1);
	~sOsc.("drawPlatesTexAspeed", 0);
	~sOsc.("drawPlatesTexBspeed", 0);
	~sOsc.("drawPlatesTexCspeed", 0);
}
)

( // start the cutter and images ! -----------------------------------------//
Pbindef(\playBufqebP, *[ instrument: \playBuf,
	bufnum:	~getSmp.("qebrus_machines_snip4"),
	dur: Pn(
		Pshuf([1/2, 1/2, 1/4, Pn(1/8,2), 1/2],8) * 1/2,
		inf),
	legato:[0.9,0.1,0.2] * Pwhite(0.9,1),
	amp: Pwhite(0.5,0.6) * 0.7,
	rate: Pwhite(0.99,1),
	hpff: 100,

	start: Pseq([
		Pn(0, 60),
		Pwhite(0,0.15, 40),
		Pwhite(0,0.25, 50),
		Pwhite(0,0.35, 40),
		Pwhite(0,0.55, 50),
		Pwhite(0,0.65, 50),
		Pwhite(0,0.55, 100),
		Pwhite(0,0.25, 50),
		Pwhite(0,0.15, 40),
		Pwhite(0,0.55, 100),
		Pwhite(0,0.25, 50),
		Pwhite(0,0.15, 40),
		Pwhite(0,0.05, 40),
	], inf),
	sdel: Pdup(50, Pxrand([1,2,3,4]*0.001,inf)),
	outBus: 	~lpfFXBus1,

	speedrange: Pseq([Pseg([0,0,50,50,0], [90,120,120,60]), Pn(0)]),
	brightness: 127,
	osc: Pfunc { |e|
		~sOsc.("drawPlatesToggle", 1);
		~sOsc.("drawPlatesimgAid", 19.rand);
		~sOsc.("drawPlatesimgBid", 19.rand);
		~sOsc.("drawPlatesimgCid", 19.rand);
		~sOsc.("drawPlatesTexAspeed", e.speedrange.rand);
		~sOsc.("drawPlatesTexBspeed", e.speedrange.rand);
		~sOsc.("drawPlatesTexCspeed", e.speedrange.rand);
		~sOsc.("drawPlatesTexAdirection", 127.rand);
		~sOsc.("drawPlatesTexBdirection", 127.rand);
		~sOsc.("drawPlatesTexCdirection", 127.rand);
	~sOsc.("drawPlatesimgAbright", e.brightness);
	~sOsc.("drawPlatesimgBbright", e.brightness);
	~sOsc.("drawPlatesimgCbright", e.brightness);

	};
]).play(quant:4);
)


( // start fade in granular, + highhats ------------------------------------//
Pbindef(\granPlayP, *[
	instrument: \granPlay,
	dur: 		4,
	legato: 	1,
	amp: 		Pseq([ Pseg([0,0.5], 90), Pn(0.5)]),
	pitch: 		[0.5,1],
	autoPosN: 	0.03,
	autoPosS: 	0.01,
	outBus: 	~lpfFXBus1,
	gpos: 		Pdup(6, Pxrand([0,0.1,0.3],inf)),
	bufnum: 	~getSmp.("qebrus_machines_snip4")
]).play(quant:4);

Pbindef(\playBufHat5, *[ instrument: \playBuf,
	bufnum: ~getSmp.("Hats079"),
	legato:1,
	dur:1/4,
	amp: 2 * Pseq([1,1/5],inf) * Pwhite(0.3,1),
	rate: Pwhite(0.95,1.01),
]).play(quant:8);
);

( // start bass
Pbindef(\sawp2,
	\instrument, \softSaw,
	\dur, 10,
	\attackTime, 5,
	\releaseTime,5,
	\legato,1,
	\scale, Scale.minor,
	\note, Pxrand([-4,-2,0,1],inf),
	\out, ~satBus1,
	\octave, 3,
	\amp, 0.9,
).play(quant:4);
)

// slow cutoff
~lpfFX1.set(\cutoffLag, 30, \cutoff, 500);


// fadout images, 60 sec -------------------------------------//
Pbindef(\playBufqebP, *[ brightness: Pseq([Pseg([127, 0],60), Pn(0)]) ]);

// stop highhat
Pbindef(\playBufHat5).stop;

( // start closing filter
Pbindef(\sawp2).stop;
~lpfFX1.set(\cutoffLag, 40, \cutoff, 50);
)

( // stop cutter buf, granulator, off video
Pbindef(\playBufqebP).stop;
Pbindef(\granPlayP).stop;
~sOsc.("drawPlatesToggle", 0);
)

// end credit?
~oscTitles8.value;

~sOsc.("drawPlatesToggle", 0);

////////////////////////////////////////////////////////////////////////////////////////
// ENDE ////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////