I was implementing an android FFT guitar tuner. Here, I have 2 text views, 1 for displaying the frequency of the sound coming from the mic port and the other 1 for displaying the pitch notation. To match with the frequency, I wrote an if condition which is having about 35 if else if conditions consisting of 150 lines (approximately). Now, when a sound is heard, the value is updated in the frequency text view, but the note pitch text view is getting late to updated since there are so many conditions to be checked before updating the text view, so when the conditions are checked, again the frequency text view may have changed and got itself updated. What can I do to get rid of this problem? The final output is not efficient..Please help me with this…
if(frequency >= 62 && frequency <= 65)
{
note.setText("C");
}
else if(frequency >= 70 && frequency <= 74)
{
note.setText("C#");
}
else if(frequency >= 77 && frequency <= 81)
{
note.setText("D");
}
else
{
note.setText("Frequency is out of range");
}
My answer has following assumption:
According to Pitch to Frequency mappings, it can be divided into 8 groups.
http://peabody.sapp.org/class/st2/lab/notehz/
Step 1:
You may consider 2 level IF-ELSE, which the first level is to determine the group, and second level is to determine the individual pitch note. To reduce the potential amount of IF_ELSE executed.
Example:
Step 2: You can make a HashMap for each group. Improving the second level searching.
Let says Group 3 has a HashMap like this:
You code will look similar to this:
So finally, you will have 8 IF-ELSE, and each contain 1 map searching.
==================================================================================
Another thing that you can try:
Try to supply frequency in a sampling of 500ms (Or some reasonable value) instead of real time input.
This is to prevent the congestion in UI thread’s update.
==================================================================================
Again, I am not a music guy. It is all base on programming perspective. Not sure if it is helpful for you.