我做了一个小应用程序,可以从我的电脑中捕获声音。系统的简单过程是当我播放音乐文件时它将捕获语音,当我播放捕获的东西时,它将播放。总的来说一切正常。现在我想做的是我想打印回放。假设我捕获了一个像“嗨,早上好”的声音,现在当我按下播放时,它应该用文本打印录制的东西。捕获和回放编码如下。
private void captureAudio() {
try {
final AudioFormat format = getFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
final TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
Runnable runner = new Runnable() {
int bufferSize = (int) format.getSampleRate() * format.getFrameSize();
byte buffer[] = new byte[bufferSize];
public void run() {
out = new ByteArrayOutputStream();
running = true;
try {
while (running) {
int count = line.read(buffer, 0, buffer.length);
if (count > 0) {
out.write(buffer, 0, count);
}
}
out.close();
} catch (IOException e) {
System.err.println("I/O problems: " + e);
System.exit(-1);
}
}
};
Thread captureThread = new Thread(runner);
captureThread.start();
} catch (LineUnavailableException e) {
System.err.println("Line unavailable: " + e);
System.exit(-2);
}
}
private void playAudio() {
try {
byte audio[] = out.toByteArray();
InputStream input = new ByteArrayInputStream(audio);
final AudioFormat format = getFormat();
final AudioInputStream ais = new AudioInputStream(input, format, audio.length / format.getFrameSize());
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
Runnable runner = new Runnable() {
int bufferSize = (int) format.getSampleRate() * format.getFrameSize();
byte buffer[] = new byte[bufferSize];
public void run() {
try {
int count;
while ((count = ais.read(buffer, 0, buffer.length)) != -1) {
if (count > 0) {
line.write(buffer, 0, (char)count);
System.out.print((char)count);
}
}
line.drain();
line.close();
} catch (IOException e) {
System.err.println("I/O problems: " + e);
System.exit(-3);
}
}
};
Thread playThread = new Thread(runner);
playThread.start();
} catch (LineUnavailableException e) {
System.err.println("Line unavailable: " + e);
System.exit(-4);
}
}
请有人建议/帮助我克服这个问题 谢谢。
答案 0 :(得分:1)
看看CMU sphinx!为了能够将捕获的audion转换为语音,您可以使用Sphinx api。但请注意,语音识别的准确性并不能帮助您,因为系统仍处于开发阶段。在android中,您可以使用google的语音识别,它具有公平accuracy。但仍然不会将您捕获的语音转换为具有所需精度的语音。因此,最好有一个固定的歌词文本,以播放音频的速度显示。
希望有所帮助!!