/*
*
      
_______
                       
_____
   
_____ _____
  

*
     
|__
   
__|
                     
|
  
__ \ / ____|__ \
 

*
        
| | __ _ _ __ ___
  
______| || | (___ | |__) |
*
        
| |/ _` | '__/ __|/ _ \/ __| |
  
| |\___ \|___/
 

*
        
| | (_| | |
  
\__ \ (_) \__ \ |__| |____) | |
     

*
        
|_|\__,_|_|
  
|___/\___/|___/_____/|_____/|_|
     

*
                                                         

* -------------------------------------------------------------
*
* TarsosDSP is developed by Joren Six at IPEM, University Ghent
*
  

* -------------------------------------------------------------
*
*
  
Info:
 
http://0110.be/tag/TarsosDSP
*
  
Github:
 
https://github.com/JorenSix/TarsosDSP
*
  
Releases:
 
http://0110.be/releases/TarsosDSP/
*
  

*
  
TarsosDSP includes modified source code by various authors,
*
  
for credits and info, see README.
*
 

*/


package be.tarsos.dsp.example;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.reflect.InvocationTargetException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.TargetDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import be.tarsos.dsp.AudioDispatcher;
import be.tarsos.dsp.AudioEvent;
import be.tarsos.dsp.io.jvm.JVMAudioInputStream;
import be.tarsos.dsp.pitch.PitchDetectionHandler;
import be.tarsos.dsp.pitch.PitchDetectionResult;
import be.tarsos.dsp.pitch.PitchProcessor;
import be.tarsos.dsp.pitch.PitchProcessor.PitchEstimationAlgorithm;

public class PitchDetectorExample extends JFrame implements PitchDetectionHandler {

	
/**
	 
*
 

	 
*/
	
private static final long serialVersionUID = 3501426880288136245L;

	
private final JTextArea textArea;

	
private AudioDispatcher dispatcher;
	
private Mixer currentMixer;
	

	
private PitchEstimationAlgorithm algo;
	

	
private ActionListener algoChangeListener = new ActionListener(){
		
@Override
		
public void actionPerformed(final ActionEvent e) {
			
String name = e.getActionCommand();
			
PitchEstimationAlgorithm newAlgo = PitchEstimationAlgorithm.valueOf(name);
			
algo = newAlgo;
			
try {
				
setNewMixer(currentMixer);
			
} catch (LineUnavailableException e1) {
				
e1.printStackTrace();
			
} catch (UnsupportedAudioFileException e1) {
				
e1.printStackTrace();
			
}
	
}};

	
public PitchDetectorExample() {
		
this.setLayout(new GridLayout(0, 1));
		
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
this.setTitle("Pitch Detector");
		

		
JPanel inputPanel = new InputPanel();
		
add(inputPanel);
		
inputPanel.addPropertyChangeListener("mixer",
				
new PropertyChangeListener() {
					
@Override
					
public void propertyChange(PropertyChangeEvent arg0) {
						
try {
							
setNewMixer((Mixer) arg0.getNewValue());
						
} catch (LineUnavailableException e) {
							
// TODO Auto-generated catch block
							
e.printStackTrace();
						
} catch (UnsupportedAudioFileException e) {
							
// TODO Auto-generated catch block
							
e.printStackTrace();
						
}
					
}
				
});
		

		
algo = PitchEstimationAlgorithm.YIN;
		

		
JPanel pitchDetectionPanel = new PitchDetectionPanel(algoChangeListener);
		

		
add(pitchDetectionPanel);
	

		

		
textArea = new JTextArea();
		
textArea.setEditable(false);
		
add(new JScrollPane(textArea));
	
}


	

	
private void setNewMixer(Mixer mixer) throws LineUnavailableException,
			
UnsupportedAudioFileException {
		

		
if(dispatcher!= null){
			
dispatcher.stop();
		
}
		
currentMixer = mixer;
		

		
float sampleRate = 44100;
		
int bufferSize = 1024;
		
int overlap = 0;
		

		
textArea.append("Started listening with " + Shared.toLocalString(mixer.getMixerInfo().getName()) + "\n");

		
final AudioFormat format = new AudioFormat(sampleRate, 16, 1, true,
				
true);
		
final DataLine.Info dataLineInfo = new DataLine.Info(
				
TargetDataLine.class, format);
		
TargetDataLine line;
		
line = (TargetDataLine) mixer.getLine(dataLineInfo);
		
final int numberOfSamples = bufferSize;
		
line.open(format, numberOfSamples);
		
line.start();
		
final AudioInputStream stream = new AudioInputStream(line);

		
JVMAudioInputStream audioStream = new JVMAudioInputStream(stream);
		
// create a new dispatcher
		
dispatcher = new AudioDispatcher(audioStream, bufferSize,
				
overlap);

		
// add a processor
		
dispatcher.addAudioProcessor(new PitchProcessor(algo, sampleRate, bufferSize, this));
		

		
new Thread(dispatcher,"Audio dispatching").start();
	
}

	
public static void main(String... strings) throws InterruptedException,
			
InvocationTargetException {
		
SwingUtilities.invokeAndWait(new Runnable() {
			
@Override
			
public void run() {
				
try {
					
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
				
} catch (Exception e) {
					
//ignore failure to set default look en feel;
				
}
				
JFrame frame = new PitchDetectorExample();
				
frame.pack();
				
frame.setVisible(true);
			
}
		
});
	
}


	
@Override
	
public void handlePitch(PitchDetectionResult pitchDetectionResult,AudioEvent audioEvent) {
		
if(pitchDetectionResult.getPitch() != -1){
			
double timeStamp = audioEvent.getTimeStamp();
			
float pitch = pitchDetectionResult.getPitch();
			
float probability = pitchDetectionResult.getProbability();
			
double rms = audioEvent.getRMS() * 100;
			
String message = String.format("Pitch detected at %.2fs: %.2fHz ( %.2f probability, RMS: %.5f )\n", timeStamp,pitch,probability,rms);
			
textArea.append(message);
			
textArea.setCaretPosition(textArea.getDocument().getLength());
		
}
	
}
}