Link to all related files (chuck, audio files, instructions, scores)
ChucK Code for the instrument. A Beginning.
NOTE: All the initial variable declarations are at the bottom, to make this web page more about functions than variable declaration.
while(true) {
1::day => now;
}
fun void LeftBell() {
while(true) {
if (vayprev > yThreshold) {
<<< vay >>>;
if (ring == 0 && (vayAvg - vayPrevAvg) > yDiff){
<<<"DIFF: ",(vayAvg - vayPrevAvg)>>>;
1=>ring;
}
if(ring && (vayAvg - vayPrevAvg) < yDiff ){
<<<"AAding!", vayprev>>>;
1.0 => bell1.rate;
vayprev*20 => bell1.gain;
0 => bell1.pos;
50::ms => now;
0=>ring;
//wipeMemory();
} //<<<"DIFF: ",(vayAvg - vayPrevAvg)>>>;
}
vay => vayprev;
5::ms => now;
}
}
fun void RightBell() {
while(true) {
if (vbyprev > yThreshold) {
<<< vby >>>;
if (ring2 == 0 && (vbyAvg - vbyPrevAvg) > yDiff){
<<<"DIFF: ",(vbyAvg - vbyPrevAvg)>>>;
1=>ring2;
}
if(ring2 && (vbyAvg - vbyPrevAvg) < yDiff ){
<<<"AAding!", vbyprev>>>;
1.0 => bell2.rate;
vbyprev*20 => bell2.gain;
0 => bell2.pos;
50::ms => now;
0=>ring2;
//wipeMemory();
} //<<<"DIFF: ",(vayAvg - vayPrevAvg)>>>;
}
vby => vbyprev;
5::ms => now;
}
}
fun void GetJoystickInput() {
while( true )
{
// wait on HidIn as event
hi => now;
// messages received
while( hi.recv( msg ) )
{
// dual joysticks axis motion
if( msg.isAxisMotion() )
{
if( msg.which == 0 )
{
ax => axprev;
msg.axisPosition => ax;
Math.fabs(ax - axprev) => vax;
}
else if( msg.which == 1 )
{
ay => ayprev;
msg.axisPosition => ay;
Math.fabs(ay - ayprev) => vay;
if(vay != vayprev){
vayPrevs[i] => vayPrevPrevs[i];
vay => vayPrevs[i];
i++;
if (i == keepInMem) 0 => i;
calcAvgs();
}
}
else if( msg.which == 2 )
{
az => azprev;
msg.axisPosition*-1 => az;
Math.fabs(az - azprev) => vaz;
}
else if( msg.which == 3 )
{
bx => bxprev;
msg.axisPosition => bx;
Math.fabs(bx - bxprev) => vbx;
}
else if( msg.which == 4 )
{
by => byprev;
msg.axisPosition => by;
Math.fabs(by - byprev) => vby;
if(vby != vbyprev){
vbyPrevs[n] => vbyPrevPrevs[n];
vby => vbyPrevs[n];
n++;
if (n == keepInMem) 0 => n;
calcBAvgs();
}
}
else if( msg.which == 5 )
{
bz => bzprev;
msg.axisPosition*-1 => bz;
Math.fabs(bz - bzprev) => vbz;
}
}
// footpedal message
else if( msg.isButtonDown() )
{
1 => fp;
<<< "footpedal depressed " + fp >>>;
}
else if( msg.isButtonUp() )
{
0 => fp;
<<< "footpedal released " + fp >>>;
}
}
}
}
fun void calcAvgs(){
float curAvgPrev, curAvgPrevPrev;
for (int j; j < keepInMem; j++){
curAvgPrev + vayPrevs[j] => curAvgPrev;
curAvgPrevPrev + vayPrevPrevs[j] => curAvgPrevPrev;
}
curAvgPrev/keepInMem => vayAvg;
curAvgPrevPrev/keepInMem => vayPrevAvg;
}
fun void calcBAvgs(){
float curAvgPrev, curAvgPrevPrev;
for (int j; j < keepInMem; j++){
curAvgPrev + vbyPrevs[j] => curAvgPrev;
curAvgPrevPrev + vbyPrevPrevs[j] => curAvgPrevPrev;
}
curAvgPrev/keepInMem => vbyAvg;
curAvgPrevPrev/keepInMem => vbyPrevAvg;
}
Hid hi; HidMsg msg;
// all joystick values are roughly normalized -1 to 1
float ax; // left joystick x axis
float ay; // left joystick y axis
float az; // left joystick z axis
float bx; // right joystick x axis
float by; // right joystick y axis
float bz; // right joystick z axis
0 => int fp;
float axprev, ayprev, azprev, bxprev, byprev, bzprev;
float vax, vay, vaz, vbx, vby, vbz;
float vaxprev, vayprev, vazprev, vbxprev, vbyprev, vbzprev;
float vaxprevprev, vayprevprev, vayprevprevprev, vazprevprev, vbxprevprev, vbyprevprev, vbzprevprev;
float vayAvg;//Keeps an average of the last vay, vay-1, vay-2
float vayPrevAvg; //Keeps an average of the last vay-3, vay-4, vay-5
float vbyAvg, vbyPrevAvg; //More of the same
6 => int keepInMem;
float vayPrevs[keepInMem];
float vayPrevPrevs[keepInMem];
float vbyPrevs[keepInMem];
float vbyPrevPrevs[keepInMem];
int i, n; //global for loop? Wow style != good.
SndBuf bell1 => dac;
SndBuf bell2 => dac;
"PATHNAME" => bell1.read;
"PATHNAME" => bell2.read;
1.0 => bell1.gain;
1.0 => bell2.gain;
// which joystick 0 => int device;
// get from command line if( me.args() ) me.arg(0) => Std.atoi => device;
// open joystick 0, exit on fail if( !hi.openJoystick( device ) ) me.exit();
<<< "joystick '" + hi.name() + "' ready", "" >>>;
spork ~GetJoystickInput();
spork ~LeftBell();
spork ~RightBell();
0 => int ring;
0 => int ring2;
0.01 => float yThreshold;
0.01 => float yDiff;