12

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
  • 72,711
  • 10
  • 44
  • 81
AndyOHart
  • 4,813
  • 7
  • 23
  • 45
  • 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
  • @BjornRoche Thanks for that I will have a look at them :) and thanks for the C code, should help me out. – AndyOHart Feb 07 '13 at 16:56
  • @AndyOHart may I ask how your project turned out/was it doable? I'm in a similar spot right now, have to do an android app (no prior experience in android development) for a university project and want to do a guitar tuner, not sure how doable that would be – Brian Nov 03 '20 at 21:34

5 Answers5

11

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
  • 869
  • 1
  • 7
  • 7
  • Really appreciate the input. We have until March 22nd I think until we have to hand in the final project. I think it's going to be quite hard to do but it seems like using a library could be an option! – AndyOHart Feb 10 '13 at 23:47
  • 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. – AndyOHart Feb 13 '13 at 13:31
  • Let me rank your tasks by order of difficulty: – Babson Feb 13 '13 at 21:56
  • 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
  • 68,014
  • 12
  • 81
  • 143
4

If your instructor is okay with you using a library for the audio processing, here is the source of a complete android guitar tuner app using libpd:

https://github.com/nettoyeurny/Making-Musical-Apps/tree/master/android/GuitarTuner

To use it, you will also need to learn the basics of the Pure Data audio synthesis programming language. The tools needed for a tuner are not too extensive, and are laid out in the above app. Obviously you would need to do some work to make this your own work.

Here is a very good intro to using Pure Data:

http://en.flossmanuals.net/pure-data/

anthropomo
  • 3,892
  • 1
  • 22
  • 39
  • As far as I know he is okay with using a library for it. Thank you so much for that it will actually help us so much. Really appreciate that man! – AndyOHart Feb 07 '13 at 16:59
2

Good Example for Guitar turner application. Using Jtransfrom.

https://github.com/nivwusquorum/Simple-Guitar-Tuner

Ranjithkumar
  • 669
  • 5
  • 15
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
  • 8,640
  • 4
  • 28
  • 43
  • Yeah definitely. Getting it working but maybe not 100% correct would be good enough as long as we can get some sort of system in place. – AndyOHart Feb 07 '13 at 17:00