

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.SampleManager;
import net.beadsproject.beads.ugens.SamplePlayer;

public class OnsetTestInteractive {

	public static void main(String[] args) throws Exception {
		AudioContext ac = new AudioContext();	
		
		//sample
		final SamplePlayer sp = new SamplePlayer(ac, SampleManager.sample("audio/1234.aif"));
		sp.setLoopType(SamplePlayer.LoopType.LOOP_FORWARDS);
		ac.out.addInput(sp);
		
		//set up the chopper upper
		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);
		SpectralDifference sd = new SpectralDifference(ac.getSampleRate());
		//sd.setFreqWindow(80.f,1100.f);
		ps.addListener(sd);
		PeakDetector od = new PeakDetector();
		sd.addListener(od);
		od.setThreshold(0.2f);
		od.setAlpha(.9f);
		od.addMessageListener(new Bead(){protected void messageReceived(Bead b){System.out.print(".");}});
				
		//run
		ac.start();
	}
}
