I'm trying to build a very simple audio streamer with a source and a receiver. But I have some interferences when I receive the sound in my "receiver". I'm using the UDP protocol. Is there a way to "improve" my code to avoid those interferences?
Here is my audio server:
import java.io.File;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class AudioPlayerServer implements Runnable {
private SourceDataLine sLine;
private AudioFormat audioFormat;
private AudioInputStream audioInputStream=null;
private String host="127.0.0.1";
private int port=8000;
private DatagramSocket server;
private DatagramPacket packet;
private long startTime;
private long endTime=System.nanoTime();;
private long elapsed=System.nanoTime();;
private double sleepTime;
private long sleepTimeMillis;
private int sleepTimeNanos, epsilon;
AudioPlayerServer(String host, int port) {
this.host=host;
this.port=port;
init();
}
public void init() {
File file = new File("test.wav");
try {
audioInputStream=AudioSystem.getAudioInputStream(file);
} catch (Exception e) {
e.printStackTrace();
}
audioFormat = new AudioFormat(44100, 16, 2, true, false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
System.out.println(info);
try {
server = new DatagramSocket();
System.out.println("Server started");
} catch (SocketException e) {
e.printStackTrace();
}
}
public void run() {
try {
byte bytes[] = new byte[4096];
byte bytes2[] = new byte[1024];
int bytesRead=0;
//The sending rythm of the data have to be compatible with an audio streaming.
//So, I'll sleep the streaming thread for (1/SampleRate) seconds * (bytes.lenght/4) - epsilon
//=> bytes.lenght/4 because 4 values = 1 frame => For ex, in 1024 bits, there are 1024/4 = 256 frames
//epsilon because the instructions themselves takes time.
//The value have to be convert in milliseconds et nanoseconds.
sleepTime=(1024/audioFormat.getSampleRate());
epsilon=400000;
sleepTimeMillis=(long)(sleepTime*1000);
sleepTimeNanos=(int)((sleepTime*1000-sleepTimeMillis)*1000000);
System.out.println("Sleep time :"+sleepTimeMillis+" ms, "+sleepTimeNanos+" ns");
while ((bytesRead=audioInputStream.read(bytes, 0, bytes.length))!= -1) {
//getSignalLevel(bytes);
try {
//startTime=System.nanoTime();
packet = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(host), port);
packet.setData(bytes);
server.send(packet);
packet.setLength(bytes.length);
//endTime=System.nanoTime();
//System.out.println(endTime-startTime);
Thread.sleep(sleepTimeMillis,sleepTimeNanos);
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("No bytes anymore !");
} catch (Exception e) {
e.printStackTrace();
}
sLine.close();
System.out.println("Line closed");
}
}
Here is the client:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class AudioReceiver implements Runnable{
private String host;
private int port;
private SourceDataLine sLine;
private AudioFormat audioFormat;
byte[] buffer=new byte[4096];
DatagramPacket packet;
AudioReceiver (String host, int port) {
this.host=host;
this.port=port;
init();
Thread t1=new Thread(new Reader());
t1.start();
}
public void init() {
audioFormat = new AudioFormat(44100, 16, 2, true, false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try {
System.out.println(info);
sLine=(SourceDataLine) AudioSystem.getLine(info);
System.out.println(sLine.getLineInfo() + " - sample rate : "+audioFormat.getSampleRate());
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
System.out.println("Client started");
try {
sLine.open(audioFormat);
} catch (Exception e){
e.printStackTrace();
}
sLine.start();
System.out.println("Line started");
try {
DatagramSocket client = new DatagramSocket(port, InetAddress.getByName(host));
while (true) {
try {
packet = new DatagramPacket(buffer, buffer.length);
//System.out.println("Reception beggins for host "+host+" : "+port);
client.receive(packet);
//System.out.println("Reception ends");
buffer=packet.getData();
//sLine.write(packet.getData(), 0, buffer.length);
packet.setLength(buffer.length);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
}
public class Reader implements Runnable {
public void run() {
while (true) {
if (packet!=null) {
sLine.write(packet.getData(), 0, buffer.length);
}
}
}
}
}