public void run() { // get line and buffer from ThreadLocals SourceDataLine line = (SourceDataLine) localLine.get(); byte[] buffer = (byte[]) localBuffer.get(); if (line == null || buffer == null) { // the line is unavailable return; } // copy data to the line try { int numBytesRead = 0; while (numBytesRead != -1) { // if paused, wait until unpaused synchronized (pausedLock) { if (paused) { try { pausedLock.wait(); } catch (InterruptedException ex) { return; } } } // copy data numBytesRead = source.read(buffer, 0, buffer.length); if (numBytesRead != -1) { line.write(buffer, 0, numBytesRead); } } } catch (IOException ex) { ex.printStackTrace(); } }
/** Creates an AudioInputStream from a sound from an input stream */ public AudioInputStream getAudioInputStream(InputStream is) { try { if (!is.markSupported()) { is = new BufferedInputStream(is); } // open the source stream AudioInputStream source = AudioSystem.getAudioInputStream(is); // convert to playback format return AudioSystem.getAudioInputStream(playbackFormat, source); } catch (UnsupportedAudioFileException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } catch (IllegalArgumentException ex) { ex.printStackTrace(); } return null; }
private int load(int addr) { if (addr == RN) { return rand.nextInt(); } if (addr == KY) { return keys; } if (addr == KB) { if (keyQueue.size() > 0) { return keyQueue.remove(); } return -1; } if (addr == CO) { try { return System.in.read(); } catch (java.io.IOException e) { e.printStackTrace(); } } if (addr == XO) { if (xi.containsKey(m[XA])) { try { return normalizeRead(xi.get(m[XA])); } catch (IOException e) { e.printStackTrace(); } } return -1; } if (addr == XS) { return 3; // supports reading/writing local files } return m[addr]; }
/** Creates an AudioInputStream from a sound from the file system. */ public AudioInputStream getAudioInputStream(String filename) { try { return getAudioInputStream(new FileInputStream(filename)); } catch (IOException ex) { ex.printStackTrace(); return null; } }
private void stor(int addr, int value) { if (addr == CO) { System.out.print((char) value); return; } if (addr == AU) { abuffer[apointer] = (byte) value; if (apointer < abuffer.length - 1) { apointer++; } } if (addr == XO) { if (xo.containsKey(m[XA])) { try { xo.get(m[XA]).write(value); } catch (IOException e) { e.printStackTrace(); } } } if (addr == XS) { try { if (value == X_CLOSE) { if (xi.containsKey(m[XA])) { xi.get(m[XA]).close(); } if (xo.containsKey(m[XA])) { xo.get(m[XA]).close(); } xi.remove(m[XA]); xo.remove(m[XA]); } if (value == X_OPEN_READ) { xi.put(xc, new PushbackInputStream(new FileInputStream(extractString(m[XA])))); m[XA] = xc++; } if (value == X_OPEN_WRITE) { xo.put(xc, new FileOutputStream(extractString(m[XA]))); m[XA] = xc++; } } catch (IOException e) { m[XA] = -1; } } m[addr] = value; }
public static void Play() { try { // Open an audio input stream. File soundFile = new File("foo.wav"); AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile); // Get a sound clip resource. Clip clip = AudioSystem.getClip(); // Open audio clip and load samples from the audio input stream. clip.open(audioIn); clip.start(); } catch (UnsupportedAudioFileException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (LineUnavailableException e) { e.printStackTrace(); } }
// Constructor to construct each element of the enum with its own sound file. SoundEffect(String soundFileName) { try { // Use URL (instead of File) to read from disk and JAR. URL url = this.getClass().getClassLoader().getResource(soundFileName); // Set up an audio input stream piped from the sound file. AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url); // Get a clip resource. clip = AudioSystem.getClip(); // Open audio clip and load samples from the audio input stream. clip.open(audioInputStream); } catch (UnsupportedAudioFileException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (LineUnavailableException e) { e.printStackTrace(); } }
/** Loads a Sound from an AudioInputStream. */ public Sound getSound(AudioInputStream audioStream) { if (audioStream == null) { return null; } // get the number of bytes to read int length = (int) (audioStream.getFrameLength() * audioStream.getFormat().getFrameSize()); // read the entire stream byte[] samples = new byte[length]; DataInputStream is = new DataInputStream(audioStream); try { is.readFully(samples); is.close(); } catch (IOException ex) { ex.printStackTrace(); } // return the samples return new Sound(samples); }
public int read() throws IOException { // we don't use this IOException ioe = new IOException(getClass().getName() + ".read:\n\tInternal Error."); ioe.printStackTrace(); throw ioe; }