package jmsltestsuite.jsyn2;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import com.softsynth.jmsl.JMSL;
import com.softsynth.jmsl.JMSLMixerContainer;
import com.softsynth.jmsl.MusicJob;
import com.softsynth.jmsl.jsyn2.JSynMusicDevice;
import com.softsynth.jmsl.jsyn2.SamplePlayingInstrument;
import com.softsynth.shared.time.TimeStamp;

public class TestSamplePitchbend extends MusicJob {

	// private SamplePlayingInstrument instrument;
	private JMSLMixerContainer mixer;

	int voiceToken1 = -1;
	int voiceToken2 = -1;

	void buildMixer() {
		mixer = new JMSLMixerContainer();
		mixer.addInstrument(instrument);
		mixer.start();
	}

	public void turnVoicesOn() {
		double playTime = JMSL.now();
		double[] d1 = { 0.0, 59, 0.95, 8.0 };
		// start two voices on the same pitch, but tokens keep them distinct.
		voiceToken1 = (Integer) instrument.on(playTime, 1.0, d1);
		voiceToken2 = (Integer) instrument.on(playTime, 1.0, d1);
	}

	public double repeat(double playTime) {
		com.jsyn.util.VoiceAllocator voiceAllocator = ((SamplePlayingInstrument) getInstrument()).getVoiceAllocator();
		JSynMusicDevice dev = JSynMusicDevice.instance();
		double jsynOnTime = dev.jmslTimeToJSynTime(playTime);
		TimeStamp ts = new TimeStamp(jsynOnTime);

		double bend1 = getRepeatCount() - 100;
		double bend2 = 100 - bend1;
		// bend one voice up the other down
		voiceAllocator.setPort(voiceToken1, "centDeviation", bend1, ts);
		voiceAllocator.setPort(voiceToken2, "centDeviation", bend2, ts);

		return playTime + 0.05;
	}

	public double stop(double playTime) {
		System.out.println("MusicJob calling instrument.off() on both tokens");
		((SamplePlayingInstrument) instrument).off(voiceToken1, playTime);
		((SamplePlayingInstrument) instrument).off(voiceToken2, playTime);
		return playTime;
	}

	public void buildInstrument() {
		setInstrument(new SamplePlayingInstrument());
		((SamplePlayingInstrument) getInstrument()).setPlaybackMode(SamplePlayingInstrument.SAMPLE_LOOPING);
		((SamplePlayingInstrument) getInstrument()).setDirectory(new File("D:/documents/JMSLScoreWork/JMSLScoreSamples"));
		instrument.getMusicDevice().open();

		((SamplePlayingInstrument) getInstrument()).addSamplePitch("yves/SopranoSaxShort.wav", 65);
		((SamplePlayingInstrument) getInstrument()).addSamplePitch("yves/SopranoSaxOctave.wav", 77);

		((SamplePlayingInstrument) getInstrument()).buildFromAttributes();
	}

	public static void main(String args[]) {

		JMSL.clock.setAdvance(0.2);

		final TestSamplePitchbend test = new TestSamplePitchbend();
		test.buildInstrument();
		test.buildMixer();
		test.setRepeats(200);

		java.awt.Frame f = new java.awt.Frame("JSyn2 TestSamplePitchbend");
		f.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				JMSL.closeMusicDevices();
				System.exit(0);
			}
		});
		f.add(test.mixer.getPanAmpControlPanel());
		Button stopButton;
		f.add(BorderLayout.SOUTH, stopButton = new Button("Start"));
		f.pack();
		f.setVisible(true);

		stopButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				test.turnVoicesOn();
				test.launch(JMSL.now() + 3);
			}
		});
		f.pack();

	}

}