Example usage for javax.sound.sampled AudioInputStream skip

List of usage examples for javax.sound.sampled AudioInputStream skip

Introduction

In this page you can find the example usage for javax.sound.sampled AudioInputStream skip.

Prototype

@Override
public long skip(long n) throws IOException 

Source Link

Document

Skips over and discards a specified number of bytes from this audio input stream.

Usage

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();/* www.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());
}