Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

SegmenterPanel.java

00001  /*
00002  *  Copyright 2006-2007 Columbia University.
00003  *
00004  *  This file is part of MEAPsoft.
00005  *
00006  *  MEAPsoft is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License version 2 as
00008  *  published by the Free Software Foundation.
00009  *
00010  *  MEAPsoft is distributed in the hope that it will be useful, but
00011  *  WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with MEAPsoft; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00018  *  02110-1301 USA
00019  *
00020  *  See the file "COPYING" for the text of the license.
00021  */
00022 
00023 package com.meapsoft.gui;
00024 
00025 import java.awt.*;
00026 import java.awt.event.*;
00027 import java.io.*;
00028 import java.util.*;
00029 import javax.swing.*; 
00030 import javax.swing.border.*; 
00031 
00032 import com.meapsoft.*;
00033 
00034 
00041 public class SegmenterPanel extends MEAPsoftGUIPanel
00042 {
00043         //segmenter GUI
00044         JCheckBox enableBox;
00045         JRadioButton eventStyleButton;
00046         JRadioButton beatStyleButton;
00047         JCheckBox firstFrameBox;
00048         JTextField inputSoundFileField;
00049         JLabel outputSegFileLabel;
00050         JSlider thresholdSlider;
00051     JSlider densitySlider;
00052     JCheckBox halfTempoBox;
00053     JButton listenButton;
00054 
00055     JPanel controlPanel;
00056     JPanel eventPanel;
00057     JPanel beatPanel;
00058     
00062     public SegmenterPanel(MEAPsoftGUI msg)
00063     {
00064         super(msg);
00065         BuildSegmenterGUI();
00066 
00067         title = "Segmenter";
00068         helpURL += "#" + title;
00069     }
00070 
00071         private void BuildSegmenterGUI()
00072         {
00073                 Color c = new Color((int)(Math.random() * 127 + 127),
00074                                         (int)(Math.random() * 127 + 127),
00075                                         (int)(Math.random() * 127 + 127));
00076         color = c;
00077 
00078                 setBackground(c);
00079                 BoxLayout sbl = new BoxLayout(this, BoxLayout.Y_AXIS);
00080                 setLayout(sbl);
00081                 
00082                 JPanel segmenterEnablePanel = new JPanel();
00083 
00084                 segmenterEnablePanel.setBackground(c);
00085                 
00086                 enableBox = new JCheckBox("ENABLE SEGMENTER");
00087                 enableBox.setBackground(c);
00088                 enableBox.setSelected(true);
00089                 segmenterEnablePanel.add(enableBox);
00090                 
00091                 helpButton = new JLabel("(help)");
00092                 //helpButton.setBackground(c.darker());
00093                 helpButton.setForeground(Color.blue);
00094                 helpButton.addMouseListener(this);
00095                 segmenterEnablePanel.add(helpButton);
00096                 
00097                 add(segmenterEnablePanel);
00098                 
00099         Box segmenterControlsPanel = Box.createVerticalBox();
00100                 segmenterControlsPanel.setBackground(c);
00101                 TitledBorder title = BorderFactory.createTitledBorder(
00102                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), 
00103                         "Segmenter Controls");
00104                 title.setTitleJustification(TitledBorder.CENTER);
00105                 segmenterControlsPanel.setBorder(title);
00106                         
00107                 JPanel inputFileNamePanel = new JPanel();
00108                 inputFileNamePanel.setBackground(c);
00109                 
00110                 JLabel inputFileNameBoxLabel = new JLabel("input sound file:");
00111                 inputFileNamePanel.add(inputFileNameBoxLabel);
00112         
00113                 inputSoundFileField = new JTextField("chris_mann.wav");
00114                 inputSoundFileField.setColumns(20);
00115                 inputSoundFileField.addActionListener(this);
00116                 inputSoundFileField.setActionCommand("setInputFile");
00117                 inputFileNamePanel.add(inputSoundFileField);
00118                 
00119                 JButton inputBrowseButton = new JButton("browse");
00120                 inputBrowseButton.setBackground(c);
00121                 inputBrowseButton.addActionListener(this);
00122                 inputBrowseButton.setActionCommand("browseInputFile");
00123                 inputFileNamePanel.add(inputBrowseButton);
00124                 
00125                 listenButton = new JButton("listen");
00126                 listenButton.setBackground(c);
00127                 listenButton.addActionListener(this);
00128                 listenButton.setActionCommand("listen");
00129                 inputFileNamePanel.add(listenButton);
00130                 
00131                 segmenterControlsPanel.add(inputFileNamePanel);
00132                 
00133                 JPanel detectorTypePanel = new JPanel();
00134                 detectorTypePanel.setBackground(c);
00135                 ButtonGroup onsetDetectorTypeGroup = new ButtonGroup();
00136                 eventStyleButton = new JRadioButton("detect events");
00137                 eventStyleButton.setBackground(c);
00138         eventStyleButton.addActionListener(this);
00139         eventStyleButton.setActionCommand("event_detector");
00140                 beatStyleButton = new JRadioButton("detect beats");
00141                 beatStyleButton.setBackground(c);
00142         beatStyleButton.addActionListener(this);
00143         beatStyleButton.setActionCommand("beat_detector");
00144                 onsetDetectorTypeGroup.add(eventStyleButton);
00145                 onsetDetectorTypeGroup.add(beatStyleButton);
00146                 detectorTypePanel.add(eventStyleButton);
00147                 detectorTypePanel.add(beatStyleButton);
00148                 eventStyleButton.setSelected(true);
00149 
00150                 segmenterControlsPanel.add(detectorTypePanel);
00151                 
00152 
00153         eventPanel = new JPanel();
00154         eventPanel.setLayout(new BoxLayout(eventPanel, BoxLayout.Y_AXIS));
00155                 eventPanel.setBackground(c);
00156         JPanel thresholdPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
00157         thresholdPanel.setBackground(c);
00158                 JLabel thresholdLabel = new JLabel("segment sensitivity: ");
00159                 thresholdPanel.add(thresholdLabel);
00160                 thresholdSlider = new JSlider(JSlider.HORIZONTAL, 0, 20, 1);
00161                 thresholdSlider.setBackground(c);
00162                 thresholdSlider.setValue(10);
00163                 Hashtable labelTable = new Hashtable();
00164                 labelTable.put( new Integer(0), new JLabel("low") );
00165                 labelTable.put( new Integer(20), new JLabel("high") );
00166                 thresholdSlider.setLabelTable( labelTable );
00167                 thresholdSlider.setPaintLabels(true);
00168                 thresholdSlider.setMajorTickSpacing(2);
00169                 thresholdSlider.setPaintTicks(true);
00170                 thresholdPanel.add(thresholdSlider);
00171         eventPanel.add(thresholdPanel);
00172 
00173         JPanel densityPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
00174         densityPanel.setBackground(c);
00175         JLabel densityLabel = new JLabel("segment density: ");
00176         densityPanel.add(densityLabel);
00177         densitySlider = new JSlider(JSlider.HORIZONTAL, 0, 20, 1);
00178         densitySlider.setBackground(c);
00179         densitySlider.setValue(15);
00180         densitySlider.setMajorTickSpacing(2);
00181         densitySlider.setPaintTicks(true);
00182                 densitySlider.setLabelTable(labelTable);
00183                 densitySlider.setPaintLabels(true);
00184         densityPanel.add(densitySlider);
00185         eventPanel.add(densityPanel);
00186 
00187                 beatPanel = new JPanel();
00188         beatPanel.setBackground(c);
00189         halfTempoBox = new JCheckBox("cut tempo in half");
00190         halfTempoBox.setBackground(c);
00191         halfTempoBox.setSelected(false);
00192         beatPanel.add(halfTempoBox);
00193 
00194         // controlPanel is a wrapper around the event/beat detector knobs
00195         controlPanel = new JPanel();
00196                 controlPanel.setBackground(c);
00197         controlPanel.add(eventPanel);
00198                 segmenterControlsPanel.add(controlPanel);
00199                 
00200                 firstFrameBox = new JCheckBox("1st event = track start");
00201                 firstFrameBox.setBackground(c);
00202                 firstFrameBox.setSelected(true);
00203         firstFrameBox.setAlignmentX(Component.CENTER_ALIGNMENT);
00204                 segmenterControlsPanel.add(firstFrameBox);
00205         
00206         add(segmenterControlsPanel);
00207 
00208                 JPanel outputSegFileNamePanel = new JPanel();
00209                 outputSegFileNamePanel.setBackground(c);
00210                 
00211                 JLabel sFNL = new JLabel("output segment file: ");
00212                 outputSegFileNamePanel.add(sFNL);
00213                 outputSegFileLabel = new JLabel(" " + dataBaseName + ".seg ");
00214                 outputSegFileLabel.setOpaque(true);
00215                 outputSegFileLabel.setBackground(c.darker());
00216                 outputSegFileNamePanel.add(outputSegFileLabel);
00217 
00218         add(Box.createRigidArea(new Dimension(0, 10)));
00219         add(outputSegFileNamePanel);
00220         }
00221 
00222         public void actionPerformed(ActionEvent arg0)
00223         {
00224                 String command = arg0.getActionCommand();
00225 
00226                 if (command.equals("listen"))
00227                 {
00228             if (inputSoundFileNameFull == null)
00229             {
00230                 GUIUtils.ShowDialog("You need to pick an input file!!!", GUIUtils.MESSAGE, meapsoftGUI.jframe);
00231                 return;
00232             }
00233             PlaySoundFile(inputSoundFileNameFull);
00234                 }
00235                 else if (command.equals("browseInputFile"))
00236                 {
00237                         String names[] = GUIUtils.FileSelector(GUIUtils.OPEN, meapsoftGUI.dataDirectory, meapsoftGUI.jframe);
00238                         
00239                         if (names[0] == null)
00240                                 return;
00241 
00242                         SetInputFileName(names);
00243                 }
00244                 else if (command.equals("setInputFile"))
00245                 {
00246             String name = inputSoundFileField.getText(); 
00247             // default directory
00248             String names[] = {meapsoftGUI.dataDirectory + slash + name, name};
00249 
00250             // does outputFileNameField contain a full path?
00251             if((new File(name)).isAbsolute())
00252                 names[0] = name;
00253 
00254             String[] nameSplit = name.split("["+slash+"]");
00255             names[1] = nameSplit[nameSplit.length-1];
00256 
00257                         SetInputFileName(names);
00258                 }
00259         else if(command.equals("event_detector"))
00260         {
00261             controlPanel.add(eventPanel);
00262             controlPanel.remove(beatPanel);
00263             RefreshGUI();
00264         }
00265         else if(command.equals("beat_detector"))
00266         {
00267             controlPanel.remove(eventPanel);
00268             controlPanel.add(beatPanel);
00269             RefreshGUI();
00270         }
00271     }
00272         
00273         public synchronized int run()
00274         {               
00275         if(!enableBox.isSelected())
00276             return 0;
00277 
00278         if (inputSoundFileNameFull == null)
00279         {
00280             GUIUtils.ShowDialog("You need to pick an input file!!!", GUIUtils.MESSAGE, meapsoftGUI.jframe);
00281             return -1;
00282         }
00283 
00284         // for event detector:
00285         //want value to be between 0 and 3
00286         double thresh = 3.0-thresholdSlider.getValue()/6.666;
00287         // want value between 1 and 0
00288         double smtime = (20.0-densitySlider.getValue())/20.0;
00289 
00290         // for beat detector:
00291         double tempoMult = 1.0;
00292         if(halfTempoBox.isSelected())
00293             tempoMult = 0.5;
00294 
00295                 boolean beatOnsetDetector = beatStyleButton.isSelected();
00296                 boolean firstFrameOnset = firstFrameBox.isSelected();
00297                 
00298                 String segmentsFileName = dataDirectory + slash + outputSegmentsFileName;
00299                 
00300                 Segmenter segmenter = new Segmenter(inputSoundFileNameFull,
00301                         segmentsFileName, thresh, smtime, beatOnsetDetector, firstFrameOnset);
00302         segmenter.setTempoMultiplier(tempoMult);
00303         segmenter.writeMEAPFile = meapsoftGUI.writeMEAPFile;
00304 
00305         JPanel progressPanel = new JPanel();
00306         progressPanel.add(new JLabel("Segmenting: "));
00307         JProgressBar progressBar = new JProgressBar(segmenter.getProgress());
00308         progressBar.setStringPainted(true);
00309         progressPanel.add(progressBar);
00310         meapsoftGUI.setProgressPanel(progressPanel);
00311 
00312         try
00313         {
00314             segmentFile = segmenter.processAudioFile(inputSoundFileNameFull);
00315 
00316             if(segmenter.writeMEAPFile)
00317                 segmentFile.writeFile();
00318 
00319             segmentFile = segmenter.getSegFile();
00320         }
00321         catch (Exception e)
00322         {
00323             GUIUtils.ShowDialog(e, "Error running Segmenter", GUIUtils.MESSAGE, meapsoftGUI.jframe);
00324 
00325             return -1;
00326         }
00327 
00328         //System.out.println("found "+segmentFile.chunks.size()+" chunks.");
00329 
00330         return 0;
00331         }
00332 }

Generated on Tue Feb 6 19:02:27 2007 for MEAPsoft by doxygen1.2.18