So, I filled the microphone with a sine wave (or something that in some indefinite sense resembles a sine wave), and your program works fine.
My specific changes were as follows:
package audioclient; import java.io.*; import java.net.*; import java.nio.ByteBuffer; import javax.sound.sampled.*; public class Mic { public byte[] buffer; private int port; static AudioInputStream ais; public static void main(String[] args) { TargetDataLine line; DatagramPacket dgp; AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED; float rate = 44100.0f; int channels = 2; int sampleSize = 16; boolean bigEndian = true; InetAddress addr; AudioFormat format = new AudioFormat(encoding, rate, sampleSize, channels, (sampleSize / 8) * channels, rate, bigEndian); DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); if (!AudioSystem.isLineSupported(info)) { System.out.println("Line matching " + info + " not supported."); return; } try { line = (TargetDataLine) AudioSystem.getLine(info);
Obviously, I misinterpreted it as a 512-byte chunk and distorted the sine wave, but the fact is that it produced exactly the sound it was intended for - a breathtaking crack at a certain height.
This, I do not suspect, that the problem is clearly in your code. The first thing I would like to check out is what your system uses for audio. Do you have multiple microphones connected? Maybe a microphone for a webcam? You can grab a utility such as PulseAudio Volume Control for verification. If you have not tested the functionality of your microphone, you can do this too; they have a life expectancy.
It is not unusual to scramble bits in an audio stream, and it is not difficult; but I don’t see anywhere where you could do it.
One thought might be to change your program to try to play the sound locally before sending it to the server. That way, you can at least determine if there is a problem before or after the microphone.
source share