Search in sources :

Example 16 with UnsupportedAudioFileException

use of javax.sound.sampled.UnsupportedAudioFileException in project screenbird by adamhub.

the class AudioCache method playAudio.

/**
     * Thread for playing audio.
     */
private synchronized void playAudio() {
    try {
        // Start a new thread for playing audio
        if (this.playThread != null) {
            this.playThread = null;
        }
        if (this.cacheStream != null) {
            // Release for reading
            this.cacheStream.flush();
            this.cacheStream.close();
            this.cacheStream = null;
        }
        // Load audio cache 
        log(String.format("Loading audio cache %s %d", this.cacheFile.getAbsolutePath(), this.cacheFile.length()));
        final FileInputStream input = new FileInputStream(this.cacheFile);
        log("Loaded audio cache file with size" + input.available());
        // Set up hardware for playback
        try {
            AudioInputStream audioStream = AudioSystem.getAudioInputStream(this.cacheFile);
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioStream.getFormat());
            dataLine = (SourceDataLine) AudioSystem.getLine(info);
        } catch (IOException e) {
            log(e);
        } catch (UnsupportedAudioFileException e) {
            log(e);
        }
        // Set up audio playback thread.
        Thread runner = new Thread("Preview Audio Thread") {

            @Override
            public void run() {
                try {
                    // Marks audio in playing state
                    isPlaying = true;
                    // Prep hardware for audio playback
                    dataLine.open();
                    dataLine.start();
                    // Read data for playing audio
                    int bytesRead = 0;
                    int buffSize = dataLine.getBufferSize();
                    byte[] data = new byte[buffSize];
                    // Compute total time of audio playback
                    totalTimeMS = (long) ((input.available() * 1000) / ((double) audioFormat.getFrameSize() * audioFormat.getSampleRate()));
                    log("TimeMS: " + (startTimeMS));
                    // Prep offsets for accurate audio playback
                    currOffset = (long) ((startTimeMS / 1000) * (double) audioFormat.getFrameSize() * audioFormat.getSampleRate());
                    currTimeMS = startTimeMS;
                    log(String.format("Seek to MS[%d] Bytes[%d] TotalBytes[%d]", startTimeMS, currOffset, input.available()));
                    // If not starting at begining of audio file
                    input.skip(currOffset);
                    // Play the entire audio
                    while ((bytesRead = input.read(data, 0, data.length)) != -1 && isPlaying) {
                        currOffset += bytesRead;
                        // Update current time of audio that is being played
                        currTimeMS = (long) ((currOffset * 1000) / ((double) audioFormat.getFrameSize() * audioFormat.getSampleRate())) - 600;
                        // Check to see if sequence has been scrubbed
                        if (scrubIndex != null && !scrubIndex.isEmpty() && // Is current second in scrub index array
                        scrubIndex.indexOf((int) (currTimeMS / 1000)) >= 0) {
                            // Do not write to audio line
                            continue;
                        }
                        // Write to audio line
                        dataLine.write(data, 0, data.length);
                    }
                    if (isPlaying && dataLine != null) {
                        dataLine.drain();
                        dataLine.flush();
                    }
                    // Kills video feed
                    currTimeMS = totalTimeMS;
                    isPlaying = false;
                    if (dataLine != null) {
                        dataLine.stop();
                    }
                    log("Done with Audio");
                } catch (LineUnavailableException e) {
                    log("No sound line available!" + e);
                } catch (IOException e) {
                    log(e);
                } finally {
                    // Release audio playback hardware
                    try {
                        dataLine.close();
                    } catch (NullPointerException e) {
                    // This always throws an exception for some reason
                    }
                    try {
                        input.close();
                    } catch (IOException e) {
                        log(e);
                    } catch (NullPointerException e) {
                    // Do nothing
                    }
                }
                // Stop running playback thread
                this.interrupt();
                playThread = null;
            }
        };
        // Start audio playback thread
        playThread = null;
        playThread = runner;
        playThread.start();
    } catch (LineUnavailableException e) {
        System.err.println("Line unavailable: " + e);
        System.exit(-4);
    } catch (FileNotFoundException e) {
        log(e);
    } catch (IOException e) {
        log(e);
    }
}
Also used : DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException)

Example 17 with UnsupportedAudioFileException

use of javax.sound.sampled.UnsupportedAudioFileException in project playn by threerings.

the class BigClip method open.

