Shepard Tones

ChucK script for ascending Shepard tones (using a cosine envelope). I wrote it in the airport while reading "Music, Cognition, and Computerized Sound: An Introduction to Psychoacoustics." For fun!

6 => int count;
1.0 / (count $ float) => float gain;
27.5 => float base;
1.01 => float grow;
float cap;

// initialize oscillators
SinOsc s[count];
for(0 => int i; i < count; i++) {
  s[i] => dac;
  gain => s[i].gain;
  if(i == 0)
    base => s[i].freq;
  else
    s[i-1].freq() * 2 => s[i].freq;
  s[i].freq() * 2 => cap;
}

// gain envelope function
cap => float width;
fun float g(float freq) {
  return 0.5 * Math.cos((2 * Math.PI * freq) / width - Math.PI) + 0.5;
}

// synthesize!
do {
  for(0 => int i; i < count; i++) {
    s[i].freq() * grow => s[i].freq;
    if(s[i].freq() > cap)
      base => s[i].freq;
    7 * g(s[i].freq()) / (count $ float) => s[i].gain;
  }
} while(100::ms => now);

Comments

Click here to view the comments on this post.