Frequency detection on Android - AudioRecord

眉间皱痕 提交于 2020-01-02 21:49:29

问题


This is my second question because i hawe a problem. I've to implement a simple Frequency detection on Android. For the DFT i've found the jTransform library (https://sites.google.com/site/piotrwendykier/software/jtransforms).

In the API of jTransform http://incanter.org/docs/parallelcolt/api/edu/emory/mathcs/jtransforms/fft/DoubleFFT_1D.html#complexForward%28double%5B%5D%29 i had seen that i've to pass a double vector at the complexForward() method, and obliviously not a File.

So, for first, it's correct what I thought? And so, that i can't use the MediaRecorder, because it generate a File? I need a buffer, then i use AudioRecorder.

But here's the problem: i can't initialize the AudioRecorder Object with no one configuration.

The findAudioRecord() try all possible configurations to initialized the AudioRecord and at the first working configurations the methods end (for the return). But it end, and return null.

Really, i can't understand the problem. It's possible that is my phone?

Thanks for your help! and sorry for my english...

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;
} 

回答1:


Put following permission uses-permission android:name="android.permission.RECORD_AUDIO outside application tag



来源:https://stackoverflow.com/questions/13376780/frequency-detection-on-android-audiorecord

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!