Dr. Dobb's のチュートリアルに基づいて構築されたシンセサイザーに組み込まれた 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)));
}