Example usage for javax.sound.sampled AudioSystem getAudioFileFormat

List of usage examples for javax.sound.sampled AudioSystem getAudioFileFormat

Introduction

In this page you can find the example usage for javax.sound.sampled AudioSystem getAudioFileFormat.

Prototype

public static AudioFileFormat getAudioFileFormat(final File file)
        throws UnsupportedAudioFileException, IOException 

Source Link

Document

Obtains the audio file format of the specified File .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    AudioFileFormat fformat = AudioSystem.getAudioFileFormat(new File("audiofile"));

    fformat = AudioSystem.getAudioFileFormat(new URL("http://hostname/audiofile"));

    if (fformat.getType() == AudioFileFormat.Type.AIFC) {
    } else if (fformat.getType() == AudioFileFormat.Type.AIFF) {
    } else if (fformat.getType() == AudioFileFormat.Type.AU) {
    } else if (fformat.getType() == AudioFileFormat.Type.WAVE) {
    }// w  ww.j a va 2s . c  o m
}

From source file:org.getmansky.Cache.java

private static String getNameFromTags(File file) {
    try {// www  .  j av a2s. c o  m
        AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);
        Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
        String title = (String) properties.get("title");
        String author = (String) properties.get("author");
        if (StringUtils.isNotBlank(title) && StringUtils.isNotBlank(author)) {
            return author + "  " + title;
        }
    } catch (UnsupportedAudioFileException | IOException ex) {
        log.fatal(ex, ex);
    }
    return null;
}

From source file:org.getmansky.Cache.java

private static double getDuration(File file) throws UnsupportedAudioFileException, IOException {
    AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);
    if (fileFormat instanceof TAudioFileFormat) {
        Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
        String key = "duration";
        Long microseconds = (Long) properties.get(key);
        return microseconds / 1000;
    } else {/*from   ww  w . ja  v a  2 s . co  m*/
        throw new UnsupportedAudioFileException();
    }
}

From source file:net.sourceforge.subsonic.service.jukebox.AudioPlayer.java

public AudioPlayer(InputStream in, Listener listener) throws Exception {
    this.in = new BufferedInputStream(in);
    this.listener = listener;

    AudioFormat format = AudioSystem.getAudioFileFormat(this.in).getFormat();
    line = AudioSystem.getSourceDataLine(format);
    line.open(format);//from  w w w. j  ava  2s .  c  om
    LOG.debug("Opened line " + line);

    if (line.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
        gainControl = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
        setGain(0.5f);
    }
    new AudioDataWriter();
}

From source file:org.madsonic.service.jukebox.AudioPlayer.java

public AudioPlayer(InputStream in, Listener listener) throws Exception {
    this.in = new BufferedInputStream(in);
    this.listener = listener;

    AudioFormat format = AudioSystem.getAudioFileFormat(this.in).getFormat();
    line = AudioSystem.getSourceDataLine(format);
    line.open(format);//from w  ww  .j  a  v a 2 s  .  co m
    LOG.debug("Opened line " + line);

    if (line.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
        gainControl = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
        setGain(DEFAULT_GAIN);
    }
    new AudioDataWriter();
}

From source file:org.apache.tika.parser.audio.AudioParser.java

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
        throws IOException, SAXException, TikaException {
    // AudioSystem expects the stream to support the mark feature
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream);
    }/*from ww w  .  java2s  .  c  o  m*/
    stream = new SkipFullyInputStream(stream);
    try {
        AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(stream);
        Type type = fileFormat.getType();
        if (type == Type.AIFC || type == Type.AIFF) {
            metadata.set(Metadata.CONTENT_TYPE, "audio/x-aiff");
        } else if (type == Type.AU || type == Type.SND) {
            metadata.set(Metadata.CONTENT_TYPE, "audio/basic");
        } else if (type == Type.WAVE) {
            metadata.set(Metadata.CONTENT_TYPE, "audio/vnd.wave");
        }

        AudioFormat audioFormat = fileFormat.getFormat();
        int channels = audioFormat.getChannels();
        if (channels != AudioSystem.NOT_SPECIFIED) {
            metadata.set("channels", String.valueOf(channels));
            // TODO: Use XMPDM.TRACKS? (see also frame rate in AudioFormat)
        }
        float rate = audioFormat.getSampleRate();
        if (rate != AudioSystem.NOT_SPECIFIED) {
            metadata.set("samplerate", String.valueOf(rate));
            metadata.set(XMPDM.AUDIO_SAMPLE_RATE, Integer.toString((int) rate));
        }
        int bits = audioFormat.getSampleSizeInBits();
        if (bits != AudioSystem.NOT_SPECIFIED) {
            metadata.set("bits", String.valueOf(bits));
            if (bits == 8) {
                metadata.set(XMPDM.AUDIO_SAMPLE_TYPE, "8Int");
            } else if (bits == 16) {
                metadata.set(XMPDM.AUDIO_SAMPLE_TYPE, "16Int");
            } else if (bits == 32) {
                metadata.set(XMPDM.AUDIO_SAMPLE_TYPE, "32Int");
            }
        }
        metadata.set("encoding", audioFormat.getEncoding().toString());

        // Javadoc suggests that some of the following properties might
        // be available, but I had no success in finding any:

        // "duration" Long playback duration of the file in microseconds
        // "author" String name of the author of this file
        // "title" String title of this file
        // "copyright" String copyright message
        // "date" Date date of the recording or release
        // "comment" String an arbitrary text

        addMetadata(metadata, fileFormat.properties());
        addMetadata(metadata, audioFormat.properties());
    } catch (UnsupportedAudioFileException e) {
        // There is no way to know whether this exception was
        // caused by the document being corrupted or by the format
        // just being unsupported. So we do nothing.
    }

    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();
    xhtml.endDocument();
}

