Java 8 AudioFormat - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 AudioFormat class.

This code example shows how to use the following methods:getChannels, getFrameSize, getSampleRate, isBigEndian
 
    public Resampler24To16(AudioSource src) throws IOException {
        this.src = src;
        AudioInfo audioInfo = src.getAudioInfo();
        this.srcFormat = audioInfo.getFormat();
        buffer = ByteBuffer.allocate(96000 * 2 * srcFormat.getFrameSize());
        newFormat = new AudioFormat(srcFormat.getSampleRate(), 16, srcFormat.getChannels(), true,
                srcFormat.isBigEndian());
    } 
 
    public MediaInfo.AudioInfo getAudioInfo() throws IOException {
        AudioInfo audioInfo = src.getAudioInfo();
        return new AudioInfo(audioInfo.getFourcc(), audioInfo.getTimescale(), audioInfo.getDuration(),
                audioInfo.getNFrames(), audioInfo.getName(), null, newFormat, audioInfo.getLabels());
    } 
 
    public AudioFrame getFrame(ByteBuffer result) throws IOException {
        buffer.rewind();
        AudioFrame from = src.getFrame(buffer);
        if (from == null)
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
	private TargetDataLine line;
	private byte[] buffer = new byte[1024 * 4];
 
	public JavaSoundAudioRecorder (int samplingRate, boolean isMono) {
		try { 
			AudioFormat format = new AudioFormat(Encoding.PCM_SIGNED, samplingRate, 16, isMono ? 1 : 2, isMono ? 2 : 4,
				samplingRate, false);
			line = AudioSystem.getTargetDataLine(format);
			line.open(format, buffer.length);
			line.start();
		} catch (Exception ex) {
			throw new GdxRuntimeException("Error creating JavaSoundAudioRecorder.", ex);
		} 
	} 
 
	public void read (short[] samples, int offset, int numSamples) {
		if (buffer.length < numSamples * 2) buffer = new byte[numSamples * 2];
 
		int toRead = numSamples * 2;
		int read = 0;
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:matches, toString
    static AudioFormat f1;
    static AudioFormat f2;
 
    public static void main(String[] args) throws Exception {
        AudioFormat f3;
        f1 = new AudioFormat(44100, 16, 2, true, false);
        f2 = new AudioFormat(Encoding.PCM_SIGNED,
                AudioSystem.NOT_SPECIFIED,
                AudioSystem.NOT_SPECIFIED,
                AudioSystem.NOT_SPECIFIED,
                AudioSystem.NOT_SPECIFIED,
                AudioSystem.NOT_SPECIFIED, false);
        test(true); 
 
//        f1 = new AudioFormat(44100, 8, 16, true, false); 
        f2 = new AudioFormat(Encoding.PCM_SIGNED,
                AudioSystem.NOT_SPECIFIED,
                AudioSystem.NOT_SPECIFIED,
                AudioSystem.NOT_SPECIFIED,
                AudioSystem.NOT_SPECIFIED,

6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getChannels, getEncoding, getSampleSizeInBits
     * or has a sample size in bits other than 8 or 16. 
     */ 
    public DDSAudioInputStream(DoubleDataSource source, AudioFormat format)
    { 
        super(new ByteArrayInputStream(new byte[0]), format, AudioSystem.NOT_SPECIFIED);
        if (format.getChannels()>1) {
            throw new IllegalArgumentException("Can only produce mono audio");
        } 
        if (!format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED) &&
                !format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)) {
            throw new IllegalArgumentException("Can only produce PCM_SIGNED or PCM_UNSIGNED audio");
        } 
        int bitsPerSample = format.getSampleSizeInBits();
        if (bitsPerSample != 8 && bitsPerSample != 16 && bitsPerSample != 24) {
            throw new IllegalArgumentException("Can deal with sample size 8 or 16 or 24, but not " + bitsPerSample);
        } 
        this.source = source;
        this.sampleBuf = new double[SAMPLEBUFFERSIZE];
        assert frameSize == bitsPerSample/8;        
    } 
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
     */ 
    public static void main(String[] args)
    { 
        int channels = 1;
        int fs = 16000;
        AudioFormat audioFormat = new AudioFormat(
                AudioFormat.Encoding.PCM_SIGNED, 16000, 16, channels, 2*channels, fs,
                false); 
         
        TargetDataLine microphone = null;
        try { 
            DataLine.Info info = new DataLine.Info(TargetDataLine.class,
                    audioFormat);
            microphone = (TargetDataLine) AudioSystem.getLine(info);
            microphone.open(audioFormat);
            System.out.println("Microphone format: "+microphone.getFormat());
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            System.exit(1);
        } 

6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:matches, toString
                AudioSystem.NOT_SPECIFIED,
                AudioSystem.NOT_SPECIFIED,
                AudioSystem.NOT_SPECIFIED, true);
        test(false); 
 
        f1 = new AudioFormat(44100, 8, 8, true, false);
        test(true); 
 
        if (success) {
            out("The test PASSED."); 
        } else { 
            out("The test FAILED."); 
            throw new Exception("The test FAILED");
        } 
    } 
 
    static void test(boolean shouldMatch) {
        out("testing:"); 
        out("  - " + f1.toString());
        out("  - " + f2.toString());

6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getChannels, getSampleSizeInBits
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
 
public class ClipSetPos { 
 
    final static AudioFormat audioFormat = new AudioFormat(44100f, 16, 2, true, true);
    final static int frameLength = 44100 * 2; // 2 seconds
    final static byte[] dataBuffer
            = new byte[frameLength * (audioFormat.getSampleSizeInBits()/8)
                       * audioFormat.getChannels()];
    final static int MAX_FRAME_DELTA = 20;
 
    public static void main(String[] args) {
        boolean testPassed = true;
        Clip clip = null;
        try { 
            clip = (Clip)AudioSystem.getLine(new DataLine.Info(Clip.class, audioFormat));
            clip.open(audioFormat, dataBuffer, 0, dataBuffer.length);
        } catch (LineUnavailableException ex) {
            log(ex);
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getFrameRate, getFrameSize
     * @param format the desired audio format of the audio input stream. getFrameSize() and getFrameRate() 
     * must return meaningful values.  
     */ 
    public SilenceAudioInputStream(double duration, AudioFormat format)
    { 
        super(new ByteArrayInputStream(new byte[(int) (format.getFrameSize()*format.getFrameRate()*duration)]),
                format, (long) (format.getFrameRate()*duration));
    } 
 
} 
 
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getChannels, getSampleSizeInBits
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
 
public class ClipSetPos { 
 
    final static AudioFormat audioFormat = new AudioFormat(44100f, 16, 2, true, true);
    final static int frameLength = 44100 * 2; // 2 seconds
    final static byte[] dataBuffer
            = new byte[frameLength * (audioFormat.getSampleSizeInBits()/8)
                       * audioFormat.getChannels()];
    final static int MAX_FRAME_DELTA = 20;
 
    public static void main(String[] args) {
        boolean testPassed = true;
        Clip clip = null;
        try { 
            clip = (Clip)AudioSystem.getLine(new DataLine.Info(Clip.class, audioFormat));
            clip.open(audioFormat, dataBuffer, 0, dataBuffer.length);
        } catch (LineUnavailableException ex) {
            log(ex);
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getChannels, getSampleRate, getSampleSizeInBits
		if (source==null) throw new JavaLayerException("cannot obtain source audio line", t);
    } 
 
	public int millisecondsToBytes(AudioFormat fmt, int time)
	{ 
		return (int)(time*(fmt.getSampleRate()*fmt.getChannels()*fmt.getSampleSizeInBits())/8000.0);
	} 
 
	protected void closeImpl() 
	{ 
		if (source!=null)
		{ 
			source.close();
		} 
	} 
 
	protected void writeImpl(short[] samples, int offs, int len)
		throws JavaLayerException 
	{ 
		if (source==null)
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
See Code Examples for Java 8 AudioFormat Methods: