1 00:00:00,000 --> 00:00:00,499 2 00:00:00,499 --> 00:00:03,052 BRIAN YU: Finally, let's take a look at frequency. 3 00:00:03,052 --> 00:00:05,010 Frequency is going to be a function that you'll 4 00:00:05,010 --> 00:00:10,380 implement in helpers.c that takes as input a string, which we'll call note, 5 00:00:10,380 --> 00:00:12,270 and output an integer. 6 00:00:12,270 --> 00:00:14,770 So what is that note going to look like? 7 00:00:14,770 --> 00:00:17,190 Well, the note is just going to be a string that might be 8 00:00:17,190 --> 00:00:19,530 something like A sharp 4, for example. 9 00:00:19,530 --> 00:00:23,280 A note, possibly an accidental, and then an octave number. 10 00:00:23,280 --> 00:00:28,180 But if there is no accidental, that note might just be A4, for instance. 11 00:00:28,180 --> 00:00:30,270 So what are you going to do with that note? 12 00:00:30,270 --> 00:00:34,500 Well first, you're going to parse the string and figure out its note 13 00:00:34,500 --> 00:00:35,640 and it's octave. 14 00:00:35,640 --> 00:00:39,990 So in the case of the string A sharp 4, the note is A sharp 15 00:00:39,990 --> 00:00:42,300 and the octave is octave number four. 16 00:00:42,300 --> 00:00:45,150 But you'll need to figure out where in the string the note part is 17 00:00:45,150 --> 00:00:48,300 and where in the string the octave part is. 18 00:00:48,300 --> 00:00:52,830 Next, you'll calculate the frequency of the note in the given octave. 19 00:00:52,830 --> 00:00:57,300 In music, we say that every note has a different frequency, a different pitch, 20 00:00:57,300 --> 00:00:59,760 that makes each note sound different from one another. 21 00:00:59,760 --> 00:01:03,870 And those frequencies are generally represented in a unit called Hertz. 22 00:01:03,870 --> 00:01:05,730 More on that in just a moment. 23 00:01:05,730 --> 00:01:09,960 And finally, you'll return that frequency as an integer value in Hertz 24 00:01:09,960 --> 00:01:11,980 back to the user. 25 00:01:11,980 --> 00:01:16,080 So how do we figure out what the frequency is of any given note? 26 00:01:16,080 --> 00:01:19,020 Well, the world is sort of standardized upon the idea 27 00:01:19,020 --> 00:01:22,650 that the note A4, or A in the fourth octave, 28 00:01:22,650 --> 00:01:26,050 will have a frequency of 440 Hertz. 29 00:01:26,050 --> 00:01:28,420 So that's something that you can take for granted. 30 00:01:28,420 --> 00:01:31,270 If the user passes in A4 into your function, 31 00:01:31,270 --> 00:01:35,490 then the output is just going to be 440 as the return value. 32 00:01:35,490 --> 00:01:39,360 But what if the note isn't A4 and it's some other note? 33 00:01:39,360 --> 00:01:43,380 Well, generally when we're talking about frequency, we can follow these rules. 34 00:01:43,380 --> 00:01:48,960 If A4 is 440 Hertz, then for every semi-tone up that we move, 35 00:01:48,960 --> 00:01:52,980 remembering that a semi-tone is just a movement of one key on the piano 36 00:01:52,980 --> 00:01:55,830 keyboard, for every semi-tone up, we move, 37 00:01:55,830 --> 00:02:01,110 we're going to multiply the frequency by 2 to the power of 1/12. 38 00:02:01,110 --> 00:02:04,410 And likewise, for every semi-tone down we move, 39 00:02:04,410 --> 00:02:08,562 we're going to divide the frequency by 2 to the power of 1/12. 40 00:02:08,562 --> 00:02:11,520 Let's take a look at what that actually looks like from the perspective 41 00:02:11,520 --> 00:02:13,410 of the piano keyboard. 42 00:02:13,410 --> 00:02:16,140 So we know that A4 is 440 Hertz. 43 00:02:16,140 --> 00:02:17,800 And that's just the standard. 44 00:02:17,800 --> 00:02:22,996 But what about one semi-tone up from it, A sharp 4, or equivalently B flat 4. 45 00:02:22,996 --> 00:02:24,870 Well, all we're going to do there is remember 46 00:02:24,870 --> 00:02:28,410 that when we move up one semi-tone, we take the frequency 47 00:02:28,410 --> 00:02:31,980 and we multiply it by 2 to the power of 1/12. 48 00:02:31,980 --> 00:02:36,630 So if we take 440 and multiply it by 2 to the power of 1/12, 49 00:02:36,630 --> 00:02:42,440 we get about 466 Hertz as the frequency for A sharp 4. 50 00:02:42,440 --> 00:02:48,150 What about one more semi-tone up to B4, the next semi-tone after A sharp 4. 51 00:02:48,150 --> 00:02:53,340 Well, we'll take 466, approximately, and multiply that by 2 52 00:02:53,340 --> 00:02:57,270 to the power of 1/12, which is really the same thing as taking 53 00:02:57,270 --> 00:03:03,070 440 and multiplying it by 2 to the power of 1/12, twice, 54 00:03:03,070 --> 00:03:08,010 which is also the same thing as taking 440 and multiplying it to 2 55 00:03:08,010 --> 00:03:13,950 to the power of 2 over 12, which would come out to be about 494 Hertz. 56 00:03:13,950 --> 00:03:18,770 Likewise, if we were to move one semi-tone up from B4 to C5, 57 00:03:18,770 --> 00:03:21,720 remembering that new octaves always start with C, 58 00:03:21,720 --> 00:03:27,450 we get 440 times 2 to the 3 over 12, which is about 523 Hertz, 59 00:03:27,450 --> 00:03:28,750 and so on and so forth. 60 00:03:28,750 --> 00:03:32,970 The next note is going to be multiplying 440 and by 2 to the 4 over 12, 61 00:03:32,970 --> 00:03:37,630 then 5 over 12, 6, 7, 8, 9, 10, 11. 62 00:03:37,630 --> 00:03:42,720 And finally, when we hit A5, exactly one octave above A4, 63 00:03:42,720 --> 00:03:47,190 we've now moved 12 semitones away, or a full octave. 64 00:03:47,190 --> 00:03:55,000 And what we get is 440 times 2 to the 12 over 12, which is just 2 to 1, or 2. 65 00:03:55,000 --> 00:03:59,640 And when we multiply 440 by 2, we get 880. 66 00:03:59,640 --> 00:04:05,400 So notice here that A5, exactly one octave above A4, all we had to do 67 00:04:05,400 --> 00:04:10,650 is take A4's frequency, 440 Hertz, and multiply it by 2 68 00:04:10,650 --> 00:04:14,220 to get 880 Hertz for A5. 69 00:04:14,220 --> 00:04:17,100 So what does this actually mean in terms of frequency? 70 00:04:17,100 --> 00:04:21,480 Well, if A4 is 440 Hertz, then any time we want to take a note 71 00:04:21,480 --> 00:04:25,980 and go up an octave, we multiply it by 2 to the 12 over 12, 72 00:04:25,980 --> 00:04:28,590 which is just the same thing as multiplying it by 2. 73 00:04:28,590 --> 00:04:31,770 And likewise to go down an octave, we just take the frequency 74 00:04:31,770 --> 00:04:33,270 and divide it by 2. 75 00:04:33,270 --> 00:04:36,180 And this doesn't just apply to A4, given any note, 76 00:04:36,180 --> 00:04:40,020 if you want to move it up an octave, all you have to do is take its frequency 77 00:04:40,020 --> 00:04:41,580 and multiply it by 2. 78 00:04:41,580 --> 00:04:45,660 And to move it down an octave, you just take its frequency and divide it by 2. 79 00:04:45,660 --> 00:04:49,680 So now let's think about how we're going to take a note represented as a string 80 00:04:49,680 --> 00:04:53,790 and figure out what the letter of the note is, what it's accidental is, 81 00:04:53,790 --> 00:04:55,690 and what it's octave is. 82 00:04:55,690 --> 00:05:00,550 So if we have a note represented as a string that can take any form of A4, 83 00:05:00,550 --> 00:05:04,550 to B flat 4, to C sharp 5 or anything of the like, 84 00:05:04,550 --> 00:05:06,850 but we know that the note is going to be a string 85 00:05:06,850 --> 00:05:08,830 and the first character of the note is always 86 00:05:08,830 --> 00:05:13,390 going to be the letter of the note, either A, or B, or C, D, E, F, or G. 87 00:05:13,390 --> 00:05:15,160 That much we can rely on. 88 00:05:15,160 --> 00:05:18,910 The second character of the note depends a little bit upon what note it is. 89 00:05:18,910 --> 00:05:23,150 If the note is a sharp or flat and has an accidental as a result, 90 00:05:23,150 --> 00:05:27,130 then the second character of that note string is going to be that accidental. 91 00:05:27,130 --> 00:05:31,360 For example, in the string A sharp 5, the second character 92 00:05:31,360 --> 00:05:32,950 is that sharp symbol. 93 00:05:32,950 --> 00:05:35,620 On the other hand, if there is no sharp or flat, 94 00:05:35,620 --> 00:05:39,220 as we might call a natural note, then the second character 95 00:05:39,220 --> 00:05:41,470 isn't going to be the sharp or flat symbol, 96 00:05:41,470 --> 00:05:43,150 but it's going to be the octave symbol. 97 00:05:43,150 --> 00:05:47,320 So for instance, if we had the note C4, where there is no sharp or flat, 98 00:05:47,320 --> 00:05:50,530 C is still the first character, the letter of the note that we want. 99 00:05:50,530 --> 00:05:55,480 But the second character is going to be the octave, octave number four. 100 00:05:55,480 --> 00:05:58,870 Finally, if there was an accidental, sharp or flat, 101 00:05:58,870 --> 00:06:01,030 we still need to figure out what octave it's in. 102 00:06:01,030 --> 00:06:04,750 So if there was an accidental, then the third character of the string 103 00:06:04,750 --> 00:06:06,340 is going to be the octave. 104 00:06:06,340 --> 00:06:11,080 So in the case of a note like B flat 4, B is the first character, 105 00:06:11,080 --> 00:06:12,730 and that's the letter of the note. 106 00:06:12,730 --> 00:06:16,810 Flat, or the represented by the lowercase b symbol, is the accidental. 107 00:06:16,810 --> 00:06:20,240 That's what's going to show up as the second character in the string. 108 00:06:20,240 --> 00:06:22,240 And finally, the third character of the string 109 00:06:22,240 --> 00:06:24,350 is going to be the octave number. 110 00:06:24,350 --> 00:06:27,040 And so now that we understand how to actually represent a note, 111 00:06:27,040 --> 00:06:28,748 let's walk through the steps of how we're 112 00:06:28,748 --> 00:06:32,380 going to go from taking an arbitrary note that could be any note, 113 00:06:32,380 --> 00:06:34,690 and turning it into an actual frequency. 114 00:06:34,690 --> 00:06:36,572 But let's take it step by step. 115 00:06:36,572 --> 00:06:38,530 The first thing that you'll probably want to do 116 00:06:38,530 --> 00:06:41,230 is to implement different octaves of a. 117 00:06:41,230 --> 00:06:43,840 You know that for A4, you can just return the number 118 00:06:43,840 --> 00:06:47,290 440 because we've said that that is the number of Hertz that 119 00:06:47,290 --> 00:06:49,540 represents the note A4. 120 00:06:49,540 --> 00:06:55,480 But what about other octaves of the note A, like A3, or A2, or A5, or A6. 121 00:06:55,480 --> 00:06:58,810 Well, we know that to change the octave, we just need to multiply 122 00:06:58,810 --> 00:07:00,850 or divide by some power of 2. 123 00:07:00,850 --> 00:07:05,740 To get from A4 to A5, we take 440 and multiply it by 2. 124 00:07:05,740 --> 00:07:10,750 To get from A4 to A6, we take 440 and multiply it by 2 twice, 125 00:07:10,750 --> 00:07:13,000 or multiply it by 4. 126 00:07:13,000 --> 00:07:17,830 Likewise, to go from A4 to A3, we'll have to divide by 2 127 00:07:17,830 --> 00:07:20,740 to get 220 Hertz for A3. 128 00:07:20,740 --> 00:07:25,420 And so we can use multiplication or division by some power of 2 129 00:07:25,420 --> 00:07:29,680 to be able to figure out what the frequency of some different octave of A 130 00:07:29,680 --> 00:07:30,920 is going to be. 131 00:07:30,920 --> 00:07:33,220 And finally, be sure that when you do this math, 132 00:07:33,220 --> 00:07:37,470 you're rounding any decimals that you get to the nearest integer. 133 00:07:37,470 --> 00:07:40,040 So now that we've got different octaves of A, 134 00:07:40,040 --> 00:07:43,860 let's think about accidentals, sharps or flats that might modify A, 135 00:07:43,860 --> 00:07:47,880 and how we're going to adjust for those frequencies as well. 136 00:07:47,880 --> 00:07:52,220 So step two is going to be supporting A sharp and A flat. 137 00:07:52,220 --> 00:07:56,210 Well, we know that to make a note sharp, since a sharp just means exactly one 138 00:07:56,210 --> 00:08:00,110 semi-tone ahead of the previous note, all we have to do 139 00:08:00,110 --> 00:08:05,000 is multiply the original frequency by 2 to the power of 1 over 12, 140 00:08:05,000 --> 00:08:07,640 remembering that in order to get from one frequency 141 00:08:07,640 --> 00:08:10,070 to the frequency exactly one semitone above it, 142 00:08:10,070 --> 00:08:13,400 we just multiply by this 2 to 1 over 12. 143 00:08:13,400 --> 00:08:16,520 Likewise, to make a note flat, we'll do the opposite, 144 00:08:16,520 --> 00:08:19,790 divide the frequency by 2 to 1 over 12. 145 00:08:19,790 --> 00:08:25,580 So think about, if we have A, and A is 4 is represented by 440 Hertz, 146 00:08:25,580 --> 00:08:30,140 and we want to represent A sharp 4, example, all we need to do 147 00:08:30,140 --> 00:08:35,090 is take the frequency 440 and multiply it by 2 to 1 over 12. 148 00:08:35,090 --> 00:08:38,780 And likewise for A flat, we would take the frequency 440 149 00:08:38,780 --> 00:08:41,760 and divide it by 2 to the 1 over 12. 150 00:08:41,760 --> 00:08:46,310 But as you do this, also make sure to be able to support A sharp and A flat 151 00:08:46,310 --> 00:08:48,140 in multiple different octaves. 152 00:08:48,140 --> 00:08:55,320 Sure, A sharp 4 is just A4's 440 Hertz multiplied by 2 to the 1 over 12. 153 00:08:55,320 --> 00:08:58,760 But what about A sharp 5 or A sharp 6? 154 00:08:58,760 --> 00:09:01,130 Well, if you remember that in order to go up an octave, 155 00:09:01,130 --> 00:09:04,580 you just multiply the frequency by 2, and to go down an octave 156 00:09:04,580 --> 00:09:08,690 you just divide the frequency by 2, then if you know what A sharp 4 is, 157 00:09:08,690 --> 00:09:11,870 all you need to do is multiply that frequency by 2 158 00:09:11,870 --> 00:09:14,300 in order to get A sharp 5. 159 00:09:14,300 --> 00:09:18,140 So you might think about, first, adjusting for sharps or flats, 160 00:09:18,140 --> 00:09:21,189 and then adjusting for different octaves. 161 00:09:21,189 --> 00:09:23,480 Or alternatively, you could do it the other way around. 162 00:09:23,480 --> 00:09:26,240 First adjust for the octave by multiplying or dividing 163 00:09:26,240 --> 00:09:27,680 by some power of two. 164 00:09:27,680 --> 00:09:31,210 And then adjusting for a sharp and a flat. 165 00:09:31,210 --> 00:09:35,350 And finally, once we've supported A, A sharp, and A flat, 166 00:09:35,350 --> 00:09:39,430 the last step is going to be to support all different kinds of notes, where 167 00:09:39,430 --> 00:09:42,920 we'll use A as our starting point, and figure out how many semi-tones 168 00:09:42,920 --> 00:09:45,229 away various notes are. 169 00:09:45,229 --> 00:09:47,020 And if you take a look at a piano keyboard, 170 00:09:47,020 --> 00:09:48,830 you should be able to figure this out. 171 00:09:48,830 --> 00:09:51,040 If you remember which note has which letter, 172 00:09:51,040 --> 00:09:55,300 you should be able to calculate how many semitones away B is from A, 173 00:09:55,300 --> 00:09:59,200 how many semitones C is away from A, and as well for all the other nodes 174 00:09:59,200 --> 00:10:00,970 that appear on the piano keyboard. 175 00:10:00,970 --> 00:10:05,590 An important thing to remember is that octaves start at C. In other words, 176 00:10:05,590 --> 00:10:09,010 C5 is one semitone above B4. 177 00:10:09,010 --> 00:10:12,190 This becomes clearer if you take a look at a picture of the piano that 178 00:10:12,190 --> 00:10:14,920 represents all the different notes and what octaves they belong, 179 00:10:14,920 --> 00:10:19,750 where you'll notice that the fourth octave ranges from C4 all the way up 180 00:10:19,750 --> 00:10:20,890 to B4. 181 00:10:20,890 --> 00:10:24,910 But as soon as we move from B4 to the note one semi-tone after that, 182 00:10:24,910 --> 00:10:29,110 we arrive at C5, which is the beginning of the fifth octave. 183 00:10:29,110 --> 00:10:31,600 So while it may seem a little bit counterintuitive, 184 00:10:31,600 --> 00:10:35,860 A4 and B4 are actually higher notes than C4, 185 00:10:35,860 --> 00:10:38,470 even though C comes later in the alphabet, 186 00:10:38,470 --> 00:10:41,140 because C marks the start of the octave. 187 00:10:41,140 --> 00:10:44,620 And so bearing in mind which notes belong to which octaves, 188 00:10:44,620 --> 00:10:46,840 and when new octaves are starting, can help you 189 00:10:46,840 --> 00:10:49,350 out when it comes to computing the frequency. 190 00:10:49,350 --> 00:10:51,850 So ultimately, the logic that you're going to want to follow 191 00:10:51,850 --> 00:10:55,600 is to, first, figure out what the string actually represents, 192 00:10:55,600 --> 00:10:58,420 what the letter of the note is, what accidental it has, 193 00:10:58,420 --> 00:11:00,010 and what octave it has. 194 00:11:00,010 --> 00:11:06,370 Then using A4 as your baseline, where you know that A4 is equal to 440 Hertz, 195 00:11:06,370 --> 00:11:07,330 you can adjust. 196 00:11:07,330 --> 00:11:11,840 Any time you move up a semitone, multiply by 2 to the power of 1/12. 197 00:11:11,840 --> 00:11:16,330 Anytime you move down a semi-tone, divide by 2 to the power of 1/12. 198 00:11:16,330 --> 00:11:18,640 And if you ever want to jump a full octave, 199 00:11:18,640 --> 00:11:21,310 remember that to go up an octave, you just take a frequency 200 00:11:21,310 --> 00:11:22,990 and multiply it by 2. 201 00:11:22,990 --> 00:11:26,620 And any time you want to go down an octave, you just take the frequency 202 00:11:26,620 --> 00:11:28,660 and you divide it by 2. 203 00:11:28,660 --> 00:11:31,690 At the end, you'll likely end up with a floating point number 204 00:11:31,690 --> 00:11:33,650 with digits after the decimal point. 205 00:11:33,650 --> 00:11:36,850 But remember that the frequency function is supposed to return an integer. 206 00:11:36,850 --> 00:11:40,270 So once again, make sure you're rounding your number to the nearest integer 207 00:11:40,270 --> 00:11:44,270 in order to make sure that your output matches the desired frequency level. 208 00:11:44,270 --> 00:11:47,530 So those are the steps to figuring out frequency, after which point 209 00:11:47,530 --> 00:11:50,980 you should be able to hear songs and listen to them based on the functions 210 00:11:50,980 --> 00:11:53,980 that you've implemented inside of helpers.c. 211 00:11:53,980 --> 00:11:55,600 On that note, I'll leave you to it. 212 00:11:55,600 --> 00:11:56,290 Good luck. 213 00:11:56,290 --> 00:11:59,850 My name is Brian, and this was music. 214 00:11:59,850 --> 00:12:01,451