BRIAN YU: Finally, let's take a look at frequency.
Frequency is going to be a function that you'll
implement in helpers.c that takes as input a string, which we'll call note,
and output an integer.
So what is that note going to look like?
Well, the note is just going to be a string that might be
something like A sharp 4, for example.
A note, possibly an accidental, and then an octave number.
But if there is no accidental, that note might just be A4, for instance.
So what are you going to do with that note?
Well first, you're going to parse the string and figure out its note
and it's octave.
So in the case of the string A sharp 4, the note is A sharp
and the octave is octave number four.
But you'll need to figure out where in the string the note part is
and where in the string the octave part is.
Next, you'll calculate the frequency of the note in the given octave.
In music, we say that every note has a different frequency, a different pitch,
that makes each note sound different from one another.
And those frequencies are generally represented in a unit called Hertz.
More on that in just a moment.
And finally, you'll return that frequency as an integer value in Hertz
back to the user.
So how do we figure out what the frequency is of any given note?
Well, the world is sort of standardized upon the idea
that the note A4, or A in the fourth octave,
will have a frequency of 440 Hertz.
So that's something that you can take for granted.
If the user passes in A4 into your function,
then the output is just going to be 440 as the return value.
But what if the note isn't A4 and it's some other note?
Well, generally when we're talking about frequency, we can follow these rules.
If A4 is 440 Hertz, then for every semi-tone up that we move,
remembering that a semi-tone is just a movement of one key on the piano
keyboard, for every semi-tone up, we move,
we're going to multiply the frequency by 2 to the power of 1/12.
And likewise, for every semi-tone down we move,
we're going to divide the frequency by 2 to the power of 1/12.
Let's take a look at what that actually looks like from the perspective
of the piano keyboard.
So we know that A4 is 440 Hertz.
And that's just the standard.
But what about one semi-tone up from it, A sharp 4, or equivalently B flat 4.
Well, all we're going to do there is remember
that when we move up one semi-tone, we take the frequency
and we multiply it by 2 to the power of 1/12.
So if we take 440 and multiply it by 2 to the power of 1/12,
we get about 466 Hertz as the frequency for A sharp 4.
What about one more semi-tone up to B4, the next semi-tone after A sharp 4.
Well, we'll take 466, approximately, and multiply that by 2
to the power of 1/12, which is really the same thing as taking
440 and multiplying it by 2 to the power of 1/12, twice,
which is also the same thing as taking 440 and multiplying it to 2
to the power of 2 over 12, which would come out to be about 494 Hertz.
Likewise, if we were to move one semi-tone up from B4 to C5,
remembering that new octaves always start with C,
we get 440 times 2 to the 3 over 12, which is about 523 Hertz,
and so on and so forth.
The next note is going to be multiplying 440 and by 2 to the 4 over 12,
then 5 over 12, 6, 7, 8, 9, 10, 11.
And finally, when we hit A5, exactly one octave above A4,
we've now moved 12 semitones away, or a full octave.
And what we get is 440 times 2 to the 12 over 12, which is just 2 to 1, or 2.
And when we multiply 440 by 2, we get 880.
So notice here that A5, exactly one octave above A4, all we had to do
is take A4's frequency, 440 Hertz, and multiply it by 2
to get 880 Hertz for A5.
So what does this actually mean in terms of frequency?
Well, if A4 is 440 Hertz, then any time we want to take a note
and go up an octave, we multiply it by 2 to the 12 over 12,
which is just the same thing as multiplying it by 2.
And likewise to go down an octave, we just take the frequency
and divide it by 2.
And this doesn't just apply to A4, given any note,
if you want to move it up an octave, all you have to do is take its frequency
and multiply it by 2.
And to move it down an octave, you just take its frequency and divide it by 2.
So now let's think about how we're going to take a note represented as a string
and figure out what the letter of the note is, what it's accidental is,
and what it's octave is.
So if we have a note represented as a string that can take any form of A4,
to B flat 4, to C sharp 5 or anything of the like,
but we know that the note is going to be a string
and the first character of the note is always
going to be the letter of the note, either A, or B, or C, D, E, F, or G.
That much we can rely on.
The second character of the note depends a little bit upon what note it is.
If the note is a sharp or flat and has an accidental as a result,
then the second character of that note string is going to be that accidental.
For example, in the string A sharp 5, the second character
is that sharp symbol.
On the other hand, if there is no sharp or flat,
as we might call a natural note, then the second character
isn't going to be the sharp or flat symbol,
but it's going to be the octave symbol.
So for instance, if we had the note C4, where there is no sharp or flat,
C is still the first character, the letter of the note that we want.
But the second character is going to be the octave, octave number four.
Finally, if there was an accidental, sharp or flat,
we still need to figure out what octave it's in.
So if there was an accidental, then the third character of the string
is going to be the octave.
So in the case of a note like B flat 4, B is the first character,
and that's the letter of the note.
Flat, or the represented by the lowercase b symbol, is the accidental.
That's what's going to show up as the second character in the string.
And finally, the third character of the string
is going to be the octave number.
And so now that we understand how to actually represent a note,
let's walk through the steps of how we're
going to go from taking an arbitrary note that could be any note,
and turning it into an actual frequency.
But let's take it step by step.
The first thing that you'll probably want to do
is to implement different octaves of a.
You know that for A4, you can just return the number
440 because we've said that that is the number of Hertz that
represents the note A4.
But what about other octaves of the note A, like A3, or A2, or A5, or A6.
Well, we know that to change the octave, we just need to multiply
or divide by some power of 2.
To get from A4 to A5, we take 440 and multiply it by 2.
To get from A4 to A6, we take 440 and multiply it by 2 twice,
or multiply it by 4.
Likewise, to go from A4 to A3, we'll have to divide by 2
to get 220 Hertz for A3.
And so we can use multiplication or division by some power of 2
to be able to figure out what the frequency of some different octave of A
is going to be.
And finally, be sure that when you do this math,
you're rounding any decimals that you get to the nearest integer.
So now that we've got different octaves of A,
let's think about accidentals, sharps or flats that might modify A,
and how we're going to adjust for those frequencies as well.
So step two is going to be supporting A sharp and A flat.
Well, we know that to make a note sharp, since a sharp just means exactly one
semi-tone ahead of the previous note, all we have to do
is multiply the original frequency by 2 to the power of 1 over 12,
remembering that in order to get from one frequency
to the frequency exactly one semitone above it,
we just multiply by this 2 to 1 over 12.
Likewise, to make a note flat, we'll do the opposite,
divide the frequency by 2 to 1 over 12.
So think about, if we have A, and A is 4 is represented by 440 Hertz,
and we want to represent A sharp 4, example, all we need to do
is take the frequency 440 and multiply it by 2 to 1 over 12.
And likewise for A flat, we would take the frequency 440
and divide it by 2 to the 1 over 12.
But as you do this, also make sure to be able to support A sharp and A flat
in multiple different octaves.
Sure, A sharp 4 is just A4's 440 Hertz multiplied by 2 to the 1 over 12.
But what about A sharp 5 or A sharp 6?
Well, if you remember that in order to go up an octave,
you just multiply the frequency by 2, and to go down an octave
you just divide the frequency by 2, then if you know what A sharp 4 is,
all you need to do is multiply that frequency by 2
in order to get A sharp 5.
So you might think about, first, adjusting for sharps or flats,
and then adjusting for different octaves.
Or alternatively, you could do it the other way around.
First adjust for the octave by multiplying or dividing
by some power of two.
And then adjusting for a sharp and a flat.
And finally, once we've supported A, A sharp, and A flat,
the last step is going to be to support all different kinds of notes, where
we'll use A as our starting point, and figure out how many semi-tones
away various notes are.
And if you take a look at a piano keyboard,
you should be able to figure this out.
If you remember which note has which letter,
you should be able to calculate how many semitones away B is from A,
how many semitones C is away from A, and as well for all the other nodes
that appear on the piano keyboard.
An important thing to remember is that octaves start at C. In other words,
C5 is one semitone above B4.
This becomes clearer if you take a look at a picture of the piano that
represents all the different notes and what octaves they belong,
where you'll notice that the fourth octave ranges from C4 all the way up
to B4.
But as soon as we move from B4 to the note one semi-tone after that,
we arrive at C5, which is the beginning of the fifth octave.
So while it may seem a little bit counterintuitive,
A4 and B4 are actually higher notes than C4,
even though C comes later in the alphabet,
because C marks the start of the octave.
And so bearing in mind which notes belong to which octaves,
and when new octaves are starting, can help you
out when it comes to computing the frequency.
So ultimately, the logic that you're going to want to follow
is to, first, figure out what the string actually represents,
what the letter of the note is, what accidental it has,
and what octave it has.
Then using A4 as your baseline, where you know that A4 is equal to 440 Hertz,
you can adjust.
Any time you move up a semitone, multiply by 2 to the power of 1/12.
Anytime you move down a semi-tone, divide by 2 to the power of 1/12.
And if you ever want to jump a full octave,
remember that to go up an octave, you just take a frequency
and multiply it by 2.
And any time you want to go down an octave, you just take the frequency
and you divide it by 2.
At the end, you'll likely end up with a floating point number
with digits after the decimal point.
But remember that the frequency function is supposed to return an integer.
So once again, make sure you're rounding your number to the nearest integer
in order to make sure that your output matches the desired frequency level.
So those are the steps to figuring out frequency, after which point
you should be able to hear songs and listen to them based on the functions
that you've implemented inside of helpers.c.
On that note, I'll leave you to it.
Good luck.
My name is Brian, and this was music.