Hi every one...I am modifying an application that make the Pc speaker have acces to the rtp stream comming from a telephone. The problem is, the rtp stream could be one of G.711U or G.711A or G.729 or G.729A depending on which codec was setup and the Speaker supports PCM_UNSIGNED. i need to do the conversion. The app works but I get horrible noise and I think is because the lack of conversion. The code is:

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;

public class TutorialSpeaker implements MediaSink{

// Constants for Audio Format
private static final AudioFormat.Encoding ENCODING =
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,

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 =
} catch (LineUnavailableException e) {
throw new IOException(e.getMessage());

int bytesWritten = 0;

if(sourcedataline.available() >= src.remaining()){
bytesWritten = sourcedataline.write(src.array(),src.arrayOffset(),

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

return  bytesWritten;

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

* 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;



Edited 3 Years Ago by happygeek: fixed formatting

They probably don't even remember this place exists seeing as this thread was made almost 3 years ago and their last post was 2.5 years ago.

This article has been dead for over six months. Start a new discussion instead.