Hi,
I got the code below from google. Just want to know how does this code
get frequency from the audio file that the system recorded. it seems
like all the calculation have nothing to do with
recorder.startRecording()/recorder.read(audio_data, 0,
CHUNK_SIZE_IN_BYTES / 2);.

im working on final year project and tomorrow is the presentation day.
doing this so many month already but still dont know how to get the
frequency from audio file in SD card and audio file the system
recording. pls help me
______________________________________________________________________________________________________________

    private final static int frequency = 8000;
    private RealDoubleFFT transformer;
    int blockSize = 256;
    private final static int BUFFER_SIZE_IN_MS = 3000;
    private final static int CHUNK_SIZE_IN_SAMPLES = 4096; // = 2 ^
    // CHUNK_SIZE_IN_SAMPLES_POW2
    private final static int CHUNK_SIZE_IN_MS = 1000 *
CHUNK_SIZE_IN_SAMPLES / frequency;
    private final static int BUFFER_SIZE_IN_BYTES = frequency *
BUFFER_SIZE_IN_MS / 1000 * 2;
    private final static int CHUNK_SIZE_IN_BYTES = frequency *
CHUNK_SIZE_IN_MS / 1000 * 2;
    private final static int MIN_FREQUENCY = 50; // HZ
    private final static int MAX_FREQUENCY = 600; // HZ - it's for
guitar,
    public native void DoFFT(double[] data, int size);  // an NDK
library 'fft-jni'

______________________________________________________________________________________________________________
private void getFreq() {

short[] audio_data = new short[BUFFER_SIZE_IN_BYTES / 2];

                double[] data = new double[CHUNK_SIZE_IN_SAMPLES * 2];

                final int min_frequency_fft = Math.round(MIN_FREQUENCY
* CHUNK_SIZE_IN_SAMPLES / frequency);
                final int max_frequency_fft = Math.round(MAX_FREQUENCY
* CHUNK_SIZE_IN_SAMPLES / frequency);

while (isRecording) {
                        recorder.startRecording();
                        recorder.read(audio_data, 0,
CHUNK_SIZE_IN_BYTES / 2);
                        recorder.stop();
                        for (int i = 0; i < CHUNK_SIZE_IN_SAMPLES; i+
+) {
                                data[i * 2] = audio_data[i];
                                data[i * 2 + 1] = 0;
                        }

                        DoFFT(data, CHUNK_SIZE_IN_SAMPLES);
                        double best_frequency = min_frequency_fft;
                        double best_amplitude = 0;

                        HashMap<Double, Double> frequencies = new
HashMap<Double, Double>();

                        final double draw_frequency_step = 1.0 *
frequency / CHUNK_SIZE_IN_SAMPLES;

                        for (int i = min_frequency_fft; i <=
max_frequency_fft; i++) {

                                        final double current_frequency = i * 
1.0 *
frequency / CHUNK_SIZE_IN_SAMPLES;
                                final double draw_frequency =
Math.round((current_frequency - MIN_FREQUENCY)
                                                                /
draw_frequency_step)* draw_frequency_step + MIN_FREQUENCY;

                                final double current_amplitude =
Math.pow(data[i * 2], 2) + Math.pow(data[i * 2 + 1], 2);
                                final double normalized_amplitude =
current_amplitude * Math.pow(MIN_FREQUENCY * MAX_FREQUENCY, 0.5) /
current_frequency;

                                Double current_sum_for_this_slot =
frequencies.get(draw_frequency);

                                if (current_sum_for_this_slot == null)
                                        current_sum_for_this_slot =
0.0;

                                frequencies.put(draw_frequency,
Math.pow(current_amplitude, 0.5)/ draw_frequency_step +
current_sum_for_this_slot);

                                if (normalized_amplitude >
best_amplitude) {
                                        best_frequency =
current_frequency;
                                        best_amplitude =
normalized_amplitude;
                                }
                        }
                }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to