我正在尝试开发一个从乐器中提取音频的 Android 应用程序。我正在使用带有 Jtransforms 的快速傅里叶变换方法。这是我到目前为止所拥有的:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new readFrequencies().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class readFrequencies extends AsyncTask<Void,Integer,Integer> {
@Override
protected Integer doInBackground(Void... arg0) {
AudioRecord recorder = null;
int bufferSize = 0;
boolean recording = true;
int rate = 8000;
short audioFormat = AudioFormat.ENCODING_PCM_16BIT;
short channelConfig = AudioFormat.CHANNEL_IN_MONO;
try {
bufferSize = AudioRecord.getMinBufferSize(rate,channelConfig, audioFormat);
recorder = new AudioRecord(AudioSource.DEFAULT, rate,
channelConfig, audioFormat, bufferSize);
if (recorder.getState() == AudioRecord.STATE_INITIALIZED) {
/*
* Android 4.1.2
*
int recorder_id = recorder.getAudioSessionId();
if (NoiseSuppressor.isAvailable()) NoiseSuppressor.create(recorder_id);
*/
}
else {
Toast.makeText(getApplicationContext(),
"Error en la inicialización", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {}
short[] audioData = new short[bufferSize];
if (recorder != null) {
while (recording) {
if (recorder.getRecordingState() == AudioRecord.RECORDSTATE_STOPPED) {
recorder.startRecording();
}
else {
int numshorts = recorder.read(audioData,0,audioData.length);
if ((numshorts != AudioRecord.ERROR_INVALID_OPERATION) &&
(numshorts != AudioRecord.ERROR_BAD_VALUE)) {
// Hann
double[] preRealData = new double[bufferSize];
double PI = 3.14159265359;
for (int i = 0; i < bufferSize; i++) {
double multiplier = 0.5 * (1 - Math.cos(2*PI*i/(bufferSize-1)));
preRealData[i] = multiplier * audioData[i];
}
DoubleFFT_1D fft = new DoubleFFT_1D(bufferSize);
double[] realData = new double[bufferSize * 2];
for (int i=0;i<bufferSize;i++) {
realData[2*i] = preRealData[i];
realData[2*i+1] = 0;
}
fft.complexForward(realData);
double magnitude[] = new double[bufferSize / 2];
for (int i = 0; i < magnitude.length; i++) {
double R = realData[2 * i];
double I = realData[2 * i + 1];
magnitude[i] = Math.sqrt(I*I + R*R);
}
int maxIndex = 0;
double max = magnitude[0];
for(int i = 1; i < magnitude.length; i++) {
if (magnitude[i] > max) {
max = magnitude[i];
maxIndex = i;
}
}
int frequency = rate * maxIndex / bufferSize;
publishProgress(frequency);
}
else {
if (numshorts == AudioRecord.ERROR_BAD_VALUE) {
Toast.makeText(getApplicationContext(),
"ERROR_BAD_VALUE", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(),
"ERROR_INVALID_OPERATION", Toast.LENGTH_SHORT).show();
}
return -1;
}
}
}
if (recorder.getState() == AudioRecord.RECORDSTATE_RECORDING)
recorder.stop(); //stop the recorder before ending the thread
recorder.release();
recorder=null;
}
return 0;
}
protected void onProgressUpdate(Integer... f) {
TextView texto = (TextView) findViewById(R.id.texto);
texto.setText(String.valueOf(f[0]));
}
protected void onPostExecute(Integer f) {
TextView texto = (TextView) findViewById(R.id.texto);
int frecuencias = f.intValue();
texto.setText(String.valueOf(frecuencias));
}
}
}
使用此代码,我可以从产生纯信号的频率发生器中获得准确的频率。但是,当我尝试使用乐器进行相同操作时,我会获得随机频率。我知道,当涉及到真实仪器时,产生的信号包含可能会影响最终结果的谐波,但我不知道在这种情况下获得真实频率。有人可以帮助我吗?
我使用过 TarsosDSP,也尝试过自相关方法,但我无法得到我想要的。
提前致谢。