@Override
public void open(AudioFormat format, byte[] data, int offset, int bufferSize) throws LineUnavailableException {
    byte[] input = new byte[bufferSize];
    for (int ii = 0; ii < input.length; ii++) {
        input[ii] = data[offset + ii];
    }
    ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
    try {
        AudioInputStream ais1 = AudioSystem.getAudioInputStream(inputStream);
        AudioInputStream ais2 = AudioSystem.getAudioInputStream(format, ais1);
        open(ais2);
    } catch (UnsupportedAudioFileException uafe) {
        throw new IllegalArgumentException(uafe);
    } catch (IOException ioe) {
        throw new IllegalArgumentException(ioe);
    }
// TODO - throw IAE for invalid frame size, format.
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException)

Example 18 with UnsupportedAudioFileException

use of javax.sound.sampled.UnsupportedAudioFileException in project openblocks by mikaelhg.

the class SoundManager method loadSound.

public static Sound loadSound(String soundFileName) {
    final URL url = SoundManager.class.getResource(soundFileName);
    if (url == null) {
        System.out.println("Could not find resource " + soundFileName);
        return null;
    }
    AudioInputStream audioInputStream;
    try {
        audioInputStream = AudioSystem.getAudioInputStream(url);
    } catch (UnsupportedAudioFileException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
    final AudioFormat format = audioInputStream.getFormat();
    final Clip clip;
    try {
        DataLine.Info info = new DataLine.Info(Clip.class, format);
        clip = (Clip) AudioSystem.getLine(info);
        clip.open(audioInputStream);
    } catch (LineUnavailableException e) {
        System.out.println("Sorry, sound is not available");
        return null;
    } catch (IOException e) {
        return null;
    }
    return new Sound(clip);
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) Clip(javax.sound.sampled.Clip) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) DataLine(javax.sound.sampled.DataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException) IOException(java.io.IOException) AudioFormat(javax.sound.sampled.AudioFormat) URL(java.net.URL)

Example 19 with UnsupportedAudioFileException

use of javax.sound.sampled.UnsupportedAudioFileException in project jdk8u_jdk by JetBrains.

the class AuFileReader method getAudioFileFormat.

// METHODS TO IMPLEMENT AudioFileReader
/**
     * Obtains the audio file format of the input stream provided.  The stream must
     * point to valid audio file data.  In general, audio file providers may
     * need to read some data from the stream before determining whether they
     * support it.  These parsers must
     * be able to mark the stream, read enough data to determine whether they
     * support the stream, and, if not, reset the stream's read pointer to its original
     * position.  If the input stream does not support this, this method may fail
     * with an IOException.
     * @param stream the input stream from which file format information should be
     * extracted
     * @return an <code>AudioFileFormat</code> object describing the audio file format
     * @throws UnsupportedAudioFileException if the stream does not point to valid audio
     * file data recognized by the system
     * @throws IOException if an I/O exception occurs
     * @see InputStream#markSupported
     * @see InputStream#mark
     */
