Search code examples
javaandroidsocketsvoice

Noise in sending voice from android to PC over socket


I'm trying to send voice from android to PC over sockets. My code is working but at PC end what I hear is lot of noise, I am even unable to listen to any voice.

Here is the android code that i'm using:

     byte buffer[]=new byte[1024];
     int buffersize = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
         mic=new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize);
         mic.startRecording();
               
                  while(running){
                        count=mic.read(buffer, 0, buffer.length);
                        if(count>0){
                           OutputStream.write(buffer, 0, count);
                        }
                  }

Here is PC Code:

    float sampleRate=44100;
    int sampleSize=16;
    int channel=1;
    boolean sign=true;
    boolean bigendian=true;
    AudioFormat format=new AudioFormat(sampleRate, sampleSize, channel, sign, bigendian);
    
    SourceDataLine voiceLine;
    DataLine.Info LineInfo=new DataLine.Info(SourceDataLine.class, format);
    if(AudioSystem.isLineSupported(LineInfo)){
        System.out.println("Line Supported...");
    }else{
        System.out.println("not supported Line...");
    }
    voiceLine = (SourceDataLine) AudioSystem.getLine(LineInfo);
    voiceLine.open(format);
    voiceLine.start();
    byte buffer[] = new byte[1024];
    inputStream=new BufferedInputStream(connection.getInputStream());    
    while(true){
                 count=inputStream.read(buffer,0,buffer.length);
                 InputStream in;
                 in=new ByteArrayInputStream(buffer);
                 AudioInputStream ais=new AudioInputStream(in, format, buffer.length /format.getFrameSize());
                 ais.read(audio, 0, count);
                 voiceLine.write(audio, 0, count);
            }

What I test so far:

  • Voice is clear when check with android to android.
  • Voice is clear when i attach headphone to android phone.
  • Voice is not clear without headphone attach to android phone(lot of noise).

What I have tried so far:

  • Use less SAMPLE RATE(8000).
  • Use UDP instead of TCP.
  • Try different buffer size.

But got nothing.


Solution

  • I ended up solving this myself.

    It turned out that I had to change it to bigendian=false;, and now it works.