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);
}
}