Search in sources :

Example 76 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project algorithms-learning by brianway.

the class StdAudio method readByte.

// return data as a byte array
private static byte[] readByte(String filename) {
    byte[] data = null;
    AudioInputStream ais = null;
    try {
        // try to read from file
        File file = new File(filename);
        if (file.exists()) {
            ais = AudioSystem.getAudioInputStream(file);
            data = new byte[ais.available()];
            ais.read(data);
        } else // try to read from URL
        {
            URL url = StdAudio.class.getResource(filename);
            ais = AudioSystem.getAudioInputStream(url);
            data = new byte[ais.available()];
            ais.read(data);
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
        throw new RuntimeException("Could not read " + filename);
    } catch (UnsupportedAudioFileException e) {
        System.out.println(e.getMessage());
        throw new RuntimeException(filename + " in unsupported audio format");
    }
    return data;
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL)

Example 77 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project algorithms-learning by brianway.

the class StdAudio method save.

/**
 * Saves the double array as an audio file (using .wav or .au format).
 *
 * @param filename the name of the audio file
 * @param samples the array of samples
 */
public static void save(String filename, double[] samples) {
    // assumes 44,100 samples per second
    // use 16-bit audio, mono, signed PCM, little Endian
    AudioFormat format = new AudioFormat(SAMPLE_RATE, 16, 1, true, false);
    byte[] data = new byte[2 * samples.length];
    for (int i = 0; i < samples.length; i++) {
        int temp = (short) (samples[i] * MAX_16_BIT);
        data[2 * i + 0] = (byte) temp;
        data[2 * i + 1] = (byte) (temp >> 8);
    }
    // now save the file
    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        AudioInputStream ais = new AudioInputStream(bais, format, samples.length);
        if (filename.endsWith(".wav") || filename.endsWith(".WAV")) {
            AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File(filename));
        } else if (filename.endsWith(".au") || filename.endsWith(".AU")) {
            AudioSystem.write(ais, AudioFileFormat.Type.AU, new File(filename));
        } else {
            throw new RuntimeException("File format not supported: " + filename);
        }
    } catch (IOException e) {
        System.out.println(e);
        System.exit(1);
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) AudioFormat(javax.sound.sampled.AudioFormat) File(java.io.File)

Example 78 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project competitive-programming by ttvi-cse.

the class StdAudio method stream.

// https://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html
// play a wav or aif file
// javax.sound.sampled.Clip fails for long clips (on some systems)
private static void stream(String filename) {
    SourceDataLine line = null;
    // 4K buffer
    int BUFFER_SIZE = 4096;
    try {
        InputStream is = StdAudio.class.getResourceAsStream(filename);
        AudioInputStream ais = AudioSystem.getAudioInputStream(is);
        AudioFormat audioFormat = ais.getFormat();
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        line = (SourceDataLine) AudioSystem.getLine(info);
        line.open(audioFormat);
        line.start();
        byte[] samples = new byte[BUFFER_SIZE];
        int count = 0;
        while ((count = ais.read(samples, 0, BUFFER_SIZE)) != -1) {
            line.write(samples, 0, count);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (UnsupportedAudioFileException e) {
        e.printStackTrace();
    } catch (LineUnavailableException e) {
        e.printStackTrace();
    } finally {
        if (line != null) {
            line.drain();
            line.close();
        }
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) ByteArrayInputStream(java.io.ByteArrayInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream) DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException) SourceDataLine(javax.sound.sampled.SourceDataLine) IOException(java.io.IOException) AudioFormat(javax.sound.sampled.AudioFormat)

Example 79 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project competitive-programming by ttvi-cse.

the class StdAudio method readByte.

// return data as a byte array
private static byte[] readByte(String filename) {
    byte[] data = null;
    AudioInputStream ais = null;
    try {
        // try to read from file
        File file = new File(filename);
        if (file.exists()) {
            ais = AudioSystem.getAudioInputStream(file);
            int bytesToRead = ais.available();
            data = new byte[bytesToRead];
            int bytesRead = ais.read(data);
            if (bytesToRead != bytesRead)
                throw new IllegalStateException("read only " + bytesRead + " of " + bytesToRead + " bytes");
        } else // try to read from URL
        {
            URL url = StdAudio.class.getResource(filename);
            ais = AudioSystem.getAudioInputStream(url);
            int bytesToRead = ais.available();
            data = new byte[bytesToRead];
            int bytesRead = ais.read(data);
            if (bytesToRead != bytesRead)
                throw new IllegalStateException("read only " + bytesRead + " of " + bytesToRead + " bytes");
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("could not read '" + filename + "'", e);
    } catch (UnsupportedAudioFileException e) {
        throw new IllegalArgumentException("unsupported audio format: '" + filename + "'", e);
    }
    return data;
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL)

Example 80 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project competitive-programming by ttvi-cse.

the class StdAudio method loop.

/**
 * Loops an audio file (in .wav, .mid, or .au format) in a background thread.
 *
 * @param filename the name of the audio file
 * @throws IllegalArgumentException if {@code filename} is {@code null}
 */
public static synchronized void loop(String filename) {
    if (filename == null)
        throw new IllegalArgumentException();
    // code adapted from: http://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java
    try {
        Clip clip = AudioSystem.getClip();
        InputStream is = StdAudio.class.getResourceAsStream(filename);
        AudioInputStream ais = AudioSystem.getAudioInputStream(is);
        clip.open(ais);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
    } catch (UnsupportedAudioFileException e) {
        throw new IllegalArgumentException("unsupported audio format: '" + filename + "'", e);
    } catch (LineUnavailableException e) {
        throw new IllegalArgumentException("could not play '" + filename + "'", e);
    } catch (IOException e) {
        throw new IllegalArgumentException("could not play '" + filename + "'", e);
    }
}
Also used : Clip(javax.sound.sampled.Clip) AudioClip(java.applet.AudioClip) AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) ByteArrayInputStream(java.io.ByteArrayInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream) LineUnavailableException(javax.sound.sampled.LineUnavailableException) IOException(java.io.IOException)

Aggregations

AudioInputStream (javax.sound.sampled.AudioInputStream)92 IOException (java.io.IOException)44 AudioFormat (javax.sound.sampled.AudioFormat)41 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)27 InputStream (java.io.InputStream)24 ByteArrayInputStream (java.io.ByteArrayInputStream)19 LineUnavailableException (javax.sound.sampled.LineUnavailableException)19 File (java.io.File)18 SourceDataLine (javax.sound.sampled.SourceDataLine)14 Clip (javax.sound.sampled.Clip)13 AudioFileFormat (javax.sound.sampled.AudioFileFormat)12 DataLine (javax.sound.sampled.DataLine)12 FileInputStream (java.io.FileInputStream)11 BufferedInputStream (java.io.BufferedInputStream)10 URL (java.net.URL)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 DataInputStream (java.io.DataInputStream)5 SequenceInputStream (java.io.SequenceInputStream)5 LineEvent (javax.sound.sampled.LineEvent)5 AudioSynthesizer (com.sun.media.sound.AudioSynthesizer)4