I wrote this streaming audio application in java.
It basically reads audio from the mic port and sends it using UDP to the client which then the client sends it to his speakers. The problem is that it is very choppy audio. So I am wondering what is slowing down the process. Should I not have the drain method in the toSpeaker function or should I have a seperate thread for collecting the udp packets etc etc ...
Feel free to download the 2 programs below and compile/run.
Note when runing you muxt have the mic port unmuted and the speaker port unmute the application doesn't unmute a channel by itself. Also I set the testing ip and port to localhost (127.0.0.1) and port 8888 you can change if needed.
Thanks for any help. I would really like this to go. I am unsure why it's chop I am using udp which is connectionless so if a few packets are dropped it won't resent it just moves on alot quicker then tcp.
public class MicPlayer { private static final String IP_TO_STREAM_TO = "localhost" ; private static final int PORT_TO_STREAM_TO = 8888 ; /** Creates a new instance of MicPlayer */ public MicPlayer() { } /** * @param args the command line arguments */ public static void main(String[] args) { Mixer.Info minfo[] = AudioSystem.getMixerInfo() ; for( int i = 0 ; i < minfo.length ; i++ ) { System.out.println( minfo[i] ) ; } if (AudioSystem.isLineSupported(Port.Info.MICROPHONE)) { try { DataLine.Info dataLineInfo = new DataLine.Info( TargetDataLine.class , getAudioFormat() ) ; TargetDataLine targetDataLine = (TargetDataLine)AudioSystem.getLine( dataLineInfo ) ; targetDataLine.open( getAudioFormat() ); targetDataLine.start(); byte tempBuffer[] = new byte[1000] ; int cnt = 0 ; while( true ) { targetDataLine.read( tempBuffer , 0 , tempBuffer.length ); sendThruUDP( tempBuffer ) ; } } catch(Exception e ) { System.out.println(" not correct " ) ; System.exit(0) ; } } } public static AudioFormat getAudioFormat(){ float sampleRate = 8000.0F; //8000,11025,16000,22050,44100 int sampleSizeInBits = 16; //8,16 int channels = 1; //1,2 boolean signed = true; //true,false boolean bigEndian = false; //true,false return new AudioFormat( sampleRate, sampleSizeInBits, channels, signed, bigEndian ); } public static void sendThruUDP( byte soundpacket[] ) { try { DatagramSocket sock = new DatagramSocket() ; sock.send( new DatagramPacket( soundpacket , soundpacket.length , InetAddress.getByName( IP_TO_STREAM_TO ) , PORT_TO_STREAM_TO ) ) ; sock.close() ; } catch( Exception e ) { e.printStackTrace() ; System.out.println(" Unable to send soundpacket using UDP " ) ; } } }
public class RadioReceiver extends Thread { private static final String IP_TO_STREAM_TO = "localhost" ; private static final int PORT_TO_STREAM_TO = 8888 ; /** Creates a new instance of RadioReceiver */ public RadioReceiver() { } public void run() { byte b[] = null ; while( true ) { b = receiveThruUDP() ; toSpeaker( b ) ; } } /** * @param args the command line arguments */ public static void main(String[] args) { RadioReceiver r = new RadioReceiver() ; r.start() ; } public static byte[] receiveThruUDP() { try { DatagramSocket sock = new DatagramSocket(PORT_TO_STREAM_TO) ; byte soundpacket[] = new byte[1000] ; DatagramPacket datagram = new DatagramPacket( soundpacket , soundpacket.length , InetAddress.getByName( IP_TO_STREAM_TO ) , PORT_TO_STREAM_TO ) ; sock.receive( datagram ) ; sock.close() ; return datagram.getData() ; // soundpacket ; } catch( Exception e ) { System.out.println(" Unable to send soundpacket using UDP " ) ; return null ; } } public static void toSpeaker( byte soundbytes[] ) { try{ DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class , getAudioFormat() ) ; SourceDataLine sourceDataLine = (SourceDataLine)AudioSystem.getLine( dataLineInfo ); sourceDataLine.open( getAudioFormat() ) ; sourceDataLine.start(); int cnt = 0; sourceDataLine.write( soundbytes , 0, soundbytes.length ); sourceDataLine.drain() ; sourceDataLine.close() ; } catch(Exception e ) { System.out.println("not working in speakers " ) ; } } public static AudioFormat getAudioFormat() { float sampleRate = 8000.0F; //8000,11025,16000,22050,44100 int sampleSizeInBits = 16; //8,16 int channels = 1; //1,2 boolean signed = true; //true,false boolean bigEndian = false; //true,false return new AudioFormat( sampleRate, sampleSizeInBits, channels, signed, bigEndian ); } }