import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.beadsproject.beads.core.AudioContext;
import net.beadsproject.beads.data.buffers.SineBuffer;
import net.beadsproject.beads.ugens.Envelope;
import net.beadsproject.beads.ugens.Gain;
import net.beadsproject.beads.ugens.RTInput;
import net.beadsproject.beads.ugens.WavePlayer;


public class ThrashTheSystemWithInputTest {

	public static int sleepTime = 2000;

	public static void main(String[] args) {
		final AudioContext ac = new AudioContext();
		RTInput in = new RTInput(ac);
		ac.out.addInput(in);
//		ac.checkForDroppedFrames(false);
		JFrame f = new JFrame();
		JSlider slider = new JSlider(0, 2000, 2000);
		slider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				sleepTime = ((JSlider)e.getSource()).getValue();
			}
		});
		f.setContentPane(new JPanel());
		f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
		f.getContentPane().add(new JLabel("<--- go this way to thrash it"));
		f.getContentPane().add(slider);
		f.pack();
		f.setVisible(true);
		Thread t = new Thread() {
			public void run() {
				while(true) {
					WavePlayer wp = new WavePlayer(ac, (float)Math.random() * 10000f + 500f, new SineBuffer().getDefault());
					Gain g = new Gain(ac, 1, new Envelope(ac, 0.1f));
					((Envelope)g.getGainEnvelope()).addSegment(0f, 2000f);
					g.addInput(wp);
					ac.out.addInput(g);
					try {
						sleep(sleepTime);
					} catch(Exception e) {
						e.printStackTrace();
					}
				}
			}
		};
		t.start();
		ac.start();
	}

}
