Workshop 7

Audio Synthesis

Music Theory Primer

  • In Workshop 6, I mentioned that although pitch and frequency are closely related, a higher frequency does not imply a higher pitch (at least not a noticeably higher pitch)
  • This is because, like loudness (volume), pitch is a logarithmic scale
  • In fact, like loudness, the sound of one pitch is relative to the other
  • Pitch, for the large part is a discrete scale
  • Each element in the scale is called an semitone

Music Theory Primer

  • Western music, today, is clamped to 12 semitones
  • If, after counting from note 0 to 11, we hit the 12th tone, then, we are looking at a different octave. We are now hearing note 0 of octave 1
    • Not getting into the details, but the use of 12 semitones was for practical reasons; may or may not be for aesthetic reasons

Music Theory Primer

  • Although unintuitive, pitch is denoted in either of the following notations:
DoReMiFaSolLaSi/TiDo
CDEFGABC
0245791112
  • Do note that we are omitting several semitones in the above notations

Music Theory Primer

http://www.orgellexikon.gally.ch/grafiken/klaviatur.jpg

  • In the last slide, you saw that we omitted a few semitones
  • In the keyboard you see above, the white keys represents the semitones that we explicitly mentioned (at least in the letter notation)
  • Working from the 0th octave, the left-most key represents "Doh"/"C" (0)

Music Theory Primer

http://www.orgellexikon.gally.ch/grafiken/klaviatur.jpg

  • The black keys represents the "sharps" or "flats" of those semitones
    • Sharps are denoted by "#"
      • A sharp is a semitone above a particular semitone
    • Flats are denoted by "♭"
      • A flat is a semitone below a particular semitone
  • In order to notationally represent those missing semitones, we take the one above or below, and append either a "#" or a "♭" (e.g. C#, or B♭)

Music Theory Primer

http://www.orgellexikon.gally.ch/grafiken/klaviatur.jpg

  • To go further, for stylistic reasons, music can be clamped to a particular scale
  • When composing a tune using nothing but the white keys (e.g. C, D, E, F, G, A, B, C), we are composing a tune in the C major scale
  • We are not required to constrain ourselves to the C major scale (the white keys), we can try others

Music Theory Primer

C Major Scale

C D E F G A B C

Music Theory Primer

C Minor Scale

C D E♭ F G A♭ B♭ C

Music Theory Primer

C Minor Scale

C D E♭ F G A♭ B C

Music Theory Primer

C Minor Blues Scale

C E♭ F G♭ G B♭ C

Music Theory Primer

A Major

A B C# D E F# G# A

Music Synthesis

Sine Synth

  • To synthesize music, we will first need to get the musical semitone, and then derive a "period" of our synth
  • For the period, it's this:
    • b×2s
      • b is our base frequency
        • Typically, it's set to 440 (typically, A of octave 4)
      • s is an integer variable
        • s can also be summed with a constant integer term (typically 12 × 4 = 48)

Music Synthesis

  • We can think of a synth as a function that accepts two parameters, s and t, and which returns a number
    • That number represents an amplitude
  • Our synth is a periodic function (e.g. -cf(s, t) ≤ c)

Music Synthesis

Sine synth


function synth(s, t) {
  return Math.sin(440 * Math.pow(2, s) * t);
}

Music Synthesis

Sawtooth synth


function synth(s, t) {
  // Assuming we are working with CD quality.
  var input = (440 * Math.pow(2, s) * t)/44100;
  return input - Math.floor(input);
}

Music Synthesis

ADSR envelope

  • The synths in the last few slides are only intended to output amplitude
  • What we get is audio that is abruptly cut, but there isn't any smooth transition
  • the ADSR envolope is intended to apply that smooth transition from maximum amplitude, to entire silence
  • Think about how an accoustic piano sounds

Music Synthesis

ADSR envelope

  • Attack: the time in which to increase the volume to the max, as a note is being played
  • Decay: the time in which to decrease the value to the sustain level, after the max
  • Sustain: the level to decay to
  • Release: the time it takes to go entirely silent after releasing the key

These are not set in stone

Effects Composition

Modules

Effects Composition

Effects composition is modular

Effects

  • Gain: outputs a decibel scaling of an input audio
  • Cutoff: outputs audio that either cuts out particular frequencies, or applies an effect that gives the illusion where specific frequencies were cut out
  • Delay: outputs the input audio as is, as well as buffer the input. However, after a given time, it will pop out a buffered sample, scale it down, and sum it with the input sample before outputing it; will then buffer the result. Intended to mimic echo
  • Reverb: tries to mimic echo. Except, unlike delay, it actually tries to mimic the distortion that occurs when a sound is echoed back
    • Convolution is one way to do it. This is called a "convolution reverb"

Effects Composition in Web Audio API


var input = someActualAudioTrack()
var lowpass = audioContext.createBiquadFilter();
lowpass.type = 'lowpass';
lowpass.frequency.value = 100;
lowpass.gain.value = 0;
var delay = someDelayConstructor();
var reverb = someReverbConstructor();

input.connect(lowpass);
lowpass.connect(delay);
lowpass.connect(reverb);