Advertisement
bennymartinson

ultra-conservative book organ

Jun 5th, 2012
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.19 KB | None | 0 0
  1. #include <GinSing.h>
  2. GinSing GS;
  3.  
  4. #define rcvPin 4
  5. #define sndPin 3
  6. #define ovfPin 2
  7.  
  8. #define boomPin 8
  9. #define chuckPin 9
  10. #define melodyPin 10
  11. #define stagePin 11
  12.  
  13. GinSingPoly * poly = 0x0;
  14.  
  15. GSNote pitchLookup[85] = { C_1, CS_1, D_1, DS_1, E_1, F_1, FS_1, G_1, GS_1, A_1, AS_1, B_1,
  16.                           C_2, CS_2, D_2, DS_2, E_2, F_2, FS_2, G_2, GS_2, A_2, AS_2, B_2,
  17.                           C_3, CS_3, D_3, DS_3, E_3, F_3, FS_3, G_3, GS_3, A_3, AS_3, B_3,
  18.                           C_4, CS_4, D_4, DS_4, E_4, F_4, FS_4, G_4, GS_4, A_4, AS_4, B_4,
  19.                           C_5, CS_5, D_5, DS_5, E_5, F_5, FS_5, G_5, GS_5, A_5, AS_5, B_5,
  20.                           C_6, CS_6, D_6, DS_6, E_6, F_6, FS_6, G_6, GS_6, A_6, AS_6, B_6,
  21.                           C_7, CS_7, D_7, DS_7, E_7, F_7, FS_7, G_7, GS_7, A_7, AS_7, B_7 };
  22.  
  23. #define _NUMCHORDS 4
  24. #define _NUMNOTESINCHORD 5
  25. #define _NUMNOTESINSCALE 8
  26.  
  27. int chords1[_NUMCHORDS][_NUMNOTESINCHORD] = { {-12, -17, 0, 3, 7},
  28.                                              {-12, -17, 0, 4, 10},
  29.                                              {-19, -13, 0, 5, 8},
  30.                                              {-11, -17, -1, 5, 7} };
  31. int chords2[_NUMCHORDS][_NUMNOTESINCHORD] = { {-12, -17, 0, 4, 7},
  32.                                               {-17, -10, -1, 2, 7},
  33.                                               {-10, -15, -3, 0, 6},
  34.                                               {-13, -6, -1, 3, 6} };
  35.                                              
  36. int chords3[_NUMCHORDS][_NUMNOTESINCHORD] = { {-19, -13, 0, 5, 8},
  37.                                               {-12, -17, 0, 4, 7},
  38.                                               {-21, -14, -2, 3, 6},
  39.                                               {-12, -19, -3, 0, 5} };
  40.  
  41. int (*chords)[_NUMNOTESINCHORD];
  42.                                            
  43.  
  44. int scales1[_NUMCHORDS][_NUMNOTESINSCALE] = { {-5, 0, 3, 7,    -4, -1, 2, 6},
  45.                                              {-5, -2, 4, 7,   -4, 0, 3, 6},
  46.                                              {-4, 0, 5, 8,    -5, -1, 3, 7},
  47.                                              {-5, -1, 1, 5,   -4, 0, 3, 7} };
  48. int scales2[_NUMCHORDS][_NUMNOTESINSCALE] = { {-5, 0, 4, 7,   -3, -2, 3, 6},
  49.                                               {-5, -1, 2, 7,  -3, 0, 4, 5},
  50.                                               {-3, 0, 2, 6,   -2, -1, 3, 7},
  51.                                               {-1, 3, 6, 9,   0, 4, 7, 11} };
  52.                                              
  53. int scales3[_NUMCHORDS][_NUMNOTESINSCALE] = { {-4, 0, 5, 8,   -1, 1, 4, 7},
  54.                                               {-5, 0, 4, 7,   -4, -1, 3, 5},
  55.                                               {-6, -2, 3, 6,  -3, -1, 2, 5},
  56.                                               {-7, -2, 2, 5,  -6, -3, 1, 3} };
  57.  
  58. int (*scales)[_NUMNOTESINSCALE];
  59.  
  60. #define _NUM_CHORDS_IN_SEQUENCE 12
  61. int chord_sequence1[_NUM_CHORDS_IN_SEQUENCE] = { 0, 0, 3, 0, 2, 0, 3, 1, 2, 0, 3, 0 };
  62. int chord_sequence2[_NUM_CHORDS_IN_SEQUENCE] = { 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 };
  63. int chord_sequence3[_NUM_CHORDS_IN_SEQUENCE] = { 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 };
  64. int (*chord_sequence);
  65.  
  66. int key = 36;
  67.  
  68. int prevValues[4] = {0,0,0,0};
  69. int curValues[4] = {0,0,0,0};
  70. int valuePins[4] = {boomPin, chuckPin, melodyPin, stagePin};
  71.  
  72. float distortion = 0.;
  73.  
  74. void setup()
  75. {
  76.   GS.begin(rcvPin, sndPin, ovfPin);
  77.  
  78.   Serial.begin(9600);
  79.   pinMode(7, INPUT);
  80.   pinMode(8, INPUT);
  81.  
  82.   poly = GS.getPoly();
  83.   poly->begin();
  84.   poly->setEnvelope(0, AT_8MS, 1.f, DR_292MS, 1.f, DR_145MS, 0.0f);
  85.   poly->setEnvelope(1, AT_8MS, .75f, DR_292MS, .4f, DR_292MS, 0.0f);
  86.   poly->setEnvelope(2, AT_8MS, .75f, DR_292MS, .4f, DR_292MS, 0.0f);
  87.   poly->setEnvelope(3, AT_8MS, .75f, DR_292MS, .4f, DR_292MS, 0.0f);
  88.   poly->release(4);
  89.   poly->setWaveform(0, TRIANGLE);
  90.  
  91.   advanceState();
  92. }
  93.  
  94. int chord = -1;
  95. int boomchuck = -1;
  96. int beat = -1;
  97.  
  98. int state = -1;
  99.  
  100.  
  101. int chord_counter = -1;
  102.  
  103. int cue = -1;
  104. int prev_pick = -1;
  105. int boomWait = 1000;
  106. int chuckWait = 1000;
  107. int melodyWait = 1000;
  108. GSNote pitch;
  109.  
  110. int nextMelodyNote = -1;
  111. int r;
  112.  
  113. void loop()
  114. {
  115.   int cue = waitForCue();
  116.   if (cue == 0) {
  117.     boomchuck = (boomchuck+1) % 2;
  118.     boomWait = 0;
  119.     if (boomchuck == 0) {
  120.       chord_counter = (chord_counter + 1) % _NUM_CHORDS_IN_SEQUENCE;
  121.       chord = chord_sequence[chord_counter];
  122.       if (chord_counter % 4 == 0 && state == 2) {
  123.         key = (key-2) % 12 + 36;
  124.         distortion += 0.05;
  125.         setDistortion(distortion);
  126.       }
  127.     }
  128.     pitch = pitchLookup[getPitchNum(chord, boomchuck)];
  129.      
  130.     poly->setNote(0, pitch);
  131.     poly->trigger(0);
  132.   } else if (cue == 1) {
  133.     for (int i=1; i<4; i++) {
  134.       pitch = pitchLookup[getPitchNum(max(chord, 0), i+1)];
  135.       poly->setNote(i, pitch);
  136.       poly->trigger(i);
  137.       chuckWait = 0;
  138.     }
  139.   } else if (cue == 2) {
  140.     if (nextMelodyNote < 0) {
  141.       r = prev_pick;
  142.       while (r % 4 == prev_pick) {
  143.         r = random(0, 4);
  144.         int r2 = random(0, 3);
  145.         if (r2 == 0) {
  146.           // play appogiatura
  147.           nextMelodyNote = r;
  148.           r += 4;
  149.         }
  150.       }
  151.     } else {
  152.       r = nextMelodyNote;
  153.       nextMelodyNote = -1;
  154.     }
  155.    
  156.     prev_pick = r;
  157.     if (state == 0) {
  158.       poly->setPortamento(4, 1., 0.3);
  159.     } else if (state == 2) {
  160.       poly->setPortamento(4, 1., 0.2);
  161.     } else {
  162.       poly->setPortamento(4, 1., 1);
  163.     }
  164.     pitch = pitchLookup[getScaleNum(max(chord, 0), r)];
  165.     poly->setNote(4, pitch);
  166.     poly->trigger(4);
  167.     melodyWait = 0;
  168.   } else if (cue == 3) {
  169.     advanceState();
  170.   }
  171.  
  172.   if (boomWait == 250) {
  173.     poly->release(0);
  174.   }
  175.   if (chuckWait == 50) {
  176.     poly->release(1);
  177.     poly->release(2);
  178.     poly->release(3);
  179.   }
  180.   if (melodyWait == 500) {
  181.     poly->release(4);
  182.   }
  183.   delay(10);
  184.   boomWait += 10;
  185.   chuckWait += 10;
  186.   melodyWait += 10;
  187. }
  188.  
  189. int waitForCue() {
  190.   while (true) {
  191.     for (int i=0; i<4; i++) {
  192.       prevValues[i] = curValues[i];
  193.       curValues[i] = digitalRead(valuePins[i]);
  194.       if (curValues[i] == 1 && prevValues[i] == 0) {
  195.         return i;
  196.       }
  197.     }
  198.     return -1;
  199.   }
  200. }
  201.  
  202. void advanceState() {
  203.   chord = -1;
  204.   boomchuck = -1;
  205.   beat = -1;
  206.   chord_counter = -1;
  207.   state = (state+1) % 3;
  208.   if (state == 0) {
  209.     distortion = 0.;
  210.     setDistortion(distortion);
  211.     key = 36;
  212.     chords = chords1;
  213.     scales = scales1;
  214.     chord_sequence = chord_sequence1;
  215.   } else if (state == 1) {
  216.     distortion = 0.3;
  217.     setDistortion(distortion);
  218.     key += 6;
  219.     chords = chords2;
  220.     scales = scales2;
  221.     chord_sequence = chord_sequence2;
  222.   } else if (state == 2) {
  223.     distortion = 0.;
  224.      setDistortion(distortion);
  225.     chords = chords3;
  226.     scales = scales3;
  227.     chord_sequence = chord_sequence3;
  228.   }
  229. }
  230.  
  231. int getPitchNum(int chordNum, int noteNum) {
  232.   return chords[chordNum][noteNum % _NUMNOTESINCHORD] + key + floor(noteNum/_NUMNOTESINCHORD)*12;
  233. }
  234.  
  235. int getScaleNum(int chordNum, int noteNum) {
  236.   return scales[chordNum][noteNum % _NUMNOTESINSCALE] + key + 12 + floor(noteNum/_NUMNOTESINSCALE)*12;
  237. }
  238.  
  239. void setDistortion(float amt) {
  240.   for (int i=0; i<5; i++) {
  241.     poly->setFreqDist(i, amt);
  242.   }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement