A 5K Java Guitar Tuner

So after writing a 5K Twitter Client, a 5K TODO app and a 5K Fullscreen Text Editor I thought that’d be enough example apps to get things started. However while adding a list of app ideas to the 5K App page I suddenly decide that one of the ideas really appealed. The idea was to write a guitar tuner, that would listen on the computers mic and show the user what pitch it thought the note played was. I’m not much of a guitar player, but when I do play it’s always nice to have a guitar that’s in tune. As I’m unable to tune a guitar by ear I rely on guitar tuners and I always seem to be out of the 9V batteries my electronic tuner needs. Given my laptop is usually nearby, being able to use it as a guitar tuner would be most useful.

Check out the video below or download 5KTuner for yourself (requires Java):

The app is more or less non-interactive. It simply starts up tries to work out what note is being played. It shows a simple slider that moves according to what note is being played and indicates how close to that note you are. In addition the raw frequency (in hz) is shown, as well a chart that shows the autocorrelation function of the input sound. A red line is drawn on this chart when a “valid” note is heard and indicates the wavelength of the overall frequency.

The vital statistics are:

  • Final (obfuscated) app size: 3538 bytes
  • Final (un-obfuscated) app size: 4649 bytes
  • Final Source-code size: 8532 bytes
  • Lines of code: 245

This was my first foray into audio programming and I’m grateful that in the end it wasn’t too tricky. Initially I’d been looking into using the Fast Fourier Transform to work out the frequencies, but the autocorrelation function was much easier. It’s essentially just two for loops and a bit of maths. You listen for a certain amount of audio and then compare the first half of the sample with itself, but shifted. By working out the amount of shift where the difference is smallest you can find the overall frequency of the sample.

It could probably be made more robust, but if you want to see how it works (or improve it) you can download the source code.

