{"id":109,"date":"2012-05-08T08:54:45","date_gmt":"2012-05-08T16:54:45","guid":{"rendered":"http:\/\/www.earlevel.com\/main\/?p=109"},"modified":"2019-02-24T15:01:03","modified_gmt":"2019-02-24T23:01:03","slug":"a-wavetable-oscillator%e2%80%94part-2","status":"publish","type":"post","link":"https:\/\/www.earlevel.com\/main\/2012\/05\/08\/a-wavetable-oscillator%e2%80%94part-2\/","title":{"rendered":"A wavetable oscillator\u2014Part 2"},"content":{"rendered":"<p>From <a href=\"\/main\/2012\/05\/04\/a-wavetable-oscillator\u2014part-1\/\">Part 1<\/a>, we have an oscillator. But we need to broaden it to allow scaling of harmonic content based on pitch so that we have all the harmonic content we need at the low frequency end, and, as we move up, eliminate those harmonics that would be above the the range of hearing and mirror (alias) back into the audible range.<\/p>\n<h3>How many harmonics do we need?<\/h3>\n<p>To start, we pick a lowest wavetable frequency. Let&#8217;s say 40 Hz (it could be 80 Hz, it could be 20 Hz\u2014it could be 1 Hz). The highest harmonic that fits under half our sample rate is 551 (22050 \/ 40 is 551.25). So, we could make a sawtooth table of 551 harmonics. The top harmonic would be only 10 Hz from the aliasing threshold\u2014Nyquist. If we shift that wavetable up one octave to 80 Hz, the top harmonic would double\u2014which means it would alias back down to 20 Hz. And the top two hundred harmonics would be below 16 kHz&#8230;Ouch! That means as we glide the wavetable frequency from 40 Hz to 80 Hz, we&#8217;ll be inundated with harmonics aliasing downward throughout the audio range.<\/p>\n<p>Of course, we could go the other direction, using that table from 40 Hz downward (and each higher table handling one octave downward similarly). But that means that as we shift down an octave, all of those harmonics at the top of the audio range will shift down to half the audio range\u2014our highest harmonics would now top out at about 11 kHz before the next table takes over and restores harmonics to the top of the audio range. We&#8217;ll hear this shortcoming.<\/p>\n<p>OK, we can cheat some and go for the middle ground. Most people don&#8217;t really hear to 20 kHz\u2014adults are lucky if they can hear 14 kHz. If we go for 368 harmonics, that puts the top harmonic at 14.72 kHz for 40 Hz, and the alias would fold back to 14.66 kHz (44100 &#8211; 14720 x 2). I think this will work fine for me\u2014I did a quick and dirty check a while back and could hear a 14 kHz tone, but not 15 kHz. The rest of you are on your own. Just kidding (and it ends up my hearing&#8217;s not that bad anyway\u2014more later, in the &#8220;end notes&#8221; installment). Instead of requiring each subtable to cover an octave, we could use twice as many and have them cover half of an octave. Or an even smaller increment. Or we could just oversample the oscillator and expand our shallow frequency headroom greatly, from a few kHz to tens of kHz.<\/p>\n<p>I&#8217;ll continue to develop this oscillator in octaves in order to keep the explanation simple (and because I&#8217;m going for &#8220;good&#8221; quality, to leave you to decide if you can sacrifice some quality for performance or memory, or whether you want to improve quality), but the extension to use more closely-spaces subtables is trivial. Oversampling 2x is also easy, but will complicate the explanation\u2014you&#8217;re on your own if you want to go that way, but it&#8217;s a good learning exercise.<\/p>\n<h3>Wavetable size<\/h3>\n<p>First, let&#8217;s back up and figure out how long our tables need to be. Recalling that we need to sample a signal at greater than twice the highest frequency component, that means that for 368 harmonics, we need at least 368 x 2 + 1 samples, or a table length of at least 737 samples. But also remember that we&#8217;ll be using linear interpolation, so we need to oversample for good results. That means at least 2x, for 1474 samples.<\/p>\n<p>We have good reasons to choose a table length that&#8217;s a power of two, however. There are optimizations we can do that take advantage of binary arithmetic, such as zero-cost wrapping of the table index for fixed-point indices, for instance. Plus, we get a huge boost in building these tables if we use the FFT, compared to summing sine waves individually. Also, we&#8217;ll definitely want to use the FFT if we let users define their own waves\u2014and the FFT is best suited for tables whose lengths are a power of two. From 1474, our next highest power of two is 2048, which gives us a bit more oversampling as well. Memory&#8217;s cheap, so no problem going bigger, but let&#8217;s see where this gets us.<\/p>\n<p>The bottom subtable will be used for that first octave and all pitches below. The second subtable takes over from 80 Hz to 160 Hz, and so on, as frequency doubles (and pitch goes up one octave). We use a suitable, progressively smaller range of harmonics for each table as we go up\u2014dropping the upper half of our harmonics for each higher octave. The top table will be a sine wave, always, so we can just add tables until the last one has one harmonic.<\/p>\n<p><i>Next: let&#8217;s hear the results in <a href=\"\/main\/2012\/05\/09\/a-wavetable-oscillator\u2014part-3\/\">Part 3<\/a><\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>From Part 1, we have an oscillator. But we need to broaden it to allow scaling of harmonic content based on pitch so that we have all the harmonic content we need at the low frequency end, and, as we &hellip; <a href=\"https:\/\/www.earlevel.com\/main\/2012\/05\/08\/a-wavetable-oscillator%e2%80%94part-2\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,4,20,24],"tags":[],"_links":{"self":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/109"}],"collection":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":3,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":1317,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/109\/revisions\/1317"}],"wp:attachment":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}