Here the code for preparing the Audio

package contoh.GetCheck;


import java.util.ArrayList;

import java.util.Collections;

import java.util.List;



import android.media.AudioFormat;

import android.media.AudioRecord;

import android.media.MediaRecorder;

import android.util.Log;



public class Main extends ActionBarActivity {

private static int audioSource = MediaRecorder.AudioSource.MIC;

private static int sampleRate = 44100;

private static int channelConfig = AudioFormat.CHANNEL_IN_MONO;

private static int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

private static int bufferSize = AudioRecord.getMinBufferSize
 (sampleRate,channelConfig,audioFormat);

public static int captureTime;//second

public static final int totalTimePerFrame=25;

public static int totalFramePerSampling;



private static List<Short> sampling=new ArrayList<Short>();


private static AverageEnergy AverageEnergy=new AverageEnergy ();

private static ShortTimeEnergy STE=new ShortTimeEnergy ();

private static HarmonicLineAssociation HLA=new HarmonicLineAssociation ();




public static double statusWavelet;

public static double statusAverageEnergy;

public static double statusZCR;

public static double statusSTE;

public static double statusHLA;


public static String threshold="";

static FFT fft=new FFT ();

Here,the code for record processing:

public static void capture (int captureTime){

Main.captureTime=captureTime;

totalFramePerSampling=(captureTime*1000)/totalTimePerFrame;



AudioRecord recorder=new AudioRecord (audioSource, sampleRate, channelConfig, audioFormat, bufferSize);

recorder.startRecording();



long start=System.currentTimeMillis();



while (true){

short[] buffer =new short[bufferSize /4];

recorder.read(buffer, 0, bufferSize /4);

for (int i=0; i<buffer.length;i++) {

sampling.add(buffer[i]);

}

long end=System.currentTimeMillis();

if (end-start>captureTime*1000)break;

}


Here the code for framing:

Log.d("How", "Sample captured");

Frame []frames=PreProcessor.preProceed (sampling);

Log.d("How", "Frame-Length: "+frames.length);

Log.d("How", "Frame[0]-Sampling-Size:"+frames[0].getSampling().size());


double []coeffsWavelet=Wavelet.transform (frames);

float [] coeffsFFT=new float[coeffsWavelet.length];

for (int i=0; i<coeffsWavelet.length; i++){

coeffsForFFT[i]=(float)coeffsWavelet[i];

}

fft.computerFFT(coeffsForFFT);


for (int i=0; i<coeffsWavelet.length; i++){

coeffsWavelet[i]=coeffsForFFT[i];

}

The code for extraction: i wanna use 4 kind of extraction such : Average energy, Energy Bandwidth Ratio, Zero Crossing Rate, Short Time Energy, and Harmonic Line Association.

double []coeffsAverageEnergy=averageEnergy.extract (frames);

double []coeffsZCR=zcr.extract (frames);

double []coeffsSTE=ste.extract (frames);

double []coeffsHLA=hla.extract (frames);

}

}


if(captureTime==10){

//10s

for(int i=0; i<MainActivity.wavelet10.size(); i++){




MainActivity.wavelet10.get(i).setDistance(EuclideanDistance.getDistance
    (coeffsWavelet, MainActivity.wavelet10.get(i).getCoefficients()));

// Log.d("How", "Wavelet10["+i+"]: 

"+MainActivity.wavelet10.get(i).getCoefficients().size());

}


Collections.sort(MainActivity.wavelet10, TrainingData.DISTANCE_COMPARATOR);

statusWavelet=MainActivity.wavelet10.get(0).getStatus();

Log.d("How", "Wavelet Distance[min]:"+MainActivity.wavelet10.get(0).getDistance()+", [max]:" +

" "+MainActivity.wavelet10.get(MainActivity.wavelet10.size()-1).getDistance());


for(int i=0; i<MainActivity.averageEnergy10.size(); i++){



MainActivity.averageEnergy10.get(i).setDistance(EuclideanDistance.getDistance
    (coeffsWavelet, MainActivity.averageEnergy10.get(i).getCoefficients()));

// Log.d("How", "Average10["+i+"]: 

"+MainActivity.averageEnergy10.get(i).getCoefficients().size());

}

Collections.sort(MainActivity.averageEnergy10,
TrainingData.DISTANCE_COMPARATOR);

statusAverageEnergy=MainActivity.averageEnergy10.get(0).getStatus();

Log.d("How", "Average " +
"Distance[min]:"+MainActivity.averageEnergy10.get(0).getDistance()+", [max]: " +

MainActivity.averageEnergy10.get(MainActivity.averageEnergy10.size()-
1).getDistance());


for(int i=0; i<MainActivity.zcr10.size(); i++){



MainActivity.zcr10.get(i).setDistance(EuclideanDistance.getDistance
(coeffsWavelet, MainActivity.zcr10.get(i).getCoefficients()));

// Log.d("How", "ZCR10["+i+"]: 

"+MainActivity.zcr10.get(i).getCoefficients().size());

}



Collections.sort(MainActivity.zcr10, TrainingData.DISTANCE_COMPARATOR);

statusZCR=MainActivity.zcr10.get(0).getStatus();

Log.d("How", "ZCR 
Distance[min]:"+MainActivity.zcr10.get(0).getDistance()+", [max]:
"+MainActivity.zcr10.get(MainActivity.zcr10.size()-1).getDistance());

}

else if(captureTime==20){

//20

for(int i=0; i<MainActivity.wavelet20.size(); i++){


MainActivity.wavelet20.get(i).setDistance(EuclideanDistance.getDistance
    (coeffsWavelet, MainActivity.wavelet20.get(i).getCoefficients()));

}



Collections.sort(MainActivity.wavelet20,TrainingData.DISTANCE_COMPARATOR);


statusWavelet=MainActivity.zcr20.get(0).getStatus();


for(int i=0; i<MainActivity.averageEnergy20.size(); i++){


MainActivity.averageEnergy20.get(i).setDistance
    (EuclideanDistance.getDistance(coeffsWavelet,
MainActivity.averageEnergy20.get(i).getCoefficients()));

}

Collections.sort(MainActivity.averageEnergy20, TrainingData.DISTANCE_COMPARATOR);


statusAverageEnergy=MainActivity.averageEnergy20.get(0).getStatus();

for(int i=0; i<MainActivity.zcr20.size(); i++){

MainActivity.zcr20.get(i).setDistance(EuclideanDistance.getDistance(coeffsWavelet, MainActivity.zcr20.get(i).getCoefficients()));

}

Collections.sort(MainActivity.zcr20, TrainingData.DISTANCE_COMPARATOR);

statusZCR=MainActivity.zcr20.get(0).getStatus();

}

    Is my source code are right??Please correct me if mine are wrong.
    Thanks for your attention

Related posts

Recent Viewed