Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've been working on trying to create a guitar tuner for android based off of the code in https://code.google.com/p/android-guitar-tuner/[^] and the FFT algorithm I've found on the internet, I don't remember the actual link to it. I was using a class called Detector.java to input the sound and send it the the FFT algorithm then send it back into ShowResult in my main activity.

When I run the program I see the Logs coming up showing the best frequencies correctly, but the screen on my phone stays black when I keep the setContentView(R.layout.activity_tuner), but when I add anything to Analyze.java and try to use it as a custom view it crashes. I've searched google multiple times trying to figure out how to get a custom view set up though it doesn't seem to work when I try some of the examples I've seen.

I know my java file Analyze doesn't have much in it and that is my custom view I would like to try and display my background image. I'm just confused on where to start to get it to display the image. I'm still fairly new to the whole idea of using onDraw any guidance would be appreciated.

My code is as follows:

activity_tuner.xml:
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:custom="http://schemas.android.com/apk/res/com.example.thenxttuner"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:removed="@drawable/graybackgroundtunerpage"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Tuner" >
	<com.example.thenxttuner.Analyze
	    android:id="@+id/custView"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:layout_margin="5dp"/>
    <ImageView
        android:id="@+id/display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/dashdrawable" />

    <Button
        android:id="@+id/start"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/display"
        android:layout_below="@+id/display"
        android:layout_marginTop="21dp"
        android:background="@color/limegreen"
        android:text="Stop Tuning" />

</RelativeLayout> 


Tuner.java:
Java
public class Tuner extends Activity{
	boolean recording = true;
	Button start;
	Thread myThread;
	TextView text, texttwo, textthree;
	private double pitch;
	Analyze back;
	private static final String TAG = "Tuner";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
                back = new Analyze(this);
		setContentView(back);
		start = (Button) findViewById(R.id.start);
		
		start.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(recording==true){
					start.setText("Stop Tuning");
					recording=false;
					onStart();
				}else{
					start.setText("Start Tuning");
					recording=true;
					onStop();
				}
			}
		});
	}
	
	@Override
	public void onStart(){
		super.onStart();
		//setContentView(R.layout.activity_tuner);
		//Log.v(TAG,  "in onStart()");
	
		myThread = new Thread(new Detector(this, new Handler()));
		myThread.run();
	}

	@Override
	public void onStop(){
		super.onStop();
		//Log.v(TAG, "in onStop()");
		
		myThread.interrupt();
	}


	public void ShowResult(final HashMap<Double, Double> frequencies, final double best){
		Log.v(TAG,  "best = "+best);
		
		pitches[0][0] = 82.41;
		pitches[0][1] = 87.31;
		pitches[0][2] = 92.5;
		pitches[0][3] = 98;
		pitches[0][4] = 103.8;
		pitches[1][0] = 110;
		pitches[1][1] = 116.54;
		pitches[1][2] = 123.48;
		pitches[1][3] = 130.82;
		pitches[1][4] = 138.59;
		pitches[2][0] = 147.83;
		pitches[2][1] = 155.56;
		pitches[2][2] = 164.81;
		pitches[2][3] = 174.62;
		pitches[2][4] = 185;
		pitches[3][0] = 196;
		pitches[3][1] = 207;
		pitches[3][2] = 220;
		pitches[3][3] = 233.08;
		pitches[4][0] = 246.96;
		pitches[4][1] = 261.63;
		pitches[4][2] = 277.18;
		pitches[4][3] = 293.66;
		pitches[4][4] = 311.13;
		pitches[5][0] = 329.63;
		pitches[5][1] = 349.23;
		pitches[5][2] = 369.99;
		pitches[5][3] = 392;
		pitches[5][4] = 415.3;
		pitches[5][5] = 440;
	}
	double[][] pitches = new double[6][6];
}


Analyze.java:
Java
public class Analyze extends View {
	private static final String TAG = "Analyze";
	private final static int UI_UPDATE = 100;
	private Handler AnaHandler;
	private Timer timer;


	

	public Analyze(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
		
		
		/**UI update
		AnaHandler = new Handler();
		timer = new Timer();
	
		timer.schedule(new TimerTask(){
			public void run(){
				AnaHandler.post(new Runnable(){
					public void run(){
						invalidate();
					}
				});
			}
		}, 
		UI_UPDATE,
		UI_UPDATE);**/ 
		// not sure if needed 
	
		
	}

	@Override
	public void onDraw(Canvas canvas){
		
		
	}


}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900