Thursday, May 31, 2012

Hardcoded class of musical notes

Here is a class I've written using the chart located on this page: http://www.zytrax.com/tech/audio/audio.html#notes. The explanation in the comments should hopefully be sufficient to understand how to use it. It should be helpful for anyone wanting to work with creating music in ChucK. I may in the future add some scales to it, but since there is such a wide variety
of scales available in music it would probably be more efficient for those to be added by the individual. For scales, using an array would probably be the best way to go. The code is below the jump.





//This is a class that holds the frequency values of all notes
//from octaves 0 through 8, including sharps and flats.
//The values can be accessed for an instance of the class x
//by calling, for example, x.A3, or x.D2s (for D2 sharp).
//There are currently no flats implemented--instead, use equivalent
//notes. For example, Dsharp = Eflat.
//While all these notes can be calculated from just a few values
//using some formulas, many decimal points would be involved so it's
//cleaner to just use these hardcoded numbers.

class Notes{
    
    //C notes
    16 => int C0;
    33 => int C1;
    65 => int C2;
    131 => int C3;
    262 => int C4;
    523 => int C5;
    1047 => int C6;
    2093 => int C7;
    4186 => int C8;
    
    //C# notes
    17 => int C0s;
    35 => int C1s;
    69 => int C2s;
    139 => int C3s;
    277 => int C4s;
    554 => int C5s;
    1109 => int C6s;
    2217 => int C7s;
    4435 => int C8s;
    
    //D notes
    18 => int D0;
    37 => int D1;
    73 => int D2;
    147 => int D3;
    294 => int D4;
    587 => int D5;
    1175 => int D6;
    2349 => int D7;
    4699 => int D8;
    
    //D# notes
    19 => int D0s;
    39 => int D1s;
    78 => int D2s;
    156 => int D3s;
    311 => int D4s;
    622 => int D5s;
    1245 => int D6s;
    2489 => int D7s;
    4978 => int D8s;
    
    //E notes
    21 => int E0;
    41 => int E1;
    82 => int E2;
    165 => int E3;
    330 => int E4;
    659 => int E5;
    1319 => int E6;
    2637 => int E7;
    5274 => int E8;
    
    //F notes
    22 => int F0;
    44 => int F1;
    87 => int F2;
    175 => int F3;
    349 => int F4;
    698 => int F5;
    1397 => int F6;
    2794 => int F7;
    5588 => int F8;
    
    //F# notes
    23 => int F0s;
    46 => int F1s;
    93 => int F2s;
    185 => int F3s;
    370 => int F4s;
    740 => int F5s;
    1480 => int F6s;
    2960 => int F7s;
    5920 => int F8s;
    
    //G notes
    25 => int G0;
    49 => int G1;
    98 => int G2;
    196 => int G3;
    392 => int G4;
    784 => int G5;
    1568 => int G6;
    3136 => int G7;
    6272 => int G8;
    
    //G# notes
    26 => int G0s;
    52 => int G1s;
    104 => int G2s;
    208 => int G3s;
    415 => int G4s;
    831 => int G5s;
    1661 => int G6s;
    3322 => int G7s;
    6645 => int G8s;
    
    //A notes
    28 => int A0;
    55 => int A1;
    110 => int A2;
    220 => int A3;
    440 => int A4;
    880 => int A5;
    1760 => int A6;
    3520 => int A7;
    7040 => int A8;
    
    //A# notes
    29 => int A0s;
    58 => int A1s;
    117 => int A2s;
    233 => int A3s;
    466 => int A4s;
    932 => int A5s;
    1864 => int A6s;
    3729 => int A7s;
    7459 => int A8s;
    
    //B notes
    31 => int B0;
    62 => int B1;
    123 => int B2;
    247 => int B3;
    493 => int B4;
    988 => int B5;
    1976 => int B6;
    3951 => int B7;
    7902 => int B8;
    
}

// Test code for the class
SinOsc s => Gain g => dac;
//turn the gain down to prevent too-loud noises
.1 => g.gain;
new Notes @=> Notes x;

// Change the value of s.freq to test out different notes
while( true ) {
    x.A4s => s.freq;
    200::ms => now;
    
}



No comments:

Post a Comment