0

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?




UPDATE: 8/30/2015
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();
 }
 }
asked Aug 12, 2015 at 5:11
4
  • 1
    'Not for audio' why not? Where did you get that idea? It's false. Commented Aug 12, 2015 at 5:38
  • ^^^ yeah.... of course it works for audio, (and video, assuming your ISP is good enough). I'm listening to streaming music now. Computer data is all binary, if it works for text, it will work for audio etc. Commented Aug 12, 2015 at 9:49
  • What I meant to say way, I working example for text, not for audio Commented Aug 12, 2015 at 12:51
  • You aren't using the BufferedReader at all, so you should remove it, and the ByteArrayOutputStream and ByteArrayInputStream in the server are both redundant: you should write what you receive from the client directory to the audio stream. "Couldn't get I/O for the connection to: " + serverHostname is not an adequate error message, whatever the Java Tutorial may contain. You must print the exception itself. Commented Aug 30, 2015 at 7:47

1 Answer 1

1

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.

answered Aug 12, 2015 at 5:25
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the suggestion KDM. I actually had started there, but being new to this it took a bit for me to grasp. Granted, I haven't had a chance to work on it as much because of summer classes, but I'm almost there I think. I'll post the code I have so far here in a bit.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.