私はAndroid向けのチューナーアプリを開発していますが、基本周波数を得るのは苦労しています。Android - AsyncTaskの値を表示する方法
問題が2つあります。 1)基本周波数を正しく計算していません。 2)UIに表示できるように、この値を正しく渡していません。
Craigyのおかげで、私は何らかのデータを連続的に渡すことができたので、AsyncTaskを正常に実装しました。さて、私はちょうど表示するために正しい値を取得する必要があります。ここで
がAsyncTaskです:
protected class Tune extends AsyncTask<Double, Double, Double>
{
protected void onPreExecute()
{
super.onPreExecute();
btnTune.setText("Stop Tuning");
}
protected Double doInBackground(Double... params)
{
while(tuning)
{
try{
acquire();
hello = computeFFT();
publishProgress(hello);
//Thread.sleep(1);
} catch(Exception e) {
}
}
return 100.0;
}
protected void onProgressUpdate(Double...values)
{
super.onProgressUpdate(values);
//DecimalFormat d = new DecimalFormat("#3.65");
freq.setText(values + " Hz");;
}
protected double onPostExecute(Double...hello)
{
tuning = false;
freq.setText("STOP!");
return 75.0;
}
}
私はそれが正しく設定されていることと思いますが、何らかの理由で私はpublishProgressとonProgressUpdateにもう表示するために何かを得ることができません。なにが問題ですか?どのように私はUIを更新するはずです。私はそれをしばらく働いていたが、それ以上はなかった。私は読んで研究しているすべてのものから
public double computeFFT(){
//Conversion from short to float
float[] micBufferData = new float[buffer];//size may need to change
final int bytesPerSample = 2; // As it is 16bit PCM
final float amplification = 50; // choose a number as you like
for (int index = 0, floatIndex = 0; index < buffer - bytesPerSample + 1; index += bytesPerSample, floatIndex++) {
float sample = 0;
for (int b = 0; b < bytesPerSample; b++) {
int v = audioData[index + b];
if (b < bytesPerSample - 1 || bytesPerSample == 1) {
v &= 0xFF;
}
sample += v << (b * 8);
}
float sample32 = amplification * (sample/32768);
micBufferData[floatIndex] = sample32;
}
sampleRate = sampleRateInHz;
//Take FFT of float data
fftgdx.forward(micBufferData);
//Fill in imaginary and real arrays from FFT data
tmpi = fftgdx.getImaginaryPart();
tmpr = fftgdx.getRealPart();
//Create array of magnitude = sqrt(real*real + imag*imag)
for(int i=0;i<micBufferData.length;i++)
{
real[i] = (double) tmpr[i];
imag[i] = (double) tmpi[i];
mag[i] = Math.sqrt((real[i]*real[i]) + (imag[i]*imag[i]));
}
// find largest peak in power spectrum
double max_magnitude = -1.0;
int max_index = -1;
for(int i = 0; i<(buffer/2.0-1); i++)
{ if(mag[i] > max_magnitude)
max_magnitude = mag[i];
max_index = i;
}
// convert index of largest peak to frequency
frequency = (max_index*sampleRateInHz)/(buffer);
return frequency;
}
が、これは右のようだ:
は、ここに私のFFTです。あなたによく見えますか?値を正しく戻していますか?
ありがとうございました。その戻り値の型がvoid
、ないdouble
であるように、それははるかに高く評価:)
このように@Overrideを追加するだけですか? – dustinrwh
AsyncTaskに 'double onPostExecute(...)'というメソッドがありません。これは 'void onPostExecute(...)'です。戻り値の型を 'void'に変更してください。 –
また、Devunwiredは指摘しているように、パラメータの種類を変更する必要があります。 –