Skip to content

Instantly share code, notes, and snippets.

@jdpage
Last active August 29, 2015 14:02
Show Gist options
  • Save jdpage/5e0a316fcafd24a8fb23 to your computer and use it in GitHub Desktop.
Save jdpage/5e0a316fcafd24a8fb23 to your computer and use it in GitHub Desktop.
Two-Octave Shepherd Organ
// octave count?
2 => int octaves;
// device to open
0 => int device;
// get from command line
if( me.args() ) me.arg(0) => Std.atoi => device;
MidiIn min;
MidiMsg msg;
// try to open MIDI port (see chuck --probe for available devices)
if( !min.open( device ) ) me.exit();
// print out device that was opened
<<< "MIDI device:", min.num(), " -> ", min.name() >>>;
class StopEvent extends Event
{
int note;
}
Gain g => dac;
1.0 => g.gain;
StopEvent s;
fun void key(int k, int note)
{
note => Std.mtof => float pitch;
pitch / 261.625565 => Math.log2 => Std.fabs => float vol;
Moog hi => g;
1.0 => hi.filterSweepRate;
0.2 => hi.filterQ;
1.0 - Math.min(vol / 3, 1.0) => hi.gain;
pitch => hi.freq;
1.0 => hi.noteOn;
while (true)
{
s => now;
if (s.note == k)
break;
}
1.0 => hi.noteOff;
}
// infinite time loop
while( true )
{
// wait on midi event
min => now;
// get the midimsg
while( min.recv( msg ) )
{
// <<< msg.data1, msg.data2, msg.data3 >>>;
if (msg.data1 == 144)
{
spork ~ key(msg.data2, msg.data2);
msg.data2 - octaves * 12=> int m;
while (m > 12)
{
spork ~ key(msg.data2, m);
m - octaves * 12 => m;
}
msg.data2 + octaves * 12 => m;
while (m < 108)
{
spork ~ key(msg.data2, m);
m + octaves * 12 => m;
}
}
else if (msg.data1 == 128)
{
msg.data2 => s.note;
s.broadcast();
}
else if (msg.data1 == 176 && msg.data2 == 7)
{
msg.data3 / 128.0 => g.gain;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment