luisator 0 Newbie Poster

Hi every one I am working in an appliction that lets the speaker get access to the rtp stream coming from an Ip telephone. The idea is to have all the soun of the telephone in the PC. I get the voice but I also get a horrible noise al the time!!! The code I am using for the speaker is the following:

package sampleapps.clientmediastack;


import java.io.IOException;
import java.nio.ByteBuffer;


import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.DataLine;


import com.avaya.api.media.channels.MediaSink;


/**
* This is a Sample Tutorial Speaker Class. It just shows how to plug in a
* speaker (MediaSink) to the Client Media Stack Audio RTP Stream.  */
public class TutorialSpeaker implements MediaSink{


// Constants for Audio Format
private static final AudioFormat.Encoding ENCODING =
AudioFormat.Encoding.PCM_UNSIGNED;
private static final int SAMPLE_SIZE_IN_BITS = 8;    // 8bits per PCM sample
private static final float SAMPLE_RATE = 8000;        // 8Khz sampling rate
private static final int CHANNELS = 1;               // monoaural system


// sampleSizeInBytes * channels
private static final int FRAME_SIZE = CHANNELS * SAMPLE_SIZE_IN_BITS / 8;


//sampleRate/frameSize;   frames per sec
private static final float FRAME_RATE = SAMPLE_RATE;


// true for network byte order
private static final boolean BIG_ENDIAN = true;


private SourceDataLine sourcedataline;


private AudioFormat audioFormat = null;


private DataLine.Info targetInfo = null;


private String codec;


private int packetSize;


public TutorialSpeaker(){


audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
SAMPLE_RATE,SAMPLE_SIZE_IN_BITS,
CHANNELS,FRAME_SIZE,
FRAME_RATE,BIG_ENDIAN);


targetInfo = new DataLine.Info(SourceDataLine.class,audioFormat);


if (!AudioSystem.isLineSupported(targetInfo)) {
System.out.println("WARNING: Unsupported Source Data Line Format");
}


}


/**
* The Client Media Stack (i.e. The Audio RTP Receiver)
* uses the write method to deliver a ByteBuffer to the Speaker (MediaSink)
*/
public int write(ByteBuffer src) throws IOException {


// Start the Speaker if not already open
if((sourcedataline == null) || (!sourcedataline.isOpen())){
try {
sourcedataline =
(SourceDataLine)AudioSystem.getLine(targetInfo);
sourcedataline.open(audioFormat);
sourcedataline.start();
} catch (LineUnavailableException e) {
throw new IOException(e.getMessage());
}
}


int bytesWritten = 0;


if(sourcedataline.available() >= src.remaining()){


// IMPORTANT:  The incoming RTP stream could be one of G.711U or
// G.711A or G.729 or G.729A depending on which codec was setup.
// The Speaker supports PCM_UNSIGNED. So you will need to
// do the conversion.
// DO THE CONVERSION HERE
this.setCodec(PCM_UNSIGNED,20);
// Writes the data to the Speaker
bytesWritten = sourcedataline.write(src.array(),src.arrayOffset(),
src.remaining());


// System.out.println("bytes Written= " +bytesWritten);
}


return  bytesWritten;
}


/**
* Closes the Speaker (and hence this MediaSink)
*/
public void close() throws IOException {
if(sourcedataline != null)
sourcedataline.close();
}


/**
* Return the status of the Speaker
*/
public boolean isOpen() {
return true;
}


/**
* Sets the Codec type and packet Size of the source.
* This is the codec and packetSize of the incoming Audio RTP
* Stream.
*/
public void setCodec(String codec, int packetSize) {
this.codec = codec;
this.packetSize = packetSize;
}


/**
* Gets the codec of the Audio RTP Stream being used
* by the Speaker.
*/
public String getCodec() {
return codec;
}


/**
* Gets the packetSize of the Audio RTP Stream being used by
* the Speaker.
*/
public int getPacketSize() {
return packetSize;
}
}

Please help me!!!!! I´ve been working in this for weeks and can´t get rid of the noise!!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.