Assignment 2: Sound Generation using Wavetables
Goal
To understand how wavetables work and how they can be used
to produce different frequencies and different waveshapes.
Task list
- Alter your sine wave program (or write a new one) that
uses a wavetable for generation. Set the initial parameters as follows
- sampling rate 48,000 samples per sec (note, not
44,100)
- frequency 480 cycles per sec (between B flat and B
natural above middle C)
- wavetable length 1,000 samples
- one channel (mono)
- 2 signed bytes per sample
- linear encoding
- length 4 secs.
- Use a for loop to place samples for one cycle of a sine
wave into the wavetable.
- Use the formula f×nt/sr to calculate the
wavetable increment and generate 4 secs of sound by multiplying the
increment by the output sample number and using the result as an index
into the table. (Use rounding and the mod operation to ensure that the
index is an integer.) In this first case, the increment is 10 so we are
skipping 9 samples and taking every tenth.
- Verify that your program works by using the utility aplay
with the same parameters as above on the file that the program produces.
What sound do you hear?
- Change the frequency to 960 and reduce the table size to
100 samples. Again load a sine wave into the table, and use it to generate
4 secs. of sound. The sound should change to an octave above the first
sound. The increment is now 2.
- Change the sampling rate to 44,100, keeping table length
and frequency the same. Generate 4 secs. of sound. It should not alter
even though we have a non-integral table increment.
- Change the frequency to 120, go back to a smpling rate of
48,000, and keep the table size at 100. Now the increment is 0.25, so we
are reusing samples. Generate 4 secs. of sound. Describe the quality of
the sound.
- Alter the index calculation to use linear interpolation
instead of rounding. Generate 4 secs. of sound. It should be somewhat
cleaner sounding.
- Go back to f = 480, and table size = 1,000. Generate
values in the wavetable for a sawtooth wave and describe the quality of the
sound.
- Generate values for a square wave in the wavetable and
describe the quality of the sound.
- Generate random values in the table using the function
random(), and describe the quality of that sound.
Hints
- Use an array of samples for the wavetable
- You don’t actually need to use the mod operator (which is
slow) to calculate the index. Instead check if the value equals or exceeds
the table size, and subtract the table size if it does. This is faster.
- The sawtooth and square waves look like this: