Receive AudioFormat from a TCP type connection - Java javax.sound.sampled

Java examples for javax.sound.sampled:Audio

Description

Receive AudioFormat from a TCP type connection

Demo Code


//package com.java2s;
import java.io.BufferedReader;

import java.io.IOException;

import javax.sound.sampled.AudioFormat;

public class Main {
    /**/*from  ww w.j  a va  2s. c o  m*/
     * Receive AudioFormat from a TCP type connection as sent by
     * {@link #sendAudioFormatDownTCP(PrintWriter, AudioFormat)
     * sendAudioFormatDownTCP}
     * 
     * @param tcpFrom
     *            the TCP type connection
     * @return the AudioFormat
     * @throws IOException
     *             if there is a problem
     */
    public static AudioFormat receiveAudioFormatFromTCP(
            BufferedReader tcpFrom) throws IOException {
        String strEncode = tcpFrom.readLine();
        // for the constructor need to convert string back to encoding
        AudioFormat.Encoding encoding = null;
        if (strEncode.equalsIgnoreCase("ALAW")) {
            encoding = AudioFormat.Encoding.ALAW;
        } else if (strEncode.equalsIgnoreCase("PCM_FLOAT")) {
            encoding = AudioFormat.Encoding.PCM_FLOAT;
        } else if (strEncode.equalsIgnoreCase("PCM_SIGNED")) {
            encoding = AudioFormat.Encoding.PCM_SIGNED;
        } else if (strEncode.equalsIgnoreCase("PCM_UNSIGNED")) {
            encoding = AudioFormat.Encoding.PCM_UNSIGNED;
        } else if (strEncode.equalsIgnoreCase("ULAW")) {
            encoding = AudioFormat.Encoding.ULAW;
        }
        float sampleRate = Float.parseFloat(tcpFrom.readLine());
        int sampleSizeInBits = Integer.parseInt(tcpFrom.readLine());
        int channels = Integer.parseInt(tcpFrom.readLine());
        int frameSize = Integer.parseInt(tcpFrom.readLine());
        float frameRate = Float.parseFloat(tcpFrom.readLine());
        boolean bigEndian = Boolean.parseBoolean(tcpFrom.readLine());
        AudioFormat audioformat = new AudioFormat(encoding, sampleRate,
                sampleSizeInBits, channels, frameSize, frameRate, bigEndian);
        return audioformat;
    }
}

Related Tutorials