// jedem Würfel ist eine Geraeuschfamilie zugeordnet
/*
Es gibt (nach bodo und norbert):

-1- Rhythmus
-2- Tiere
-3- Akkorde
-4- Sprache
-5- Spielekonsole

*/

Server.default = s = Server.internal;

Server.default = s = Josef.startServer;
s.makeGui;

s.freeAll

s.boot;

(
q = ();

q.filenamebase = "/Users/till/Documents/animaxsamples/";

// be sure to have wav or aiff files here.

q.filenames = (
	red: 
		[ // Tiere
			"SoundIdeas6000/6003 - Animals/97.wav",
			"SoundIdeas6000/6003 - Animals/93.wav",
			"SoundIdeas6000/6003 - Animals/92.wav",
			"SoundIdeas6000/6003 - Animals/91.wav",
			"SoundIdeas6000/6003 - Animals/88.wav",
			"SoundIdeas6000/6003 - Animals/35.wav"
		],
	green:
		[ // chords
			"LFSaw-chords/echo.aif",
			"LFSaw-chords/bells.aif",
			"LFSaw-chords/guitar.aif",
			"LFSaw-chords/Synthie.aif",
			"LFSaw-chords/Oboe.aif",
			"LFSaw-chords/Piano.aif"
		],
	blue: // crowd
		[
			"SoundIdeas6000/6014 - Crowd #2/Track 01.aiff",
			"SoundIdeas6000/6014 - Crowd #2/Track 02.aiff",
			"SoundIdeas6000/6014 - Crowd #2/Track 03.aiff",
			"SoundIdeas6000/6014 - Crowd #2/Track 04.aiff",
			"SoundIdeas6000/6014 - Crowd #2/Track 05.aiff",
			"SoundIdeas6000/6014 - Crowd #2/Track 16.aiff"
		],
	yellow: // water
		[
			"SoundIdeas6000/6035 - Water/Track  1.aiff",
			"SoundIdeas6000/6035 - Water/Track  2.aiff",
			"SoundIdeas6000/6035 - Water/Track  3.aiff",
			"SoundIdeas6000/6035 - Water/Track  4.aiff",
			"SoundIdeas6000/6035 - Water/Track  5.aiff",
			"SoundIdeas6000/6035 - Water/Track 16.aiff"
		],
	orange: // drums
		[
			"LFSaw-chords/drum01.aif",
			"LFSaw-chords/drum02.aif",
			"LFSaw-chords/drum03.aif",
			"LFSaw-chords/drum04.aif",
			"LFSaw-chords/drum05.aif",
			"LFSaw-chords/drum06.aif"
		]

);

(s.name == 'Josef1').if({
	q.filenamebase = "/Network/Servers/stitch/srv/nfs/volume1/home/till/samples/";
	q.buffers = q.filenames.collect{|cubefiles|
		cubefiles.collect{|name|
			Buffer.readChannel(s, q.filenamebase ++ name, channels: [0], action:{"file % read.\n".postf(name) })
		}
	};
},{
	q.buffers = q.filenames.collect{|cubefiles|
		cubefiles.collect{|name|
			var sfile, buf;
			//MP3.readToBuffer(s, q.filenamebase ++ name)
			name.postln;
			sfile = SoundFile.openRead(q.filenamebase ++ name);
			buf = (sfile.numChannels > 1).if({
				Buffer.readChannel(s, q.filenamebase ++ name, channels: [0], action:{"file % read.\n".postf(name) })
			}, {
				Buffer.read(s, q.filenamebase ++ name, action:{"file % read.\n".postf(name) })
			});
			sfile.close;
			buf
		}
	};
});

// only first channel of the file is read 

/* test
q.buffers.choose.choose.play 

q.sfile = SoundFile.openRead(q.filenamebase ++ q.filenames[\green][6]);

q.sfile.numChannels
q.sfile.close;

// */

)

(
q.cubes = [\red, \green, \blue, \orange, \yellow].collect{|color, i| BufferBlock(color, 0, q.buffers[color], i)};
BufferBlock.sendSynth;
q.buffers.asArray.flatten.do{_.normalize};
)

(
s.bind{
q.cubes[0].play;
q.cubes[1].play;
q.cubes[2].play;
q.cubes[3].play;
q.cubes[4].play;
}
)

q.cubes[0].stop;
q.cubes[1].stop;
q.cubes[2].stop;
q.cubes[3].stop;
q.cubes[4].stop;

/*
q.cubes[0].setFace(108, \amps, 1)
q.cubes[0].set(\amps, 1, 0)

q.cubes[0].synth.set(\amps2, 1)
*/

q.cubes[0].set(\amp, 0.5) // red
q.cubes[1].set(\amp, 0.1) // green
q.cubes[2].set(\amp, 0.5) // blue
q.cubes[3].set(\amp, 0.8) // orange
q.cubes[4].set(\amp, 0.5) // yellow

/*
q.cubes.do{|cube|
	cube.set(\amp, 1);
	cube.out = 0
}
*/

//////////////////////
// Start SETO

(
q.seto = SETO_OSCTUIOServer("2Dobj", nil, JITseto, SETOIDistance); 
q.seto.start;

JITseto.action = {|me|
	var cube = q.cubes.detect{|cube| cube.ids.includes(me.id)};
	cube.notNil.if{
		
		//cube.color.postln;
		cube.setFace(me.id, \amps, ((sin(me.rotEuler.first) * 0.95).abs + 0.05))
		//cube.setFace(me.id, \amps, 0.9)
	}
};

/*
JITseto.action = nil; // */
)

///////////////////////
// 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.cubes.do{|cube|
				cube.set(\masterAmp, val.linlin(0, 127, 0, 1))	
			}
		};

		(chan < 8).if{ // red (left-most)
			q.cubes[chan].set(\amp, val.linlin(0, 127, 0, 1))	
		};
	};
	
	(ctlNum == 17).if{ // lower button -> mute
		(chan == 8).if{ // master (right-most)
			q.cubes.do{|cube|
				cube.set(\masterMute, val.linlin(0, 127, 0, 1))	
			}
		};

		(chan < 8).if{ // red (left-most)
			q.cubes[chan].set(\mute, val.linlin(0, 127, 0, 1))	
		};
	};
	
}
)

q.cubes.do{|cube|
	cube.synthParams.add(\masterMute -> [0]);
	cube.synthParams.add(\mute -> [0]);
}

q.cubes[3].play
q.cubes[3].out = 0

q.cubes[3].stop

q.cubes.do{|cube|
	cube.set(\amps, 3);
}

q.cubes.do{|cube|
	cube.stop;
}

q.cubes[3].play
q.cubes[3].out = 0
q.cubes[3].stop

q.cubes[1].set(side: 2, what: \rates, val: 0.5);

6.do{|i|
	q.cubes[1].set(side: i, what: \amps, val: 0.5);
}

6.do{|i|
	q.cubes[1].set(side: i, what: \rates, val: rrand(0.5, 2.0));
}

6.do{|i|
	q.cubes[0].set(side: i, what: \rates, val: rrand(1, 4.0));
}

x.setn(\amps, [1, 0, 0, 0, 0, 0])
x.setn(\amps, [0, 0, 1, 0, 0, 0])
x.setn(\amps, 1!6)

x.setn(\amps, [10, 0, 0, 0, 0, 0, 0])

x.setn(\amps, [1, 1, 1, 1, 1, 1, 1])