examples.be.tarsos.dsp.example.SampleExtractor Maven / Gradle / Ivy
The newest version!
/*
* _______ _____ _____ _____
* |__ __| | __ \ / ____| __ \
* | | __ _ _ __ ___ ___ ___| | | | (___ | |__) |
* | |/ _` | '__/ __|/ _ \/ __| | | |\___ \| ___/
* | | (_| | | \__ \ (_) \__ \ |__| |____) | |
* |_|\__,_|_| |___/\___/|___/_____/|_____/|_|
*
* -------------------------------------------------------------
*
* 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.BorderLayout;
import java.awt.GridLayout;
import java.awt.KeyEventPostProcessor;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import be.tarsos.dsp.AudioDispatcher;
import be.tarsos.dsp.StopAudioProcessor;
import be.tarsos.dsp.WaveformSimilarityBasedOverlapAdd;
import be.tarsos.dsp.WaveformSimilarityBasedOverlapAdd.Parameters;
import be.tarsos.dsp.io.jvm.AudioDispatcherFactory;
import be.tarsos.dsp.io.jvm.AudioPlayer;
import be.tarsos.dsp.io.jvm.JVMAudioInputStream;
import be.tarsos.dsp.io.jvm.WaveformWriter;
import be.tarsos.dsp.resample.RateTransposer;
public class SampleExtractor extends JFrame {
/**
*
*/
private static final long serialVersionUID = -8749523013657700525L;
private final JFileChooser fileChooser;
private final JSpinner startSelectionSpinner;
private final JSpinner endSelectionSpinner;
private final JSpinner[] centsSpinner;
private final JSpinner[] durationSpinner;
private final JLabel[] sampleLabel;
private final JButton playSelection;
private final JCheckBox[] saveSampleCheckboxes;
private File file;
private final char[] codes = {'e','r','t','y','u','i','o','p'};
private double currentDuration = 0;
private ChangeListener selectionChangedListener = new ChangeListener() {
@Override
public void stateChanged(ChangeEvent event) {
double endValue = (Double) endSelectionSpinner.getValue();
double startValue = (Double) startSelectionSpinner.getValue();
if(endValue < startValue){
startSelectionSpinner.removeChangeListener(this);
startSelectionSpinner.setValue(endValue);
startSelectionSpinner.addChangeListener(this);
}
for(int i = 0 ; i < durationSpinner.length;i++){
double duration = (Double) durationSpinner[i].getValue();
if(duration == 0 || duration == currentDuration){
durationSpinner[i].setValue(selectionDuration());
}
}
currentDuration = selectionDuration();
}
};
public SampleExtractor(){
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Sample Exctractor: Extract & Modify Samples");
fileChooser = new JFileChooser();
startSelectionSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 10000, 0.1));
startSelectionSpinner.setEnabled(false);
startSelectionSpinner.addChangeListener(selectionChangedListener);
endSelectionSpinner = new JSpinner(new SpinnerNumberModel(0,0,10000,0.1));
endSelectionSpinner.setEnabled(false);
endSelectionSpinner.addChangeListener(selectionChangedListener);
centsSpinner = new JSpinner[codes.length];
durationSpinner = new JSpinner[codes.length];
sampleLabel = new JLabel[codes.length];
saveSampleCheckboxes = new JCheckBox[codes.length];
playSelection = new JButton("Play");
playSelection.setEnabled(false);
playSelection.addActionListener(new ActionListener() {
AudioDispatcher dispatcher = null;
@Override
public void actionPerformed(ActionEvent arg0) {
if(((Double)startSelectionSpinner.getValue())<((Double)endSelectionSpinner.getValue())){
try {
dispatcher = AudioDispatcherFactory.fromFile(file, 1024, 0);
dispatcher.skip((Double)startSelectionSpinner.getValue());
dispatcher.addAudioProcessor(new StopAudioProcessor((Double)endSelectionSpinner.getValue()));
dispatcher.addAudioProcessor(new AudioPlayer(JVMAudioInputStream.toAudioFormat(dispatcher.getFormat())));
} catch (UnsupportedAudioFileException e1) {
} catch (IOException e1) {
} catch (LineUnavailableException e) {
}
new Thread(dispatcher).start();
}
}
});
JPanel fileChooserPanel = new JPanel(new GridLayout(2,4));
fileChooserPanel.setBorder(new TitledBorder("1. Choose your audio to extract samples from (wav mono)"));
JButton chooseFileButton = new JButton("Load a file...");
chooseFileButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
int returnVal = fileChooser.showOpenDialog(SampleExtractor.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
setFile(file);
} else {
//canceled
}
}
});
fileChooserPanel.add(new JLabel("Load a file"));
fileChooserPanel.add(new JLabel("Start (s)"));
fileChooserPanel.add(new JLabel("End (s)"));
fileChooserPanel.add(new JLabel("Play Selection"));
fileChooserPanel.add(chooseFileButton);
fileChooserPanel.add(startSelectionSpinner);
fileChooserPanel.add(endSelectionSpinner);
fileChooserPanel.add(playSelection);
this.add(fileChooserPanel,BorderLayout.NORTH);
JPanel samplePanel = new JPanel(new GridLayout(0,1));
JPanel sampleSubPanel = new JPanel(new GridLayout(1,0));
sampleSubPanel.add(new JLabel("Key"));
sampleSubPanel.add(new JLabel("Cents"));
sampleSubPanel.add(new JLabel("Duration"));
samplePanel.add(sampleSubPanel);
samplePanel.add(sampleSubPanel);
final KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
for(int i = 0;i