我正在编写一个应用程序,需要监听麦克风并提供实时的音量和音高输出。我已经解决了音高识别的问题。我已经进行了大量fft的研究,并找到了Android库TarsosDSP,使得监听音高变得非常简单:
AudioDispatcher dispatcher =
AudioDispatcherFactory.fromDefaultMicrophone(22050,1024,0);
PitchDetectionHandler pdh = new PitchDetectionHandler() {
@Override
public void handlePitch(PitchDetectionResult res, AudioEvent e){
final float pitchInHz = res.getPitch();
runOnUiThread(new Runnable() {
@Override
public void run() {
processPitch(pitchInHz);
}
});
}
};
AudioProcessor pitchProcessor = new PitchProcessor(PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, pdh);
dispatcher.addAudioProcessor(pitchProcessor);
Thread audioThread = new Thread(dispatcher, "Audio Thread");
audioThread.start();
我已经学会如何使用内置的android.getMaxAmplitude()方法进行振幅检测。
但我的问题是,我无论如何都无法同时进行两者。问题在于你似乎不能运行多个麦克风实例。例如,如果您尝试在单独的线程上运行两个不同的实时录音,则会出现问题。我已经搜索整个互联网,试图找到一些示例代码来帮助我入手,但我找不到任何东西。有人曾经做过类似的事情吗?
编辑 我已经发现可以使用Pitchdetectionhandler中的AudioEvent。 audioevent.getbytebuffer()返回一个带有以字节为单位的音频数据的字节数组,根据文档:https://0110.be/releases/TarsosDSP/TarsosDSP-latest/TarsosDSP-latest-Documentation/ 。
如果我没有弄错,当转换为short[]时,最高值就是最高幅度对吗?
但:
final byte[] audioBytes = e.getByteBuffer();
short[] shortArray = new short[audioBytes.length];
for (int index = 0; index < audioBytes.length; index++) {
shortArray[index] = (short) audioBytes[index];
float item = shortArray[index];
if (item > amp){
amp = item;
}
}
在这种情况下,AMP始终返回127。而且这种方法在实时环境中并不真正有效?
所以还有三个问题。如果我的基本思路是正确的,那么为什么它总是返回127?我该如何在实时环境中使用它?
- 列表项
pitchInHz = res.getPitch();
后面也可以跟着.getMaxAmplitude();
代码吗? - VC.One