|
Urbi SDK Remote for Java
2.7.5
|
00001 /* 00002 * Copyright (C) 2010-2011, Gostai S.A.S. 00003 * 00004 * This software is provided "as is" without warranty of any kind, 00005 * either expressed or implied, including but not limited to the 00006 * implied warranties of fitness for a particular purpose. 00007 * 00008 * See the LICENSE file for more information. 00009 */ 00010 00011 package urbi; 00012 00013 import javax.swing.*; 00014 import javax.swing.border.*; 00015 import java.awt.*; 00016 import java.awt.event.*; 00017 import java.io.*; 00018 import javax.sound.sampled.*; 00019 00020 /** 00021 * The SoundSampler class helps to capture, play and record audio data. 00022 * <p> 00023 * <p> 00024 * @author Bastien Saltel 00025 */ 00026 00027 public class SoundSampler extends JFrame 00028 { 00029 /** A status value indiucating if the sampler is in capture mode. */ 00030 protected boolean running; 00031 00032 private SoundAction action = null; 00033 00034 public SoundSampler() 00035 { 00036 super("Sound Sampler"); 00037 setDefaultCloseOperation(EXIT_ON_CLOSE); 00038 createUI(); 00039 setVisible(true); 00040 pack(); 00041 setVisible(true); 00042 } 00043 00044 public void setAction(SoundAction action) 00045 { 00046 this.action = action; 00047 } 00048 00049 /** 00050 * Creates the user interface to process the operations. 00051 * <p> 00052 */ 00053 private void createUI() 00054 { 00055 setFont(new Font("Serif", Font.PLAIN, 12)); 00056 setSize(200, 350); 00057 Container content = getContentPane(); 00058 00059 final JButton capture = new JButton("Capture"); 00060 final JButton stop = new JButton("Stop"); 00061 final JButton play = new JButton("Play"); 00062 00063 final JButton save = new JButton("Save"); 00064 00065 capture.setEnabled(true); 00066 stop.setEnabled(false); 00067 play.setEnabled(false); 00068 save.setEnabled(false); 00069 ActionListener captureListener = new ActionListener() 00070 { 00071 public void actionPerformed(ActionEvent e) 00072 { 00073 capture.setEnabled(false); 00074 stop.setEnabled(true); 00075 play.setEnabled(false); 00076 save.setEnabled(false); 00077 captureAudio(); 00078 } 00079 }; 00080 capture.addActionListener(captureListener); 00081 content.add(capture, BorderLayout.NORTH); 00082 00083 ActionListener stopListener = new ActionListener() 00084 { 00085 public void actionPerformed(ActionEvent e) 00086 { 00087 capture.setEnabled(true); 00088 stop.setEnabled(false); 00089 play.setEnabled(true); 00090 save.setEnabled(true); 00091 running = false; 00092 action.stopAudio(); 00093 } 00094 }; 00095 stop.addActionListener(stopListener); 00096 content.add(stop, BorderLayout.WEST); 00097 00098 ActionListener playListener = new ActionListener() 00099 { 00100 public void actionPerformed(ActionEvent e) 00101 { 00102 playAudio(); 00103 } 00104 }; 00105 play.addActionListener(playListener); 00106 content.add(play, BorderLayout.EAST); 00107 00108 ActionListener saveListener = new ActionListener() 00109 { 00110 public void actionPerformed(ActionEvent e) 00111 { 00112 FileDialog fd = new FileDialog(SoundSampler.this); 00113 fd.setVisible(true); 00114 if (fd.getFile() == null) 00115 return ; 00116 String path = fd.getDirectory() + fd.getFile(); 00117 saveAudio(path); 00118 } 00119 }; 00120 save.addActionListener(saveListener); 00121 Panel bottom = new Panel(new GridLayout(2, 1)); 00122 Panel topBottom = new Panel(); 00123 topBottom.add(save); 00124 bottom.add(topBottom); 00125 content.add(bottom, BorderLayout.SOUTH); 00126 00127 addWindowListener(new WindowAdapter() 00128 { 00129 public void windowClosing(WindowEvent e) 00130 { 00131 dispose(); 00132 System.exit(0); 00133 } 00134 }); 00135 } 00136 00137 private void captureAudio() 00138 { 00139 Runnable runner = new Runnable() 00140 { 00141 public void run() 00142 { 00143 running = true; 00144 action.captureAudio(); 00145 while (running) 00146 {} 00147 } 00148 }; 00149 Thread captureThread = new Thread(runner); 00150 captureThread.start(); 00151 } 00152 00153 private void playAudio() 00154 { 00155 Runnable runner = new Runnable() 00156 { 00157 public void run() 00158 { 00159 action.playAudio(); 00160 } 00161 }; 00162 Thread playThread = new Thread(runner); 00163 playThread.start(); 00164 } 00165 00166 private void saveAudio(final String path) 00167 { 00168 Runnable runner = new Runnable() 00169 { 00170 public void run() 00171 { 00172 action.saveAudio(path); 00173 } 00174 }; 00175 Thread saveThread = new Thread(runner); 00176 saveThread.start(); 00177 } 00178 }