Outils pour utilisateurs

Outils du site


issue100:programmer_en_python

Ceci est une ancienne révision du document !


First, let me say Happy 100 to Ronnie and the crew. It’s a privilege to be part of this milestone.

This time I thought that I’d share some information on my new obsession. I’ve started repairing and building stringed musical instruments like guitars and violins. Believe it or not, there is a lot of math involved in musical instruments. Today, we will look at some of the math involved with the length of strings and where the frets should be placed on the fretboard.

Take a look at the picture of the guitar. I annotated various items in the image. The important things to look at are the Nut near the top of the fingerboard, the Frets, the Bridge near the bottom, and the white “line” within the bridge called the Saddle. The purpose of the frets is to create a perfect spot to change the length of the string to create a note that is in tune. The positions of these frets are not arbitrary, but mathematically determined.

Now, the physics of vibrating strings tells us that if you half the vibrating string length of a theoretically perfect string, you will double the frequency of the vibrations. In the case of a guitar, this string length is between the nut and the saddle. This distance is referred to the Scale Length of the guitar. The half-point that allows for the doubled frequency is fret # 12. If correctly done, just by lightly placing your finger on the string at this location, you get a pleasing tone. There are a few other positions that this will happen, but the 12th fret should be the perfect location for this doubling, making the note go up one octave.

Different scale lengths will create different feel and tones. For example, guitars like the Fender Stratocasters® have a scale length of 25½”, which produces a rich and strong bell-like tone. On the other hand, Gibson guitars often use a scale length of 24¾”. This creates a lower string tension which makes an easier playing feel and a warmer tone. Other guitar manufacturers decided that a scale length of 25” makes a clearer tone than either of the other two “standard” scale lengths.

So with the ability of a guitar maker to come up with their own scale length, the spacing of the frets will have to be recalculated. This is something that luthiers (guitar makers) have been dealing with for hundreds of years.

In the past, there was a technique called the rule of 18 which involves successively dividing the scale length minus the offset to the previous fret by 18. While this kind of worked, the tones were off, the higher up the fingerboard the player went. These days, we use a different constant. This constant is 17.817. By using this “new” constant, the 12th fret or octave is at the exact position to be half the scale length of the string.

Now, these calculations are easy enough to do by paper and pencil or even a simple calculator, it’s just as easy to create a Python program to do the calculations for us in just a second. Once you have the positions, you simply saw a slot for the fret at the correct positions and then hammer in the frets.

So, let’s take a look at the program.

We want to create a program that will prompt for the scale length of the guitar (or bass), do the calculations and then print out the distances. The calculations and all returned lengths are all in inches, so all our friends that use metric measurements, please add the proper conversion calculations. After almost 5 years, you should be able to do this with ease.

We don’t need to import any libraries for this so we will start off by defining a couple of variables.

ScaleLength = 0

CumulativeLength = 0

Next we will create a routine (top right) that will be called repeatedly as we “travel down” the fingerboard. We will pass two values into this routine. One is the scale length and the other is the cumulative distance from the nut to the previous fret.

In this routine, we take the scale length, subtract the cumulative distance and assign that value to BridgeToFret. We then take that value, divide it by our constant (17.817), add back in the cumulative distance and then return that value to our calling routine. Remember, we could simply have returned the calculated value without assigning it to a variable name. However, if we ever want to inspect the calculated values, it’s easier to do if we assign the value before we return it.

Now we will make our worker routine. We’ve done this kind of thing many times in the past. We will pass it the scale length and it will loop for up to 24 frets (range(1,25)). Even if your project has less than 24 frets, you will have the correct positions of all the frets you do have. I chose 24 because that’s the maximum of frets for most guitars. When we get into the loop, we check the fret number (x) and if it is 1, we pass the cumulative length as 0, since this is the first calculation. Otherwise, we pass the last cumulative length in and it becomes the result from the calculation routine. Finally, we print each fret number followed by a formatted version of the cumulative length.

Finally, we have the code that does the prompting for the scale length. I’m sure you will remember the format for the raw_input routine, since we have used it so many times before. Something you might not remember: that raw_input always returns a string, so when we pass it off to the DoWork routine, we have to pass it as a floating point number so the routine will work correctly. Of course, we could simply pass it as a string, but we would have to deal with the conversion in the DoWork routine.

ScaleLength = raw_input(“Please enter Scale Length of guitar → “)

DoWork(float(ScaleLength))

You might wonder what good this program will do if you aren’t going to build a guitar from scratch. It can be valuable when you're looking at buying a used guitar or trying to tweak a guitar with a floating bridge. Also, if you are a guitar player, this might have been something you didn’t know about guitars.

Of course, the code is available from pastebin at http://pastebin.com/A2RNECt5.

issue100/programmer_en_python.1440940717.txt.gz · Dernière modification : 2015/08/30 15:18 de auntiee