ChucK/foo.ck

From CSWiki
Revision as of 10:54, 5 September 2008 by Tom (talk | contribs) (formatting)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
// Boolean_Quartet.ck
// copyright 2007 Les Hall
// This software released under the GNU General Protective License
// experiments with creating music from base n sequences
// four instruments in Boolean logic sequence

// control variables
150 => float frequency;
0.25 => float gain1;  // SinOsc
0.25 => float gain2;  // HF plucked
0.25 => float gain3;  // LF plucked
0.15 => float gain4;  // guitar
2 => int n; // numeric base of the sequence

// gain of each instrument
Gain g1;
Gain g2;
Gain g3;
Gain g4;
gain1 => g1.gain;  // binary count SinOsc's
gain2 => g2.gain;  // high frequency plucked instrument
gain3 => g3.gain;  // low frequency plucked instrument
gain4 => g4.gain;  // screaming guitar instrument

// the background oscillators
SinOsc s[5];
for (0 => int i; i<5; i++) {
    // the patches
    s[i] => g1 => dac;
    // the frequencies
    (5 - i) * frequency => s[i].freq;
}

// The plucked treble instrument
Impulse i1;
JCRev r1;
r1.mix(0.02);
BPF f[5];
i1 => f[0] => r1 => g2 => dac;
i1 => f[1] => r1 => g2 => dac;
i1 => f[2] => r1 => g2 => dac;
i1 => f[3] => r1 => g2 => dac;
i1 => f[4] => r1 => g2 => dac;
for (0 => int i; i < 5; i++) {
    // the patches
    //i1 => BPF f[i] => r1 => dac;
    // the quality factors
    100 => f[i].Q;
    // the frequencies
    (i + 1) * frequency => f[i].freq;
}
// the gains
0.5 => f[0].gain;
1.0 => f[1].gain;
0.5 => f[2].gain;
0.5 => f[3].gain;
0.4 => f[4].gain;

// the plucked bass instrument
Impulse i2;
JCRev r2;
r2.mix(0.05);
BPF fb[5];
// the patches
i2 => fb[0] => r2 => g3 => dac;
i2 => fb[1] => r2 => g3 => dac;
i2 => fb[2] => r2 => g3 => dac;
i2 => fb[3] => r2 => g3 => dac;
i2 => fb[4] => r2 => g3 => dac;
for (0 => int i; i < 5; i++) {
    // the quality factors
    200 => fb[i].Q;
    // the frequencies
    (i + 1) * frequency => f[i].freq;
    // the gains
    2.0 / (i + 1) => f[i].gain;
}

// the screaming guitar instrument
SinOsc sg[5];
JCRev rg;
0.20 => rg.mix;
for (0 => int i; i<5; i++) {
    // the patches
    sg[i] => rg => g4 => dac;
    // the frequencies
    (i + 1) * frequency => sg[i].freq;
}
// the gains
1.0 => sg[0].gain;
0.8 => sg[1].gain;
0.6 => sg[2].gain;
0.4 => sg[3].gain;
0.2 => sg[4].gain;


// time loop
while (true) {
    // print the base for each sequence
    <<< "base", n >>>;

    // calculate the logic threshold
    (n-1) / 2.0 => float t;

    // loop in a 5-bit binary sequence and pluck the strings
    for (0 => int j1; j1 < n; j1++) {
        j1 / 16.0 / (n-1) => s[0].gain;
        for (0 => int j2; j2 < n; j2++) {
            j2 / 8.0 / (n-1) => s[1].gain;
            for (0 => int j3; j3 < n; j3++) {
                j3 / 4.0 / (n-1) => s[2].gain;
                for (0 => int j4; j4 < n; j4++) {
                    j4 / 2.0 / (n-1) => s[3].gain;
                    if (j4 > t) {
                        // the low frequency plucked instrument
                        (1.5 * (2 * (n - 1) + j3) / (3 *(n - 1))) * frequency => float freq;
                        for (0 => int i; i < 5; i++) {
                            (i + 1) * freq => fb[i].freq;
                        }
                        1000.0 => i2.next;
                    }
                    for (0 => int j5; j5 < n; j5++) {
                        j5 / 1.0 / (n-1) => s[4].gain;
                        // the high frequency plucked instrument
                        if ((j2 > t) && (j4 > t) || (j5 > t) && (j3 > t)) {
                            (2 * (j1 + j2 + j3 + j4 + j5) / (5 * (n - 1))) * frequency => float freq;
                            for (0 => int i; i < 5; i++) {
                                (i + 1) * freq => f[i].freq;
                            }
                            300.0 => i1.next;
                        }
                        // the screaming guitar
                        if ((j3 > t) && (j4 > t)) {
                            (0.5 * ((j1 > t) + (j2 > t) + (j3 > t) + (j4 > t) + (j5 > t))) * frequency => float freq;
                            for (0 => int i; i<5; i++) {
                                (i + 1) * freq => sg[i].freq;
                            }
                        }
                        (n - j5) / (n $ float) * gain4 => g4.gain;
                        100::ms => now; // advance time
                    }
                }
            }
        }
    }

    // shut off the oscillators
    for (0 => int i; i < 5; i++) {
        0 => s[i].gain;
    }
    0 => g4.gain;

    // increment the base number
    n++;

    // advance time between binary counts
    <<< "pause" >>>;
    3::second => now;
}