Frage

I have looked at every single answer here and I do not understand them... I am able to obtain the graph but how can I just have a single value for the current frequency... I'd appreciate code answers rather than mathematical ones..

 public class RecordAudio extends AsyncTask<Void, double[], Void> {

    @Override
    protected Void doInBackground(Void... arg0) {

        try {
            // int bufferSize = AudioRecord.getMinBufferSize(frequency,
            // AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
            int bufferSize = AudioRecord.getMinBufferSize(frequency,
                    channelConfiguration, audioEncoding);

            AudioRecord audioRecord = new AudioRecord(
                    MediaRecorder.AudioSource.MIC, frequency,
                    channelConfiguration, audioEncoding, bufferSize);

            //  double[] audioDataDoubles = new double[(blockSize*2)]; // Same values as above, as doubles
            //   -----------------------------------------------
            double[] re = new double[blockSize];
            double[] im = new double[blockSize];
            double[] magnitude = new double[blockSize];
            int sampleRate = 8000;                             // Sample rate in Hz

            short[] buffer = new short[blockSize];
            double[] toTransform = new double[blockSize];

            audioRecord.startRecording();

            // started = true; hopes this should true before calling
            // following while loop

            while (started) {
                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
                transformer.ft(toTransform);



                publishProgress(toTransform);



            }

            audioRecord.stop();

        } catch (Throwable t) {
            t.printStackTrace();
            Log.e("AudioRecord", "Recording Failed");
        }
        return null;
    }
    public   double Index2Freq(int i, double samples, int nFFT) {
        return (double) i * (samples / nFFT / 2.);
    }
    public   int calculateF(int sampleRate, double [] audioData){

        int numSamples = audioData.length;
        int numCrossing = 0;
        for (int p = 0; p < numSamples-1; p++)
        {
            if ((audioData[p] > 0 && audioData[p + 1] <= 0) ||
                    (audioData[p] < 0 && audioData[p + 1] >= 0))
            {
                numCrossing++;
            }
        }

        float numSecondsRecorded = (float)numSamples/(float)sampleRate;
        float numCycles = numCrossing/2;
        float frequency = numCycles/numSecondsRecorded;

        return (int)frequency;
    }

    @Override
    protected void onProgressUpdate(double[]... toTransform) {

        canvas.drawColor(Color.BLACK);

        for (int i = 0; i < toTransform[0].length; i++) {
            int x = i;
            int downy = (int) (100 - (toTransform[0][i] * 10));
            int upy = 100;



            canvas.drawLine(x, downy, x, upy, paint);
        }

        imageView.invalidate();

           TxtV.setText("Frequency = "+String.valueOf(calculateF(8000, toTransform[0])));

        // TODO Auto-generated method stub
        // super.onProgressUpdate(values);
    }

}
War es hilfreich?

Lösung

If by current frequency, you mean something about captured audio, realize that live sound is composed of a massive number of frequencies, so you will never get just one frequency result.

This is true even for a single musical pitch (except maybe for synthesized pure sinusoidal test data in zero noise).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top