35 thoughts on “A 5K Java Guitar Tuner

  1. Hi John

    This is a great little app – and it seems to run a lot faster than some of the ones using the FFT method.

    What I would like to ask you, is would it be possible to get this running on a web browser? I am looking to get something like this on a guitar website I am putting together?

    I am proficient in web languages, but Java is toally new to me. My only real programming experence is some VB and C++ a few years back.

    Would you be able to help me out? I will of course sing your praises over the new website 🙂

    All the best,

    Alex Holsgrove

  2. Hi Alex,

    Glad you like the app. It’s really just a knock-around thing, but it might be possible to get it running in a web browser. There are two options:

    • Java Applet
    • Java Webstart

    They both have constraints on what kind of things you can do. I’m not sure whether one can access the microphone from a java applet (as it may be outside of the security sandbox), but I imagine it’s possible with Java webstart.

    You’re certainly welcome to take the source and do what you want with it – consider the code as in the public domain. Though some level of attribution would be nice. I might have a go at getting it turned into a webstart app, but can’t promise any sort of timescale on that – plenty to be getting on with at work and otherwise.



  3. Hi John, thanks for the reply.
    I am sure it’s possible to access the system sound as I have seen a similar application here: http://www.seventhstring.com/tuner/tuner.html
    It’s a bit over the top, but proves that it can be done.

    I can appreciate you having a lack of time to build the web version – I know that feeling all to well.
    If you do make any progress, I’d love to hear for you [webmaster at blind-summit.co.uk]
    Thanks again for making the source available.


  4. John !
    I thank you very much.. I was looking a simple way to analyze notes from sound and you just handed it to me on a golden plate.

    I was so frustrated of trying to figure out how to use the FFT and what the hell is a complex number (I’m no mathematician that’s for sure 😉

    Thank you,
    and have a good day,
    Mikronator from Israel

  5. Great application John! Very well done here.

    Are you able to provide me with an email address I can use to contact you on? Would like to request permission to make ues of your code in a non-commercial application I’m developing.

    Thank you

  6. Hi John,

    This is a great little app, its helped me a lot to understand frequency analysis. Do you know if the autocorrelation function can be used to analyse the amplitude as well?



  7. Hi Mike,

    well the amplitudes are already present in the original data. The auto-correlation function is trying to take a set of amplitudes and work out their frequency. Analysing the amplitudes should be much more straightforward…


  8. Hi John,

    Do you know how I can extract the amplitudes from the original data? I’m a bit of a novice at signal processing. Any help you can give will be much appreciated.



  9. Hi Mike,

    the actual data being read by the call to targetDataLine.read in the code just represents the pressure at a given point in time (so it’s actually the pressure on the microphone at that instance converted to a voltage). The raw data is just a bunch of bytes, so if you us a 16bit sample size (as my code does) you need to join every pair of bytes together to give you one pressure value. That’s the bit with the comment “convert two bytes into single value”.

    From there for example, the amplitude of the overall sample can then be worked out by just finding the difference between the highest and lowest values.

    Be aware though, this was my first audio programming experience, so you’ll want to double-check everything…



  10. @corly – I wouldn’t worry about the size too much. Your tuner seemed to load up fast enough. Though, as yours is in Flash you’ll need to wait for the latest version to come out. At the moment the current version can’t access the microphone, but that’s set to change with the new version.

  11. Hi John,

    i am doing a project for my studies and i needed some code to get the frequency out of the mic. so i found your tuner which helped me a lot, but there’s one little thing i have to get rid of though: theres always a delay of about half a second. could you tell me how i could do that. i can send you my project if it helps. thanks in advance

  12. Hi Kadir,

    there is an artificial delay that I introduced to stop the app chewing up 100% CPU, so maybe start by removing/reducing that. Look at line 241 in GT.java and you should see a call to Thread.sleep() being passed a value of 250ms (quarter of a second).


  13. Hi

    I’m new to java, what did you use to compile the jar program?
    Also would this work on a mobile, this is my eventual aim.


  14. Hi James,

    I just manually compiled it using javac, then ran it through the jar command. There’s a build.sh script with with source code for compiling it – which I think contains an obfuscation step too.

    In principle you could adapt this to work on mobile – assuming you can access the microphone. However this really isn’t a very efficient way of pitch detection. For that you probably need to look at something like the “Fast Fourier Transform”, but that’s quite a bit more complex.


  15. Hi John,
    I found your code to this and found it to be extremely helpful for a project I am doing for a class. My project is a game-like program that will listen to guitar input and compare it to what is supposed to be played. Could I have permission to use your code and do you have any ideas on my project?


  16. Hi Josh,

    there’s some license info over here to look at:


    Basically yep, you can use it, just make sure you give me some attribution.

    You may find though, that you will need to use the Fast-Fourier Transform to get your app working efficiently. However the correlation technique in my code will at least let you get started.



  17. Hi John!first of all thanks for this good job.second thing ,and the reason why i write.I would like to use your code in my school work,is it possible?of course you will be quoted and everything will be covered with proper licences. But i have a question number two. Do you think it will be hard to make app that will write down times when guitar player wasnt accurate in his play (little bending of notes,etc..)? then it will make some output file,and it would be the tool for guitar players to analyze their practicing and it should motivate him/her to practice more. i am begginer at the programming,but this doesnt seems so hard.thanks for reply. Jakub

  18. Hi Jakub,

    yep of course you can use this code – as long as you do stick to the licence: http://www.psychicorigami.com/license/ and of course as long as that’s ok with your teachers.

    As to question two – it’s certainly possible, but it’d probably be a bit complex. I’m also not sure whether the tuner would react quick enough. As it’s only meant for tuning a guitar it’s only really designed to cope with repeatedly playing the same note. If the guitar was played very quickly (or more than one note was played) it wouldn’t get all the notes. You’d also have to record all of the notes and when they were played, so you could then compare them with the “ideal”.



  19. Hi, just as a project for fun I’m making various audio utilities in java, I one day hope to combine them into a simple DAW of some sort. I am designing my own GUI and adding to it but I am using your math to find the relation between frequencies and note names, it wasn’t in the source code so I wanted to know how you’d like to be credited. Like I said its for fun and I don’t ever plan to sell it but I may post the source code somewhere and I thought you’d like to be credited for your work.

  20. Hi Kevin,

    credit-wise just mention my name and maybe put in a link to the blog post? That’s more than enough. Glad you found it useful.



  21. Hi Mr.John,

    i’m a student and learning android application.
    your video really interesting, and i want to know how to open your source code in eclipse environment.

    Thanks & be my teacher.

  22. Hi Jack,

    I depends on what you’re doing, but I’d probably just start a new project in eclipse, then copy GT.java from the source tar.gz file (http://www.psychicorigami.com/source/5KTuner.tar.gz) into the project’s directory. That should be enough really.

    Just a warning though – I’m not sure this code will work on Android as is. It’s also not “good” code – as it was written to optimise for code size. It’s just a curiosity that you made find useful to look at.



  23. It works like a charm on java, thanks Mr.John.
    I’ll wait for android version as i tried that didn’t work on.

    keep your great job,

  24. Sir, thank you for your source on giving us idea, this is really cool,. . . I am analyzing on how the use of FFT algorothm to create a sound frequency and such, I am not much of a mathematician, but this is really helpful, thank you sir,. . .

  25. Thank you for this! I am trying to run it but I get the following error:
    “Exception in thread “main” javax.sound.sampled.LineUnavailableException”

    do you have any suggestions for how to fix this? I thought it could be a microphone issue, but I have a Microsoft LifeCam plugged in to a USB port.

  26. Hi Vicki,

    not really 100% sure why that’s happening. Possibly it means you’ve got another application open that’s using the microphone. Or the app might not be detecting the microphone.

    To be honest though, this really was just a bit of demo code. I wouldn’t necessarily expect it to be very robust.



Comments are closed.