之前,我问过有关使用 FFT 和复杂类获取频率 wav 音频的问题,
在那里,我需要从 AudioRecord 输入计算 FFT 值——>从麦克风,我设法得到了 FFT 值......
现在我需要从我之前保存的 *.wav 音频文件中计算 FFT 值,我将音频保存在项目中“res”文件夹内的“raw”文件夹中
我仍然使用相同的 FFT 类:http ://www.cs.princeton.edu/introcs/97data/FFT.java
与之配套的复杂类:http: //introcs.cs.princeton.edu/java/97data/Complex.java.html
我使用这种方法从我的原始文件夹中读取音频文件,然后我调用方法 calculateFFT 来使用它
private static final int RECORDER_BPP = 16;
private static final int RECORDER_SAMPLERATE = 44100;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private void asli(){
int counter = 0;
int data;
InputStream inputStream = getResources().openRawResource(R.raw.b1);
DataInputStream dataInputStream = new DataInputStream(inputStream);
List<Integer> content = new ArrayList<Integer>();
try {
while ((data = dataInputStream.read()) != -1) {
content.add(data);
counter++; }
} catch (IOException e) {
e.printStackTrace();}
int[] b = new int[content.size()];
int cont = 0;
byte[] audio = convertArray(b);
}
转换为字节的方法
public byte[] convertArray(int[] array) {
int minBufferSize = AudioTrack.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING);
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING,minBufferSize, AudioTrack.MODE_STREAM);
byte[] newarray = new byte[array.length];
for (int i = 0; i < array.length; i++) {
newarray[i] = (byte) ((array[i]) & 0xFF); }
absNormalizedSignal = calculateFFT(newarray);
return newarray;
}
这是CalculateFFT方法
public double[] calculateFFT(byte[] signal)
{
final int mNumberOfFFTPoints =1024;
double mMaxFFTSample;
double temp;
Complex[] y;
Complex[] complexSignal = new Complex[mNumberOfFFTPoints];
double[] absSignal = new double[mNumberOfFFTPoints/2];
for(int i = 0; i < mNumberOfFFTPoints; i++){
temp = (double)((signal[2*i] & 0xFF) | (signal[2*i+1] << 8)) / 32768.0F;
complexSignal[i] = new Complex(temp,0.0);
}
y = FFT.fft(complexSignal);
mMaxFFTSample = 0.0;
mPeakPos = 0;
for(int i = 0; i < (mNumberOfFFTPoints/2); i++)
{
absSignal[i] = Math.sqrt(Math.pow(y[i].re(), 2) + Math.pow(y[i].im(), 2));
if(absSignal[i] > mMaxFFTSample)
{
mMaxFFTSample = absSignal[i];
mPeakPos = i;
}
}
return absSignal;
}
我也使用这个CalculateFFT方法来处理来自AudioRecorder的音频->之前有麦克风输入的那个......我设法从AudioRecorder获得价值,但我未能从我的音频文件中获得价值......我不打算播放音频..我只需要用 FFT 处理它。
我的代码有什么问题吗??:o 似乎我无法从方法 Asli(); 但我不知道哪一部分是错的。。
任何帮助将不胜感激... :) 谢谢