/*
	LUIS - Landes Umwelt Informations System
	Sonifikation von Feinstaubdaten an verschiedenen Messstationen in Graz
	
	frauenberger@iem.at - 4.8.2005
	
*/

// Create environment and read data file 
(
	s.boot;
	p = p ?? { ProxySpace.new }; 
	p.push;
	q = q ? (); 
	
	d = TabFileReader.read(Document.current.path.dirname ++ "/" ++ "merged.csv", true);
	q.data = d.flop.copyRange(2,5).asFloat;
	q.rain = d.flop[6].asFloat; 	
	q.peak = q.data.collect({ |ch| ch.maxItem }).maxItem(_.abs);
	q.data = q.data / q.peak;	// normalise data 
	q.databuf = q.data.collect({ | ch | Buffer.loadCollection(s, ch); });
	
	q.rain = q.rain / q.rain.maxItem;
	q.rainbuf = Buffer.loadCollection(s, q.rain);

	~nord.ar;
	~sued.ar;
	~platte.ar;
	~mitte.ar;
	~niederschlag.ar;
)

// plot some data if you want 
(
	q.rainbuf.plot;
	q.databuf[0].plot;
	q.databuf[1].plot;
	q.databuf[2].plot;
	q.databuf[3].plot;
)

// create the synths for the sources
(
		// index runs in hours 
	~index = { arg bufBeginDay = 0, bufEndDay = 31, secsPerDay = 1;
		Phasor.ar(0, 1 / (secsPerDay * s.sampleRate), bufBeginDay, bufEndDay) * 24;
	};
		// weekDayShift 
	~marker = { |dayVol=0, weekVol=0, loopVol=1, weekDayShift=2|
		Ringz.ar(Slope.ar(~index.ar).sign * 0.1 * loopVol, 1000, 0.2) +  // loop
		Ringz.ar(Slope.ar((~index.ar - (weekDayShift * 24)).mod(168)).sign * 0.1 * weekVol, 500, 0.3) + // week
		Ringz.ar(Slope.ar( (~index.ar + 0).mod(24)).sign * 0.1 * dayVol, 250, 0.4); // day
	};

	~marker.play(vol: 0.1);
	
	e = {  arg buf = 0, baseFreq = 100, spread = 2000;
		
		var out, data, ringTime, filterFreq; 
		data = BufRd.ar(1, buf, ~index.ar);	
		ringTime = data;
		filterFreq = baseFreq+(data*spread);
		
		out = Ringz.ar(PinkNoise.ar(0.5), filterFreq, ringTime) * ((data.exp - 1)/10);
		AmpComp.kr(filterFreq) * out;
	};

	~nord.set(\buf, q.databuf[0].bufnum); ~nord = e;
	~sued.set(\buf, q.databuf[1].bufnum); ~sued = e;
	~platte.set(\buf, q.databuf[2].bufnum); ~platte = e;
	~mitte.set(\buf, q.databuf[3].bufnum); ~mitte = e;
	
	~niederschlag.set(\buf, q.rainbuf.bufnum);
	~niederschlag = { arg buf, up = 0.5, fan = 1.0, 
		baseFreq = 2500, freqSpread = 2000, hpfFreq = 1000;
		var data, out, freq; 
		data = BufRd.ar(1, buf, ~index.ar);	
		data = data.lag(up).max(data.lag(fan));
		data * HPF.ar(LFDNoise3.ar(baseFreq + (data * freqSpread) * 2, 1), hpfFreq) ! 2;
	};
			// out to stereo:
	~nord.play(0, vol: 0.3);
	~sued.play(1, vol: 0.3);
	~platte.play(0, vol: 0.3);
	~mitte.play(1, vol: 0.3);
	~niederschlag.play(5, vol: 0.1);

//			// to multichan:
//	~nord.play(0, vol: 0.3);
//	~sued.play(1, vol: 0.3);
//	~platte.play(2, vol: 0.3);
//	~mitte.play(3, vol: 0.3);
//	~niederschlag.play(4, vol: 0.1);
)

			// day rhythm:
	~index.set(\secsPerDay, 1);
	~marker.set(\dayVol, 0, \weekVol, 0, \loopVol, 1);
	~marker.play(vol: 1);

	~marker.set(\dayVol, 1);

			// week:
	~index.set(\secsPerDay, 1);
	~marker.set(\weekVol, 1);

			// rain influence:
	~index.set(\secsPerDay, 2);
	~niederschlag.play(1, vol: 0.5);
	~index.set(\bufBeginDay, 2, \bufEndDay, 9);	// a good rain week

	~index.set(\bufBeginDay, 0, \bufEndDay, 31);	// full month

// online control

ProxyMixer6(p);