Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created July 17, 2011 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordanorelli/1087639 to your computer and use it in GitHub Desktop.
Save jordanorelli/1087639 to your computer and use it in GitHub Desktop.
polyrythm impulse to wave pair example
class Voice
{
1.0 => static float multiplier;
1.0 => static float spread;
float freq;
float pan;
Impulse imp;
Pan2 p;
fun static Voice Voice(float freq, float pan)
{
Voice v;
freq => v.freq;
pan => v.pan;
spork ~ v.cycle();
return v;
}
fun static void increaseSpread()
{
Math.min(1.0, spread + 0.05) => spread;
}
fun static void decreaseSpread()
{
Math.max(0.0, spread - 0.05) => spread;
}
fun void cycle()
{
float targetFreq;
dur targetBeat;
imp => p => dac;
while(true)
{
freq * multiplier => targetFreq;
1::second / targetFreq => targetBeat;
targetBeat - (now % targetBeat) => now;
pan * spread => p.pan;
1.0 => imp.next;
}
}
}
fun void keyboardListener()
{
Hid hi;
HidMsg msg;
0 => int device; // the device number for the keyboard.
// It's generally either 0 or 1. For me it's 0.
if(!hi.openKeyboard(device)) me.exit();
<<< "keyboard '" + hi.name() + "' ready", "" >>>;
while(true)
{
hi => now;
while(hi.recv(msg))
{
if(msg.isButtonDown())
{
if(msg.ascii == 68) // d
2 *=> Voice.multiplier;
else if(msg.ascii == 72) // h
2 /=> Voice.multiplier;
else if(msg.ascii == 73) // i
Voice.decreaseSpread();
else if(msg.ascii == 79) // o
Voice.increaseSpread();
}
}
}
}
Voice.Voice(3.0, -1);
Voice.Voice(2.0, 1);
spork ~ keyboardListener();
while(true) { 100::ms => now; }
@md2perpe
Copy link

What language is this? Some special for a music device?

@jordanorelli
Copy link
Author

this is ChucK, a language for audio synthesis programming. It's quite fun!

http://chuck.cs.princeton.edu/

a user on the ChucK mailing list asked about creating a script that would demonstrate that a waveform, slowed down repeatedly, is analagous to a beat on another timescale. This example illustrates this principle by allowing the user to alter the speed that a pair of clicking noises is generated until it's fast enough to become a pair of tones.

@md2perpe
Copy link

Okey, thanks! I'm no music guy, but maybe I'll try it out, just to see how it work.

@jordanorelli
Copy link
Author

It has a really excellent concurrency model, and it's highly accurate in generating time-based events. So if you need to run a number of highly accurate, simultaneous timers for some reason, it's useful. You could, for example, run a timer in a ChucK process and have it generate OSC messages that are consumed by another process and interpreted, which is how people make visualizations, e.g. http://vimeo.com/17397162

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment