import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.LineUnavailableException;

import net.beadsproject.beads.analysis.featureextractors.FFT;
import net.beadsproject.beads.analysis.featureextractors.PeakDetector;
import net.beadsproject.beads.analysis.featureextractors.PowerSpectrum;
import net.beadsproject.beads.analysis.featureextractors.SpectralDifference;
import net.beadsproject.beads.analysis.segmenters.ShortFrameSegmenter;
import net.beadsproject.beads.core.AudioContext;
import net.beadsproject.beads.core.Bead;
import net.beadsproject.beads.data.buffers.SineBuffer;
import net.beadsproject.beads.ugens.RTInput;
import net.beadsproject.beads.ugens.WavePlayer;

public class RTInputTest {
	
	public static void main(String[] args) throws LineUnavailableException, IOException {
		  AudioContext ac = new AudioContext(512, 5000, new AudioFormat(44100, 16, 2, true, true));
		  RTInput input = new RTInput(ac, ac.getAudioFormat());
		  
		  ShortFrameSegmenter sfs = new ShortFrameSegmenter(ac);
			sfs.addInput(ac.out);
			ac.out.addDependent(sfs);
			int chunkSize = 512;
			sfs.setChunkSize(chunkSize);
			sfs.setHopSize(chunkSize/2);
			
			//set up the fft
			FFT fft = new FFT();
			sfs.addListener(fft);
			PowerSpectrum ps = new PowerSpectrum();
			fft.addListener(ps);
					
			//set up spectral difference
			SpectralDifference sd = new SpectralDifference(ac.getSampleRate());
			//sd.setFreqWindow(80.f,1100.f);
			ps.addListener(sd);
			
			PeakDetector od = new PeakDetector();
			od.setThreshold(0.2f);
			od.setAlpha(.9f);			
			sd.addListener(od);
			/*
			od.addThresholdListener(new FeatureExtractor<float[],float[]>(){
				public void process(float[] f)
				{
					System.out.print(f[0]);
					System.out.print(" ");
				}
			});		  */
			od.addMessageListener(new Bead(){protected void messageReceived(Bead message){System.out.print(".");}});
		  
			WavePlayer wp = new WavePlayer(ac, 440f, new SineBuffer().getDefault());
			ac.out.addInput(wp);
			
		  ac.out.addInput(input);
		  ac.start();
	}
}
