这是我的第二个问题,因为我有一个问题。我必须在 Android 上实现一个简单的频率检测。对于 DFT,我找到了 jTransform 库(https://sites.google.com/site/piotrwendykier/software/jtransforms)。
在 jTransform 的 API http://incanter.org/docs/parallelcolt/api/edu/emory/mathcs/jtransforms/fft/DoubleFFT_1D.html#complexForward%28double%5B%5D%29我看到我必须在 complexForward() 方法中传递一个双向量,并且不经意地不是一个文件。
所以,首先,我的想法是正确的吗?所以,我不能使用 MediaRecorder,因为它会生成一个文件?我需要一个缓冲区,然后我使用 AudioRecorder。
但问题是:我无法在没有任何配置的情况下初始化 AudioRecorder 对象。
findAudioRecord() 尝试所有可能的配置来初始化 AudioRecord,并且在第一个工作配置中方法结束(返回)。但它结束了,并返回 null。
真的,我无法理解这个问题。有可能是我的手机吗?
谢谢你的帮助!对不起我的英语...
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.media.MediaRecorder.AudioSource;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class FFTActivity extends Activity {
private TextView textView1;
private TextView textView2;
private static final int RECORDSTATE_RECORDING = 3;
private static final int RECORDER_BPP = 16;
private static final String AUDIO_RECORDER_FILE_EXT_WAV = ".wav";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw";
private static final int RECORDER_SAMPLERATE = 44100;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };
private int bufferSize = 0;
private AudioRecord recorder = null;
private int cont;
int cont2 = 0;
int contENCODING = 0;
int contCHANNEL = 0;
String s ="";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView1 = (TextView) findViewById(R.id.testo1);
textView2 = (TextView) findViewById(R.id.testo2);
bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING);
recorder = findAudioRecord();
}
public AudioRecord findAudioRecord() {
for (int rate : mSampleRates) {
for (short audioFormat : new short[] { AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT }) {
for (short channelConfig : new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO }) {
try {
int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);
if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
// check if we can instantiate and have a success
AudioRecord recorder = new AudioRecord(AudioSource.DEFAULT, rate, channelConfig, audioFormat, (bufferSize*2));
textView2.setText("NOT ERROR_BAD_VALUE "+mSampleRates[cont2] +" "+ bufferSize);
if (recorder.getState() == AudioRecord.STATE_INITIALIZED){
textView2.setText("getState(): "+recorder.getState()+" getMinBufferSize(): "+AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING));
return recorder;
}
}
} catch (Exception e) {
textView2.setText("ECCEZZIONE"+e);
}
contCHANNEL++;
}
contENCODING++;
}
cont2++;
}
textView1.setText("NON INIZIALIZZATO " + contCHANNEL + " " + contENCODING);
return null;
}