我正在制作一个基于 Dobb 博士的教程构建的合成器中的 Java 声码器,每当我尝试从麦克风获取样本时,即使有麦克风输入,它也只会为样本返回零。这是 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(麦克风样本提供类):
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)));
}