View Raw SPL
/*****************************************************************************
*                                                                            *
*   GNOTES.SPL    Copyright (C) 1997 DSP Development Corporation             *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:       Randy Race                                                 *
*                                                                            *
*   Synopsis:     Generates notes of the chromatic and other scales          *
*                                                                            *
*   Revisions:    27 May 1997  RRR  Creation                                 *
*                                                                            *
*****************************************************************************/


/* 
 *  Generates a list of global hot variables of the chromatic scale.
 *  Base is normally set to 440, i.e. concert pitch "A".
 */


/* undefine e, Euler's number macro */
#undef e

/* macro to create a note based on the frequency */
#define note(f) gsin(nlen, 1/nrate, f)


gnotes(base = 440)
{
        /* undefine e, Euler's number macro */
        undefmacro("e");

        /* set hotvar a0 to value of base */
        eval(strcat("sethotvar(a0, ", strnum(base), ")"));

        /* global variable - ratio between semi-tones */
        setvar(k, 2.0^(1/12.0));

        sethotvar(a0s, k^1  * a0); /* A# */
        sethotvar(b0,  k^2  * a0); /* B  */

        sethotvar(c,   k^3  * a0); /* middle C */
        sethotvar(cs,  k^4  * a0); /* C# */
        sethotvar(d,   k^5  * a0); /* D  */
        sethotvar(ds,  k^6  * a0); /* D# */
        sethotvar(e,   k^7  * a0); /* E  */
        sethotvar(f,   k^8  * a0); /* F  */
        sethotvar(fs,  k^9  * a0); /* F# */
        sethotvar(g,   k^10 * a0); /* G  */
        sethotvar(gs,  k^11 * a0); /* G# */
        sethotvar(a,   k^12 * a0); /* A  */
        sethotvar(as,  k^13 * a0); /* A# */
        sethotvar(b,   k^14 * a0); /* C# */
        sethotvar(c2,  k^15 * a0); /* high C */

        sethotvar(nrate, 10000);   /* sample rate for generated notes */
        sethotvar(nlen,   5000);   /* length for generated notes */
}


/* cmaj scale */
gcmajscale()
{
        return(gscale(gfcmaj()));
}


/* cmaj chord */
gcmaj()
{
        return(note(c)+note(e)+note(g));
}


/* cmin chord */
gcmin()
{
        return(note(c)+note(ds)+note(g));
}


/* next octave */
gnotes2(base)
{
        sethotvar(c2s, k^16 * a0); /* C# */
        sethotvar(d2,  k^17 * a0); /* D  */
        sethotvar(d2s, k^18 * a0); /* D# */
        sethotvar(e2,  k^19 * a0); /* E  */
        sethotvar(f2,  k^20 * a0); /* F  */
        sethotvar(f2s, k^21 * a0); /* F# */
        sethotvar(g2,  k^22 * a0); /* G  */
        sethotvar(g2s, k^23 * a0); /* G# */
        sethotvar(a2,  k^24 * a0); /* A  */
        sethotvar(a2s, k^25 * a0); /* A# */
        sethotvar(b2,  k^26 * a0); /* B  */
        sethotvar(c4,  k^27 * a0); /* C  */
}


/* generate Pythagorean scale */
gharm(base)
{
        local a, b, c, k, r;

        a = {base, 
                    base * 9  / 8,
                    base * 5  / 4,
                    base * 4  / 3,
                    base * 3  / 2,
                    base * 5  / 3,
                    base * 15 / 8,
                    base * 2};

        /* 16 note harmonic scale */
        b = {};
        r = 2^(1/16);

        loop (k = 0..15)
        {
                b = concat(b, {base * r^k});
        }

        /* 12 note chromatic scale */
        c = {};
        r = 2^(1/12);

        loop (k = 0..11)
        {
                c = concat(c, {base * r^k});
        }

        setvunits(a, "Natural");
        setvunits(b, "16 note");
        setvunits(c, "Chromatic");

        return(a, b, c);
}


/* generate frequencies of middle C major scale */
gfcmaj()
{
        local s;

        s = {c, d, e, f, g, a, b, c2};
        setvunits(s, "Hertz");
        return(s);
}


/* generate frequencies of middle C chromatic scale */
gfchrom()
{
        local s;

        s = {c, cs, d, ds, e, f, fs, g, gs, a, as, b, c2};
        setvunits(s, "Hertz");
        return(s);
}


/* generate frequencies of high C chromatic scale */
gfchrom2()
{
        local s;

        s = {c2s, d2, d2s, e2, f2, f2s, g2, g2s, a2, a2s, b2, c4};
        setvunits(s, "Hertz");
        return(s);
}
 

gscale(f)
{
        local k, s;

        s = {};
        setdeltax(s, 1/nrate);

        loop (k = 1..length(f))
        {
                s = concat(s, note(f[k]));
        }

        return(s);
}