{"id":125,"date":"2012-11-18T14:41:04","date_gmt":"2012-11-18T22:41:04","guid":{"rendered":"http:\/\/www.earlevel.com\/main\/?p=125"},"modified":"2019-11-19T00:02:12","modified_gmt":"2019-11-19T08:02:12","slug":"a-wavetable-oscillator-end-notes","status":"publish","type":"post","link":"https:\/\/www.earlevel.com\/main\/2012\/11\/18\/a-wavetable-oscillator-end-notes\/","title":{"rendered":"A wavetable oscillator\u2014end notes"},"content":{"rendered":"<p>I wrote the wavetable oscillator code from scratch as I wrote this series of articles, using just the thought process I gave. Hence, no references to other articles on wavetable oscillators. The concept of a phase accumulator is fairly obvious; one of the first books I recall reading about it was <i>Musical Applications of Microprocessors<\/i>, by Hal Chamberlin, about 30 years ago (I have an autographed first edition), though the book didn&#8217;t explore the concept of multiple tables per oscillator to control aliasing.<\/p>\n<p>These end notes are an opportunity to touch on various aspects of the oscillator.<\/p>\n<h3>Oscillator examples<\/h3>\n<p>First, a few examples. Here&#8217;s a pulse-width modulation example, using a sine-wave instance of an oscillator to modulate the pulse width of another oscillator:<\/p>\n<p>PWM 110 Hz mod 0.3 Hz sine, 4s<br \/>\n<!--[if lt IE 9]><script>document.createElement('audio');<\/script><![endif]-->\n<audio class=\"wp-audio-shortcode\" id=\"audio-125-1\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"\/main\/wp-content\/uploads\/2012\/06\/pwm-110-Hz-mod-0.3-Hz-sine-4s.mp3?_=1\" \/><a href=\"\/main\/wp-content\/uploads\/2012\/06\/pwm-110-Hz-mod-0.3-Hz-sine-4s.mp3\">\/main\/wp-content\/uploads\/2012\/06\/pwm-110-Hz-mod-0.3-Hz-sine-4s.mp3<\/a><\/audio><\/p>\n<p>Three detuned and summed sawtooth oscillators:<\/p>\n<p>Saw times 3 detuned 55 Hz, 4s<br \/>\n<audio class=\"wp-audio-shortcode\" id=\"audio-125-2\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"\/main\/wp-content\/uploads\/2012\/06\/saw-times-3-detuned-55-Hz-4s.mp3?_=2\" \/><a href=\"\/main\/wp-content\/uploads\/2012\/06\/saw-times-3-detuned-55-Hz-4s.mp3\">\/main\/wp-content\/uploads\/2012\/06\/saw-times-3-detuned-55-Hz-4s.mp3<\/a><\/audio><\/p>\n<p>But we aren&#8217;t limited to computing our wavetables. Here, I recorded myself singing &#8220;aahh&#8230;&#8221; (originally, 110 Hz, the second A below middle C). I cut one cycle of that in an audio editor and stretched (resampled) it to 2048 samples, and did an FFT of that. From there it was easy to create higher octaves by eliminating harmonics in the upper octave and doing an inverse FFT to make the next higher wavetable\u2014the same process as we did with the computed waves. Here it is, swept 20 Hz to 20 kHz (but keep in mind that the typical useful range is in the lower octaves):<\/p>\n<p>Ahh sweep 20-20k, 20s<br \/>\n<audio class=\"wp-audio-shortcode\" id=\"audio-125-3\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"\/main\/wp-content\/uploads\/2012\/06\/ahh-sweep-4096-20-20k-20s.mp3?_=3\" \/><a href=\"\/main\/wp-content\/uploads\/2012\/06\/ahh-sweep-4096-20-20k-20s.mp3\">\/main\/wp-content\/uploads\/2012\/06\/ahh-sweep-4096-20-20k-20s.mp3<\/a><\/audio><\/p>\n<h3>Sawtooth revisited<\/h3>\n<p>Because I wanted to show the harmonic series of sine waves building a sawtooth in the earlier articles, I didn&#8217;t mention that sawtooth waves traditionally ramp up (remember the charging capacitor in the analog oscillator?). Creating this wave from sines make it ramp down\u2014often called an inverted sawtooth. It really doesn&#8217;t make a difference in the audio range; in the sub-audio range for a control signal, you might prefer one or the other depending on the effect you&#8217;re after. But either way, it&#8217;s trivial to build the up-ramping sawtooth, and I did that in the final version just for fun. I left out the 20-40 Hz octave in the tutorial to make a point, but I&#8217;m including it here\u2014a non-inverted sawtooth sweep, and you&#8217;ll notice that the lowest octave is brighter with a true 20 Hz table:<\/p>\n<p>Saw sweep 2048 lin 20-20k (base=20Hz), 20s<br \/>\n<audio class=\"wp-audio-shortcode\" id=\"audio-125-4\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"\/main\/wp-content\/uploads\/2012\/06\/saw-sweep-2048-lin-20-20k-base20Hz-20s.mp3?_=4\" \/><a href=\"\/main\/wp-content\/uploads\/2012\/06\/saw-sweep-2048-lin-20-20k-base20Hz-20s.mp3\">\/main\/wp-content\/uploads\/2012\/06\/saw-sweep-2048-lin-20-20k-base20Hz-20s.mp3<\/a><\/audio><\/p>\n<h3>More on low-end brightness<\/h3>\n<p>Though we don&#8217;t often listen to the audio output of an oscillator in the sub-audio frequency range, the harmonic content is apparent for the brighter waveforms such as sawtooth. Here&#8217;s what it sounds like when we sweep from audio range down to 1 Hz with our wavetables built for the audio range:<\/p>\n<p>Saw sub-audio test (20-20k 2048), 20s<br \/>\n<audio class=\"wp-audio-shortcode\" id=\"audio-125-5\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"\/main\/wp-content\/uploads\/2012\/06\/saw-sub-audio-test-20-20k-2048-20s.mp3?_=5\" \/><a href=\"\/main\/wp-content\/uploads\/2012\/06\/saw-sub-audio-test-20-20k-2048-20s.mp3\">\/main\/wp-content\/uploads\/2012\/06\/saw-sub-audio-test-20-20k-2048-20s.mp3<\/a><\/audio><\/p>\n<p>Note how the harmonic content drops as the ticks slow.<\/p>\n<p>Because the lower the frequency, the less that aliasing is a factor (since the sample resolution for a single cycle increases the lower we go), we could switch to a more straight-forward sawtooth for sub-audio. Or, because our oscillator allows a variable table size for each subtable, we could simply place a long wavetable to handle all sub-audio. Here&#8217;s the same oscillator, but with a 32768-sample sawtooth table to handle everything below 20 Hz:<\/p>\n<p>Saw sub-audio test (20-20k 2048 + 32768 ramp), 20s<audio class=\"wp-audio-shortcode\" id=\"audio-125-6\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"\/main\/wp-content\/uploads\/2012\/06\/saw-sub-audio-test-20-20k-2048-+-32768-ramp-20s.mp3?_=6\" \/><a href=\"\/main\/wp-content\/uploads\/2012\/06\/saw-sub-audio-test-20-20k-2048-+-32768-ramp-20s.mp3\">\/main\/wp-content\/uploads\/2012\/06\/saw-sub-audio-test-20-20k-2048-+-32768-ramp-20s.mp3<\/a><\/audio><\/p>\n<h3>Optimizations<\/h3>\n<p>My goal with the accompanying code was to make it simple and understandable. It&#8217;s pretty quick, though\u2014the full audio sweep takes about 0.9 seconds to generate 1000 seconds of audio on my computer. And there are certainly ways to make it faster, though in general a little bit faster makes it a lot less readable, and often requires some loss of generality. For instance, we could make oscillator table selection faster by mandating octave spacing, then using a fast-log2 floating-point trick to to get to the proper table in a single step\u2014but that would put a limitation on the oscillator, the relevant code would become unreadable to many people, and in practice the speed gain is small.<\/p>\n<p>Some people might prefer a fixed-point phase accumulator. The math is fast and precise, and you can get a free wrap-around when you increment. But the double-precision floating point phase accumulator of this oscillator is extremely precise, is plenty fast, and the code is very easy to follow.<\/p>\n<h3>Interpolation<\/h3>\n<p>If you search the web for wavetable oscillators, you&#8217;ll find terrific educational papers and forum threads discussing various types of interpolation and their relative performance. It may leave you wondering why you should trust the lowly and relatively pathetic linear interpolation presented here. Good point.<\/p>\n<p>It&#8217;s about table size. If your table is big enough, you don&#8217;t need any interpolation at all\u2014truncation is fine. But linear interpolation is only a bit more costly, and gives a boost that&#8217;s probably worth the effort. Higher forms of interpolation require more sample points in the calculation (for instance, needing two points on each side of the point to be calculated, instead of one point on each side for linear interpolation). They are great for squeezing better quality out of a small table, but you need to ask yourself why you are using the small tables. If you&#8217;re implementing this on a modern computer, what is the point of increasing your calculations so that you can use a 512-sample table instead of a 2048-sample table\u2014on a systems that has gigabytes of free memory?<\/p>\n<h3>Constant versus variable table size<\/h3>\n<p>If we use a constant subtable size, as each subtable moves up to cover the next octave, with half of the harmonic content, they become another factor of 2 x oversampled. The top octave will always be a sine wave, and at the 2048 samples we&#8217;re using for our subtables, it&#8217;s extremely oversampled\u2014way more than we need or will be able to discern.<\/p>\n<p>And while I said that we want to be at least 2 x oversampled at our lowest subtable, for linear interpolation to have good fidelity, at the same time it&#8217;s unlikely that we&#8217;ll hear it in the lowest octave. The reason is that low frequency wavetables for a sawtooth will be very crowded in the high harmonics\u2014half of them are in the top octave of our hearing, spaced closely. So, we could go to 1024-sample tables for 40 Hz and up, or 2048 for 20 Hz and up (as in the \u201cSaw sweep 2048 lin 20-20k\u201d example above), and you probably won&#8217;t hear the difference. All of the higher tables will still be increasingly oversampled. Here&#8217;s the 1024 equivalent of the 2048 example in Part 3 of the wavetable oscillator series:<\/p>\n<p>Saw sweep 1024 trunc 20-20k (base=20Hz), 20s<br \/>\n<audio class=\"wp-audio-shortcode\" id=\"audio-125-7\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-1024-trunc-20-20k-20s.mp3?_=7\" \/><a href=\"\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-1024-trunc-20-20k-20s.mp3\">\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-1024-trunc-20-20k-20s.mp3<\/a><\/audio><\/p>\n<p>In a nutshell, we might suspect that it will be easier to hear the benefits of oversampling in the upper octaves, but at the same time the minimum table size to hold their required harmonic content is smaller.<\/p>\n<p>We could just as easily go to a constant oversampling ratio by dropping the subtable length by a factor of two as we go to the next octave&#8217;s subtable. It&#8217;s easy enough to make all of these things variable in your code, so that you can play with tradeoffs.<\/p>\n<h3>Lesser interpolation experiments<\/h3>\n<p>Linear interpolation is referred to as &#8220;first order&#8221; interpolation. Truncation is &#8220;zero order&#8221; interpolation, implying none. So let&#8217;s hear how awful the oscillator would sound with no interpolation:<\/p>\n<p>Saw sweep 2048 trunc 20-20k, 20s<br \/>\n<audio class=\"wp-audio-shortcode\" id=\"audio-125-8\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-2048-trunc-20-20k-base20-20s.mp3?_=8\" \/><a href=\"\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-2048-trunc-20-20k-base20-20s.mp3\">\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-2048-trunc-20-20k-base20-20s.mp3<\/a><\/audio><\/p>\n<p>Um, that\u2019s not as bad as expected\u2014or should we not have expected it to be bad? Remember, we&#8217;re using fixed table sizes so far, which makes the higher, more sensitive tables progressively more oversampled. And oversampling helps improve any interpolation, even none.<\/p>\n<p>Let&#8217;s explore variable table sizes, to keep a constant oversampling ratio per octave. We&#8217;ll start with the worst-case of 1 x oversampling, and with truncation:<\/p>\n<p>Saw sweep variable 1x trunc 20-20k, 20s<br \/>\n<audio class=\"wp-audio-shortcode\" id=\"audio-125-9\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-variable-1x-trunc-20-20k-20s.mp3?_=9\" \/><a href=\"\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-variable-1x-trunc-20-20k-20s.mp3\">\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-variable-1x-trunc-20-20k-20s.mp3<\/a><\/audio><\/p>\n<p>As expected, the aliasing is bad, and gets worse in the higher octaves. Here it is again, with linear interpolation:<\/p>\n<p>Saw sweep variable 1x lin 20-20k, 20s<br \/>\n<audio class=\"wp-audio-shortcode\" id=\"audio-125-10\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-variable-1x-lin-20-20k-20s.mp3?_=10\" \/><a href=\"\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-variable-1x-lin-20-20k-20s.mp3\">\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-variable-1x-lin-20-20k-20s.mp3<\/a><\/audio><\/p>\n<p>Even though 1 x oversampling is still inadequate, the improvement with linear interpolation is obvious. Let\u2019s try again, but with 4 x oversampling; first with no interpolation (and let&#8217;s focus on the higher octaves):<\/p>\n<p>Saw sweep trunc 1280-20k, constant 4x oversample, 20s<br \/>\n<audio class=\"wp-audio-shortcode\" id=\"audio-125-11\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-trunc-20-20k-base1280-constant-4x-oversample-20s.mp3?_=11\" \/><a href=\"\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-trunc-20-20k-base1280-constant-4x-oversample-20s.mp3\">\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-trunc-20-20k-base1280-constant-4x-oversample-20s.mp3<\/a><\/audio><\/p>\n<p>Now with linear interpolation:<\/p>\n<p>Saw sweep lin 1280-20k, constant 4x oversample, 20s<br \/>\n<audio class=\"wp-audio-shortcode\" id=\"audio-125-12\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-lin-20-20k-base1280-constant-4x-oversample-20s.mp3?_=12\" \/><a href=\"\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-lin-20-20k-base1280-constant-4x-oversample-20s.mp3\">\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-lin-20-20k-base1280-constant-4x-oversample-20s.mp3<\/a><\/audio><\/p>\n<p>You can still hear a little aliasing at the top, but 4 x is adequate for much of the range. We might consider 8 x, but a problem with this approach is that we&#8217;re wasting a lot of table space for the lower octaves, and not achieving the objective of saving memory by using variable tables. Instead of keeping a constant oversampling ratio, we can go with a variable table size, but impose a minimum length, so that just the top octaves are progressively more oversampled, where we need it most.<\/p>\n<p>Here are constant 1 x oversampled tables, but with a minimum of 64 samples so that the top octaves are oversampled progressively higher; first truncated, then with linear interpolation:<\/p>\n<p>Saw sweep 2048 trunc 20-20k (base=20, variable limit 64), 20s<br \/>\n<audio class=\"wp-audio-shortcode\" id=\"audio-125-13\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-2048-trunc-20-20k-base20-variable-limit-64-20s.mp3?_=13\" \/><a href=\"\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-2048-trunc-20-20k-base20-variable-limit-64-20s.mp3\">\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-2048-trunc-20-20k-base20-variable-limit-64-20s.mp3<\/a><\/audio><\/p>\n<p>Saw sweep 2048 lin 20-20k (base=20, variable limit 64), 20s<br \/>\n<audio class=\"wp-audio-shortcode\" id=\"audio-125-14\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-2048-lin-20-20k-base20-variable-limit-64-20s1.mp3?_=14\" \/><a href=\"\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-2048-lin-20-20k-base20-variable-limit-64-20s1.mp3\">\/main\/wp-content\/uploads\/2012\/11\/saw-sweep-2048-lin-20-20k-base20-variable-limit-64-20s1.mp3<\/a><\/audio><\/p>\n<p>A couple of things we\u2019ve learned from these exercises: The upper octaves need progressively more oversampling to sound as good as the lower octaves\u2014at least for a waveform like the sawtooth. And linear interpolation is a noticeable improvement over truncation.<\/p>\n<p>But I hope you\u2019ve also realized that truncation is a viable alternative, as long as the table size is big enough. And the \u201cbig enough\u201d qualification is not difficult if you\u2019re using fixed table sizes, which yield progressively higher oversampling in higher octaves.<\/p>\n<p>While variable table size is helpful in memory-resticted environments, it\u2019s not very important in most modern computing environments. I included the ability to handle variable table sizes within an oscillator for educational experimentation, mainly.<\/p>\n<p>Note: Some people are thinking\u2026if truncation might be acceptable, why not use rounding instead? The answer is\u2026there is no difference in noise\/distortion between truncation and rounding\u2014use whichever is most convenient (which may depend on the computer language you use or rounding mode of the processor). If you doubt that truncation and rounding are equivalent for our use, consider that rounding is equivalent to adding 0.5 and truncating; this means that rounding in our lookup table is equivalent to truncation except for a phase shift of half a sample\u2014something we can\u2019t hear (because there\u2019s no reference to make it matter, but if it really bothers you, you could always create the wavetables pre-shifted by half a sample!).<\/p>\n<h3>Setting oscillator frequency<\/h3>\n<p>We set the pitch by setting the increment that gets added to our phasor for every new sample lookup. An increment of 0.5 steps half-way through the table. For a single-cycle table, that would correspond to a frequency of half the sample rate\u201422.05 kHz, half of our 44.1 kHz sample rate. Another way to say that is that the increment for a frequency of 22.05 kHz is 22050 \/ 44100, or 0.5.<\/p>\n<p>So, the increment for any  frequency is that frequency divided by the sample rate. For 440 Hz, it&#8217;s 440 \/ 44100, or 0.00997732426303855.<\/p>\n<p>On the subject of frequency, one simple and useful addition to the oscillator might be the ability to adjust the phase, for quadrature and phase modulation effects.<\/p>\n<h3>Hearing<\/h3>\n<p>OK, I hear better than I thought. 15k loud and clear, 16k is plenty clear too, but I could tell it wasn&#8217;t quite as loud. 17k is getting pretty tough, needing plenty of volume. But in this case I was listening to naked sine waves at pretty decent volume\u2014I stand by my reasoning that it would be awfully tough to hear those aliased harmonics\u2014even if we were playing ridiculously high notes with no synth filter, at high volume, and soloed. But, if you disagree, just go with closer wavetable spacing!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wrote the wavetable oscillator code from scratch as I wrote this series of articles, using just the thought process I gave. Hence, no references to other articles on wavetable oscillators. The concept of a phase accumulator is fairly obvious; &hellip; <a href=\"https:\/\/www.earlevel.com\/main\/2012\/11\/18\/a-wavetable-oscillator-end-notes\/\">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":[4,20,24],"tags":[],"_links":{"self":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/125"}],"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=125"}],"version-history":[{"count":5,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"predecessor-version":[{"id":960,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/125\/revisions\/960"}],"wp:attachment":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}