Search in sources :

Example 86 with AudioFormat

use of javax.sound.sampled.AudioFormat in project Minim by ddf.

the class JSMinim method getAudioRecordingClip.

/** @deprecated */
public AudioRecording getAudioRecordingClip(String filename) {
    Clip clip = null;
    AudioMetaData meta = null;
    AudioInputStream ais = getAudioInputStream(filename);
    if (ais != null) {
        AudioFormat format = ais.getFormat();
        if (format instanceof MpegAudioFormat) {
            AudioFormat baseFormat = format;
            format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
            // converts the stream to PCM audio from mp3 audio
            ais = getAudioInputStream(format, ais);
        }
        DataLine.Info info = new DataLine.Info(Clip.class, ais.getFormat());
        if (AudioSystem.isLineSupported(info)) {
            // Obtain and open the line.
            try {
                clip = (Clip) AudioSystem.getLine(info);
                clip.open(ais);
            } catch (Exception e) {
                error("Error obtaining Javasound Clip: " + e.getMessage());
                return null;
            }
            Map<String, Object> props = getID3Tags(filename);
            long lengthInMillis = -1;
            if (props.containsKey("duration")) {
                Long dur = (Long) props.get("duration");
                lengthInMillis = dur.longValue() / 1000;
            }
            meta = new MP3MetaData(filename, lengthInMillis, props);
        } else {
            error("File format not supported.");
            return null;
        }
    }
    if (meta == null) {
        // this means we're dealing with not-an-mp3
        meta = new BasicMetaData(filename, clip.getMicrosecondLength() / 1000, -1);
    }
    return new JSAudioRecordingClip(clip, meta);
}
Also used : TargetDataLine(javax.sound.sampled.TargetDataLine) DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) AudioMetaData(ddf.minim.AudioMetaData) MpegAudioFormat(javazoom.spi.mpeg.sampled.file.MpegAudioFormat) LineUnavailableException(javax.sound.sampled.LineUnavailableException) MalformedURLException(java.net.MalformedURLException) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Clip(javax.sound.sampled.Clip) AudioInputStream(javax.sound.sampled.AudioInputStream) AudioFormat(javax.sound.sampled.AudioFormat) MpegAudioFormat(javazoom.spi.mpeg.sampled.file.MpegAudioFormat)

Example 87 with AudioFormat

use of javax.sound.sampled.AudioFormat in project JWildfire by thargor6.

the class JWFAudioDevice method getSourceLineInfo.

protected DataLine.Info getSourceLineInfo() {
    AudioFormat fmt = getAudioFormat();
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt);
    return info;
}
Also used : DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) AudioFormat(javax.sound.sampled.AudioFormat)

Example 88 with AudioFormat

use of javax.sound.sampled.AudioFormat in project JWildfire by thargor6.

the class JWFAudioDevice method getAudioFormat.

protected AudioFormat getAudioFormat() {
    if (audioFormat == null) {
        Decoder decoder = getDecoder();
        audioFormat = new AudioFormat(decoder.getOutputFrequency(), 16, decoder.getOutputChannels(), true, false);
    }
    return audioFormat;
}
Also used : Decoder(javazoom.jl.decoder.Decoder) AudioFormat(javax.sound.sampled.AudioFormat)

Example 89 with AudioFormat

use of javax.sound.sampled.AudioFormat in project algs4 by kevin-wayne.

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 90 with AudioFormat

use of javax.sound.sampled.AudioFormat in project algs4 by kevin-wayne.

the class StdAudio method init.

// open up an audio stream
private static void init() {
    try {
        // 44,100 samples per second, 16-bit audio, mono, signed PCM, little Endian
        AudioFormat format = new AudioFormat((float) SAMPLE_RATE, BITS_PER_SAMPLE, 1, true, false);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
        line = (SourceDataLine) AudioSystem.getLine(info);
        line.open(format, SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE);
        // the internal buffer is a fraction of the actual buffer size, this choice is arbitrary
        // it gets divided because we can't expect the buffered data to line up exactly with when
        // the sound card decides to push out its samples.
        buffer = new byte[SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE / 3];
    } catch (LineUnavailableException e) {
        System.out.println(e.getMessage());
    }
    // no sound gets made before this call
    line.start();
}
Also used : DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException) AudioFormat(javax.sound.sampled.AudioFormat)

Aggregations

AudioFormat (javax.sound.sampled.AudioFormat)112 AudioInputStream (javax.sound.sampled.AudioInputStream)43 IOException (java.io.IOException)24 DataLine (javax.sound.sampled.DataLine)21 SourceDataLine (javax.sound.sampled.SourceDataLine)21 AudioFileFormat (javax.sound.sampled.AudioFileFormat)18 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)18 LineUnavailableException (javax.sound.sampled.LineUnavailableException)17 File (java.io.File)15 InputStream (java.io.InputStream)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 TargetDataLine (javax.sound.sampled.TargetDataLine)7 MpegAudioFormat (javazoom.spi.mpeg.sampled.file.MpegAudioFormat)7 BufferedInputStream (java.io.BufferedInputStream)6 FileInputStream (java.io.FileInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 DataInputStream (java.io.DataInputStream)5 Vector (java.util.Vector)5 SequenceInputStream (java.io.SequenceInputStream)4 Clip (javax.sound.sampled.Clip)4