Dav*_*erg 5 java audio javafx minim beat-detection
嗨,大家好!
我是音频编程的新手,想构建一个 Java FX 应用程序,它与来自我的麦克风或线路输出的音频进行交互。
我在谷歌上搜索图书馆,发现 minim 似乎很受欢迎。它是为处理而编写的,但您也可以在 Java 中使用它。问题是我没有找到关于如何做到这一点的好文档。(我不想使用处理的原因是我想构建一个非常高级的gui,我认为在JavaFX中更容易做到)。
作为第一步,我试图建立一个对歌曲中的每个节拍都有反应的库。我的代码如下所示:
public class Main extends Application {
/* Used to trick minim constructor (needs an object with those methods) */
class MinimInput {
String sketchPath( String fileName ) {
return "";
}
InputStream createInput(String fileName) {
return new InputStream() {
@Override
public int read() throws IOException {
return 0;
}
};
};
}
@Override
public void start(Stage stage) throws Exception {
...
//Some gui logic here!
...
stage.setScene(scene);
stage.show();
/* Beat detect thread */
Thread th = new Thread(new Runnable() {
@Override
public void run() {
Minim minim = new Minim(new MinimInput());
AudioInput input = minim.getLineIn(Minim.STEREO, 1024); //Tried differen values here
BeatDetect beatDetect = new BeatDetect();
beatDetect.setSensitivity(1000); //Tried different values here
int i=0;
while (true) {
beatDetect.detect(input.mix);
if(beatDetect.isHat()) {
System.out.print("HAT");
}
if(beatDetect.isSnare()) {
System.out.print("SNARE");
}
if (beatDetect.isKick()) {
System.out.print("KICK");
}
}
}
});
th.start();
}
}
Run Code Online (Sandbox Code Playgroud)
我怀疑这可能与 while(true) 循环有关,并且我的声音缓冲区变小了,但我不知道该怎么做。任何人都可以指出我正确的方向吗?
我也非常感谢有关音频可视化编程的一般资源以及有关如何在 java 和 minim 中执行此操作的更具体信息(或有关如何使用其他库执行此操作的提示和示例,如果它们更易于使用)。这对我来说是一个全新的领域,帮我打破它!:)
我找到了解决方案。在 init 上为 BeatDetect 提供 timeSize 和 SampleRate 就可以达到目的:
所以...这就是我得到的:
BeatDetect beatDetect = new BeatDetect(1024, 44100.0f);
Run Code Online (Sandbox Code Playgroud)
1024 与您从 mime 获取 AudioInput 时指定的值相同。可以使用调试找到采样率并查看以下输入值:
输入>流>格式>采样率
| 归档时间: |
|
| 查看次数: |
2798 次 |
| 最近记录: |