In this post I will go through the solution to Problem Set 3 in CS50. In this Problem Set, we start by writing out a text file that we will use to eventually play the happy birthday song. This part is one of the easier of this case. The first character designates the letter associated with that note (i.e. A, B, C, D, etc.). The next character designates the octave of that note. The @ separates the note from the length of that note. If the note is a sharp, we designate that with a # after the letter. Next, all you have to do is make sure that your notes correspond with the ones given for the happy birthday song.

Now we can actually get to some code. In this exercise, we are tasked with finishing the functions that will allow our text to be converted into a .wav file. The first of these that I will cover is the duration. Now, we know from the above text that our duration is represented by a fraction. However, we need to know how many eighths there are in each fraction. The math for this is very simple. First we get the 0th character from our string which will be the numerator. The 2nd character will then be our denominator. Therefore, if we take the number 8 and divide it by the denominator, and then multiply that by the numerator, we will have our number of eighths.

I am going to skip frequency for now and cover that implementation last. For the is_rest function, we are only concerned with knowing whether we have come across a line that is empty. We learned that a string, even one that does not contain a value, ends with the null terminator “\0”. So, with this knowledge we just have to check to see if the 0th character in the string is in fact the null terminator. If it is, we return true, else we return false.

Now for the most complex part of the solution, which is the frequency. The first variable I have set up will get the octave from the string. We know that the octave will always be last, so we can get the string length minus one and this will give us the position of the octave. Next, we declare a double precision variable for the frequency and set it equal to 0.0. The next series of lines may look a little confusing, but this is just a multi-way if statement that will return the frequency based on the letter of the note. You could also implement this as a switch case statement and land in the same place.

The next set of statements loops through the octaves to adjust the frequency. Remember that we are told that A4 is 440 htz, and that our frequency will adjust whether the octave is lower or higher. In this solution, we loop through the octave applying an adjustment for the frequency based on whether it is higher or lower than A4. The final step is to adjust the frequency one last time if there is a flat or sharp designation. We simply look at the 1st element in the string which is where these symbols will be and apply the math that adjusts the frequency. Our final step is to round the frequency (remember it is a double) and return it from the function.

And that is it for Music. I hope that this helped you come up with another solution to this Problem Set. If you found this information useful, please remember to share this post on your favorite social media by clicking on the buttons below. As always…Happy Programming!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s