package net.beadsproject.touch.pianopush;

import net.beadsproject.beads.core.AudioContext;
import net.beadsproject.beads.data.Buffer;
import net.beadsproject.beads.events.KillTrigger;
import net.beadsproject.beads.ugens.Envelope;
import net.beadsproject.beads.ugens.Function;
import net.beadsproject.beads.ugens.Gain;
import net.beadsproject.beads.ugens.GranularSamplePlayer;
import net.beadsproject.beads.ugens.WavePlayer;
import net.beadsproject.touch.perform.Performer;

public class MyPerformer extends Performer {

	AudioContext ac;
	Envelope gainEnv;
	Envelope masterRateEnv;
	GranularSamplePlayer gsp;
	Envelope masterPitch;
	Envelope pitchModAmp;
	Function pitchMod;
	Envelope randomness;
	Envelope pitchLFOFreq;
	
	
	public MyPerformer(AudioContext ac) {
		this.ac = ac;
		
		//set up connection point
		gainEnv = new Envelope(ac, 0f);
		Gain g = new Gain(ac, 2, gainEnv);
		masterRateEnv = new Envelope(ac, 1f);
		gsp = new GranularSamplePlayer(ac, 2);	//there is only one sound per contact
		gsp.setKillOnEnd(true);
		gsp.setKillListener(new KillTrigger(g)); 	//this ensures that when gsp ends, g ends.
		
		pitchLFOFreq = new Envelope(ac, 0.1f);
		WavePlayer pitchLFO = new WavePlayer(ac, pitchLFOFreq, Buffer.SINE);
		masterPitch = new Envelope(ac, 1f);
		pitchModAmp = new Envelope(ac, 0f);
		pitchMod = new Function(pitchLFO, masterPitch, pitchModAmp) {
			public float calculate() {
				return x[0] * x[2] + x[1];
			}
		};
		gsp.setPitchEnvelope(pitchMod);
		randomness = new Envelope(ac, 0f);
		gsp.setRandomnessEnvelope(randomness);
		
		g.addInput(gsp);
		
		ac.out.addInput(g);
	}
}
