Saturday, February 9, 2008

Tuning a guitar with(out) the help of the computer

If you are a beginner trying to learn guitar playing, one of the first things you need to learn is to tune your guitar. There are many sources from which you can read how to properly do this - just Google for them. The one I think is a nice cover of the topic is this one on wikiHow, but choosing any of the other ones will probably be as good.

Some of the above Web pages describe the process of tuning the guitar without the additional devices, called tuners. I will stick to the usual way most of the people do it when they are playing the guitar for their friends - tuning by ear. This can be hard to learn if you are just starting. For example, if we follow the wikiHow instructions from the above, we come to the step 6. which, among other things, says:

When the string is slightly out of tune, the E from the guitar will combine with the E from the sound source (i.e. piano), and cause the sound to "waver" in pitch.
If you are a beginner, you will probably have hard time realizing how to find that "waver" sound. Probably the best way to learn this is to have somebody with you to point at the sounds you should be listening for.

Computers can help us here. I found an excellent thing on the Internet, called csound. It is open source, you don't need to install it (just unzip) and it seems very powerful. I didn't need all of that power - just a basic oscillator and output to a WAV file - which it supports out of the box. I am on Windows right now, so I did not try the latest version at the time of the writing (5.08Beta), since there was not a compilation for Windows - I used 4.23f13gbs. There was, however, no manual for this version - I used the manual for 5.08, which proved useful for the basic needs I had.

It has a programming language, which seemed to me as an XML on first sight, because of few of the "tags". Here is a sample csound program, which plays a sine wave for 5 seconds:
<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
; Audio out Audio in No messages
-odac -iadc -d ;;;RT audio I/O
; For Non-realtime ouput leave only the line below:
; -o oscils.wav -W ;;; for file output any platform
</CsOptions>

<CsInstruments>
; Initialize the global variables.
sr = 44100 ; Audio sample rate.
ksmps = 10 ; Number of samples in a control period.
nchnls = 1 ; Number of channels.

; Instrument #1 - a fast sine oscillator.
instr 1
iamp = 10000 ; Amplitude.
icps = 440 ; Frequency in Hz.
iphs = 0 ; Start phase.

; Make and ouput the fast sine oscillator.
a1 oscils iamp, icps, iphs
out a1
endin
</CsInstruments>

<CsScore>
i 1 0 5 ; Play Instrument #1, starting at time 0 for 5 seconds.
e ; End the last section of the score.
</CsScore>
</CsoundSynthesizer>
one-ins.csd

The file is almost copied from the manual. To find it, go to the examples folder of the manual (HTML version of the manual) and open oscils.csd. I added a few minor comments while exploring the manual just to make it easier to follow it, but it was like 10 minutes to understand the options and the commands. You can see almost everything in the comments. One important thing is the -o option in <CsOptions> tag. It allows the user to save the file instead of outputting the sound directly to the speakers.

Now, I changed the above to the following:
<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
; Audio out Audio in No messages
; -odac -iadc -d ;;;RT audio I/O
; For Non-realtime ouput leave only the line below:
-o 440-445.wav -W ;;; for file output any platform
</CsOptions>

<CsInstruments>
; Initialize the global variables.
sr = 44100 ; Audio sample rate.
ksmps = 10 ; Number of samples in a control period.
nchnls = 1 ; Number of channels.

; Instrument #1 - a fast sine oscillator.
instr 1
; 440 Hz.
a1 oscils 10000, 440, 0
out a1
endin

; Instrument #2 - a fast sine oscillator.
instr 2
; 445 Hz.
a1 oscils 10000, 445, 0
out a1
endin
</CsInstruments>

<CsScore>
i 1 0 5 ; Play Instrument #1, starting at time 0 for 5 seconds.
i 2 0 5 ; Play Instrument #2, starting at time 0 for 5 seconds.
e ; End the last section of the score.
</CsScore>
</CsoundSynthesizer>
two-ins.csd

Now, if you save the above to two-ins.csd and execute the above file with:
csound two-ins.csd
you will get a nice 5-second 440-445.wav file. Play it. You will see a (pretty annoying) "waver" sound. That is it. What this means is that the two sinusoids of the frequencies 440 Hz and 445 Hz are close. The closeness makes them output this sound - why is another topic, but you can read about it here.

There is no need for somebody else to tell you the above sounds bad to your ear. Let's make a few more files - change the 445 Hz in the line: a1 oscils 10000, 445, 0 to the following numbers (one for each run):
443
441
440.6
440.1
You might prefer to uncomment:
-odac           -iadc     -d     ;;;RT audio I/O
instead of the
-o 440-450.wav -W ;;; for file output any platform
so you don't make the files, but listen to it directly.

If you "take a hear" at the results, you might notice that the more the two sounds become closer, the less you hear the annoying wobble sound. That is all you need to make your guitar sound well, to tune it. You can now just follow the wikiHow guide. You still need a 440 Hz tone as a reference (the replacement for the piano in the wikiHow guide) - you can use the first file mentioned here to generate a WAV.

For those who don't want to install csound just for this, I have made the WAVs mentioned here ready for the download, together with the actual scripts (for those who want to mess with csound). You can get them here.

Obviously, there are people (like me) who just like everything done by the computer if possible. In that case, I would recommend the following application: AP Guitar Tuner 1.02. It is freeware and it just rocks. Plug in your guitar or, if you have an acoustic guitar, move the microphone close to your strings and pluck one by one until you get the gauge is at 0. You can even tune it non-standardly if you want (i.e. not the standard EBGDAE tuning).

Happy tuning!

No comments: