I've been working on a java project that makes calls from an usb modem. The application works pefrectly on my computer, but when I tryed to run it on a lower-specs one, the audio stream of the person calling from the pc goes out perfectly and it's perfectly heared on the phone called. But the audio that should be heared by the pc user gets delayed (3 to 5 secs), with white noise, and makes literally impossible to make a conversation.
Some things to take in mind:
- My computer is an i3 4gb RAM notebook, and the low specs are Pentium 4 1gb RAM desktop.
- I tested the CPU and RAM usages, the application consumes 20 - 25% of the cpu on my computer, almost 100% on the low-specs one, and about 30 - 40mb from RAM on both cases.
- The application also has a call recording feature, and for some reason the output files are written perfectly (no delays or iterferences).
Any clue on what could be the problem or how can it be solved?
Class made to handle the audio after I start the new thread:(ingoing call audio)
public class SerialVoiceReader implements Runnable{
/** The running. */
private volatile boolean running = true;
/** The in. */
DataInputStream in;
/** The af. */
AudioFormat af;
/** The samples per frame. */
private int samplesPerFrame = 160;
/** The audio buffer size. */
private int audioBufferSize = samplesPerFrame * 2 ; //20ms delay
private String tel;
private String timestamp;
public SerialVoiceReader ( DataInputStream in, AudioFormat af){
this.in = in;
this.af = af;
}
public void run (){
try
{
Info infos = new Info(SourceDataLine.class, af);
SourceDataLine dataLine = (SourceDataLine) AudioSystem.getLine(infos);
dataLine.open(dataLine.getFormat(),audioBufferSize *2);
dataLine.start();
// set the volume up
if (dataLine.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
FloatControl volume = (FloatControl) dataLine.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(volume.getMaximum());
}
// get a field from GUI to set as part of the file name
tel = CallGUI.telField.getText();
timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());
// save the stream to a file to later set the header and make it .wav format
FileOutputStream fos = new FileOutputStream("Llamadas/" + timestamp + "-" + tel + "-OUT.raw");
// the audio buffer writing (this is the audio that goes out on the call)
while (running){
byte[] buffer = new byte[audioBufferSize];
int offset = 0;
int numRead = 0;
while (running && (offset < buffer.length && (numRead = this.in.read(buffer, offset, buffer.length - offset)) >= 0))
{
offset += numRead;
}
if(running && offset>=0){
dataLine.write(buffer, 0, offset);
fos.write(buffer);
}
}
dataLine.stop();
dataLine.drain();
dataLine.close();
fos.close();
}
catch ( Exception e )
{
}
}
Class made to handle the audio after I start the new thread:(outgoing call audio)
public class SerialVoiceWriter implements Runnable{
/** The running. */
private volatile boolean running = true;
/** The out. */
DataOutputStream out;
/** The af. */
AudioFormat af;
/** The samples per frame. */
private int samplesPerFrame = 160;
/** The audio buffer size. */
private int audioBufferSize = samplesPerFrame * 2; //20ms delay
private String tel;
private String timestamp;
public SerialVoiceWriter ( DataOutputStream out, AudioFormat af, Boolean playMessage)
{
this.out = out;
this.af = af;
}
public void run ()
{
try
{
Info infos = new Info(TargetDataLine.class, af);
TargetDataLine dataLine = (TargetDataLine) AudioSystem.getLine(infos);
dataLine.open(dataLine.getFormat(),audioBufferSize*2 );
dataLine.start();
tel = CallGUI.telField.getText();
timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());
FileOutputStream fis = new FileOutputStream("Llamadas/" + timestamp + "-" + tel + "-IN.raw");
while (running){
byte[] audioBuffer = new byte[audioBufferSize];
int offset = 0;
int numRead = 0;
while (running && (offset < audioBuffer.length && (numRead = dataLine.read(audioBuffer, offset, audioBuffer.length - offset)) > 0))
{
offset += numRead;
}
if(running && offset>=0){
this.out.write(audioBuffer);
fis.write(audioBuffer);
}
}
dataLine.flush();
dataLine.stop();
dataLine.close();
fis.close();
dataLine = null;
}
catch (Exception e )
{
}
}
Thank you in advice