Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

compressing and uncompresing chunks of audio data

843802Mar 29 2009 — edited Mar 31 2009
Hello,

I am doing a project in Java, it has a server and a client, the aim of server is to read an audio file (a .WAV file, actually ) chunk by chunk and compress is it to certain quality (using both lossy and lossless techniques) as specified by the client and transmit it, the client will decompress it and play it out. I am using two ports for it, one for control signals, another for the actual audio data transmission. So far I, my application initiates the GUI, looks up servers, connects to it after finding one using the two ports, gets audio files' list and plays the clip.
playing is done by reading the wav file using FileIputStream, 128*1024 bytes at a time, writing it to socket outputstream. On client side this stream is used to create the audio format object and play the clip. Obviously, this is not the way it is to be done, correct? what's the best way of encoding that 128*1024 byte chunk so that it can be re created on client?



The code that I've got together looks something like this:

STREAMER:
dataOut = dataSocket.getOutoutStream();
..
..
fis = new FileInputStream( new File(fileName));
byte[] b = new byte[1024];
try{
 while (fis.available()!=0) {
     fis.read(b);
     dataOut.write(b);
  }  
}
catch( .. ) {  }
CLIENT:
dataIn = dataSocket.getInputStream();
try {
    while (dataIn.available()!=0) {
	play(din);
    }

}
	
catch ( ..) { }
..
..
void play(InputStream is){
..  // here AudioInputStream object is created using getAudioInputStream(is)
..  //an AudioFoamat object is created using audioInputStream.getFormat();

//then a default audioFormat is created , changing the encoding to PCM_SIGNED and bit-dept to 16 
//then using this default format, we play the audio:
//we construct an AudioInputStream using this default format. then play using this audioInputStream.

    DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
    SourceDataLine line = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
    line.open(audioFormat);
    line.start();


byte[] abData = new byte[128*1024];
  int nBytesRead = 0
  while (nBytesRead != -1) {
     try {
      nBytesRead = audioInputStream.read(abData, 0, abData.length);
      } catch( .. ) {  }
     if (nBytesRead >= 0) {
       line.write(abData, 0, nBytesRead);
    }
  }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 28 2009
Added on Mar 29 2009
5 comments
210 views