

import java.io.FileOutputStream;

import net.beadsproject.beads.analysis.featureextractors.BasicDataWriter;
import net.beadsproject.beads.analysis.featureextractors.BasicTimeDataWriter;
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.data.SampleManager;
import net.beadsproject.beads.data.buffers.OneWindow;
import net.beadsproject.beads.ugens.SamplePlayer;

public class MorePeakTests {

	public static void main(String[] args) throws Exception {
		AudioContext ac = new AudioContext();	
		//sample
		SamplePlayer sp = new SamplePlayer(ac, SampleManager.sample("audio/1234.aif"));
		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);
				
		//set up spectral difference
		SpectralDifference sd = new SpectralDifference(ac.getSampleRate());
		sd.setFreqWindow(80.f,1100.f);
		// sd.setFreqWindow(2000.f,10000.f);
		ps.addListener(sd);
		
		// test the effect of varying width of moving average window in peak detector
		
		// Peak Detectors
		int mValues[] = {3,4,5,6,7,8,9,10,11,12,13,14,15,16,32,64,128,256,512,1024};
		
		String analysisDataOutputDir = "output/morepeaks";	
		PeakDetector pd[] = new PeakDetector[mValues.length];
		for (int i=0;i<mValues.length;i++)
		{
			pd[i] = new PeakDetector();
//			pd[i].useMeanFilterOfSize(mValues[i]);
			sd.addListener(pd[i]);
			pd[i].addListener(new BasicTimeDataWriter(ac, new FileOutputStream(analysisDataOutputDir + "/pd" + i)));
		}			
		
		//print some data			
		ps.addListener(new BasicDataWriter(new FileOutputStream(analysisDataOutputDir + "/ps")));
		sd.addListener(new BasicTimeDataWriter(ac, new FileOutputStream(analysisDataOutputDir + "/sd")));
		// pd.addListener(new BasicTimeDataWriter(ac, new FileOutputStream(analysisDataOutputDir + "/pd")));
				
		// also output the waveform for comparison
		ShortFrameSegmenter basicseg = new ShortFrameSegmenter(ac);
		basicseg.setWindow(new OneWindow().getDefault());
		basicseg.setChunkSize(512);
		basicseg.setHopSize(512);
		basicseg.addInput(ac.out);
		ac.out.addDependent(basicseg);
		basicseg.addListener(new BasicDataWriter(new FileOutputStream(analysisDataOutputDir + "/wave")));
				
		//time the playback to 2s
		ac.runForNSecondsNonRealTime(2);
	}
}