From source file:com.morty.podcast.writer.PodCastUtils.java

/**
 * Gets the duration of an audio file./*from  w w  w . j av  a2 s  . c o  m*/
 * @param file
 * @return the number of milliseconds in the file
 * @throws UnsupportedAudioFileException
 * @throws IOException
 */
public static long getDurationWithMp3Spi(File file) throws UnsupportedAudioFileException, IOException {
    m_logger.info("Determining length of MP3 file");

    AudioFileFormat audioFile = AudioSystem.getAudioFileFormat(file);
    if (audioFile instanceof TAudioFileFormat) {
        m_logger.debug("TAudioFileFormat file found");
        Map<?, ?> properties = ((TAudioFileFormat) audioFile).properties();
        String key = "duration";
        Long numberOfMicroseconds = (Long) properties.get(key);
        m_logger.info("Duration of [" + numberOfMicroseconds + "] microseconds");
        return (numberOfMicroseconds.longValue() / 1000);
    } else {
        m_logger.info("Unsupported Mp3 fileformat.");
        throw new UnsupportedAudioFileException();
    }

}

From source file:it.sardegnaricerche.voiceid.sr.VCluster.java

public void trimSegments(File inputFile) throws IOException {
    String base = Utils.getBasename(inputFile);
    File mydir = new File(base);
    mydir.mkdirs();//from  w w  w . j a v a 2 s .c o  m
    String mywav = mydir.getAbsolutePath() + "/" + this.getLabel() + ".wav";
    AudioFileFormat fileFormat = null;
    AudioInputStream inputStream = null;
    AudioInputStream shortenedStream = null;
    AudioInputStream current = null;
    int bytesPerSecond = 0;
    long framesOfAudioToCopy = 0;
    wavFile = new File(mywav);
    try {
        fileFormat = AudioSystem.getAudioFileFormat(inputFile);
        AudioFormat format = fileFormat.getFormat();
        boolean firstTime = true;

        for (VSegment s : this.getSegments()) {
            bytesPerSecond = format.getFrameSize() * (int) format.getFrameRate();
            inputStream = AudioSystem.getAudioInputStream(inputFile);
            inputStream.skip(0);
            inputStream.skip((int) (s.getStart() * 100) * bytesPerSecond / 100);
            framesOfAudioToCopy = (int) (s.getDuration() * 100) * (int) format.getFrameRate() / 100;

            if (firstTime) {
                shortenedStream = new AudioInputStream(inputStream, format, framesOfAudioToCopy);
            } else {
                current = new AudioInputStream(inputStream, format, framesOfAudioToCopy);
                shortenedStream = new AudioInputStream(new SequenceInputStream(shortenedStream, current),
                        format, shortenedStream.getFrameLength() + framesOfAudioToCopy);
            }
            firstTime = false;
        }
        AudioSystem.write(shortenedStream, fileFormat.getType(), wavFile);
    } catch (Exception e) {
        logger.severe(e.getMessage());
        e.printStackTrace();
    } finally {
        if (inputStream != null)
            try {
                inputStream.close();
            } catch (Exception e) {
                logger.severe(e.getMessage());
            }
        if (shortenedStream != null)
            try {
                shortenedStream.close();
            } catch (Exception e) {
                logger.severe(e.getMessage());
            }
        if (current != null)
            try {
                current.close();
            } catch (Exception e) {
                logger.severe(e.getMessage());
            }
    }
    logger.fine("filename: " + wavFile.getAbsolutePath());
}

From source file:xtrememp.tag.GenericInfo.java

/**
 * Load and parse info from an URL./*  w  w w .  j a va  2s  . co m*/
 *
 * @param input
 * @throws IOException
 * @throws UnsupportedAudioFileException
 */
@Override
public void load(URL input) throws IOException, UnsupportedAudioFileException {
    location = input.toString();
    AudioFileFormat aff = AudioSystem.getAudioFileFormat(input);
    loadInfo(aff);
}

From source file:xtrememp.tag.GenericInfo.java

/**
 * Load and parse info from an input stream.
 *
 * @param input/* w  ww. j a va 2s  . c om*/
 * @throws IOException
 * @throws UnsupportedAudioFileException
 */
@Override
public void load(InputStream input) throws IOException, UnsupportedAudioFileException {
    AudioFileFormat aff = AudioSystem.getAudioFileFormat(input);
    loadInfo(aff);
}