在我的 Beat-Detection 中,我使用快速傅立叶变换来检测音频信号中的低音。我正在录制一个基于独奏的鼓,而不是移动声音或音量。随着时间的推移绘制值之后。我得到非常量的值。他们差别很大。也许你知道为什么会发生这种情况?我只能猜测,但也许我没有为 FFT 使用正确的 Buffersize 或 WindowSize?
下一个绘制的图形和源代码
私有类 RecordingThread 扩展 Thread {
private boolean mShallContinue = true;
@Override
public void run() {
// Compute the minimum required audio buffer size and allocate the
// buffer.
mBufferSize = 4096;// AudioRecord.getMinBufferSize(SAMPLING_RATE,
// //4096;//
// AudioFormat.CHANNEL_IN_MONO,
mAudioBuffer = new short[1024];// [mBufferSize / 2];
bufferDouble2 = new int[mBufferSize / 2];
bufferDouble = new int[(blockSize - 1) * 2];
camera = Camera.open();
}
AudioRecord record = new AudioRecord(
MediaRecorder.AudioSource.DEFAULT, SAMPLING_RATE,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, mBufferSize);
short[] buffer = new short[blockSize];
double[] audioDataDoubles = new double[(blockSize * 2)];
double[] re = new double[blockSize];
double[] im = new double[blockSize];
double[] magnitude = new double[blockSize];
// start collecting data
record.startRecording();
DoubleFFT_1D fft = new DoubleFFT_1D(blockSize);
synchronized (this) {
while (shallContinue()) {
/** decibels */
record.read(mAudioBuffer, 0, 1024);
// updateDecibelLevel();
/** frequency */
// /windowing!?
for (int i = 0; i < mAudioBuffer.length; i++) {
bufferDouble2[i] = (int) mAudioBuffer[i];
}
for (int i = 0; i < blockSize - 1; i++) {
double x = -Math.PI + 2 * i * (Math.PI / blockSize);
double winValue = (1 + Math.cos(x)) / 2.0;
bufferDouble[i] = (int) (bufferDouble2[i] * winValue);
}
int bufferReadResult = record.read(buffer, 0, blockSize);
// Read in the data from the mic to the array
for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
audioDataDoubles[2 * i] = (double) buffer[i] / 32768.0; // signed
// 16
// bit
audioDataDoubles[(2 * i) + 1] = 0.0;
}
// audiodataDoubles now holds data to work with
fft.complexForward(audioDataDoubles); // complexForward
for (int i = 0; i < blockSize; i++) {
// real is stored in first part of array
re[i] = audioDataDoubles[i * 2];
// imaginary is stored in the sequential part
im[i] = audioDataDoubles[(i * 2) + 1];
// magnitude is calculated by the square root of
// (imaginary^2 + real^2)
magnitude[i] = Math.sqrt((re[i] * re[i])
+ (im[i] * im[i]));
}
magnitude[0] = 0.0;
magnitude2 = magnitude[2];
magnitude3 = magnitude[3];
magnitude4 = magnitude[4];
updateShortBuffer();
bufferCount++;
updateLongBuffer();
// if (detectedRoomRMS == 200)
updateFrequency();
System.out.println(System.currentTimeMillis() + " M2: "
+ magnitude2 + " M3: " + magnitude3 + " M4: "
+ magnitude4 + " M5: " + magnitude[5] + " M10: "
+ magnitude[10] + " M20: " + magnitude[20] + " M24: "
+ magnitude[24] + " M48: " + magnitude[48] + " LONG20: "
+ rms_Long_Buffer_five + " LONNG: "
+ rms_Long_Buffer);
}
record.stop(); // stop recording please.
record.release(); // Destroy the recording, PLEASE!
}
}
/**
* true if the thread should continue running or false if it should stop
*/
private synchronized boolean shallContinue() {
return mShallContinue;
}
/**
* Notifies the thread that it should stop running at the next
* opportunity.
*/
private synchronized void stopRunning() {
mShallContinue = false;
}
}
// / post the output frequency to TextView
private void updateFrequency() {
tvfreq.post(new Runnable() {
String RoomRMS;
String s;
public void run() {
if (RMSMessureDone == false) {
String l = "..";
String KK = "...";
tvfreq.setTextColor(Color.WHITE);
if ((rmsCounter > 10))
tvfreq.setText(KK); //
else
tvfreq.setText(l);
} else {
BPM = round(BPM, 1);
s = Double.toString(BPM);
s = s + " bpm";
tvfreq.setTextColor(Color.WHITE);
tvfreq.setText((s));
RoomRMS = Double.toString(detectedRoomRMS);
tvdb.setText(RoomRMS);
}
}
});
}