Will attempt to calculate a buffer size that can hold the given time of audio data. - Java java.util

Java examples for java.util:Time Format

Description

Will attempt to calculate a buffer size that can hold the given time of audio data.

Demo Code

/*/*www .  j  a  v a2  s. c o  m*/
 This file is part of the Greenfoot program. 
 Copyright (C) 2005-2009,2011  Poul Henriksen and Michael Kolling 
         
 This program is free software; you can redistribute it and/or 
 modify it under the terms of the GNU General Public License 
 as published by the Free Software Foundation; either version 2 
 of the License, or (at your option) any later version. 
         
 This program is distributed in the hope that it will be useful, 
 but WITHOUT ANY WARRANTY; without even the implied warranty of 
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 GNU General Public License for more details. 
         
 You should have received a copy of the GNU General Public License 
 along with this program; if not, write to the Free Software 
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. 
         
 This file is subject to the Classpath exception as provided in the  
 LICENSE.txt file that accompanied this code.
 */
//package com.java2s;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;

public class Main {
    /**
     * Will attempt to calculate a buffer size that can hold the given time of
     * audio data. If unsuccessful it will return -1.
     * 
     * @return size in bytes.
     */
    public static int getBufferSizeToHold(AudioFormat format, double seconds) {
        int bufferSize;
        if (format.getFrameRate() != AudioSystem.NOT_SPECIFIED) {
            bufferSize = (int) Math.ceil(format.getFrameSize()
                    * format.getFrameRate() * seconds);
        } else if (format.getSampleRate() != AudioSystem.NOT_SPECIFIED) {
            bufferSize = (int) Math.ceil((format.getSampleSizeInBits() / 8)
                    * format.getChannels() * format.getSampleRate()
                    * seconds);
        } else {
            bufferSize = -1;
        }
        return bufferSize;
    }
}

Related Tutorials