public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException {
    AudioFormat format = null;
    AuFileFormat fileFormat = null;
    int maxReadLength = 28;
    boolean bigendian = false;
    int magic = -1;
    int headerSize = -1;
    int dataSize = -1;
    int encoding_local = -1;
    int sampleRate = -1;
    int frameRate = -1;
    int frameSize = -1;
    int channels = -1;
    final int sampleSizeInBits;
    int length = 0;
    int nread = 0;
    AudioFormat.Encoding encoding = null;
    DataInputStream dis = new DataInputStream(stream);
    dis.mark(maxReadLength);
    magic = dis.readInt();
    if (!(magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC) || (magic == AuFileFormat.AU_SUN_INV_MAGIC) || (magic == AuFileFormat.AU_DEC_INV_MAGIC)) {
        // not AU, reset the stream, place into exception, throw exception
        dis.reset();
        throw new UnsupportedAudioFileException("not an AU file");
    }
    if ((magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC)) {
        // otherwise little-endian
        bigendian = true;
    }
    headerSize = (bigendian == true ? dis.readInt() : rllong(dis));
    nread += 4;
    dataSize = (bigendian == true ? dis.readInt() : rllong(dis));
    nread += 4;
    encoding_local = (bigendian == true ? dis.readInt() : rllong(dis));
    nread += 4;
    sampleRate = (bigendian == true ? dis.readInt() : rllong(dis));
    nread += 4;
    channels = (bigendian == true ? dis.readInt() : rllong(dis));
    nread += 4;
    if (channels <= 0) {
        dis.reset();
        throw new UnsupportedAudioFileException("Invalid number of channels");
    }
    frameRate = sampleRate;
    switch(encoding_local) {
        case AuFileFormat.AU_ULAW_8:
            encoding = AudioFormat.Encoding.ULAW;
            sampleSizeInBits = 8;
            break;
        case AuFileFormat.AU_ALAW_8:
            encoding = AudioFormat.Encoding.ALAW;
            sampleSizeInBits = 8;
            break;
        case AuFileFormat.AU_LINEAR_8:
            // $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*
            encoding = AudioFormat.Encoding.PCM_SIGNED;
            sampleSizeInBits = 8;
            break;
        case AuFileFormat.AU_LINEAR_16:
            encoding = AudioFormat.Encoding.PCM_SIGNED;
            sampleSizeInBits = 16;
            break;
        case AuFileFormat.AU_LINEAR_24:
            encoding = AudioFormat.Encoding.PCM_SIGNED;
            sampleSizeInBits = 24;
            break;
        case AuFileFormat.AU_LINEAR_32:
            encoding = AudioFormat.Encoding.PCM_SIGNED;
            sampleSizeInBits = 32;
            break;
        /*          case AuFileFormat.AU_FLOAT:
                        encoding = new AudioFormat.FLOAT;
                        sampleSizeInBits = 32;
                        break;
                        case AuFileFormat.AU_DOUBLE:
                        encoding = new AudioFormat.DOUBLE;
                        sampleSizeInBits = 8;
                        break;
                        case AuFileFormat.AU_ADPCM_G721:
                        encoding = new AudioFormat.G721_ADPCM;
                        sampleSizeInBits = 16;
                        break;
                        case AuFileFormat.AU_ADPCM_G723_3:
                        encoding = new AudioFormat.G723_3;
                        sampleSize = 24;
                        SamplePerUnit = 8;
                        break;
                        case AuFileFormat.AU_ADPCM_G723_5:
                        encoding = new AudioFormat.G723_5;
                        sampleSize = 40;
                        SamplePerUnit = 8;
                        break;
            */
        default:
            // unsupported filetype, throw exception
            dis.reset();
            throw new UnsupportedAudioFileException("not a valid AU file");
    }
    frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);
    //$$fb 2002-11-02: fix for 4629669: AU file reader: problems with empty files
    if (dataSize < 0) {
        length = AudioSystem.NOT_SPECIFIED;
    } else {
        //$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED
        length = dataSize / frameSize;
    }
    format = new AudioFormat(encoding, (float) sampleRate, sampleSizeInBits, channels, frameSize, (float) frameRate, bigendian);
    fileFormat = new AuFileFormat(AudioFileFormat.Type.AU, dataSize + headerSize, format, length);
    // Throws IOException
    dis.reset();
    return fileFormat;
}
Also used : UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) AudioFormat(javax.sound.sampled.AudioFormat) DataInputStream(java.io.DataInputStream)

Example 20 with UnsupportedAudioFileException

use of javax.sound.sampled.UnsupportedAudioFileException in project jdk8u_jdk by JetBrains.

the class AudioFileSoundbankReader method getSoundbank.

public Soundbank getSoundbank(File file) throws InvalidMidiDataException, IOException {
    try {
        AudioInputStream ais = AudioSystem.getAudioInputStream(file);
        ais.close();
        ModelByteBufferWavetable osc = new ModelByteBufferWavetable(new ModelByteBuffer(file, 0, file.length()), -4800);
        ModelPerformer performer = new ModelPerformer();
        performer.getOscillators().add(osc);
        SimpleSoundbank sbk = new SimpleSoundbank();
        SimpleInstrument ins = new SimpleInstrument();
        ins.add(performer);
        sbk.addInstrument(ins);
        return sbk;
    } catch (UnsupportedAudioFileException e1) {
        return null;
    } catch (IOException e) {
        return null;
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) IOException(java.io.IOException)

Aggregations

UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)25 IOException (java.io.IOException)17 AudioInputStream (javax.sound.sampled.AudioInputStream)15 AudioFormat (javax.sound.sampled.AudioFormat)10 AudioFileFormat (javax.sound.sampled.AudioFileFormat)8 BufferedInputStream (java.io.BufferedInputStream)6 InputStream (java.io.InputStream)5 LineUnavailableException (javax.sound.sampled.LineUnavailableException)4 DataInputStream (java.io.DataInputStream)3 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 URL (java.net.URL)3 HashMap (java.util.HashMap)3 IcyInputStream (javazoom.spi.mpeg.sampled.file.tag.IcyInputStream)3 File (java.io.File)2 PushbackInputStream (java.io.PushbackInputStream)2 MalformedURLException (java.net.MalformedURLException)2 URLConnection (java.net.URLConnection)2 InvalidMidiDataException (javax.sound.midi.InvalidMidiDataException)2 Sequence (javax.sound.midi.Sequence)2