@Override public void run() { running = true; done = false; Log.println("WAV Source START"); if (audioStream == null) try { initWav(); } catch (UnsupportedAudioFileException e1) { Log.errorDialog("ERROR", "Unsupported File Format\n" + e1.getMessage()); e1.printStackTrace(Log.getWriter()); running = false; } catch (IOException e1) { Log.errorDialog("ERROR", "There was a problem opening the wav file\n" + e1.getMessage()); e1.printStackTrace(Log.getWriter()); running = false; } while (running) { // Log.println("wav running"); if (audioStream != null) { int nBytesRead = 0; if (circularBuffer.getCapacity() > readBuffer.length) { try { nBytesRead = audioStream.read(readBuffer, 0, readBuffer.length); bytesRead = bytesRead + nBytesRead; framesProcessed = framesProcessed + nBytesRead / frameSize; // Check we have not stopped mid read if (audioStream == null) running = false; else if (!(audioStream.available() > 0)) running = false; } catch (IOException e) { Log.errorDialog("ERROR", "Failed to read from file " + fileName); e.printStackTrace(Log.getWriter()); } } else { try { Thread.sleep(1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Log.println("No room in Buffer"); } for (int i = 0; i < nBytesRead; i += 2) { // circularBuffer.add(readBuffer[i]); circularBuffer.add(readBuffer[i], readBuffer[i + 1]); } } } framesProcessed = totalFrames; cleanup(); // This might cause the decoder to miss the end of a file (testing inconclusive) but // it also ensure the file is close and stops an error if run again very quickly running = false; try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } Log.println("WAV Source EXIT"); }
private void initWav() throws UnsupportedAudioFileException, IOException { readBuffer = new byte[DEFAULT_READ_BUFFER_SIZE]; // circularBuffer = new CircularByteBuffer(67200*3); Log.println("Wavefile: " + fileName); File soundFile = null; soundFile = new File(fileName); audioStream = AudioSystem.getAudioInputStream(soundFile); audioFormat = audioStream.getFormat(); Config.wavSampleRate = (int) audioFormat.getSampleRate(); Log.println("Format: " + audioFormat); totalFrames = audioStream.getFrameLength(); frameSize = audioFormat.getFrameSize(); Log.println("Length (frames): " + totalFrames); bytesRead = 0; framesProcessed = 0; }
/** * Remove a log file from disk and report any errors. * * @param f * @throws IOException */ public static void remove(String f) throws IOException { try { // if (!Config.logFileDirectory.equalsIgnoreCase("")) { // f = Config.logFileDirectory + File.separator + f; // //System.err.println("Loading: "+log); // } File file = new File(f); if (file.exists()) if (file.delete()) { Log.println(file.getName() + " is deleted!"); } else { Log.println("Delete operation failed for: " + file.getName()); throw new IOException( "Could not delete file " + file.getName() + " Check the file system and remove it manually."); } } catch (Exception ex) { JOptionPane.showMessageDialog( MainWindow.frame, ex.toString(), "Error Deleting File", JOptionPane.ERROR_MESSAGE); } }
/** * Load a file from disk * * @param log * @throws FileNotFoundException */ public void load(String log) throws FileNotFoundException { String line; if (!Config.logFileDirectory.equalsIgnoreCase("")) { log = Config.logFileDirectory + File.separator + log; Log.println("Loading: " + log); } File aFile = new File(log); if (!aFile.exists()) { try { aFile.createNewFile(); } catch (IOException e) { JOptionPane.showMessageDialog( MainWindow.frame, e.toString(), "ERROR creating file " + log, JOptionPane.ERROR_MESSAGE); e.printStackTrace(Log.getWriter()); } } BufferedReader dis = new BufferedReader(new FileReader(log)); try { while ((line = dis.readLine()) != null) { if (line != null) { StringTokenizer st = new StringTokenizer(line, ","); String date = st.nextToken(); int id = Integer.valueOf(st.nextToken()).intValue(); int reset = Integer.valueOf(st.nextToken()).intValue(); long uptime = Long.valueOf(st.nextToken()).longValue(); int type = Integer.valueOf(st.nextToken()).intValue(); // We should never get this situation, but good to check.. if (Config.satManager.getSpacecraft(id) == null) { Log.errorDialog( "FATAL", "Attempting to Load payloads from the Payload store for satellite with Fox Id: " + id + "\n when no sattellite with that FoxId is configured. Add this spacecraft to the satellite directory and restart FoxTelem." + "\nProgram will now exit"); System.exit(1); } if (type == RT_MEASUREMENT_TYPE) { RtMeasurement rt = new RtMeasurement(id, date, reset, uptime, type, st); rtRecords.add(rt); updatedRt = true; } if (type == PASS_MEASUREMENT_TYPE) { PassMeasurement rt = new PassMeasurement(id, date, reset, uptime, type, st); passRecords.add(rt); updatedPass = true; } } } } catch (IOException e) { e.printStackTrace(Log.getWriter()); } catch (NumberFormatException n) { n.printStackTrace(Log.getWriter()); } finally { try { dis.close(); } catch (IOException e) { e.printStackTrace(Log.getWriter()); } } }