An Audio Synthesis Textbook For Musicians, Digital Artists and Programmers by Mike Krzyzaniak

  1. //FrequencyRatioSweep.c
  2. //gcc MKAiff.c FrequencyRatioSweep.c -o FrequencyRatioSweep
  3. #include "MKAiff.h"
  4. #include <math.h>
  5. #define SAMPLE_RATE 44100
  6. #define NUM_CHANNELS 1
  7. #define BITS_PER_SAMPLE 16
  8. #define NUM_SECONDS 5
  9. const int numSamples = NUM_SECONDS * NUM_CHANNELS * SAMPLE_RATE;
  10. #define PI 3.141592653589793
  11. const double TWO_PI_OVER_SAMPLE_RATE = 2*PI/SAMPLE_RATE;
  12. int main()
  13. {
  14. MKAiff* aiff = aiffWithDurationInSeconds(NUM_CHANNELS, SAMPLE_RATE, BITS_PER_SAMPLE, NUM_SECONDS);
  15. if(aiff == NULL) return 0;
  16. float audioBuffer[numSamples];
  17. double envelopeDuration=5, startingValue=0, endingValue=2;
  18. envelopeDuration *= SAMPLE_RATE;
  19. double envelopeValue = startingValue;
  20. double envelopeIncrement = ((endingValue - startingValue)/envelopeDuration);
  21. double carrierFrequency = 440,carrierAngle = 0;
  22. double modulatorFrequency, modulatorAngle = 0, modulationIndex = 2;
  23. double modulatorAmplitude = carrierFrequency * modulationIndex;
  24. int i;
  25. for(i=0; i<numSamples; i+=NUM_CHANNELS)
  26. {
  27. modulatorFrequency = carrierFrequency * envelopeValue;
  28. if(i/NUM_CHANNELS < envelopeDuration)envelopeValue += envelopeIncrement;
  29. audioBuffer[i] = sin(carrierAngle);
  30. carrierAngle += (carrierFrequency + sin(modulatorAngle) * modulatorAmplitude) * TWO_PI_OVER_SAMPLE_RATE;
  31. modulatorAngle += modulatorFrequency * TWO_PI_OVER_SAMPLE_RATE;
  32. }
  33. aiffAppendFloatingPointSamples(aiff, audioBuffer, numSamples, aiffFloatSampleType);
  34. aiffSaveWithFilename(aiff, "FrequencyRatioSweep.aif");
  35. aiffDestroy(aiff);
  36. return 0;
  37. }

Output:

Explanation of the Concepts

In this example we will explore what happens when the ratio between the carrier and modulator is manipulated in a frequency modulated timbre. Here, not only will the carrier frequency be modulated by the modulator, but the modulator will in turn be modulated by a linear envelope. This is conceptually identical to the "Accelerated Vibrato" example. The main difference is that here the envelope's values will not control the modulator frequency directly, but it will control the modulator frequency in terms of its ratio to the carrier frequency. The modulator will start at 0 times the carrier frequency, and progress linearly to twice the carrier frequency, or , in this case, 880 Hz. This means that at the very beginning of the audio file, the carrier frequency will be sub-audio, as it moves from 0 Hz up to about 22 Hz, which means that the beginning of the sound will be heard as vibrato. This is easy to hear in the aiff file.

It is interesting to observe how the timbre of the audio wave changes as the frequency ratio increases. We previously saw that amplitude modulating two sine waves results in a new pair of sine-waves whose frequencies are the sum and difference of the original frequencies. With frequency modulation, the result will be the carrier plus an infinite number of other sine wave pairs whose frequencies are equally spaced above an belowthe carrier frequency at integer multiples of the modulator frequency. These resultant frequency pairs are called 'sidebands', because there will be one on either side of the carrier. As an example, If the carrier is 400 Hz and the modulator is 220 Hz, Then we would expect to see the 440 Hz carrier, a sideband pair at 440+220 and 440-220, another pair at 440+(2*220) and 440-(2*220), and so forth. This is confirmed by the Fourier spectrograph of such a sound:

image

Subtracting larger and larger values from the carrier will eventually result in negative frequencies. This just means that the phase has been shifted by 180 degrees. It is just like a ferris wheel spinning in reverse, to use the analogy from the sine wave chapter. Where a positive and negative frequency collide (like 220 and -220 in the example above), they will have a tendency to cancel each other out, although the extent to which they are successful depends on the relative amplitude of each.

As the modulator increases in frequency, the sideband pairs become more widely spaced about the carrier. If the carrier, for instance has a frequency of 440 Hz, then the pairs will be spaced at even multiples of 440 Hz about the carrier:

image

or if the carrier has a frequency of 880 Hz, then the pairs will be spaced at even multiples of 880 Hz about the carrier:

image

Likewise for 1320 Hz:

image

Explanation of the Code

This example is Just like the previous example, except that the frequency ratio has been replaced by a linear envelope. The usual variables for the envelope are declared on lines 23-26. Line 35 sets the modulator frequency to the carrier frequency plus the current value of the envelope, and line 36 increments the value of the envelope.