11

What is the best way to process audio so I can output what note is being played? I am creating a guitar tuner for a college assignment and I am new to Android development.

I have seen the Android example on recording sounds from the Google API, but I was wondering where to go from there?

I understand I have to do a Fourier Transform, or something, to get the frequency, just wondering if anybody has any advice on how to do this?

Once we can get the correct frequency displayed on screen we will have the bulk of our project done.

Thanks for any help.

QHarr
  • 83,427
  • 12
  • 54
  • 101
Ayohaych
  • 5,099
  • 7
  • 29
  • 51
  • 1
    Possible duplicate: http://stackoverflow.com/questions/11553047/frequency-pitch-detection-for-dummies and http://stackoverflow.com/questions/9044672/android-how-to-use-microphone-in-order-to-calculate-sound-frequencies/9044713#9044713 and LOTS of others – Bjorn Roche Feb 07 '13 at 02:44
  • 1
    Instructions for pitch tracking (with code) in C: http://blog.bjornroche.com/2012/07/frequency-detection-using-fft-aka-pitch.html – Bjorn Roche Feb 07 '13 at 03:16
  • 1
    If you aren't yet familiar with the Fourier Transform then I suspect that creating/implementing the algorithms for this project will be well beyond the scope of this project (unless you've got a long time to do this project, say 6 months, and a strong mathematical background). If you don't have that amount of time then I'd follow the suggestion from @anthropomo – the_mandrill Feb 07 '13 at 09:15

3 Answers3

12

If you're never done Android development and have little or no experience with digital signal processing and the Fourier transform, you're tackling a difficult challenge.

On the other hand, if you can use an existing library for your assignment, as anthropomo suggested, you may have a good chance to pull it off.

However, if your professor won't let you use an existing library, you'll need to solve the following difficult problems:

How does your program automatically find the fundamental frequency of the note being played? Take a look at this frequency/frequency_decibelMagnitude plot of a real classical acoustic guitar playing an E2 note. Observe that the fundamental frequency (82.4 Hz) is attenuated about 17 decibels (17 dB) below the first harmonic (the first harmonic is at 164.8 Hz).

GuitarE2frequency_decibelMagnitude

Below is a closeup of the same plot, where you can see the fundamental peak more clearly:

GuitarE2frequency_decibelMagnitudeCloseup

The fundamental frequency being attenuated 17 dB below the first harmonic is a large attenuation. Below is the same E2 note spectrum, but now it's plotted on a linear frequency-magnitude axis (the vertical axis is now linear frequency magnitude instead of decibel frequency magnitude). Now you can see more clearly how far below the first harmonic the fundamental frequency peak really is.

GuitarE2frequency_linearMagnitudeCloseup

Your program will have to automatically detect the 17 dB attenuated fundamental at 82.4 Hz, but how do you do that in the general case where your program won't know ahead of time which note the user is playing on his guitar?

The above frequency spectrum is for E2 on a classical acoustic guitar. How does the spectrum differ for E2 on a steel string guitar? What about E2 on an amplified electric guitar? How will your program deal with the differences between those different spectra?

The problem is not trivial. The question is how much time do you have for this assignment, and what will your professor consider to be a completed assignment.

This reference gives deeper understanding: Musical instrument spectra to 102.4 KHz

You can plot frequency spectra and hear guitar notes E2 to Bb5, here: Musical instrument spectrum

Babson
  • 909
  • 1
  • 7
  • 7
  • If you intend to have your program automatically "guess" the note the user is playing on his guitar, and how far away from perfect pitch the user is playing that note, you're taking-on a huge task. – Babson Feb 11 '13 at 00:36
  • Why not display the sound's linear_magnitude spectrum, as seen in one of the above plots, let the user type-in the note he is playing (E2 for example), mark the fundamental frequency of E2 on the plot, and let the user tune his guitar so the spectral line of the fundamental coming from the user's guitar matches the location of E2 that your program displays? That way, you are leveraging the user's brain power, and your program has much less work to do. – Babson Feb 11 '13 at 00:48
  • Yeah I understand what you mean, unfortunatly in our functional specification we already gave an overview of how it is going to work. Do you think we used a library like Pure Data as mentioned here, it could be easy enough to display the sounds. We plan to have an Auto Tune function to pick up what note the user plays, a select tuning note where the user enters a note to tune to and it will tell you how far off you are, and then a sound note function which simply plays a midi sound so the user can tune by ear. – Ayohaych Feb 13 '13 at 13:31
  • The Auto Tune feature is going to be a challenge, Select Tune is easier but still a challenge, Tune by Ear is easy. If you can find a library that solves problems 1 and 2, happy days, otherwise you've got work. I don't have direct experience with Pure Data. Maybe someone else here does. – Babson Feb 13 '13 at 22:05
5

Do not use a bare FFT magnitude or other frequency peak estimator. They will give you very bad/wrong results for the lower note strings of most guitars. Musical pitch is a human psychoacoustic perception phenomena, very often not the same as FFT frequency (except for pure sinusoidal tones unlike those produced by real stringed instruments).

Google "pitch detection" and "pitch estimation" methods instead. Some possibilities include weighted auto-correlations, AMDF, ASDF, cepstrum/cepstral analysis, harmonic product spectrum analysis, and composite algorithms such as RAAPT and YAPT. References to several academic papers on some of these estimator algorithms might be on my web page: http://www.nicholson.com/rhn/dsp.html#1

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
1

This paper provides a comprehensive evaluation of pitch detection algorithms you might use.

As indicated, autocorrelation is a easy to implement, but not particularly accurate - particularly on real-world music instrument signals in which the fundamental is often missing. The FFT approach requires a significant amount of post-processing.

I suspect for a college assignment you would be better off with a complete working system that is not always accurate rather than a accurate one that is incomplete.

marko
  • 9,029
  • 4
  • 30
  • 46