A Chromatic Exercise Using std.mtof()
Now that Mikael Johansson pointed the way to the MIDI table, I thought it would be worthwhile to do a simple exercise that runs the full range of MIDI note values.
So here it is. chromatic.ck Just using a simple sine oscillator isn't very attractive, so I also did a version using the TubeBell STK function, modeled on the Replicant examples. tubebellChromatic.ck Permanent links are over there on the right, the new section of .CK Example links.
Here's the code for chromatic.ck:
// chromatic.ck -- run the full range of MIDI values
// author: Gary Williams (gwms[AT]corninglink{DOT}com)
0 => int i;
0.0 => float freq;
// make our patch
sinosc s => dac;
// do a random delay
fun int setdur() {
if( std.randf() > 0.7 )
{ 1000::ms => now; }
else if( std.randf() > .7 )
{ 500::ms => now; }
else if( std.randf() > -0.8 )
{ .250::second => now; }
else {
100::ms => now;
}
return(1);
}
// loop through chromatic range 0 - 127 and back down 127 - 0
for(; i < 128; i++) {
std.mtof( (float)i) => float freq;
freq => s.freq;
setdur();
chout => "i, freq: " => i => ", " => freq => endl;
}
// loop back to 0
for(; i > -1; i--) {
std.mtof( (float)i) => float freq;
freq => s.freq;
setdur();
chout => "i, freq: " => i => ", " => freq => endl;
}
// note change to random
"Change To Random" => stdout;
// our main loop
while( true )
{
std.rand2(0, 127) => i;
std.mtof( (float)i) => float freq;
freq => s.freq;
setdur();
chout => "i, freq: " => i => ", " => freq => endl;
}
On my system (a Sony VAIO laptop running Windows XP), the tones below 58 are inaudible, and the tones above around 120 are too. So for real use, the MIDI note range should probably be limited, based on the capability of the output devices.
Another thing I noticed while doing debug last night, the
stdout
print device doesn't seem to allow multiple value displays, but the chout.ck example shows the use of
chout => "string label" => v1 => "string separator" => v2 => endl;
which is really useful for debugging. I've always found that watching values on the fly is more valuable for fixing a new program than any static view, so having on-the-fly display verbs is helpful.
In the function programming I was doing yesterday, I did notice something that I hope gets added to the language: we need an
exit()
verb. At the end of the note_freq()
function, I needed to handle program errors (where the function gets called with illegal arguments). It's the kind of thing that's pretty common when you start a new program, but it's worth being able to do a quick exit with a message when the parameter tests fail.
<< Home