I am making a Vocoder in java built into a synthesizer built based on a tutorial from Dr. Dobb's and whenever i try to get samples from the microphone it just returns zeros for the samples even when there is mic input. This is the settings of the AudioFormat:
// AudioFormat parameters
public static final int SAMPLE_RATE = 22050;
private static final int SAMPLE_SIZE = 16;
private static final int CHANNELS = 1;
private static final boolean SIGNED = true;
private static final boolean BIG_ENDIAN = true;
// Chunk of audio processed at one time
public static final int BUFFER_SIZE = 1000;
public static final int SAMPLES_PER_BUFFER = BUFFER_SIZE / 2;
VoiceProvider (mic sample providing class):
public class VoiceProvider implements ISynthProvider{
private TargetDataLine line;
public VoiceProvider(){
DataLine.Info info = new DataLine.Info(TargetDataLine.class,
Player.format); // format is an AudioFormat object
if (!AudioSystem.isLineSupported(info)) {
System.out.println("o_0 line not supported!");
}
// Obtain and open the line.
try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.start();
line.open(Player.format);
} catch (LineUnavailableException ex) {
// Handle the error ...
}
}
@Override
public double getSample() {
byte[] a = {0,0};
if (line.isOpen()){
//System.out.println("0_0");
line.read(a, 0, 2);
}else{
//System.out.println("o_0");
}
System.out.println(Arrays.toString(a));
return readShort(a,0);
}
public static short readShort(byte[] data, int offset) {
return (short) (((data[offset] << 8)) | ((data[offset + 1] & 0xff)));
}
try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.start();
line.open(Player.format);
} catch (LineUnavailableException ex) {
// Handle the error ...
}
should be:
try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(Player.format);
line.start();
} catch (LineUnavailableException ex) {
// Handle the error ...
}