| 1 | package speechrecorder; |
|---|
| 2 | /** |
|---|
| 3 | * |
|---|
| 4 | * Note: This is a copy of the CapturePlayback class provided in the |
|---|
| 5 | * JavaSoundDemo, but modified for use with the SpeechRecorder |
|---|
| 6 | * applet. |
|---|
| 7 | * |
|---|
| 8 | */ |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | /* |
|---|
| 12 | * @(#)CapturePlayback.java 1.11 99/12/03 |
|---|
| 13 | * |
|---|
| 14 | * Copyright (c) 1999 Sun Microsystems, Inc. All Rights Reserved. |
|---|
| 15 | * |
|---|
| 16 | * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, |
|---|
| 17 | * modify and redistribute this software in source and binary code form, |
|---|
| 18 | * provided that i) this copyright notice and license appear on all copies of |
|---|
| 19 | * the software; and ii) Licensee does not utilize the software in a manner |
|---|
| 20 | * which is disparaging to Sun. |
|---|
| 21 | * |
|---|
| 22 | * This software is provided "AS IS," without a warranty of any kind. ALL |
|---|
| 23 | * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY |
|---|
| 24 | * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR |
|---|
| 25 | * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE |
|---|
| 26 | * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING |
|---|
| 27 | * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS |
|---|
| 28 | * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, |
|---|
| 29 | * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER |
|---|
| 30 | * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF |
|---|
| 31 | * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE |
|---|
| 32 | * POSSIBILITY OF SUCH DAMAGES. |
|---|
| 33 | * |
|---|
| 34 | * This software is not designed or intended for use in on-line control of |
|---|
| 35 | * aircraft, air traffic, aircraft navigation or aircraft communications; or in |
|---|
| 36 | * the design, construction, operation or maintenance of any nuclear |
|---|
| 37 | * facility. Licensee represents and warrants that it will not use or |
|---|
| 38 | * redistribute the Software for such purposes. |
|---|
| 39 | */ |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | import java.awt.*; |
|---|
| 43 | import java.awt.event.*; |
|---|
| 44 | import java.awt.font.*; |
|---|
| 45 | import java.awt.geom.Line2D; |
|---|
| 46 | import java.io.*; |
|---|
| 47 | import java.net.URL; |
|---|
| 48 | import java.text.*; |
|---|
| 49 | import java.util.Vector; |
|---|
| 50 | import javax.sound.sampled.*; |
|---|
| 51 | import javax.swing.*; |
|---|
| 52 | import javax.swing.border.*; |
|---|
| 53 | import net.sf.postlet.UploadManager; |
|---|
| 54 | import netscape.javascript.JSObject; |
|---|
| 55 | import java.util.zip.ZipEntry; |
|---|
| 56 | import java.util.zip.ZipOutputStream; |
|---|
| 57 | import java.util.Calendar; |
|---|
| 58 | import java.util.Random; |
|---|
| 59 | import speechrecorder.License; |
|---|
| 60 | import speechrecorder.Prompts; |
|---|
| 61 | import speechrecorder.LabelLocalizer; |
|---|
| 62 | import speechrecorder.MultiLineLabel; |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * Capture/Playback sample. Record audio in different formats and then playback the recorded audio. The captured audio can be saved either as a WAVE, AU or AIFF. Or load an audio file for streaming playback. |
|---|
| 66 | * @version @(#)CapturePlayback.java 1.11 99/12/03 |
|---|
| 67 | * @author Brian Lichtenwalter |
|---|
| 68 | */ |
|---|
| 69 | @SuppressWarnings("serial") |
|---|
| 70 | public class CapturePlayback extends JPanel implements ActionListener, net.sf.postlet.PostletInterface { |
|---|
| 71 | |
|---|
| 72 | final int bufSize = 16384; |
|---|
| 73 | public static int BUFFER_SIZE = 10240; |
|---|
| 74 | public static final String fileType = "wav"; |
|---|
| 75 | public static final int samplingRate = 48000;// jre 1.4.2 only supports max of 44100 |
|---|
| 76 | public static final int samplingRateFormat = 16; |
|---|
| 77 | public static final int numberChannels = 1; |
|---|
| 78 | |
|---|
| 79 | AudioFormat format = new AudioFormat(samplingRate, samplingRateFormat, numberChannels, true, false); |
|---|
| 80 | |
|---|
| 81 | Capture capture = new Capture(); |
|---|
| 82 | Playback playback = new Playback(); |
|---|
| 83 | ConvertAndUpload convertAndUpload = new ConvertAndUpload(); |
|---|
| 84 | CapturePlayback capturePlayback; // Needed for referencing within the inner classes |
|---|
| 85 | |
|---|
| 86 | AudioInputStream audioInputStream; |
|---|
| 87 | SamplingGraph samplingGraph; |
|---|
| 88 | |
|---|
| 89 | // int numberofPrompts = 5; |
|---|
| 90 | int numberofPrompts = 10; |
|---|
| 91 | JButton [] playA = new JButton [numberofPrompts]; //creates the array, not the objects! |
|---|
| 92 | JButton [] captA = new JButton [numberofPrompts]; //creates the array, not the objects! |
|---|
| 93 | |
|---|
| 94 | JButton uploadB; |
|---|
| 95 | JButton moreInfoB; |
|---|
| 96 | JButton aboutB; |
|---|
| 97 | |
|---|
| 98 | boolean [] play_stateA = new boolean [numberofPrompts]; |
|---|
| 99 | boolean [] capt_stateA = new boolean [numberofPrompts]; |
|---|
| 100 | |
|---|
| 101 | License licenseObject = new License(); |
|---|
| 102 | String license = licenseObject.getLicense(); |
|---|
| 103 | String VFlicense = licenseObject.getVFLicense(); |
|---|
| 104 | |
|---|
| 105 | JTextField textField; |
|---|
| 106 | |
|---|
| 107 | String fileName = "untitled"; |
|---|
| 108 | String errStr; |
|---|
| 109 | |
|---|
| 110 | double [] durationA= new double [numberofPrompts]; |
|---|
| 111 | double duration = 0; |
|---|
| 112 | |
|---|
| 113 | double seconds; |
|---|
| 114 | |
|---|
| 115 | long [] totalBytesWrittenA= new long [numberofPrompts]; |
|---|
| 116 | long totalBytesWritten = 0L; |
|---|
| 117 | |
|---|
| 118 | File file; |
|---|
| 119 | Vector lines = new Vector(); |
|---|
| 120 | |
|---|
| 121 | private File wavFile; |
|---|
| 122 | private File[] wavFileA = new File [numberofPrompts]; |
|---|
| 123 | |
|---|
| 124 | private File [] uploadWavFileA = new File [numberofPrompts]; |
|---|
| 125 | |
|---|
| 126 | private File promptsFile; |
|---|
| 127 | private File readmeFile; |
|---|
| 128 | private File licenseFile; |
|---|
| 129 | private File licenseNoticeFile; |
|---|
| 130 | |
|---|
| 131 | JProgressBar progBar; |
|---|
| 132 | int sentBytes; |
|---|
| 133 | int totalBytes; |
|---|
| 134 | int buttonClicked; |
|---|
| 135 | |
|---|
| 136 | JTextField subjectBox; |
|---|
| 137 | String subject; |
|---|
| 138 | |
|---|
| 139 | String [] promptA = new String [numberofPrompts]; |
|---|
| 140 | String [] promptidA = new String [numberofPrompts];; |
|---|
| 141 | |
|---|
| 142 | // required for the PHP uploader to work properly |
|---|
| 143 | String fileFieldName = "userfile"; |
|---|
| 144 | // ############ Localized Fields #################################### |
|---|
| 145 | JTextField usernameTextField; |
|---|
| 146 | String usernamePanelLabel; |
|---|
| 147 | String userName; |
|---|
| 148 | String usernamePanelText; |
|---|
| 149 | |
|---|
| 150 | String copyrightName; |
|---|
| 151 | String gplAccepted; |
|---|
| 152 | |
|---|
| 153 | String pleaseSelect; |
|---|
| 154 | String notApplicable; |
|---|
| 155 | |
|---|
| 156 | String genderPanelLabel; |
|---|
| 157 | JComboBox genderChooser; |
|---|
| 158 | String[] genderSelection; |
|---|
| 159 | String gender; |
|---|
| 160 | |
|---|
| 161 | String ageRangePanelLabel; |
|---|
| 162 | JComboBox ageRangeChooser; |
|---|
| 163 | String[] ageSelection; |
|---|
| 164 | String ageRange; |
|---|
| 165 | |
|---|
| 166 | String dialectPanelLabel; |
|---|
| 167 | JComboBox dialectChooser; |
|---|
| 168 | String[] dialectSelection; |
|---|
| 169 | String dialect; |
|---|
| 170 | |
|---|
| 171 | String microphonePanelLabel; |
|---|
| 172 | JComboBox microphoneChooser; |
|---|
| 173 | String[] microphoneSelection; |
|---|
| 174 | String microphone; |
|---|
| 175 | |
|---|
| 176 | String uploadText; |
|---|
| 177 | String uploadButtonLabel; |
|---|
| 178 | |
|---|
| 179 | String moreInfoText; |
|---|
| 180 | String moreInfoButtonLabel; |
|---|
| 181 | |
|---|
| 182 | String disclaimerText; |
|---|
| 183 | String aboutButtonLabel; |
|---|
| 184 | |
|---|
| 185 | String recordButton; |
|---|
| 186 | String stopButton; |
|---|
| 187 | String playButton; |
|---|
| 188 | |
|---|
| 189 | String peakWarningLabel; |
|---|
| 190 | String sampleGraphFileLabel; |
|---|
| 191 | String sampleGraphLengthLabel; |
|---|
| 192 | String sampleGraphPositionLabel; |
|---|
| 193 | String uploadingMessageLabel; |
|---|
| 194 | String uploadCompletedMessageLabel; |
|---|
| 195 | // ############ Localized Fields #################################### |
|---|
| 196 | URL endPageURL; |
|---|
| 197 | URL helpPageURL; |
|---|
| 198 | URL destinationURL; |
|---|
| 199 | String language; |
|---|
| 200 | String endpage; |
|---|
| 201 | String helppage; |
|---|
| 202 | String cookie; |
|---|
| 203 | |
|---|
| 204 | String tempdir = getTempDir(); |
|---|
| 205 | public CapturePlayback(String language, URL destinationURL, |
|---|
| 206 | URL endPageURL, URL helpPageURL, String cookie) { |
|---|
| 207 | // ############ Localized Fields #################################### |
|---|
| 208 | // !!!!!! |
|---|
| 209 | this.language = language; |
|---|
| 210 | //this.language = "BG"; // for testing |
|---|
| 211 | // !!!!!! |
|---|
| 212 | System.err.println("CapturePlayback Language:" + this.language + ":"); // debug |
|---|
| 213 | |
|---|
| 214 | LabelLocalizer labels = new LabelLocalizer(this.language); |
|---|
| 215 | usernamePanelLabel = labels.getUsernamePanelLabel(); |
|---|
| 216 | usernamePanelText = labels.getUsernamePanelText(); |
|---|
| 217 | |
|---|
| 218 | copyrightName = labels.getCopyrightName(); |
|---|
| 219 | gplAccepted = labels.getGplAccepted(); |
|---|
| 220 | |
|---|
| 221 | pleaseSelect = labels.getPleaseSelect(); |
|---|
| 222 | notApplicable = labels.getNotApplicable(); |
|---|
| 223 | |
|---|
| 224 | genderPanelLabel = labels.getGenderPanelLabel(); |
|---|
| 225 | genderSelection = labels.getGenderSelection(); |
|---|
| 226 | gender = notApplicable; // default selection |
|---|
| 227 | |
|---|
| 228 | ageRangePanelLabel = labels.getAgeRangePanelLabel(); |
|---|
| 229 | ageSelection = labels.getAgeSelection(); |
|---|
| 230 | ageRange = notApplicable; // default selection |
|---|
| 231 | |
|---|
| 232 | dialectPanelLabel = labels.getDialectPanelLabel(); |
|---|
| 233 | dialectSelection = labels.getDialectSelection(); |
|---|
| 234 | dialect = notApplicable; // default selection |
|---|
| 235 | |
|---|
| 236 | microphonePanelLabel = labels.getMicrophonePanelLabel(); |
|---|
| 237 | microphoneSelection = labels.getMicrophoneSelection(); |
|---|
| 238 | microphone = notApplicable; // default selection |
|---|
| 239 | |
|---|
| 240 | uploadText = labels.getUploadText(); |
|---|
| 241 | uploadButtonLabel = labels.getUploadButtonLabel(); |
|---|
| 242 | |
|---|
| 243 | moreInfoText = labels.getMoreInfoText(); |
|---|
| 244 | moreInfoButtonLabel = labels.getMoreInfoButtonLabel(); |
|---|
| 245 | |
|---|
| 246 | disclaimerText = labels.getDisclaimerText() ; |
|---|
| 247 | aboutButtonLabel = labels.getAboutButtonLabel(); |
|---|
| 248 | |
|---|
| 249 | recordButton = labels.getRecordButton(); |
|---|
| 250 | stopButton = labels.getStopButton(); |
|---|
| 251 | playButton = labels.getPlayButton(); |
|---|
| 252 | |
|---|
| 253 | peakWarningLabel = labels.getPeakWarningLabel(); |
|---|
| 254 | sampleGraphFileLabel = labels.getSampleGraphFileLabel(); |
|---|
| 255 | sampleGraphLengthLabel = labels.getSampleGraphLengthLabel(); |
|---|
| 256 | sampleGraphPositionLabel = labels.getSampleGraphPositionLabel(); |
|---|
| 257 | |
|---|
| 258 | uploadingMessageLabel = labels.getUploadingMessageLabel(); |
|---|
| 259 | uploadCompletedMessageLabel = labels.getUploadCompletedMessageLabel(); |
|---|
| 260 | // ############ Localized Fields #################################### |
|---|
| 261 | // if (promptA[0] == null || promptA[0].length() == 0) { |
|---|
| 262 | //Prompts prompts = new Prompts(numberofPrompts, this.language); |
|---|
| 263 | //String [][] promptArray = prompts.getPrompts(); |
|---|
| 264 | |
|---|
| 265 | // !!!!!! |
|---|
| 266 | String [][] promptArray = (new Prompts(numberofPrompts,this.language)).getPrompts(); |
|---|
| 267 | for (int i = 0; i < numberofPrompts; i++) { |
|---|
| 268 | this.promptidA [i] = promptArray[0][i]; |
|---|
| 269 | this.promptA [i] = promptArray[1][i]; |
|---|
| 270 | // System.err.println("Prompts:" + this.promptidA[i] + ":"+ this.promptA [i]); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | // this.language = language; |
|---|
| 274 | this.destinationURL = destinationURL; |
|---|
| 275 | this.endPageURL = endPageURL; |
|---|
| 276 | this.helpPageURL = helpPageURL; |
|---|
| 277 | this.cookie = cookie; |
|---|
| 278 | |
|---|
| 279 | capturePlayback = this; |
|---|
| 280 | |
|---|
| 281 | // Create WAV files to hold recordings |
|---|
| 282 | try { |
|---|
| 283 | for (int i = 0; i < numberofPrompts; i++) { |
|---|
| 284 | wavFileA [i] = new File(tempdir + "wavFile" + i + ".wav"); |
|---|
| 285 | wavFileA[i].deleteOnExit(); |
|---|
| 286 | } |
|---|
| 287 | for (int i = 0; i < numberofPrompts; i++) { |
|---|
| 288 | uploadWavFileA[i] = new File(tempdir + this.promptidA [i] + ".wav"); |
|---|
| 289 | uploadWavFileA[i].deleteOnExit(); |
|---|
| 290 | } |
|---|
| 291 | promptsFile = new File(tempdir + "prompts.txt"); |
|---|
| 292 | promptsFile.deleteOnExit(); |
|---|
| 293 | readmeFile = new File(tempdir + "readme.txt"); |
|---|
| 294 | readmeFile.deleteOnExit(); |
|---|
| 295 | licenseFile = new File(tempdir + "GPL_license.txt"); |
|---|
| 296 | licenseFile.deleteOnExit(); |
|---|
| 297 | licenseNoticeFile = new File(tempdir + "license.txt"); |
|---|
| 298 | licenseFile.deleteOnExit(); |
|---|
| 299 | } catch (Exception e) { |
|---|
| 300 | System.err.println("Unable to create WAV cache file for storing audio\n" + e); |
|---|
| 301 | return; |
|---|
| 302 | } |
|---|
| 303 | for (int i = 0; i < numberofPrompts; i++) { |
|---|
| 304 | System.err.println("CapturePlayback's WAV file for recording uploadWavFile" + i + "is:" + uploadWavFileA[i]); |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | // ############ GUI Display #################################### |
|---|
| 308 | setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
|---|
| 309 | EmptyBorder eb = new EmptyBorder(5,5,5,5); |
|---|
| 310 | SoftBevelBorder sbb = new SoftBevelBorder(SoftBevelBorder.LOWERED); |
|---|
| 311 | |
|---|
| 312 | JPanel p2 = new JPanel(); |
|---|
| 313 | p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS)); |
|---|
| 314 | // ############ User name #################################### |
|---|
| 315 | // userName is read when user clicks Upload |
|---|
| 316 | JPanel usernamePanel = new JPanel(); |
|---|
| 317 | usernamePanel.setLayout(new FlowLayout(FlowLayout.CENTER)); |
|---|
| 318 | usernamePanel.add(new JLabel(usernamePanelLabel)); |
|---|
| 319 | usernamePanel.add(usernameTextField = new JTextField(20)); |
|---|
| 320 | usernamePanel.add(new JLabel(usernamePanelText)); |
|---|
| 321 | p2.add(usernamePanel); |
|---|
| 322 | // ############ Gender #################################### |
|---|
| 323 | JPanel genderPanel = new JPanel(); |
|---|
| 324 | genderPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); |
|---|
| 325 | genderPanel.add(new JLabel(genderPanelLabel)); |
|---|
| 326 | genderPanel.add(genderChooser = new JComboBox(genderSelection)); |
|---|
| 327 | genderChooser.setSelectedIndex(0); |
|---|
| 328 | genderChooser.addActionListener(new ActionListener(){ |
|---|
| 329 | public void actionPerformed(ActionEvent e){ |
|---|
| 330 | gender = (String)genderChooser.getSelectedItem(); |
|---|
| 331 | } |
|---|
| 332 | }); |
|---|
| 333 | p2.add(genderPanel); |
|---|
| 334 | // ############ Age Range #################################### |
|---|
| 335 | JPanel ageRangePanel = new JPanel(); |
|---|
| 336 | ageRangePanel.setLayout(new FlowLayout(FlowLayout.CENTER)); |
|---|
| 337 | ageRangePanel.add(new JLabel(ageRangePanelLabel)); |
|---|
| 338 | ageRangePanel.add(ageRangeChooser = new JComboBox(ageSelection)); |
|---|
| 339 | ageRangeChooser.setSelectedIndex(0); |
|---|
| 340 | ageRangeChooser.addActionListener(new ActionListener(){ |
|---|
| 341 | public void actionPerformed(ActionEvent e){ |
|---|
| 342 | ageRange = (String)ageRangeChooser.getSelectedItem(); |
|---|
| 343 | } |
|---|
| 344 | }); |
|---|
| 345 | p2.add(ageRangePanel); |
|---|
| 346 | // ############ Pronunciation Dialect: #################################### |
|---|
| 347 | JPanel dialectPanel = new JPanel(); |
|---|
| 348 | dialectPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); |
|---|
| 349 | dialectPanel.add(new JLabel(dialectPanelLabel)); |
|---|
| 350 | dialectPanel.add(dialectChooser = new JComboBox(dialectSelection)); |
|---|
| 351 | dialectChooser.setSelectedIndex(0); |
|---|
| 352 | dialectChooser.addActionListener(new ActionListener(){ |
|---|
| 353 | public void actionPerformed(ActionEvent e){ |
|---|
| 354 | dialect = (String)dialectChooser.getSelectedItem(); |
|---|
| 355 | } |
|---|
| 356 | }); |
|---|
| 357 | p2.add(dialectPanel); |
|---|
| 358 | // ############ Microphone Type: #################################### |
|---|
| 359 | JPanel microphonePanel = new JPanel(); |
|---|
| 360 | microphonePanel.setLayout(new FlowLayout(FlowLayout.CENTER)); |
|---|
| 361 | microphonePanel.add(new JLabel(microphonePanelLabel)); |
|---|
| 362 | microphonePanel.add(microphoneChooser = new JComboBox(microphoneSelection)); |
|---|
| 363 | microphoneChooser.setSelectedIndex(0); |
|---|
| 364 | // microphoneChooser.setEditable(true); // user can add whatever they want ... |
|---|
| 365 | microphoneChooser.addActionListener(new ActionListener(){ |
|---|
| 366 | public void actionPerformed(ActionEvent e){ |
|---|
| 367 | microphone = (String)microphoneChooser.getSelectedItem(); |
|---|
| 368 | } |
|---|
| 369 | }); |
|---|
| 370 | p2.add(microphonePanel); |
|---|
| 371 | //############ Prompt container #################################### |
|---|
| 372 | JPanel promptsContainer = new JPanel(); |
|---|
| 373 | promptsContainer.setLayout(new FlowLayout(FlowLayout.CENTER)); |
|---|
| 374 | Color voxforgeColour = new Color(197, 216, 234); |
|---|
| 375 | int startPromptCount = 0; |
|---|
| 376 | // !!!!!! |
|---|
| 377 | //int promptsPerPane = 10; |
|---|
| 378 | int promptsPerPane = numberofPrompts; |
|---|
| 379 | // !!!!!! |
|---|
| 380 | // ############ Prompts panel #################################### |
|---|
| 381 | JPanel prompts = new JPanel(); |
|---|
| 382 | prompts.setLayout(new BoxLayout(prompts, BoxLayout.Y_AXIS)); |
|---|
| 383 | //Color voxforgeColour = new Color(197, 216, 234); |
|---|
| 384 | prompts.setBorder(BorderFactory.createLineBorder (voxforgeColour, 3)); |
|---|
| 385 | |
|---|
| 386 | int maxWidth = 40; |
|---|
| 387 | |
|---|
| 388 | //JPanel promptPanelA[] = new JPanel[numberofPrompts]; |
|---|
| 389 | //JPanel promptInnerPanelA[] = new JPanel[numberofPrompts]; |
|---|
| 390 | JPanel promptPanelA[] = new JPanel[promptsPerPane]; |
|---|
| 391 | JPanel promptInnerPanelA[] = new JPanel[promptsPerPane]; |
|---|
| 392 | for (int i = startPromptCount; i < promptsPerPane; i++) { |
|---|
| 393 | promptPanelA[i] = new JPanel(); |
|---|
| 394 | promptPanelA[i].setLayout(new FlowLayout(FlowLayout.LEFT)); |
|---|
| 395 | promptInnerPanelA [i]= new JPanel(); |
|---|
| 396 | promptInnerPanelA[i].setBorder(BorderFactory.createLineBorder (voxforgeColour, 1)); |
|---|
| 397 | //promptInnerPanelA[i].add(new MultiLineLabel(promptPanelA[i], i+":"+this.promptA[i], maxWidth)); |
|---|
| 398 | promptInnerPanelA[i].add(new MultiLineLabel(promptPanelA[i], this.promptA[i], maxWidth)); |
|---|
| 399 | promptPanelA[i].add(promptInnerPanelA[i]); |
|---|
| 400 | playA[i] = addButton(playButton, promptPanelA[i], false); |
|---|
| 401 | if (i==0) { |
|---|
| 402 | captA[i] = addButton(recordButton, promptPanelA[i], true); // only turn on first record button |
|---|
| 403 | } else { |
|---|
| 404 | captA[i] = addButton(recordButton, promptPanelA[i], false); |
|---|
| 405 | } |
|---|
| 406 | prompts.add(promptPanelA[i]); |
|---|
| 407 | } |
|---|
| 408 | //############ Prompt container #################################### |
|---|
| 409 | promptsContainer.add(prompts); |
|---|
| 410 | p2.add(promptsContainer); |
|---|
| 411 | |
|---|
| 412 | // ############ Sampling Graph #################################### |
|---|
| 413 | JPanel samplingPanel = new JPanel(new BorderLayout()); |
|---|
| 414 | eb = new EmptyBorder(10,20,5,20); |
|---|
| 415 | samplingPanel.setBorder(new CompoundBorder(eb, sbb)); |
|---|
| 416 | samplingPanel.add(samplingGraph = new SamplingGraph()); |
|---|
| 417 | p2.add(samplingPanel); |
|---|
| 418 | // ############ Upload Text #################################### |
|---|
| 419 | JPanel uploadTextPanel = new JPanel(); |
|---|
| 420 | uploadTextPanel.add(new JLabel(uploadText)); |
|---|
| 421 | p2.add(uploadTextPanel); |
|---|
| 422 | // ############ Upload #################################### |
|---|
| 423 | JPanel uploadButtonPanel = new JPanel(); |
|---|
| 424 | uploadButtonPanel.setBorder(new EmptyBorder(5,0,5,0)); |
|---|
| 425 | uploadB = addButton(uploadButtonLabel, uploadButtonPanel, false); // upload all submissions |
|---|
| 426 | p2.add(uploadButtonPanel); |
|---|
| 427 | // ############ Upload Progress bar #################################### |
|---|
| 428 | progBar = new JProgressBar(); |
|---|
| 429 | progBar.setStringPainted(false); |
|---|
| 430 | progBar.setString("Ready"); |
|---|
| 431 | //progBar.setVisible(false); |
|---|
| 432 | p2.add(progBar); |
|---|
| 433 | // ############ More Information Button #################################### |
|---|
| 434 | JPanel moreInfoButtonPanel = new JPanel(); |
|---|
| 435 | moreInfoButtonPanel.add(new JLabel(moreInfoText)); |
|---|
| 436 | moreInfoB = addButton(moreInfoButtonLabel, moreInfoButtonPanel, true); |
|---|
| 437 | p2.add(moreInfoButtonPanel); |
|---|
| 438 | // ############ Disclaimer #################################### |
|---|
| 439 | JPanel DisclaimerPanel = new JPanel(); |
|---|
| 440 | DisclaimerPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); |
|---|
| 441 | JPanel DisclaimerInnerPanel = new JPanel(); |
|---|
| 442 | DisclaimerInnerPanel.add(new JLabel(disclaimerText)); |
|---|
| 443 | aboutB = addButton(aboutButtonLabel, DisclaimerInnerPanel, true); |
|---|
| 444 | DisclaimerInnerPanel.setBorder(BorderFactory.createLineBorder (voxforgeColour, 3)); |
|---|
| 445 | DisclaimerPanel.add(DisclaimerInnerPanel); |
|---|
| 446 | p2.add(DisclaimerPanel); |
|---|
| 447 | //######################################################################### |
|---|
| 448 | add(p2); |
|---|
| 449 | } |
|---|
| 450 | |
|---|
| 451 | public void open() { } |
|---|
| 452 | |
|---|
| 453 | private String getTempDir() { |
|---|
| 454 | String tempdir=null; |
|---|
| 455 | try { |
|---|
| 456 | File dir=File.createTempFile("VF-dir",null); |
|---|
| 457 | dir.delete(); |
|---|
| 458 | dir.mkdir(); |
|---|
| 459 | tempdir = dir.toString(); |
|---|
| 460 | if ( !(tempdir.endsWith("/") || tempdir.endsWith("\\")) ) |
|---|
| 461 | tempdir = tempdir + System.getProperty("file.separator"); |
|---|
| 462 | |
|---|
| 463 | } catch (Exception e) { |
|---|
| 464 | System.err.println("Unable to create temp directory\n" + e); |
|---|
| 465 | } |
|---|
| 466 | return tempdir; |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | public void close() { |
|---|
| 470 | if (playback.thread != null) { |
|---|
| 471 | for (int i = 0; i < numberofPrompts; i++) { |
|---|
| 472 | playA[i].doClick(0); |
|---|
| 473 | } |
|---|
| 474 | } |
|---|
| 475 | if (capture.thread != null) { |
|---|
| 476 | for (int i = 0; i < numberofPrompts; i++) { |
|---|
| 477 | captA[i].doClick(0); |
|---|
| 478 | } |
|---|
| 479 | } |
|---|
| 480 | } |
|---|
| 481 | |
|---|
| 482 | private JButton addButton(String name, JPanel p, boolean state) { |
|---|
| 483 | JButton b = new JButton(name); |
|---|
| 484 | b.addActionListener(this); |
|---|
| 485 | p.add(b); |
|---|
| 486 | b.setEnabled(state); |
|---|
| 487 | return b; |
|---|
| 488 | } |
|---|
| 489 | |
|---|
| 490 | private void setButtonsOff() { |
|---|
| 491 | for (int i = 0; i < numberofPrompts; i++) { |
|---|
| 492 | playA[i].setEnabled(false); |
|---|
| 493 | captA[i].setEnabled(false); |
|---|
| 494 | //System.err.println("setButtonsOff" + "playA" + i + playA[i].isEnabled()+ ";captA" + i + captA[i].isEnabled());// !!!!!! |
|---|
| 495 | } |
|---|
| 496 | } |
|---|
| 497 | |
|---|
| 498 | private void saveButtonState() { |
|---|
| 499 | for (int i = 0; i < numberofPrompts; i++) { |
|---|
| 500 | // System.err.println("playA[i]" + i ); // !!!!!! |
|---|
| 501 | if (playA[i].isEnabled()) {play_stateA [i] = true;} else {play_stateA [i] = false;} |
|---|
| 502 | if (captA[i].isEnabled()) {capt_stateA [i] = true;} else {capt_stateA [i] = false;} |
|---|
| 503 | //System.err.println("saveButtonState" + "playA" + i + playA[i].isEnabled()+ ";captA" + i + captA[i].isEnabled());// !!!!!! |
|---|
| 504 | } |
|---|
| 505 | } |
|---|
| 506 | |
|---|
| 507 | private void restoreButtonState() { |
|---|
| 508 | for (int i = 0; i < numberofPrompts; i++) { |
|---|
| 509 | if (play_stateA[i]) {playA[i].setEnabled(true);} else {playA[i].setEnabled(false);} |
|---|
| 510 | if (capt_stateA[i]) {captA[i].setEnabled(true);} else {captA[i].setEnabled(false);} |
|---|
| 511 | // System.err.println("restoreButtonState" + "playA" + i + playA[i].isEnabled()+ ";captA" + i + captA[i].isEnabled());// !!!!!! |
|---|
| 512 | } |
|---|
| 513 | } |
|---|
| 514 | // !!!!!! |
|---|
| 515 | /* public boolean equals(Object obj) { |
|---|
| 516 | for (int i = 0; i < numberofPrompts; i++) { |
|---|
| 517 | if (obj.equals(playA[i])) { |
|---|
| 518 | return (this == obj); |
|---|
| 519 | } |
|---|
| 520 | } |
|---|
| 521 | return (this == obj); |
|---|
| 522 | } */ |
|---|
| 523 | // !!!!!! |
|---|
| 524 | public void actionPerformed(ActionEvent e) { |
|---|
| 525 | Object obj = e.getSource(); |
|---|
| 526 | |
|---|
| 527 | // ################### Play ####################################### |
|---|
| 528 | // !!!!!! |
|---|
| 529 | for (int i = 0; i < numberofPrompts; i++) { |
|---|
| 530 | //System.err.println("playA[i]" + i ); // !!!!!! |
|---|
| 531 | if (obj.equals(playA[i])) { |
|---|
| 532 | if (playA[i].getText().startsWith(playButton)) { |
|---|
| 533 | wavFile = wavFileA[i]; |
|---|
| 534 | duration = durationA[i]; |
|---|
| 535 | totalBytesWritten = totalBytesWrittenA[i]; |
|---|
| 536 | System.err.println("=== Play " + (i+1) + " ===");// !!!!!! |
|---|
| 537 | playback.start(); |
|---|
| 538 | System.err.println("duration:" + duration); |
|---|
| 539 | fileName = promptidA[i]; |
|---|
| 540 | samplingGraph.start(); |
|---|
| 541 | saveButtonState(); |
|---|
| 542 | setButtonsOff(); |
|---|
| 543 | captA[i].setEnabled(false); |
|---|
| 544 | playA[i].setEnabled(true); |
|---|
| 545 | playA[i].setText(stopButton); |
|---|
| 546 | } else { |
|---|
| 547 | playback.stop(); |
|---|
| 548 | samplingGraph.stop(); |
|---|
| 549 | restoreButtonState(); |
|---|
| 550 | captA[i].setEnabled(true); |
|---|
| 551 | playA[i].setText(playButton); |
|---|
| 552 | } |
|---|
| 553 | } |
|---|
| 554 | } |
|---|
| 555 | |
|---|
| 556 | for (int x = 0; x < numberofPrompts; x++) { |
|---|
| 557 | if (obj.equals(captA[x])) { |
|---|
| 558 | if (captA[x].getText().startsWith(recordButton)) { |
|---|
| 559 | file = null; |
|---|
| 560 | wavFile = wavFileA[x]; |
|---|
| 561 | System.err.println("=== Record " + (x+1) + " ==="); // !!!!!! |
|---|
| 562 | capture.start(uploadWavFileA[x]); |
|---|
| 563 | fileName = promptidA[x]; |
|---|
| 564 | samplingGraph.start(); |
|---|
| 565 | saveButtonState(); |
|---|
| 566 | setButtonsOff(); |
|---|
| 567 | captA[x].setEnabled(true); |
|---|
| 568 | captA[x].setText(stopButton); |
|---|
| 569 | moreInfoB.setEnabled(false); |
|---|
| 570 | aboutB.setEnabled(false); |
|---|
| 571 | } else { |
|---|
| 572 | lines.removeAllElements(); |
|---|
| 573 | // !!!!!! |
|---|
| 574 | try { |
|---|
| 575 | //capture.thread.sleep(1000); |
|---|
| 576 | Thread.sleep(1000); |
|---|
| 577 | } catch (InterruptedException ex) { |
|---|
| 578 | System.err.println("Recording Thread - Interrupt Exception"); |
|---|
| 579 | } |
|---|
| 580 | // !!!!!! |
|---|
| 581 | capture.stop(); |
|---|
| 582 | totalBytesWrittenA[x] = totalBytesWritten; // !!!!!! |
|---|
| 583 | durationA[x]= totalBytesWritten / (double) (format.getSampleRate() * format.getSampleSizeInBits()/ 8); |
|---|
| 584 | System.err.println("duration1:" + durationA[x]); |
|---|
| 585 | samplingGraph.stop(); |
|---|
| 586 | restoreButtonState(); |
|---|
| 587 | playA[x].setEnabled(true); |
|---|
| 588 | captA[x].setText(recordButton); |
|---|
| 589 | moreInfoB.setEnabled(true); |
|---|
| 590 | aboutB.setEnabled(true); |
|---|
| 591 | captA[x].setEnabled(true); |
|---|
| 592 | if (x < numberofPrompts-1) { |
|---|
| 593 | captA[x+1].setEnabled(true); |
|---|
| 594 | } |
|---|
| 595 | if (x == numberofPrompts-1) { |
|---|
| 596 | uploadB.setEnabled(true); |
|---|
| 597 | } |
|---|
| 598 | } |
|---|
| 599 | } |
|---|
| 600 | } |
|---|
| 601 | |
|---|
| 602 | // ################### Upload ####################################### |
|---|
| 603 | if (obj.equals(uploadB)) { |
|---|
| 604 | for (int i = 0; i < numberofPrompts; i++) { |
|---|
| 605 | playA[i].setEnabled(false); |
|---|
| 606 | captA[i].setEnabled(false); |
|---|
| 607 | } |
|---|
| 608 | uploadB.setEnabled(false); |
|---|
| 609 | try { |
|---|
| 610 | usernameTextField.selectAll(); |
|---|
| 611 | userName = usernameTextField.getText(); |
|---|
| 612 | // see java.util.regex.Pattern |
|---|
| 613 | // \w A word character: [a-zA-Z_0-9] |
|---|
| 614 | // \W A non-word character: [^\w] |
|---|
| 615 | userName = (usernameTextField.getText().replaceAll("\\W","")); |
|---|
| 616 | if (userName.length() == 0 ) { |
|---|
| 617 | userName = "anonymous"; |
|---|
| 618 | } else { |
|---|
| 619 | if (userName.length() > 40 ) { |
|---|
| 620 | userName = userName.substring(0,40); |
|---|
| 621 | } |
|---|
| 622 | } |
|---|
| 623 | } catch (NullPointerException ex) { |
|---|
| 624 | userName = "anonymous"; |
|---|
| 625 | } |
|---|
| 626 | convertAndUpload.start(); |
|---|
| 627 | } |
|---|
| 628 | // ################### More Information ####################################### |
|---|
| 629 | else if (obj.equals(moreInfoB)) { |
|---|
| 630 | JTextArea textArea = new JTextArea(license); |
|---|
| 631 | textArea.setLineWrap(true); |
|---|
| 632 | textArea.setWrapStyleWord(true); |
|---|
| 633 | JScrollPane areaScrollPane = new JScrollPane(textArea); |
|---|
| 634 | areaScrollPane.setVerticalScrollBarPolicy( |
|---|
| 635 | JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); |
|---|
| 636 | areaScrollPane.setPreferredSize(new Dimension(600, 600)); |
|---|
| 637 | |
|---|
| 638 | JOptionPane.showMessageDialog(this, areaScrollPane, |
|---|
| 639 | "More info on Copyright and GPL license", JOptionPane.PLAIN_MESSAGE); |
|---|
| 640 | } |
|---|
| 641 | // ################### About ####################################### |
|---|
| 642 | else if (obj.equals(aboutB)) { |
|---|
| 643 | JTextArea textArea = new JTextArea(VFlicense); |
|---|
| 644 | textArea.setLineWrap(true); |
|---|
| 645 | textArea.setWrapStyleWord(true); |
|---|
| 646 | JScrollPane areaScrollPane = new JScrollPane(textArea); |
|---|
| 647 | areaScrollPane.setVerticalScrollBarPolicy( |
|---|
| 648 | JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); |
|---|
| 649 | areaScrollPane.setPreferredSize(new Dimension(600, 600)); |
|---|
| 650 | |
|---|
| 651 | JOptionPane.showMessageDialog(this, areaScrollPane, |
|---|
| 652 | "About VoxForge Speech Submission Application", JOptionPane.PLAIN_MESSAGE); |
|---|
| 653 | } |
|---|
| 654 | } |
|---|
| 655 | |
|---|
| 656 | public void createAudioInputStream(File file, boolean updateComponents) { |
|---|
| 657 | if (file != null && file.isFile()) { |
|---|
| 658 | try { |
|---|
| 659 | this.file = file; |
|---|
| 660 | errStr = null; |
|---|
| 661 | audioInputStream = AudioSystem.getAudioInputStream(file); |
|---|
| 662 | fileName = file.getName(); |
|---|
| 663 | long milliseconds = (long)((audioInputStream.getFrameLength() * 1000) / audioInputStream.getFormat().getFrameRate()); |
|---|
| 664 | duration = milliseconds / 1000.0; |
|---|
| 665 | // !!!!!! |
|---|
| 666 | System.err.println("createAudioInputStream duration:" + duration); |
|---|
| 667 | // !!!!!! |
|---|
| 668 | if (updateComponents) { |
|---|
| 669 | //DEL formatControls.setFormat(audioInputStream.getFormat()); |
|---|
| 670 | samplingGraph.createWaveForm(null); |
|---|
| 671 | } |
|---|
| 672 | } catch (Exception ex) { |
|---|
| 673 | reportStatus(ex.toString()); |
|---|
| 674 | } |
|---|
| 675 | } else { |
|---|
| 676 | reportStatus("Audio file required."); |
|---|
| 677 | } |
|---|
| 678 | } |
|---|
| 679 | |
|---|
| 680 | |
|---|
| 681 | public void saveToFile(String name, AudioFileFormat.Type fileType) { |
|---|
| 682 | |
|---|
| 683 | if (audioInputStream == null) { |
|---|
| 684 | reportStatus("No loaded audio to save"); |
|---|
| 685 | return; |
|---|
| 686 | } else if (file != null) { |
|---|
| 687 | createAudioInputStream(file, false); |
|---|
| 688 | } |
|---|
| 689 | |
|---|
| 690 | // reset to the beginning of the captured data |
|---|
| 691 | try { |
|---|
| 692 | audioInputStream.reset(); |
|---|
| 693 | } catch (Exception e) { |
|---|
| 694 | reportStatus("Unable to reset stream " + e); |
|---|
| 695 | return; |
|---|
| 696 | } |
|---|
| 697 | |
|---|
| 698 | File file = new File(fileName = name); |
|---|
| 699 | try { |
|---|
| 700 | if (AudioSystem.write(audioInputStream, fileType, file) == -1) { |
|---|
| 701 | throw new IOException("Problems writing to file"); |
|---|
| 702 | } |
|---|
| 703 | } catch (Exception ex) { reportStatus(ex.toString()); } |
|---|
| 704 | samplingGraph.repaint(); |
|---|
| 705 | } |
|---|
| 706 | |
|---|
| 707 | public void saveToFile(File file, AudioFileFormat.Type fileType) { |
|---|
| 708 | |
|---|
| 709 | if (audioInputStream == null) { |
|---|
| 710 | reportStatus("No loaded audio to save"); |
|---|
| 711 | return; |
|---|
| 712 | } else if (file != null) { |
|---|
| 713 | createAudioInputStream(file, false); |
|---|
| 714 | } |
|---|
| 715 | |
|---|
| 716 | // reset to the beginning of the captured data |
|---|
| 717 | try { |
|---|
| 718 | audioInputStream.reset(); |
|---|
| 719 | } catch (Exception e) { |
|---|
| 720 | reportStatus("Unable to reset stream " + e); |
|---|
| 721 | return; |
|---|
| 722 | } |
|---|
| 723 | |
|---|
| 724 | // File file = new File(fileName = name); |
|---|
| 725 | try { |
|---|
| 726 | if (AudioSystem.write(audioInputStream, fileType, file) == -1) { |
|---|
| 727 | throw new IOException("Problems writing to file"); |
|---|
| 728 | } |
|---|
| 729 | } catch (Exception ex) { reportStatus(ex.toString()); } |
|---|
| 730 | samplingGraph.repaint(); |
|---|
| 731 | } |
|---|
| 732 | |
|---|
| 733 | private void reportStatus(String msg) { |
|---|
| 734 | if ((errStr = msg) != null) { |
|---|
| 735 | System.out.println(errStr); |
|---|
| 736 | samplingGraph.repaint(); |
|---|
| 737 | } |
|---|
| 738 | } |
|---|
| 739 | |
|---|
| 740 | |
|---|
| 741 | /** |
|---|
| 742 | * Write data to the OutputChannel. |
|---|
| 743 | */ |
|---|
| 744 | public class Playback implements Runnable { |
|---|
| 745 | |
|---|
| 746 | SourceDataLine line; |
|---|
| 747 | Thread thread; |
|---|
| 748 | |
|---|
| 749 | public void start() { |
|---|
| 750 | errStr = null; |
|---|
| 751 | thread = new Thread(this); |
|---|
| 752 | thread.setName("Playback"); |
|---|
| 753 | thread.start(); |
|---|
| 754 | } |
|---|
| 755 | |
|---|
| 756 | public void stop() { |
|---|
| 757 | thread = null; |
|---|
| 758 | } |
|---|
| 759 | |
|---|
| 760 | private void shutDown(String message) { |
|---|
| 761 | if ((errStr = message) != null) { |
|---|
| 762 | System.err.println(errStr); |
|---|
| 763 | samplingGraph.repaint(); |
|---|
| 764 | } |
|---|
| 765 | if (thread != null) { |
|---|
| 766 | thread = null; |
|---|
| 767 | samplingGraph.stop(); |
|---|
| 768 | for (int i = 0; i < numberofPrompts; i++) { |
|---|
| 769 | if (playA[i].getText().startsWith(stopButton)) { //play button gets set to "Stop" after play is pressed |
|---|
| 770 | captA[i].setEnabled(true); |
|---|
| 771 | } |
|---|
| 772 | } |
|---|
| 773 | for (int i = 0; i < numberofPrompts; i++) { |
|---|
| 774 | playA[i].setText(playButton); |
|---|
| 775 | } |
|---|
| 776 | } |
|---|
| 777 | } |
|---|
| 778 | |
|---|
| 779 | public void run() { |
|---|
| 780 | getAudioInputStream(); |
|---|
| 781 | |
|---|
| 782 | // get an AudioInputStream of the desired format for playback |
|---|
| 783 | //DEL AudioFormat format = formatControls.getFormat(); |
|---|
| 784 | AudioInputStream playbackInputStream = AudioSystem.getAudioInputStream(format, audioInputStream); |
|---|
| 785 | |
|---|
| 786 | if (playbackInputStream == null) { |
|---|
| 787 | shutDown("Unable to convert stream of format " + audioInputStream + " to format " + format); |
|---|
| 788 | return; |
|---|
| 789 | } |
|---|
| 790 | |
|---|
| 791 | // define the required attributes for our line, |
|---|
| 792 | // and make sure a compatible line is supported. |
|---|
| 793 | DataLine.Info info = new DataLine.Info(SourceDataLine.class, |
|---|
| 794 | format); |
|---|
| 795 | if (!AudioSystem.isLineSupported(info)) { |
|---|
| 796 | shutDown("Line matching " + info + " not supported."); |
|---|
| 797 | return; |
|---|
| 798 | } |
|---|
| 799 | |
|---|
| 800 | // get and open the source data line for playback. |
|---|
| 801 | try { |
|---|
| 802 | line = (SourceDataLine) AudioSystem.getLine(info); |
|---|
| 803 | line.open(format, bufSize); |
|---|
| 804 | } catch (LineUnavailableException ex) { |
|---|
| 805 | shutDown("Unable to open the line: " + ex); |
|---|
| 806 | return; |
|---|
| 807 | } |
|---|
| 808 | |
|---|
| 809 | // play back the captured audio data |
|---|
| 810 | int frameSizeInBytes = format.getFrameSize(); |
|---|
| 811 | int bufferLengthInFrames = line.getBufferSize() / 8; |
|---|
| 812 | int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes; |
|---|
| 813 | byte[] data = new byte[bufferLengthInBytes]; |
|---|
| 814 | int numBytesRead = 0; |
|---|
| 815 | |
|---|
| 816 | // start the source data line |
|---|
| 817 | line.start(); |
|---|
| 818 | ByteArrayOutputStream outbaos = new ByteArrayOutputStream(); |
|---|
| 819 | while (thread != null) { |
|---|
| 820 | try { |
|---|
| 821 | if ((numBytesRead = playbackInputStream.read(data)) == -1) { |
|---|
| 822 | break; |
|---|
| 823 | } |
|---|
| 824 | outbaos.write(data, 0, numBytesRead); |
|---|
| 825 | /// System.err.println("numBytesRead" + numBytesRead); |
|---|
| 826 | } catch (Exception e) { |
|---|
| 827 | shutDown("Error during playback: " + e); |
|---|
| 828 | break; |
|---|
| 829 | } |
|---|
| 830 | } |
|---|
| 831 | byte audioBytes[] = outbaos.toByteArray(); |
|---|
| 832 | // debug System.err.println("outbaos size:" + outbaos.size()); |
|---|
| 833 | outbaos.reset(); |
|---|
| 834 | outbaos = null; |
|---|
| 835 | samplingGraph.createWaveForm(audioBytes); |
|---|
| 836 | samplingGraph.repaint(); |
|---|
| 837 | |
|---|
| 838 | getAudioInputStream(); |
|---|
| 839 | playbackInputStream = AudioSystem.getAudioInputStream(format, audioInputStream); |
|---|
| 840 | |
|---|
| 841 | progBar.setStringPainted(true); |
|---|
| 842 | progBar.setString(samplingGraph.peakWarning ? |
|---|
| 843 | peakWarningLabel : ""); |
|---|
| 844 | while (thread != null) { |
|---|
| 845 | try { |
|---|
| 846 | if ((numBytesRead = playbackInputStream.read(data)) == -1) { |
|---|
| 847 | break; |
|---|
| 848 | } |
|---|
| 849 | int numBytesRemaining = numBytesRead; |
|---|
| 850 | while (numBytesRemaining > 0 ) { |
|---|
| 851 | numBytesRemaining -= line.write(data, 0, numBytesRemaining); |
|---|
| 852 | } |
|---|
| 853 | } catch (Exception e) { |
|---|
| 854 | shutDown("Error during playback: " + e); |
|---|
| 855 | break; |
|---|
| 856 | } |
|---|
| 857 | } |
|---|
| 858 | // we reached the end of the stream. let the data play out, then |
|---|
| 859 | // stop and close the line. |
|---|
| 860 | if (thread != null) { |
|---|
| 861 | line.drain(); |
|---|
| 862 | } |
|---|
| 863 | line.stop(); |
|---|
| 864 | line.close(); |
|---|
| 865 | line = null; |
|---|
| 866 | System.err.println("reached end of file"); |
|---|
| 867 | |
|---|
| 868 | shutDown(null); |
|---|
| 869 | // !!!!! |
|---|
| 870 | restoreButtonState(); |
|---|
| 871 | // !!!!!! |
|---|
| 872 | } |
|---|
| 873 | } // End class Playback |
|---|
| 874 | |
|---|
| 875 | |
|---|
| 876 | /** |
|---|
| 877 | * Reads data from the input channel and writes to the output stream |
|---|
| 878 | */ |
|---|
| 879 | class Capture implements Runnable { |
|---|
| 880 | |
|---|
| 881 | TargetDataLine line; |
|---|
| 882 | Thread thread; |
|---|
| 883 | File uploadWavFile; |
|---|
| 884 | |
|---|
| 885 | public void start() { |
|---|
| 886 | errStr = null; |
|---|
| 887 | thread = new Thread(this); |
|---|
| 888 | thread.setName("Capture"); |
|---|
| 889 | try { |
|---|
| 890 | thread.setPriority(Thread.MAX_PRIORITY); |
|---|
| 891 | } catch(Exception err){ |
|---|
| 892 | } |
|---|
| 893 | thread.start(); |
|---|
| 894 | } |
|---|
| 895 | |
|---|
| 896 | public void start(File uploadWavFile) { |
|---|
| 897 | this.uploadWavFile = uploadWavFile; |
|---|
| 898 | System.err.println("Capture uploadWavFile is:" + uploadWavFile); |
|---|
| 899 | |
|---|
| 900 | errStr = null; |
|---|
| 901 | thread = new Thread(this); |
|---|
| 902 | thread.setName("Capture"); |
|---|
| 903 | try { |
|---|
| 904 | thread.setPriority(Thread.MAX_PRIORITY); |
|---|
| 905 | } catch(Exception err){ |
|---|
| 906 | } |
|---|
| 907 | thread.start(); |
|---|
| 908 | } |
|---|
| 909 | |
|---|
| 910 | public void stop() { |
|---|
| 911 | thread = null; |
|---|
| 912 | } |
|---|
| 913 | |
|---|
| 914 | private void shutDown(String message) { |
|---|
| 915 | if ((errStr = message) != null && thread != null) { |
|---|
| 916 | thread = null; |
|---|
| 917 | samplingGraph.stop(); |
|---|
| 918 | System.err.println(errStr); |
|---|
| 919 | samplingGraph.repaint(); |
|---|
| 920 | } |
|---|
| 921 | } |
|---|
| 922 | |
|---|
| 923 | public void run() { |
|---|
| 924 | |
|---|
| 925 | duration = 0; |
|---|
| 926 | audioInputStream = null; |
|---|
| 927 | |
|---|
| 928 | // define the required attributes for our line, |
|---|
| 929 | // and make sure a compatible line is supported. |
|---|
| 930 | |
|---|
| 931 | //DEL AudioFormat format = formatControls.getFormat(); |
|---|
| 932 | DataLine.Info info = new DataLine.Info(TargetDataLine.class, |
|---|
| 933 | format); |
|---|
| 934 | |
|---|
| 935 | if (!AudioSystem.isLineSupported(info)) { |
|---|
| 936 | shutDown("Line matching " + info + " not supported."); |
|---|
| 937 | return; |
|---|
| 938 | } |
|---|
| 939 | |
|---|
| 940 | // get and open the target data line for capture. |
|---|
| 941 | |
|---|
| 942 | try { |
|---|
| 943 | line = (TargetDataLine) AudioSystem.getLine(info); |
|---|
| 944 | line.open(format, line.getBufferSize()); |
|---|
| 945 | } catch (LineUnavailableException ex) { |
|---|
| 946 | shutDown("Unable to open the line: " + ex); |
|---|
| 947 | return; |
|---|
| 948 | } catch (SecurityException ex) { |
|---|
| 949 | shutDown(ex.toString()); |
|---|
| 950 | // JavaSound.showInfoDialog(); |
|---|
| 951 | return; |
|---|
| 952 | } catch (Exception ex) { |
|---|
| 953 | shutDown(ex.toString()); |
|---|
| 954 | return; |
|---|
| 955 | } |
|---|
| 956 | ByteArrayOutputStream outbaos = new ByteArrayOutputStream(); |
|---|
| 957 | System.err.println("AudioFormat: " + line.getFormat()); |
|---|
| 958 | |
|---|
| 959 | BufferedOutputStream out; |
|---|
| 960 | try { |
|---|
| 961 | out = new BufferedOutputStream(new FileOutputStream( |
|---|
| 962 | wavFile |
|---|
| 963 | )); |
|---|
| 964 | |
|---|
| 965 | } catch (Exception e) { |
|---|
| 966 | shutDown("Unable to open the output stream\n" + e); |
|---|
| 967 | return; |
|---|
| 968 | } |
|---|
| 969 | |
|---|
| 970 | int frameSizeInBytes = format.getFrameSize(); |
|---|
| 971 | int bufferLengthInFrames = line.getBufferSize() / 8; |
|---|
| 972 | int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes; |
|---|
| 973 | byte[] data = new byte[bufferLengthInBytes]; |
|---|
| 974 | int numBytesRead; |
|---|
| 975 | |
|---|
| 976 | line.start(); |
|---|
| 977 | |
|---|
| 978 | totalBytesWritten = 0L; |
|---|
| 979 | try { |
|---|
| 980 | while (thread != null) { |
|---|
| 981 | if((numBytesRead = line.read(data, 0, bufferLengthInBytes)) == -1) { |
|---|
| 982 | break; |
|---|
| 983 | } |
|---|
| 984 | totalBytesWritten += numBytesRead; |
|---|
| 985 | out.write(data, 0, numBytesRead); |
|---|
| 986 | outbaos.write(data, 0, numBytesRead); |
|---|
| 987 | } |
|---|
| 988 | } catch(IOException err){ |
|---|
| 989 | System.err.println("IOException while writing WAV cache file: " + err); |
|---|
| 990 | } |
|---|
| 991 | |
|---|
| 992 | // debug System.err.println("thread==null, recording thread about to tidy up"); |
|---|
| 993 | |
|---|
| 994 | // we reached the end of the stream. stop and close the line. |
|---|
| 995 | line.stop(); |
|---|
| 996 | line.close(); |
|---|
| 997 | line = null; |
|---|
| 998 | |
|---|
| 999 | // stop and close the output stream |
|---|
| 1000 | try { |
|---|
| 1001 | out.flush(); |
|---|
| 1002 | out.close(); |
|---|
| 1003 | } catch (IOException ex) { |
|---|
| 1004 | ex.printStackTrace(); |
|---|
| 1005 | } |
|---|
| 1006 | |
|---|
| 1007 | // load bytes into the audio input stream for playback |
|---|
| 1008 | getAudioInputStream(); |
|---|
| 1009 | |
|---|
| 1010 | // saveToFile(this.uploadWavFile, AudioFileFormat.Type.WAVE); |
|---|
| 1011 | try { |
|---|
| 1012 | if (AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, uploadWavFile) == -1) { |
|---|
| 1013 | throw new IOException("Problems writing to file"); |
|---|
| 1014 | } |
|---|
| 1015 | } catch (Exception ex) { reportStatus(ex.toString()); } |
|---|
| 1016 | // debug System.err.println("About to load bytes to byte array"); |
|---|
| 1017 | byte audioBytes[] = outbaos.toByteArray(); |
|---|
| 1018 | |
|---|
| 1019 | duration = totalBytesWritten / (double) (format.getSampleRate() * format.getSampleSizeInBits()/ 8); |
|---|
| 1020 | System.err.println("capture duration:" + duration); |
|---|
| 1021 | // debug System.err.println("Calculated duration"); |
|---|
| 1022 | samplingGraph.createWaveForm(audioBytes); |
|---|
| 1023 | // debug System.err.println("Created samplingGraph"); |
|---|
| 1024 | // This is the only way to "reset" long streams - re-grab |
|---|
| 1025 | getAudioInputStream(); |
|---|
| 1026 | |
|---|
| 1027 | progBar.setStringPainted(true); |
|---|
| 1028 | progBar.setString(samplingGraph.peakWarning ? |
|---|
| 1029 | peakWarningLabel : ""); |
|---|
| 1030 | } |
|---|
| 1031 | } // End class Capture |
|---|
| 1032 | |
|---|
| 1033 | /** |
|---|
| 1034 | * Will free audio input stream if exists, and grab a new version. This seems a bit silly but there's no other way to "rewind" large files. |
|---|
| 1035 | * @uml.property name="audioInputStream" |
|---|
| 1036 | */ |
|---|
| 1037 | private void getAudioInputStream(){ |
|---|
| 1038 | if (audioInputStream != null) { |
|---|
| 1039 | try { |
|---|
| 1040 | audioInputStream.close(); |
|---|
| 1041 | } catch (IOException err) { |
|---|
| 1042 | } |
|---|
| 1043 | } |
|---|
| 1044 | // debug System.err.println("getAudioInputStream - totalBytesWritten:" + totalBytesWritten); |
|---|
| 1045 | try { |
|---|
| 1046 | // audioInputStream = AudioSystem.getAudioInputStream(wavFile); |
|---|
| 1047 | |
|---|
| 1048 | audioInputStream = new AudioInputStream(new BufferedInputStream( |
|---|
| 1049 | new FileInputStream(wavFile)), format, totalBytesWritten |
|---|
| 1050 | / (format.getChannels() * format.getSampleSizeInBits() / 8) // Length in sample frames |
|---|
| 1051 | ); |
|---|
| 1052 | |
|---|
| 1053 | |
|---|
| 1054 | } catch (Exception err) { |
|---|
| 1055 | System.err.println("Exception while reading cache file: " + err); |
|---|
| 1056 | } |
|---|
| 1057 | // debug System.err.println("Grabbed audio input stream from cache file"); |
|---|
| 1058 | } |
|---|
| 1059 | |
|---|
| 1060 | |
|---|
| 1061 | /** |
|---|
| 1062 | * uploads the file |
|---|
| 1063 | */ |
|---|
| 1064 | class ConvertAndUpload implements Runnable { |
|---|
| 1065 | |
|---|
| 1066 | Thread thread; |
|---|
| 1067 | |
|---|
| 1068 | public void start() { |
|---|
| 1069 | errStr = null; |
|---|
| 1070 | thread = new Thread(this); |
|---|
| 1071 | thread.setName("ConvertAndUpload"); |
|---|
| 1072 | System.err.println("=== Upload ==="); |
|---|
| 1073 | thread.start(); |
|---|
| 1074 | } |
|---|
| 1075 | |
|---|
| 1076 | public void stop() { |
|---|
| 1077 | thread = null; |
|---|
| 1078 | } |
|---|
| 1079 | |
|---|
| 1080 | public void run() { |
|---|
| 1081 | progBar.setVisible(true); |
|---|
| 1082 | progBar.setStringPainted(true); |
|---|
| 1083 | progBar.setMaximum(100); |
|---|
| 1084 | progBar.setString(uploadingMessageLabel); |
|---|
| 1085 | progBar.setIndeterminate(false); |
|---|
| 1086 | progBar.setMinimum(0); |
|---|
| 1087 | sentBytes = 0; |
|---|
| 1088 | try { |
|---|
| 1089 | destinationURL = new URL(destinationURL.toString()); |
|---|
| 1090 | }catch(Exception err){ |
|---|
| 1091 | System.err.println(err); |
|---|
| 1092 | } |
|---|
| 1093 | System.err.println("Destination URL is " + destinationURL); |
|---|
| 1094 | |
|---|
| 1095 | |
|---|
| 1096 | //############ audio files #################################### |
|---|
| 1097 | // !!!!!! |
|---|
| 1098 | //File[] files = new File[numberofPrompts]; |
|---|
| 1099 | File[] files = new File[numberofPrompts + 4]; |
|---|
| 1100 | // !!!!!! |
|---|
| 1101 | for (int i = 0; i < numberofPrompts; i++) { |
|---|
| 1102 | files[i] = uploadWavFileA[i]; |
|---|
| 1103 | } |
|---|
| 1104 | //############ prompt files #################################### |
|---|
| 1105 | try { |
|---|
| 1106 | /* !!!!!! |
|---|
| 1107 | * FileWriter: |
|---|
| 1108 | * Convenience class for writing character files. The constructors of this |
|---|
| 1109 | * class assume that the default character encoding and the default byte-buffer |
|---|
| 1110 | * size are acceptable. To specify these values yourself, construct an |
|---|
| 1111 | * OutputStreamWriter on a FileOutputStream. |
|---|
| 1112 | * |
|---|
| 1113 | */ |
|---|
| 1114 | //BufferedWriter out_prompts = new BufferedWriter(new FileWriter(promptsFile)); |
|---|
| 1115 | BufferedWriter out_prompts = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(promptsFile),"UTF-8")); |
|---|
| 1116 | // !!!!!! |
|---|
| 1117 | for (int i = 0; i < numberofPrompts; i++) { |
|---|
| 1118 | out_prompts.write(promptidA[i] + " " + promptA[i] + System.getProperty("line.separator")); |
|---|
| 1119 | } |
|---|
| 1120 | out_prompts.close(); |
|---|
| 1121 | } catch (IOException e) { |
|---|
| 1122 | System.err.println("Problems with prompts"); |
|---|
| 1123 | } |
|---|
| 1124 | // !!!!!! |
|---|
| 1125 | //files[11] = promptsFile; |
|---|
| 1126 | files[numberofPrompts] = promptsFile; |
|---|
| 1127 | // !!!!!! |
|---|
| 1128 | //############ ReadMe file#################################### |
|---|
| 1129 | try { |
|---|
| 1130 | // !!!!!! |
|---|
| 1131 | // BufferedWriter out_readme = new BufferedWriter(new FileWriter(readmeFile)); |
|---|
| 1132 | BufferedWriter out_readme = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(readmeFile),"UTF-8")); |
|---|
| 1133 | // !!!!!! |
|---|
| 1134 | |
|---|
| 1135 | out_readme.write("User Name:" + userName + System.getProperty("line.separator")); |
|---|
| 1136 | out_readme.write(System.getProperty("line.separator")); |
|---|
| 1137 | |
|---|
| 1138 | out_readme.write("Speaker Characteristics:" + System.getProperty("line.separator") ); |
|---|
| 1139 | out_readme.write(System.getProperty("line.separator")); |
|---|
| 1140 | out_readme.write("Gender: " + gender + System.getProperty("line.separator") ); |
|---|
| 1141 | out_readme.write("Age Range: " + ageRange + System.getProperty("line.separator")); |
|---|
| 1142 | out_readme.write("Language: " + language + System.getProperty("line.separator")); |
|---|
| 1143 | out_readme.write("Pronunciation dialect: " + dialect + System.getProperty("line.separator")); |
|---|
| 1144 | out_readme.write(System.getProperty("line.separator")); |
|---|
| 1145 | |
|---|
| 1146 | out_readme.write("Recording Information:" + System.getProperty("line.separator")); |
|---|
| 1147 | out_readme.write(System.getProperty("line.separator")); |
|---|
| 1148 | out_readme.write("Microphone make: n/a" + System.getProperty("line.separator")); |
|---|
| 1149 | out_readme.write("Microphone type: " + microphone + System.getProperty("line.separator")); |
|---|
| 1150 | out_readme.write("Audio card make: unknown" + System.getProperty("line.separator")); |
|---|
| 1151 | out_readme.write("Audio card type: unknown" + System.getProperty("line.separator")); |
|---|
| 1152 | out_readme.write("Audio Recording Software: VoxForge Speech Submission Application" + System.getProperty("line.separator")); |
|---|
| 1153 | out_readme.write("O/S:" + System.getProperty("line.separator")); |
|---|
| 1154 | out_readme.write(System.getProperty("line.separator")); |
|---|
| 1155 | |
|---|
| 1156 | out_readme.write("File Info:" + System.getProperty("line.separator")); |
|---|
| 1157 | out_readme.write(System.getProperty("line.separator")); |
|---|
| 1158 | out_readme.write("File type: " + fileType + System.getProperty("line.separator")); |
|---|
| 1159 | out_readme.write("Sampling Rate: " + samplingRate + System.getProperty("line.separator")); |
|---|
| 1160 | out_readme.write("Sample rate format: " + samplingRateFormat + System.getProperty("line.separator")); |
|---|
| 1161 | out_readme.write("Number of channels: " + numberChannels + System.getProperty("line.separator")); |
|---|
| 1162 | |
|---|
| 1163 | out_readme.close(); |
|---|
| 1164 | } catch (IOException e) { |
|---|
| 1165 | System.err.println("Problems with Gender, Age Range or Dialect"); |
|---|
| 1166 | } |
|---|
| 1167 | // !!!!!! |
|---|
| 1168 | // files[12] = readmeFile; |
|---|
| 1169 | files[numberofPrompts + 1] = readmeFile; |
|---|
| 1170 | // !!!!!! |
|---|
| 1171 | //############ License Notice File #################################### |
|---|
| 1172 | try { |
|---|
| 1173 | Calendar cal = Calendar.getInstance(); |
|---|
| 1174 | //int year = cal.get(Calendar.YEAR); |
|---|
| 1175 | // !!!!!! |
|---|
| 1176 | //BufferedWriter out_licenseNoticeFile = new BufferedWriter(new FileWriter(licenseNoticeFile)); |
|---|
| 1177 | BufferedWriter out_licenseNoticeFile = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(licenseNoticeFile),"UTF-8")); |
|---|
| 1178 | // !!!!!! |
|---|
| 1179 | String licenseNotice = "Copyright " + cal.get(Calendar.YEAR) + " " + copyrightName + System.getProperty("line.separator") |
|---|
| 1180 | + System.getProperty("line.separator") |
|---|
| 1181 | + licenseObject.getBlanklicenseNotice(); |
|---|
| 1182 | out_licenseNoticeFile.write(licenseNotice); |
|---|
| 1183 | out_licenseNoticeFile.close(); |
|---|
| 1184 | } catch (IOException e) { |
|---|
| 1185 | System.err.println("Problems with licenseNoticeFile file"); |
|---|
| 1186 | } |
|---|
| 1187 | // !!!!!! |
|---|
| 1188 | // files[13] = licenseNoticeFile; |
|---|
| 1189 | files[numberofPrompts + 2] = licenseNoticeFile; |
|---|
| 1190 | // !!!!!! |
|---|
| 1191 | //############ license file #################################### |
|---|
| 1192 | try { |
|---|
| 1193 | // !!!!!! |
|---|
| 1194 | //BufferedWriter out_licenseFile = new BufferedWriter(new FileWriter(licenseFile)); |
|---|
| 1195 | BufferedWriter out_licenseFile = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(licenseFile),"UTF-8")); |
|---|
| 1196 | // !!!!!! |
|---|
| 1197 | out_licenseFile.write(licenseObject.getGPLLicense()); |
|---|
| 1198 | out_licenseFile.close(); |
|---|
| 1199 | } catch (IOException e) { |
|---|
| 1200 | System.err.println("Problems with license file"); |
|---|
| 1201 | } |
|---|
| 1202 | // !!!!!! |
|---|
| 1203 | // files[14] = licenseFile; |
|---|
| 1204 | files[numberofPrompts + 3] = licenseFile; |
|---|
| 1205 | // !!!!!! |
|---|
| 1206 | //############ create archive file #################################### |
|---|
| 1207 | File archiveFile; |
|---|
| 1208 | Calendar cal = Calendar.getInstance(); |
|---|
| 1209 | int day = cal.get(Calendar.DATE); |
|---|
| 1210 | int month = cal.get(Calendar.MONTH) + 1; |
|---|
| 1211 | int year = cal.get(Calendar.YEAR); |
|---|
| 1212 | StringBuffer sb = new StringBuffer(11); |
|---|
| 1213 | sb.append(year); |
|---|
| 1214 | if( month < 10 ) sb.append("0"); |
|---|
| 1215 | sb.append(month); |
|---|
| 1216 | if( day < 10 ) sb.append("0"); |
|---|
| 1217 | sb.append(day); |
|---|
| 1218 | String date = sb.toString(); |
|---|
| 1219 | |
|---|
| 1220 | Random randomGenerator = new Random(); |
|---|
| 1221 | int SMALL_LETTERS_BASE_VALUE = 97; |
|---|
| 1222 | // int CAPITAL_LETTERS_BASE_VALUE = 65; |
|---|
| 1223 | StringBuffer randomID = new StringBuffer(""); // required for jvm 1.4.2 |
|---|
| 1224 | //StringBuilder randomID = new StringBuilder(""); // will not work with JVM 1.4.2 |
|---|
| 1225 | for ( int i = 0; randomID.length() < 3; i++ ) |
|---|
| 1226 | { |
|---|
| 1227 | int currentValue = randomGenerator.nextInt(26); |
|---|
| 1228 | currentValue += SMALL_LETTERS_BASE_VALUE;//convert to ASCII value for a-z |
|---|
| 1229 | randomID.append((char)currentValue); |
|---|
| 1230 | } |
|---|
| 1231 | |
|---|
| 1232 | if (userName != null){ |
|---|
| 1233 | if (language.equals("EN")) { |
|---|
| 1234 | archiveFile = new File(tempdir + userName + "-" + date + "-" + randomID + ".zip"); |
|---|
| 1235 | } else { |
|---|
| 1236 | archiveFile = new File(tempdir + language + "-" + userName + "-" + date + "-" + randomID + ".zip"); |
|---|
| 1237 | } |
|---|
| 1238 | } else { |
|---|
| 1239 | archiveFile = new File(tempdir + language + "-" + "Anonymous-" + date + "-" + randomID + ".zip"); |
|---|
| 1240 | } |
|---|
| 1241 | |
|---|
| 1242 | createZipArchive(archiveFile, files); |
|---|
| 1243 | System.err.println("Archive file location:" + archiveFile); |
|---|
| 1244 | totalBytes = ((int)archiveFile.length()) ; |
|---|
| 1245 | progBar.setMaximum((int)archiveFile.length()); |
|---|
| 1246 | //############ Upload #################################### |
|---|
| 1247 | // Upload manager needs an array but JavaUpload.php script can only handle one file at a time |
|---|
| 1248 | File[] archiveFiles = new File[1]; |
|---|
| 1249 | archiveFiles[0] = archiveFile; |
|---|
| 1250 | UploadManager u; |
|---|
| 1251 | try { |
|---|
| 1252 | u = new UploadManager(archiveFiles, capturePlayback, destinationURL, 1, fileFieldName); |
|---|
| 1253 | } catch(java.lang.NullPointerException npered){ |
|---|
| 1254 | u = new UploadManager(archiveFiles, capturePlayback, destinationURL, fileFieldName); |
|---|
| 1255 | } |
|---|
| 1256 | System.err.println("Uploading to " + destinationURL); |
|---|
| 1257 | u.start(); |
|---|
| 1258 | } |
|---|
| 1259 | |
|---|
| 1260 | /* |
|---|
| 1261 | private void shutDown(String message) { |
|---|
| 1262 | if ((errStr = message) != null) { |
|---|
| 1263 | System.err.println(errStr); |
|---|
| 1264 | } |
|---|
| 1265 | if (thread != null) { |
|---|
| 1266 | thread = null; |
|---|
| 1267 | } |
|---|
| 1268 | } |
|---|
| 1269 | */ |
|---|
| 1270 | |
|---|
| 1271 | protected void createZipArchive(File archiveFile, File[] tobeZippedFiles) { |
|---|
| 1272 | try { |
|---|
| 1273 | byte buffer[] = new byte[BUFFER_SIZE]; |
|---|
| 1274 | // Open archive file |
|---|
| 1275 | FileOutputStream stream = new FileOutputStream(archiveFile); |
|---|
| 1276 | ZipOutputStream out = new ZipOutputStream(stream); |
|---|
| 1277 | |
|---|
| 1278 | for (int i = 0; i < tobeZippedFiles.length; i++) { |
|---|
| 1279 | if (tobeZippedFiles[i] == null || !tobeZippedFiles[i].exists() |
|---|
| 1280 | || tobeZippedFiles[i].isDirectory()) |
|---|
| 1281 | continue; |
|---|
| 1282 | System.out.println("Adding " + tobeZippedFiles[i].getName()); |
|---|
| 1283 | |
|---|
| 1284 | // Add archive entry |
|---|
| 1285 | ZipEntry zipAdd = new ZipEntry(tobeZippedFiles[i].getName()); |
|---|
| 1286 | zipAdd.setTime(tobeZippedFiles[i].lastModified()); |
|---|
| 1287 | out.putNextEntry(zipAdd); |
|---|
| 1288 | |
|---|
| 1289 | // Read input & write to output |
|---|
| 1290 | FileInputStream in = new FileInputStream(tobeZippedFiles[i]); |
|---|
| 1291 | while (true) { |
|---|
| 1292 | int nRead = in.read(buffer, 0, buffer.length); |
|---|
| 1293 | if (nRead <= 0) |
|---|
| 1294 | break; |
|---|
| 1295 | out.write(buffer, 0, nRead); |
|---|
| 1296 | } |
|---|
| 1297 | in.close(); |
|---|
| 1298 | } |
|---|
| 1299 | |
|---|
| 1300 | out.close(); |
|---|
| 1301 | stream.close(); |
|---|
| 1302 | System.out.println("Adding completed OK"); |
|---|
| 1303 | } catch (Exception e) { |
|---|
| 1304 | e.printStackTrace(); |
|---|
| 1305 | System.out.println("Error: " + e.getMessage()); |
|---|
| 1306 | return; |
|---|
| 1307 | } |
|---|
| 1308 | } |
|---|
| 1309 | } |
|---|
| 1310 | |
|---|
| 1311 | /** |
|---|
| 1312 | * Render a WaveForm. |
|---|
| 1313 | */ |
|---|
| 1314 | class SamplingGraph extends JPanel implements Runnable { |
|---|
| 1315 | private Thread thread; |
|---|
| 1316 | //private Font font10 = new Font("serif", Font.PLAIN, 10); |
|---|
| 1317 | private Font font12 = new Font("serif", Font.PLAIN, 12); |
|---|
| 1318 | Color jfcBlue = new Color(204, 204, 255); |
|---|
| 1319 | Color pink = new Color(255, 175, 175); |
|---|
| 1320 | protected boolean peakWarning = false; |
|---|
| 1321 | |
|---|
| 1322 | public SamplingGraph() { |
|---|
| 1323 | setBackground(new Color(20, 20, 20)); |
|---|
| 1324 | } |
|---|
| 1325 | |
|---|
| 1326 | public void clearWaveForm() { |
|---|
| 1327 | lines.removeAllElements(); // clear the old vector |
|---|
| 1328 | repaint(); |
|---|
| 1329 | } |
|---|
| 1330 | |
|---|
| 1331 | public void createWaveForm(byte[] audioBytes) { |
|---|
| 1332 | |
|---|
| 1333 | lines.removeAllElements(); // clear the old vector |
|---|
| 1334 | |
|---|
| 1335 | AudioFormat format = audioInputStream.getFormat(); |
|---|
| 1336 | if (audioBytes == null) { |
|---|
| 1337 | try { |
|---|
| 1338 | audioBytes = new byte[ |
|---|
| 1339 | (int) (audioInputStream.getFrameLength() |
|---|
| 1340 | * format.getFrameSize())]; |
|---|
| 1341 | audioInputStream.read(audioBytes); |
|---|
| 1342 | } catch (Exception ex) { |
|---|
| 1343 | reportStatus(ex.toString()); |
|---|
| 1344 | return; |
|---|
| 1345 | } |
|---|
| 1346 | } |
|---|
| 1347 | |
|---|
| 1348 | Dimension d = getSize(); |
|---|
| 1349 | int w = d.width; |
|---|
| 1350 | int h = d.height-15; |
|---|
| 1351 | int[] audioData = null; |
|---|
| 1352 | int audioDatum = 0; // Will store values one-by-one |
|---|
| 1353 | long numPeakValues = 0; // To help detect peaking |
|---|
| 1354 | int peakThresh = (int)(Math.pow(2, format.getSampleSizeInBits()) * 0.4); |
|---|
| 1355 | // long nlengthInSamples = ais.getFrameLength() * ais.getFormat().getChannels(); |
|---|
| 1356 | int nlengthInSamples =0; |
|---|
| 1357 | if (format.getSampleSizeInBits() == 16) { |
|---|
| 1358 | // int nlengthInSamples = audioBytes.length / 2; |
|---|
| 1359 | nlengthInSamples = audioBytes.length / 2; |
|---|
| 1360 | audioData = new int[nlengthInSamples]; |
|---|
| 1361 | if (format.isBigEndian()) { |
|---|
| 1362 | for (int i = 0; i < nlengthInSamples; i++) { |
|---|
| 1363 | /* First byte is MSB (high order) */ |
|---|
| 1364 | int MSB = (int) audioBytes[2*i]; |
|---|
| 1365 | /* Second byte is LSB (low order) */ |
|---|
| 1366 | int LSB = (int) audioBytes[2*i+1]; |
|---|
| 1367 | audioData[i] = MSB << 8 | (255 & LSB); |
|---|
| 1368 | audioDatum = MSB << 8 | (255 & LSB); |
|---|
| 1369 | if(audioDatum > peakThresh) { |
|---|
| 1370 | numPeakValues++; |
|---|
| 1371 | } |
|---|
| 1372 | } |
|---|
| 1373 | } else { |
|---|
| 1374 | for (int i = 0; i < nlengthInSamples; i++) { |
|---|
| 1375 | /* First byte is LSB (low order) */ |
|---|
| 1376 | int LSB = (int) audioBytes[2*i]; |
|---|
| 1377 | /* Second byte is MSB (high order) */ |
|---|
| 1378 | int MSB = (int) audioBytes[2*i+1]; |
|---|
| 1379 | audioData[i] = MSB << 8 | (255 & LSB); |
|---|
| 1380 | audioDatum = MSB << 8 | (255 & LSB); |
|---|
| 1381 | if(audioDatum > peakThresh) { |
|---|
| 1382 | numPeakValues++; |
|---|
| 1383 | } |
|---|
| 1384 | } |
|---|
| 1385 | } |
|---|
| 1386 | } |
|---|
| 1387 | |
|---|
| 1388 | int frames_per_pixel = audioBytes.length / format.getFrameSize()/w; |
|---|
| 1389 | byte my_byte = 0; |
|---|
| 1390 | double y_last = 0; |
|---|
| 1391 | int numChannels = format.getChannels(); |
|---|
| 1392 | for (double x = 0; x < w && audioData != null; x++) { |
|---|
| 1393 | int idx = (int) (frames_per_pixel * numChannels * x); |
|---|
| 1394 | if (format.getSampleSizeInBits() == 8) { |
|---|
| 1395 | my_byte = (byte) audioData[idx]; |
|---|
| 1396 | } else { |
|---|
| 1397 | my_byte = (byte) (128 * audioData[idx] / 32768 ); |
|---|
| 1398 | } |
|---|
| 1399 | double y_new = (double) (h * (128 - my_byte) / 256); |
|---|
| 1400 | lines.add(new Line2D.Double(x, y_last, x, y_new)); |
|---|
| 1401 | y_last = y_new; |
|---|
| 1402 | } |
|---|
| 1403 | System.err.println("numPeakValues: " + numPeakValues); |
|---|
| 1404 | float proportionPeakValues = ((float)numPeakValues) / ((int)nlengthInSamples); |
|---|
| 1405 | System.err.println("proportionPeakValues: " + proportionPeakValues); |
|---|
| 1406 | peakWarning = proportionPeakValues > 0.001f; |
|---|
| 1407 | repaint(); |
|---|
| 1408 | } |
|---|
| 1409 | |
|---|
| 1410 | public void paint(Graphics g) { |
|---|
| 1411 | |
|---|
| 1412 | Dimension d = getSize(); |
|---|
| 1413 | int w = d.width; |
|---|
| 1414 | int h = d.height; |
|---|
| 1415 | int INFOPAD = 15; |
|---|
| 1416 | |
|---|
| 1417 | Graphics2D g2 = (Graphics2D) g; |
|---|
| 1418 | g2.setBackground(getBackground()); |
|---|
| 1419 | g2.clearRect(0, 0, w, h); |
|---|
| 1420 | g2.setColor(Color.white); |
|---|
| 1421 | g2.fillRect(0, h-INFOPAD, w, INFOPAD); |
|---|
| 1422 | |
|---|
| 1423 | if (errStr != null) { |
|---|
| 1424 | g2.setColor(jfcBlue); |
|---|
| 1425 | g2.setFont(new Font("serif", Font.BOLD, 18)); |
|---|
| 1426 | g2.drawString("ERROR", 5, 20); |
|---|
| 1427 | AttributedString as = new AttributedString(errStr); |
|---|
| 1428 | as.addAttribute(TextAttribute.FONT, font12, 0, errStr.length()); |
|---|
| 1429 | AttributedCharacterIterator aci = as.getIterator(); |
|---|
| 1430 | FontRenderContext frc = g2.getFontRenderContext(); |
|---|
| 1431 | LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc); |
|---|
| 1432 | float x = 5, y = 25; |
|---|
| 1433 | lbm.setPosition(0); |
|---|
| 1434 | while (lbm.getPosition() < errStr.length()) { |
|---|
| 1435 | TextLayout tl = lbm.nextLayout(w-x-5); |
|---|
| 1436 | if (!tl.isLeftToRight()) { |
|---|
| 1437 | x = w - tl.getAdvance(); |
|---|
| 1438 | } |
|---|
| 1439 | tl.draw(g2, x, y += tl.getAscent()); |
|---|
| 1440 | y += tl.getDescent() + tl.getLeading(); |
|---|
| 1441 | } |
|---|
| 1442 | } else if (capture.thread != null) { |
|---|
| 1443 | g2.setColor(Color.black); |
|---|
| 1444 | g2.setFont(font12); |
|---|
| 1445 | g2.drawString(sampleGraphLengthLabel + String.valueOf(seconds), 3, h-4); |
|---|
| 1446 | } else { |
|---|
| 1447 | g2.setColor(Color.black); |
|---|
| 1448 | g2.setFont(font12); |
|---|
| 1449 | g2.drawString(sampleGraphFileLabel + fileName + sampleGraphLengthLabel + String.valueOf(duration) + sampleGraphPositionLabel + String.valueOf(seconds), 3, h-4); |
|---|
| 1450 | |
|---|
| 1451 | if (audioInputStream != null) { |
|---|
| 1452 | // .. render sampling graph .. |
|---|
| 1453 | g2.setColor(jfcBlue); |
|---|
| 1454 | for (int i = 1; i < lines.size(); i++) { |
|---|
| 1455 | g2.draw((Line2D) lines.get(i)); |
|---|
| 1456 | } |
|---|
| 1457 | |
|---|
| 1458 | // .. draw current position .. |
|---|
| 1459 | // debug System.err.println("seconds "+seconds+";duration:"+duration+":"); |
|---|
| 1460 | if (seconds != 0) { |
|---|
| 1461 | double loc = seconds/duration*w; |
|---|
| 1462 | g2.setColor(pink); |
|---|
| 1463 | g2.setStroke(new BasicStroke(3)); |
|---|
| 1464 | g2.draw(new Line2D.Double(loc, 0, loc, h-INFOPAD-2)); |
|---|
| 1465 | } |
|---|
| 1466 | } |
|---|
| 1467 | } |
|---|
| 1468 | } |
|---|
| 1469 | |
|---|
| 1470 | public void start() { |
|---|
| 1471 | thread = new Thread(this); |
|---|
| 1472 | thread.setName("SamplingGraph"); |
|---|
| 1473 | thread.start(); |
|---|
| 1474 | seconds = 0; |
|---|
| 1475 | } |
|---|
| 1476 | |
|---|
| 1477 | public void stop() { |
|---|
| 1478 | if (thread != null) { |
|---|
| 1479 | thread.interrupt(); |
|---|
| 1480 | } |
|---|
| 1481 | thread = null; |
|---|
| 1482 | } |
|---|
| 1483 | |
|---|
| 1484 | public void run() { |
|---|
| 1485 | seconds = 0; |
|---|
| 1486 | while (thread != null) { |
|---|
| 1487 | if ((playback.line != null) && (playback.line.isOpen()) ) { |
|---|
| 1488 | |
|---|
| 1489 | long milliseconds = (long)(playback.line.getMicrosecondPosition() / 1000); |
|---|
| 1490 | |
|---|
| 1491 | seconds = milliseconds / 1000.0; |
|---|
| 1492 | // debug System.err.println("playback.line:seconds "+seconds+";milliseconds:"+milliseconds+":"); |
|---|
| 1493 | } else if ( (capture.line != null) && (capture.line.isActive()) ) { |
|---|
| 1494 | |
|---|
| 1495 | long milliseconds = (long)(capture.line.getMicrosecondPosition() / 1000); |
|---|
| 1496 | seconds = milliseconds / 1000.0; |
|---|
| 1497 | // debug System.err.println("capture.line:seconds "+seconds+";milliseconds:"+milliseconds+":"); |
|---|
| 1498 | |
|---|
| 1499 | } |
|---|
| 1500 | |
|---|
| 1501 | //try { thread.sleep(100); } catch (Exception e) { break; } |
|---|
| 1502 | try { Thread.sleep(100); } catch (Exception e) { break; } |
|---|
| 1503 | |
|---|
| 1504 | repaint(); |
|---|
| 1505 | |
|---|
| 1506 | while ((capture.line != null && !capture.line.isActive()) || |
|---|
| 1507 | (playback.line != null && !playback.line.isOpen())) |
|---|
| 1508 | { |
|---|
| 1509 | //try { thread.sleep(10); } catch (Exception e) { break; } |
|---|
| 1510 | try { Thread.sleep(10); } catch (Exception e) { break; } |
|---|
| 1511 | } |
|---|
| 1512 | } |
|---|
| 1513 | seconds = 0; |
|---|
| 1514 | repaint(); |
|---|
| 1515 | } |
|---|
| 1516 | } // End class SamplingGraph |
|---|
| 1517 | |
|---|
| 1518 | |
|---|
| 1519 | |
|---|
| 1520 | // Methods called by the "postlet" UploadManager and UploadThread |
|---|
| 1521 | public synchronized void setProgress(int a) { |
|---|
| 1522 | sentBytes += a; |
|---|
| 1523 | progBar.setValue(sentBytes); |
|---|
| 1524 | if (sentBytes == totalBytes){ |
|---|
| 1525 | //DEL progCompletion.setText(pLabels.getLabel(2)); |
|---|
| 1526 | if (endPageURL != null){ |
|---|
| 1527 | progBar.setString(uploadCompletedMessageLabel); |
|---|
| 1528 | progBar.setIndeterminate(false); |
|---|
| 1529 | System.err.println("Finished! Sending you on to "+endPageURL); |
|---|
| 1530 | ((JApplet)(getParent().getParent().getParent().getParent())).getAppletContext().showDocument(endPageURL); |
|---|
| 1531 | } else { |
|---|
| 1532 | // Just ignore this error, as it is most likely from the endpage |
|---|
| 1533 | // not being set. |
|---|
| 1534 | // Attempt at calling Javascript after upload is complete. |
|---|
| 1535 | //FORDEBUG |
|---|
| 1536 | System.err.println("setProgress(): endPageURL is null, so trying to use the javascript hook to end."); |
|---|
| 1537 | JSObject win = (JSObject) JSObject.getWindow((JApplet)getParent()); |
|---|
| 1538 | win.eval("postletFinished();"); |
|---|
| 1539 | } |
|---|
| 1540 | // Reset the applet |
|---|
| 1541 | progBar.setValue(0); |
|---|
| 1542 | // if(subject != null) { // Will be non-null if passed in as a param - even if blank |
|---|
| 1543 | // subjectBox.setEnabled(true); |
|---|
| 1544 | // } |
|---|
| 1545 | } else { |
|---|
| 1546 | // !!!!!! |
|---|
| 1547 | //FORDEBUG |
|---|
| 1548 | // debug System.err.println("setProgress(): Not reached end yet. sentBytes="+sentBytes+", totalBytes="+totalBytes); |
|---|
| 1549 | // !!!!!! |
|---|
| 1550 | } |
|---|
| 1551 | } |
|---|
| 1552 | |
|---|
| 1553 | public String getCookie(){ |
|---|
| 1554 | // If passed in as a param then we don't need to worry about trying to fetch it from the context |
|---|
| 1555 | // System.err.println("CapturePlayback Cookie: " + cookie +":\n"); |
|---|
| 1556 | // System.err.println("CapturePlayback Cookie: " + this.cookie +":\n"); |
|---|
| 1557 | // !!!!!! |
|---|
| 1558 | if (cookie != null && !cookie.equals("")) |
|---|
| 1559 | return cookie; |
|---|
| 1560 | // cookie = "test"; |
|---|
| 1561 | // !!!!!!! |
|---|
| 1562 | // Method reads the cookie in from the Browser using the LiveConnect object. |
|---|
| 1563 | // May also add an option to set the cookie using an applet parameter |
|---|
| 1564 | // JSObject win = (JSObject) JSObject.getWindow((JApplet) this.getParent()); |
|---|
| 1565 | // cookie = "" + (String) win.eval("document.cookie"); |
|---|
| 1566 | return cookie; |
|---|
| 1567 | } |
|---|
| 1568 | |
|---|
| 1569 | |
|---|
| 1570 | } |
|---|
| 1571 | |
|---|