我正在 netbeans IDE 上用 Java 创建一个吉他调音器,我希望我的程序在读取到某个频率后立即停止捕获实时音频。下面的代码开始音频捕获,但立即停止。例如,我希望它在达到 Low E 弦的频率时立即停止。到目前为止,我已经使用此网站寻求帮助:https ://docs.oracle.com/javase/tutorial/sound/capturing.html
//libraries
import static java.awt.SystemColor.info;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import static java.lang.System.in;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine.Info;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
public class AudioInputPractice {
/**
* @param args the command line arguments
* @throws javax.sound.sampled.LineUnavailableException
*/
public int read(byte[] b, int off, int len) throws IOException{
return in.read(b, off, len);
}
public static void main(String[] args){
System.out.println("Starting sound test...");
//audio
try
{
TargetDataLine line;
AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 4100, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {System.err.println("Line not Supported");}
line = (TargetDataLine)AudioSystem.getLine(info);
line.open();
ByteArrayOutputStream out= new ByteArrayOutputStream();
int numBytesRead;
byte[] data = new byte[line.getBufferSize() / 5];
System.out.println("Starting recording...");
line.start();
numBytesRead = line.read(data, 0 , data.length);
out.write(data, 0, numBytesRead);
}
catch (LineUnavailableException ex)
{
System.out.println("Error");
}
}
}