What is the error?

Put your problem here if it does not fit any of the other categories.

What is the error?

Postby arcane » Wed Mar 14, 2012 4:11 pm

Hey there guys, Am implementing an android guitar tuner, and for that I decided to use "Fast Fourier Transform" methodology to gain the frequency of the sound coming via the microphone of the android device. I implemented the following code which I referred in one of the topics discussed at stackoverflow. Can anyone please help me what has gone wrong with this code?

package com.projectTuner;

import android.app.Activity;
import android.graphics.Color;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.FFT.*;

import android.app.Activity;
import android.os.Bundle;


public class Tuner2 extends Activity implements OnClickListener{

Button btnTune;
TextView fft;
TextView freq;
TextView results;
MediaRecorder recorder;
AudioRecord tuner;
boolean startTuning = true;
int audioSource = MediaRecorder.AudioSource.MIC;
int sampleRateInHz = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM);
int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int bufferSizeInBytes;
int samples;
short[] audioBuffer;
short[] audioData;
double[] temp;
String fileName;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnTune = (Button)findViewById(R.id.btnTune);
freq = (TextView)findViewById(R.id.freq);
btnTune.setOnClickListener(this);
bufferSizeInBytes = 4096;
//bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
results = (TextView)findViewById(R.id.results);
fft = (TextView)findViewById(R.id.fft);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

if (v == btnTune)
{
onTune(startTuning);
if (startTuning) {
((Button)v).setText("Stop Tuning");
}
else {
((Button)v).setText("Start Tuning");
}
startTuning = !startTuning;
}
}

//------------------------------------------------------------>
private void onTune(boolean start) {
if(start) {
startTuning();
} else {
Toast.makeText(getApplicationContext(), "Tuning Stopped", Toast.LENGTH_SHORT).show();
tuner.stop();
}
}

private void startTuning()
{
tuner = new AudioRecord(audioSource, sampleRateInHz, channelConfig, audioFormat, bufferSizeInBytes);

audioData = new short[bufferSizeInBytes];
/* new Thread(new Runnable() {
@Override
public void run() {
while (startTuning) {
trigger();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start(); */
trigger();
}

public void trigger(){
acquire();
computeFFT();
display();
}

public void acquire(){
try {
tuner.startRecording();
samples = tuner.read(audioData, 0, bufferSizeInBytes);
}
catch (Throwable t){

}
}

public void computeFFT(){
//Conversion from short to double
double[] micBufferData = new double[bufferSizeInBytes];//size may need to change
final int bytesPerSample = 2; // As it is 16bit PCM
final double amplification = 100.0; // choose a number as you like
for (int index = 0, floatIndex = 0; index < bufferSizeInBytes - bytesPerSample + 1; index += bytesPerSample, floatIndex++) {
double sample = 0;
for (int b = 0; b < bytesPerSample; b++) {
int v = audioData[index + b];
if (b < bytesPerSample - 1 || bytesPerSample == 1) {
v &= 0xFF;
}
sample += v << (b * 8);
}
double sample32 = amplification * (sample / 32768.0);
micBufferData[floatIndex] = sample32;
}

//Create Complex array for use in FFT
Complex[] fftTempArray = new Complex[bufferSizeInBytes];
for (int i=0; i<bufferSizeInBytes; i++)
{
fftTempArray[i] = new Complex(micBufferData[i], 0);
}

//Obtain array of FFT data
final Complex[] fftArray = FFT.fft(fftTempArray);
final Complex[] fftInverse = FFT.ifft(fftTempArray);

//Create an array of magnitude of fftArray
double[] magnitude = new double[fftArray.length];
for (int i=0; i<fftArray.length; i++){
magnitude[i]= fftArray[i].abs();
}

//fft.setTextColor(Color.GREEN);
//fft.setText("fftArray is "+ fftArray[500] +" and fftTempArray is "+fftTempArray[500] + " and fftInverse is "+fftInverse[500]+" and audioData is "+audioData[500]+ " and magnitude is "+ magnitude[1] + ", "+magnitude[500]+", "+magnitude[1000]+" You rock dude!");
for(int i = 2; i < samples; i++){
fft.append(" " + magnitude[i] + " Hz");
}
}

public void display(){
results.setTextColor(Color.BLUE);
results.setText(audioData[1]+"");
for(int i = 2; i < samples; i++){
results.append(" " + audioData[i]);
}
results.invalidate();
//fft.setTextColor(Color.GREEN);
//fft.setText("Buffer size is "+bufferSizeInBytes);
//fft.setText(fftArray[1]+" Hz");
//for(int i = 2; i < samples; i++){
//fft.append(" " + fftArray[i] + " Hz");
//}
//fft.invalidate();

}
}

For FFT implementation: http://introcs.cs.princeton.edu/java/97 ... .java.html
For Complex implementation: http://introcs.cs.princeton.edu/java/97 ... .java.html

The frequency value is always 0 and when I pressed the stop tuning button, it gives out an error saying to force close the application...
arcane
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu Mar 01, 2012 7:39 am

Top

Re: What is the error?

Postby Phyll » Fri Mar 16, 2012 5:11 pm

Hi arcane,

Just getting back to this. Looks like you found an FFT method. I searched around a little bit and found a lot of stuff about guitar tuning. Even someone who wanted to pay to have one written about a year ago. Seems like i-apple something has one.

Anyway, kind of hard to tell what the error is. You should include it.

I wasn't sure what this was supposed to be either:

Code: Select all
v &= 0xFF;
}
sample += v << (b * ;
}


Something seems to be missing. I guess I could try to find it in the code you referenced but I didn't want to.

If you would like to send your project zipped up I will run it and figure out what's wrong with it. Or send it in a PM if you don't want everybody else looking at it.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: What is the error?

Postby arcane » Sat Mar 17, 2012 4:50 am

hey Phyll,

Glad to see you are back to help me...I just went through the code and found out the missing part...It has to be

sample += v << (b*8);

Hope this will help... :)
arcane
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu Mar 01, 2012 7:39 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 17 guests