0

我在一个 Android 项目上使用 FFT,并且需要 audioRecord 写入缓冲区仅 3 秒。因为,在捕获期间我不需要任何更新,所以我没有使用 AsyncTask。

当我开始处理时,我得到了 toTransform 长度和表达式类型的错误。为了使这些部件正常工作,我缺少什么或者我做错了?

PS 这一切都作为一个异步任务工作。

package com.example.learnfft;



import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity; 
import android.content.Intent;
import android.media.AudioFormat; 
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle; 
import android.os.Handler;
import android.util.Log;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
//import android.widget.Toast;
import ca.uol.aig.fftpack.RealDoubleFFT;



public class Main extends Activity implements OnClickListener {

    public int channelConfiguration = AudioFormat.CHANNEL_IN_MONO; 
    public int audioEncoding = AudioFormat.ENCODING_PCM_16BIT; 

    //FFT
    private RealDoubleFFT transformer;
    int blockSize = 1024;
    Button startStopButton;
    int frequency = 44100;
    int requestcode;
    boolean started = false;
    double freq;
    double[] magnitude;
    short[] buffer = new short[blockSize];
    double[] toTransform = new double[blockSize];


    //Create thread Handler
    Handler handler = new Handler();
    int bufferSize = AudioRecord.getMinBufferSize(frequency,
            channelConfiguration, audioEncoding);
    Timer timer = new Timer();
    AudioRecord audioRecord = new AudioRecord(
            MediaRecorder.AudioSource.MIC, frequency,
            channelConfiguration, audioEncoding, bufferSize);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button) findViewById(R.id.takeTemp);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent i = new Intent(this,recordAudio.class);
        startActivityForResult(i,requestcode);
        //recordAudio(toTransform);
        FindFrequency();
    }

    public void FindFrequency() {

        try { 
            audioRecord.startRecording();

            int bufferReadResult = audioRecord.read(buffer, 0, blockSize);

            for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
                toTransform[i] = (double) buffer[i] / 32768.0; // signed // 16 // bit

            }

             timer.schedule(new TimerTask() {
                 public void run() {
                     handler.post(new Runnable() {
                         public void run() {
                             audioRecord.stop();
                         }
                     });
                 }
             }, 3000); //Stop after 3 seconds
        } catch (Throwable t) {
            Log.e("AudioRecord", "Recording Failed");
        }
        File freqFile = new File("/mnt/sdcard/Test APKs/frequency.file");
        File magFile = new File("/mnt/sdcard/Test APKs/magnitude.file");
        int blockSize = 256;
        double[] audioDataDoubles = new double[(blockSize*2)];
        String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());



        transformer.ft(toTransform);

        for ( int x = 0; x < toTransform[0].length; x++) {  <-- Length error
            audioDataDoubles[2*x] = toTransform[0][x];      <-- is array but it must resolve to double.
            audioDataDoubles[(2*x)+1] = 0.0;
        }
        double[] re = new double[blockSize];
        double[] im = new double[blockSize];
        double[] magnitude = new double[blockSize];

        // Calculate the Real and imaginary and Magnitude.
        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[(2*i)+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]));
        }

        double peak = -1.0; 
        // Get the largest magnitude peak
        for(int i = 0; i < blockSize; i++){ 
            if(peak < magnitude[i])
                peak = magnitude[i];
                String magValue = String.valueOf(peak);
            try {
                BufferedWriter buf = new BufferedWriter(new FileWriter(magFile, true));
                buf.append(mydate + " - " + magValue);
                buf.newLine();
                buf.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
             }
        }
        // calculated the frequency
        freq = (frequency * peak)/blockSize;
        String freqValue = String.valueOf(freq);
        //Toast.makeText(Main.this, freqValue, Toast.LENGTH_SHORT).show();
        try {
            BufferedWriter buf = new BufferedWriter(new FileWriter(freqFile, true));
            buf.append(mydate + " - " + freqValue);
            buf.newLine();
            buf.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } 
    }    
}
4

1 回答 1

0
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data)
{
     super.onActivityResult(requestCode, resultCode, data);
     If(requestCode==requestcode){ 
         //requestcode is request code that you send with intent on   startActivityForResult(i,requestcode);
         //Your code here
         double[] toTransform = data.getDoubleArrayExtra("audioBuffer");
         FindFrequency(toTransform);
     }
}

您可以在http://techblogon.com/android-start-activity-for-result-example/找到更多详细信息

于 2013-10-28T04:35:28.673 回答