If you play a string instrument and if you like physics and programming and if you ever wondered if C♯ is really different to D♭, then this article is for you. Even if you think that C♯ is only a programming language and if you never heard about another language called D♭, you might consider reading on to see what it's all about.
I am presenting a small application which brings one or more musical notes to both the speakers and the monitor of your computer. You can change the pitch of the notes in either a free way or at specific intervals like octaves, fifths or thirds. The interesting thing here is how several notes interact with each other. Some combinations sound nice and others sound really bad, which is also revealed by the graphical representation.
The idea for making this program is based on a weird experience that I made ages ago every time when tuning my first guitar. Whenever I tuned it perfectly for a song with mainly G, C and D chords, I had to adjust some strings when it comes to a song in E, A and B.
At that time, I used to think that my cheap guitar was the reason for this behavior, but later on, when I was able to buy better guitars, the phenomenon still continued. It even appeared on guitars far outside of my budget, so the reason must be somewhere else.
I found the reason years later, when I started learning the violin. Since this instrument does not have frets like a guitar, one of the challenges is to find the right position for your left hand fingers. For example, the note B can be found at the first position of the A string, but where exactly is that first position?
To my surprise, there is more than one position! It depends on which B you want to play. If want to play a B which fits to G major, you will have to put your first finger a bit lower than for a B which fits to E major. If you have a violin, give it a try! Play the open G and D strings and find the perfect B on the A string. Now play this B together with the open E string. It doesn't sound right, does it? You have to move your finger a bit up to make it sound perfect.
But what makes an interval sound perfect? To answer this question, let me start with some background about a single note or tone. I try to be brief. You will find a lot more information on Wikipedia.
A Pure Tone
Sound is a periodic change in pressure propagated in a medium such as air. Our ears are able to detect these changes and if they are fast enough (but not too fast), we hear something.
The membrane of a loudspeaker produces air pressure changes when it moves periodically back and forth. If it does so 100 times a second, we will hear a very low tone, 1000 times a second will produce a high tone and 10.000 times a second will end up in a very, very high tone which most of us cannot hear anymore.
So the pitch of a tone is determined by the frequency of pressure changes. Another feature is the loudness of a tone. It is related to the amount of change, which for a speaker means how much the membrane goes back and forth. The maximum displacement is called the amplitude of a tone.
And this is all which makes up a pure tone. It has a frequency and an amplitude. A very simple way to calculate periodic changes mathematically is given by the sine or cosine functions. The amount of change at a given time
t can be specified by this equation:
delta = amplitude * cos(2PI * frequency * t);
And this is how it looks like for a frequency of 100 and an amplitude of 1:
Not very exciting, and it also does not sound that way, but that's a pure tone! The pressure is changing periodically 100 times a second (or at a frequency of 100 Hertz) so the graph shows peaks at a distance of 0.01 seconds.
Two Pure Tones
Consider two speakers, each one emitting a pure tone. The air pressure changes of both speakers need to be combined somehow, because the pressure can have a single value only at a given time and position. It turns out that the combination is a simple addition, so it looks like this:
delta1 = amplitude1 * cos(2PI * frequency1 * t);
delta2 = amplitude2 * cos(2PI * frequency2 * t);
delta = delta1 + delta2;
This simple addition of two cosine functions has some interesting aspects depending on the frequency of the two tones.
In the trivial case, where both frequencies are the same, the tone just gets louder. The peaks and troughs of tone 1 and tone 2 always meet in perfect harmony, making the result twice as high or low as before.
Another simple case is given when the second frequency is twice as high as the first one, let's say 100 Hz and 200 Hz. The peaks of tone 1 have a distance of 0.01 seconds, or 10 milliseconds, and they now meet the peaks of tone 2 which have a distance of 5 ms. At time zero, the first peak of tone 1 exactly matches the first peak of tone 2, but 5 milliseconds later, the second peak of tone 2 meets the first trough of tone 1, which raises the overall signal at that time. If both amplitudes are 1, the result looks like this:
If we listen to this signal, we will clearly hear two tones and I guess most people would agree that they sound together well. This changes drastically if the two frequencies are rather close to each other. For example, if we change the frequency of tone 2 to 110 Hz, there will be peaks in tone 2 roughly every 9 ms. Every 11th peak of tone 2 will exactly match every 10th peak of tone 1. This happens every 100 ms as you can see in the following picture. The color of tone 1 is red, the color of tone 2 is blue:
You can also see that at 50 ms and 150 ms, a peak of tone 1 exactly matches a trough of tone 2, which ends up in a sum of zero. All in all, the sum of the two tones looks like this:
Or if we look at it for a longer period:
If we compare this resulting signal with the signal before, where we could clearly identify the two initial signals of 100 Hz and 200 Hz in the sum, we now see something which looks like a single tone, whose maximum amplitude is oscillating with a frequency of 10 Hz. And if we listen to this mix of 100 Hz and 110 Hz tones, we will hear a single tone of 105 Hz, whose loudness is changing 10 times a second.
The same result is obtained if we use the following trigonometric identity:
cos(a) + cos(b) = 2 * cos((a + b) / 2) * cos((a - b) / 2)
If we choose both amplitudes of our 100 Hz and 110 Hz signals to be 1, the result can be written as:
delta = cos(2PI * 100 * t) + cos(2PI * 110 * t)
Using the above identity, we get:
delta = 2 * cos(2PI * 105 * t) * cos(2PI * 5 * t)
So the overall signal sounds like a 105 Hz tone whose amplitude is modulated by a 5 Hz cosine (which sounds like a 10 Hz vibrato, look here for details). Whether this sounds nice or not is a personal opinion, but for sure it doesn't sound as straight as the mix of 100 Hz and 200 Hz.
Real instruments do not produce pure tones but a mixture of tones which build a complex sound. For string instruments, this mixture is built from a tone with a base frequency and so called overtones with frequencies which are integer multiples of the base frequency. These overtones are also called harmonics.
So if we pluck a string with a base frequency of 110 Hz, which could be the A string of a double-bass, we will not only hear the 110 Hz tone, but also tones with 220 Hz, 330 Hz, 440 Hz, 550 Hz, 660 Hz, 770 Hz and so on. The loudness of these overtones will vary from instrument to instrument and in general will decrease with increasing frequency.
Enough Theory For Now!
If you start the application, you will hear and see a pure tone of 110 Hz. You can add up to 10 more tones by clicking on the volume control to the right of the frequency control. Change the volume with the mouse wheel.
The 11 tones are tuned to build a harmonic series, ranging from 110 Hz to 1210 Hz. For now, don't change the frequencies, but just add the second tone with a frequency of 220 Hz by clicking on the second volume control.
The two notes we hear now sound very similar. In fact whenever the frequency of a tone is multiplied by 2, our human ear tends to hear both notes as being essentially "the same." So if you now also add the two tones with 440 Hz and 880 Hz, you will more or less hear the same tone 4 times at different pitch levels.
Octave or Perfect Eighth
The interval between two tones with a frequency ratio of 2:1 is called octave. It is the most basic interval in music. Many musical scales (including our traditional Western scale) split an octave into seven different levels or notes, giving each one an identifying letter. Since we started with an A (110 Hz), it is most natural to continue like this: B, C, D, E, F, G. The next or eighth note of the scale again is an A, but this time with a frequency of 220 Hz.
In order to distinguish between the different A's, the following notation is used: the capital letter A means the note with 110 Hz, the octave at 220 Hz gets the small letter a, and from here on, every octave is denoted with prime symbols: a' = 440 Hz, a'' = 880 Hz and so on.
Perfect Fifth and Perfect Fourth
Let's come back to the application. You still might be listening to the four A's (which sounds like a pipe organ) but now add the tone with 330 Hz. This is definitely something new!
To isolate this tone, do a right-click on its volume control. All other tones will then be muted. Now add the tone with 220 Hz again and you should see something like this:
Enlarge the x-axis range by using the mouse wheel - while the mouse is over the plot area - to something about 0.5 seconds:
You can see - and hear - that this new interval is also very straight or clean, which is also called consonant. Besides the basic octave with its frequency ratio of 2:1 this is the second simplest interval, and it has a ratio of 3:2. Every third peak of the 330 Hz tone matches exactly every second peak of the 220 Hz tone.
In our Western music scale, this new tone is the fifth note (i.e., E if we start with A), and that's why this interval is called perfect fifth. It is called perfect because it is traditionally considered perfectly consonant.
If you now mute the 220 Hz tone and add the 440 Hz tone, you will hear the complementary interval to the perfect fifth, which is the perfect fourth. It is called complementary because it adds up to an octave with the perfect fifth, and again it is called perfect because of its perfect consonance. The frequency ratio of a perfect fourth is 4:3.
The fact that fifths and fourths are complementary also can be shown in a mathematical way like this: if you have a note with frequency
f and you raise it by a fifth, you'll get a note with frequency
f*3/2. Raising this by a fourth ends up in
(f*3/2)*4/3, which shortens to
f*4/2 or simpler
f*2, which is an octave.
The next tone in our harmonic series has frequency of 550 Hz. If you add this to the 330 Hz and 440 Hz tones, you'll hear a perfect A major chord. Very straight and very clean. Remove the 330 Hz tone and you'll see this:
Since the new note is the third one in an A major scale, the interval you hear now is called major third. Its frequency ratio is 5:4 and in the chosen scale the note is a C♯.
The remaining harmonics (660, 770, 880, 990, 1100, 1210) are either octaves to lower harmonics (660 = 2*330, 880 = 2*440 and 1100 = 2*550) or they belong to new notes (770, 990 and 1210).
If you select the three tones with 440 Hz, 550 Hz and 660 Hz (a perfect A major chord) and now add the 770 Hz tone, you might agree that this sounds familiar. In fact, we now hear a perfect A7 chord. It is worth noting that this new tone, which has ratio of 7:4 to the base note of A, is not part of the A major (nor A minor) scale. It is a little bit lower than a G and is called the harmonic seventh.
The next harmonic which is not an octave to a fifth or third is the one with 990 Hz. If you add this to the previous A7 chord, it might also sound familiar to you. The new tone is the 9th note in the A major scale, which also is the octave to the 2nd note, so in the end it's a B, and the chord we hear is A7/9.
Now the last tone with 1210 Hz is somewhat strange. Although it is a natural harmonic to A (110 Hz), it sounds awful together with a'' (880 Hz). That's surely because it's right between the fourth (1173 Hz) and the fifth (1320).
If you now activate all overtones to our base note A, you will hear a very individual sound which also looks very interesting:
How to Change the Frequency
Before moving on to the subject of this article, let me tell you how to change the frequency of a tone. First of all, you can enter any integer number into the edit box. But for sure, you can also change the frequency with the spin controls to the right.
The amount of change when clicking spin up or spin down depends on the numbers shown in the two edit boxes in the top right corner of the application:
The default values of 1/0 will change the frequency by 1 Hz. You can enter any numbers here and the spin controls will change the frequency by the ratio of these numbers. So if you enter 2/1, the amount of change will be an octave or 8th. The radio buttons are used to fill the ratio boxes with predefined numbers: 3/2 for a fifth, 4/3 for a fourth, 5/4 for a major third and so on.
The Syntonic Comma
A comma in music theory is a small interval between two notes. There are different types of commas and in order to show you the syntonic comma, let's tune 4 tones in the way that violins are tuned.
To do so, enter 440 into the first 4 edit boxes, because that's the frequency for the A string. Also click the radio button for a 5th, so the ratio boxes show 3/2, because violins are tuned in fifths. After setting the volume to 0.5 for all 4 tones, it should look like this:
Now change the frequency of the second tone by clicking the spin down control, which lowers the a' by a fifth, ending up in a d'. Do the same twice for the first tone, so this becomes a g. Raise the frequency of the fourth tone by clicking the up control, and this will be an e''. We now have a perfectly tuned instrument. Mute all tones except the first two and you will hear a perfect fifth above G:
Now we want to add a just major third to this, which for G is a B and can be found at the first position of the A string as I mentioned before. In order to find this note, lower the third tone by two fifths (so it's a g), raise it by an octave (so it's a g') and then raise it by a major third (so it's a b'). You now hear a perfect G major chord:
And Now For the Key Point
Mute the first two tones and add the fourth one. Since this is tuned to e'', it should sound perfectly consonant with the b' from below, because B is the fifth to E. But it doesn't sound right and if you enlarge the x-axis to about half a second, you can also see that this interval is not just:
To find the just perfect fifth to e'' on the A string, enter 660 (= e'') into the fifth edit box and raise this tone by a 5th (= b''). Now lower it by an octave and we will have a b'. Mute the 'wrong' b' and select the 'right' one and you should hear and see a just perfect interval:
The Difference Between these Two B's is the Syntonic Comma
It is also called Comma of Didymus because it is the amount by which Didymus corrected the Pythagorean major third (81:64) to a just major third (5:4).
And This Is Why My Guitar Is Always Out Of Tune
As you can see (and hear) from the above, there is more than one B. At least, if you prefer just intonation. And the same is true for all other notes. The pitch of a note always depends on its relation to a fundamental note or key.
And this is why the question "Is C♯ is different to D♭?" does not make sense! There are many C♯'s and many D♭'s depending on the key for a piece of music.
And this is also why my guitar is always out of tune! Since I do not want to re-tune my instrument for every song, I do the same trick that is being used when tuning a piano: equal temperament. When using this system, only the octaves will sound perfect while all other intervals are a bit out of tune. The big advantage of this tuning is that you can use it for all keys. The differences to just intonation are audible but will not hurt the ears! Modern tuning devices are based on this system and therefore it's a good idea to use such a device when tuning guitars.
There is not much to say about the code. A simple WPF application based upon the MVVM pattern. Many, many thanks to the creators of NAudio and OxyPlot, because you saved me from writing tons of code!
But wait! One thing should be mentioned. The edit boxes for the frequencies show rounded numbers only. The fifth below 440 Hz has a frequency of 440 * 2/3 = 293.333333 Hz but will be displayed as 293 Hz. So don't be surprised if you hear a beat when adding a manually entered 293 Hz tone to it.
- 26th April, 2017: Initial upload
I started programming in Basic, Pascal, Fortran and C in the late 1980s during my last semesters at the University of Bonn, Germany, where I studied Physics. As a professional software engineer I moved on to C++ and C# in the field of scientific data acquisition, data analysis and - my favourite - data visualization.
From the very start of my life as developer I have been a fan of graphics, especially 3D graphics. I have been working with OpenGL, XNA and WPF 3D. Planning to start with SharpDX in the near future.
Besides programming I love making music (guitar and violin), doing sports (rock climbing and volleyball) and spending time with my beloved family.