简体   繁体   中英

Read / Write streamed audio via TCP socket Java

I'm new to java and socket programing and I'm looking for more of a starting point / point in the right direction / validation of my line of thinking.

Overall, the idea I'm wanting to implement is:

  1. capture audio that I record as a .wav
  2. send it via TCP to my server
  3. Decide if I want to play audio recorded (process and play it in my tcp server) or send to be processed (like transcriber or something like that) to a different server via TCP.

The trouble I'm encountering is server side, and it's because I don't entirely understand socket programming, to be honest. I want to receive the audio to my server as a raw audio (passing byte/binary array as parameter). For most documents I've found basically they say open socket, open input / output stream, read / write, close streams, close sockets. This works for say regular text but not audio. I'm sure this can be done with audio. What would the general idea be to do this for audio .wav? Is there an API I'm not aware of that covers this?





This is TCP client / server code I have so far. For now, the "captured audio" I'm using is just an existing .wav file I read into the client output stream. The issue I'm facing right now is that the .wav created doesn't sound like the original. It sounds like noise, and is much shorter time wise.

TCP Client code:

Socket serverSocket = null;
DataOutputStream out = null;
BufferedReader in = null;

try {
    serverSocket = new Socket(serverHostname, port);
    out = new DataOutputStream(serverSocket.getOutputStream());
    in = new BufferedReader(
              new InputStreamReader(serverSocket.getInputStream()));
} catch (UnknownHostException e) {
    System.err.println("Don't know about host: " + serverHostname);
    System.exit(1);
} catch (IOException e) {
    System.err.println("Couldn't get I/O for the connection to: " + serverHostname);
    System.exit(1);
}

DataInputStream stdIn = new DataInputStream( 
                              new FileInputStream(".wav location"));

int readBytes;
byte[] temp = new byte[1024];

while( (readBytes = stdIn.read(temp, 0, temp.length)) != -1){
    out.write(temp, 0, readBytes);
}

out.flush();
out.close();
in.close();
stdIn.close();
serverSocket.close();
}

This is my Server code so far:

public void clientConnect(int socketPort) {
        ServerSocket s_Socket = null; 

        try {
             s_Socket = new ServerSocket(socketPort);

             System.out.println ("SERVER Connection Socket Created");
             try {

                 System.out.println ("Waiting for CLIENT Connection");
                  while (true){
                      new TcpAudioStreaming (c_Socket = s_Socket.accept());
                      System.out.println("CLIENT: " + c_Socket.getInetAddress());
                      System.out.println("PORT: " + c_Socket.getPort());
                      System.out.println("LOCAL PORT: " + c_Socket.getLocalPort());
                  }
             }     
             catch (IOException e) { 
                  System.err.println("Accept failed."); 
                  System.exit(1); 
             }
        }

        catch (IOException e){
             System.err.println("Could not listen on port: " + socketPort); 
             System.exit(1); 
        }      
        finally {

             try {
                 s_Socket.close(); 
             }
             catch (IOException e){
                  System.err.println("Could not close port: " + socketPort); 
                  System.exit(1); 
             }
        }
    }


    public void run() {

        boolean end = false;
        System.out.println ("New Communication Thread Started");

        try {
            //DataOutputStream outStream = new DataOutputStream(c_Socket.getOutputStream()); 
            ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 
            DataInputStream inStream = new DataInputStream(c_Socket.getInputStream()); 

            int read;
            byte[] temp = new byte[1024];

            while( (read = inStream.read(temp, 0, temp.length)) != -1){
                outStream.write(temp, 0, read);
            }

            outStream.flush();
            byte[] audioBytes = outStream.toByteArray();

            writeWave(audioBytes);

            outStream.close(); 
            inStream.close(); 
            c_Socket.close();


        } 
        catch (IOException e) {
              System.err.println("Problem with Communication Server");
              System.exit(1); 
        } 

    }

    public void writeWave(byte[] audioArry) {

        String filePath = "new .wav path";

        AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.ULAW, 8000, 8, 1, 1, 8000, false);

        try {

            ByteArrayInputStream inStream = new ByteArrayInputStream(audioArry);
            long length = (long)(audioArry.length / audioFormat.getFrameSize());
            AudioInputStream audioInputStreamTemp = new AudioInputStream(inStream, audioFormat, length);


            File fileOut = new File(filePath);

            if (AudioSystem.isFileTypeSupported(AudioFileFormat.Type.WAVE, audioInputStreamTemp)) {
                System.out.println("Trying to write");
                AudioSystem.write(audioInputStreamTemp, AudioFileFormat.Type.WAVE, fileOut);
                System.out.println("Written successfully");
            }       
        }
        catch(Exception e) {
            e.printStackTrace();
        }

    }

Java sockets or TCP doesn't distinguish between ASCII and binary data. It is the application that does the interpretation.

Start with a simple Client/Server application and move forward from there. For a intro you can see https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM