Public paste
Undefined
By: Guest | Date: Oct 29 2024 18:06 | Format: None | Expires: never | Size: 8.92 KB | Hits: 58

  1. //#include <Wire.h>
  2. //#include <Adafruit_GFX.h>
  3. //#include <Adafruit_SSD1306.h>
  4. //#include <MozziGuts.h>
  5. #include <Oscil.h>                // oscillator template
  6. #include <tables/square_no_alias_2048_int8.h>  // sine table for oscillator
  7. #include <tables/sin2048_int8.h>  // sine table for oscillator
  8. #include <tables/triangle_valve_2_2048_int8.h>  // sine table for oscillator
  9. #include <ResonantFilter.h>
  10. #define SCREEN_WIDTH 128
  11. #define SCREEN_HEIGHT 32
  12. #define OLED_RESET     -1
  13. //#define SCREEN_ADDRESS 0x3C
  14. #define INPUT_DELAY_MS 250
  15.  
  16. #include <tables/cos512_int8.h> // for filter modulation
  17. typedef struct{
  18.   unsigned long timestamp;
  19.   bool active;
  20. }timedBeat;
  21.  
  22. typedef struct{
  23.   int duration;
  24.   int freqModifier;
  25.   int frequency;
  26.   int initialFrequency;
  27. }trackSettings;
  28.  
  29. //Pin connected to latch pin (ST_CP) of 74HC595
  30. const int latchPin = 8;
  31. //Pin connected to clock pin (SH_CP) of 74HC595
  32. const int clockPin = 12;
  33. ////Pin connected to Data in (DS) of 74HC595
  34. const int dataPin = 11;
  35.  
  36. // use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file of table #included above
  37. //Oscil<SIN2048_NUM_CELLS, AUDIO_RATE> bSin(SIN2048_DATA);
  38. // use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file of table #included above
  39. Oscil<SQUARE_NO_ALIAS_2048_NUM_CELLS, AUDIO_RATE> aSin(SQUARE_NO_ALIAS_2048_DATA);
  40. //Oscil<TRIANGLE_VALVE_2_2048_NUM_CELLS, AUDIO_RATE> aSin(TRIANGLE_VALVE_2_2048_DATA);
  41. //Oscil<SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);
  42. //Oscil<SQUARE_NO_ALIAS_2048_NUM_CELLS, AUDIO_RATE> bSin(SQUARE_NO_ALIAS_2048_DATA);
  43. Oscil<SIN2048_NUM_CELLS, AUDIO_RATE> bSin(SIN2048_DATA);
  44. Oscil<SIN2048_NUM_CELLS, AUDIO_RATE> cSin(SIN2048_DATA);
  45. //Oscil<TRIANGLE_VALVE_2_2048_NUM_CELLS, AUDIO_RATE> bSin(TRIANGLE_VALVE_2_2048_DATA);
  46. // use #define for CONTROL_RATE, not a constant
  47. #define CONTROL_RATE 128  // Hz, powers of 2 are most reliable
  48.  
  49. unsigned long origin = 0;
  50. volatile unsigned long beatDelta = 150;
  51. volatile unsigned long duration = 100;
  52. unsigned long dT = 0;
  53. int initialFreq = 220;
  54. int if2 = 250;
  55. int f2 = if2;
  56. int fa = 5;
  57. int fb = 34;
  58. int freQ = initialFreq;
  59. int idx = 0;
  60.  int aState;
  61.  int aLastState;
  62.  
  63. //Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  64. // PL pin 1
  65. const int load = 7;
  66. // CE pin 15
  67. const int clockEnablePin = 4;
  68. const int switchPinA = 3;
  69. const int switchPinB = 2;
  70. // Q7 pin 7
  71. const int dataIn = 5;
  72. // CP pin 2
  73. const int clockIn = 6;
  74. int numberToDisplay = 0;
  75. int currentTrack = 0;
  76. bool active=false;
  77. trackSettings voices[]{
  78.   {100,1,82,82},
  79.   {60,3,260,260},
  80.   {60,2,392,392}
  81. };
  82. timedBeat tracks[][8]{
  83.   {
  84.   {0.0,false},
  85.   {0.0,false},
  86.   {0.0,false},
  87.   {0.0,false},
  88.   {0.0,false},
  89.   {0.0,false},
  90.   {0.0,false},
  91.   {0.0,false}
  92. },{
  93.   {0.0,false},
  94.   {0.0,false},
  95.   {0.0,false},
  96.   {0.0,false},
  97.   {0.0,false},
  98.   {0.0,false},
  99.   {0.0,false},
  100.   {0.0,false}
  101. },{
  102.   {0.0,false},
  103.   {0.0,false},
  104.   {0.0,false},
  105.   {0.0,false},
  106.   {0.0,false},
  107.   {0.0,false},
  108.   {0.0,false},
  109.   {0.0,false}
  110. }
  111. };
  112. bool activation[]{false,false,false};
  113. timedBeat beatArray[]{
  114.   {0.0,false},
  115.   {0.0,false},
  116.   {0.0,false},
  117.   {0.0,false},
  118.   {0.0,false},
  119.   {0.0,false},
  120.   {0.0,false},
  121.   {0.0,false}
  122. };
  123.  
  124. timedBeat inputButton={0.0,false};
  125.  
  126. uint8_t resonance1 = 180;
  127. LowPassFilter lpf1;
  128. Oscil<COS512_NUM_CELLS, CONTROL_RATE> kFilterMod1(COS512_DATA);
  129.  
  130. void setup() {
  131.     Serial.begin(9600);
  132.  
  133.   pinMode(latchPin, OUTPUT);
  134.   pinMode(clockPin, OUTPUT);
  135.   pinMode(dataPin, OUTPUT);
  136.   pinMode(load, OUTPUT);
  137.   pinMode(clockEnablePin, OUTPUT);
  138.   pinMode(clockIn, OUTPUT);
  139.   pinMode(switchPinA, INPUT);
  140.   pinMode(switchPinB, INPUT);
  141.   pinMode(dataIn, INPUT);
  142.   startMozzi(CONTROL_RATE);  // :)
  143.   aSin.setFreq(voices[0].frequency);         // set the frequency
  144.   bSin.setFreq(voices[1].frequency);
  145.   cSin.setFreq(voices[2].frequency);
  146.   origin = millis();
  147.  
  148.   kFilterMod1.setFreq(1.3f);
  149.   //attachInterrupt(1, switchTrack, RISING );
  150.   //attachInterrupt(0, decreaseTempo, RISING );
  151. }
  152. void updateControl() {
  153.   //  updateRegisters();
  154.   //  updateBeatsOutput();
  155.   //  updateLeds();  
  156.   checkButton();
  157.   updateTracksRegisters();
  158.   updateTracksOutput();
  159.   updateTrackLeds();
  160. }
  161.  
  162. void increaseTempo(){
  163.   Serial.print("A");
  164.   beatDelta +=1;
  165.   duration +=1;
  166. }
  167. void decreaseTempo(){
  168.    Serial.print("B");
  169.   beatDelta -=1;
  170.   duration -=1;
  171. }
  172. void checkButton(){
  173.  
  174.   unsigned long time = millis();
  175.   if((digitalRead(switchPinA)==1)&&time-inputButton.timestamp>INPUT_DELAY_MS){
  176.       inputButton.timestamp=time;
  177.       currentTrack+=1;
  178.       if(currentTrack>2)currentTrack=0;
  179.     }
  180. }
  181. void switchTrack(){
  182.   currentTrack+=1;
  183.  if(currentTrack>2)currentTrack=0;
  184.  Serial.print("current track : "+currentTrack);
  185. }
  186. void updateControls(){
  187.   aState = digitalRead(switchPinA); // Reads the "current" state of the outputA
  188.    // If the previous and the current state of the outputA are different, that means a Pulse has occured
  189.    if (aState != aLastState){    
  190.      // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
  191.      if (digitalRead(switchPinB) != aState) {
  192.   beatDelta +=1;
  193.   duration +=1;
  194.      } else {
  195.   beatDelta -=1;
  196.   duration -=1;
  197.      }
  198.    }
  199.    aLastState = aState;
  200.  
  201. }
  202. void updateTracksOutput(){
  203.  
  204.  unsigned long now = millis();
  205.  dT = now - origin;
  206.     if (dT >= beatDelta) {
  207.       origin = now;
  208.       dT=0;
  209.       if (idx == 7) idx = 0;
  210.       else idx +=1;
  211.    }
  212. int j =0;
  213. for(j=0;j<3;j++){
  214.   if(tracks[j][idx].active){
  215.          if (tracks[j][idx].active) {
  216.       if (dT >= voices[j].duration) {
  217.         activation[j] = false;
  218.       } else {
  219.         activation[j] = true;
  220.     }
  221.   }
  222.   else {
  223.     activation[j] = false;
  224.   }
  225.  
  226.   if(activation[j]){
  227.     voices[j].frequency-=voices[j].freqModifier;
  228.   }else{
  229.     voices[j].frequency=voices[j].initialFrequency;
  230.   }}
  231.  
  232. }
  233.    aSin.setFreq(voices[0].frequency);
  234.   bSin.setFreq(voices[1].frequency);
  235.   cSin.setFreq(voices[2].frequency);
  236. }
  237. void updateTracksRegisters(){
  238.   // Write pulse to load pin
  239.   digitalWrite(load, LOW);
  240.   delayMicroseconds(5);
  241.   digitalWrite(load, HIGH);
  242.   delayMicroseconds(5);
  243.   // Get data from 74HC165
  244.   digitalWrite(clockIn, HIGH);
  245.   digitalWrite(clockEnablePin, LOW);
  246.         uint8_t i;
  247.   unsigned long time = millis();
  248.   String val = "";
  249.         for (i = 0; i < 8; ++i) {
  250.                 digitalWrite(clockIn, HIGH);
  251.     if((digitalRead(dataIn)==1)&&time-tracks[currentTrack][i].timestamp>INPUT_DELAY_MS){
  252.       tracks[currentTrack][i].active=!tracks[currentTrack][i].active;
  253.       tracks[currentTrack][i].timestamp=time;
  254.     }
  255.    
  256.     val = val + (tracks[currentTrack][i].active?"1":"0");
  257.                 digitalWrite(clockIn, LOW);
  258.         }
  259.   digitalWrite(clockEnablePin, HIGH);
  260.  
  261. }
  262. void updateTrackLeds(){
  263.   digitalWrite(latchPin, LOW);
  264.   uint8_t i;
  265.         for (i = 0; i < 8; i++)  {
  266.     if(tracks[currentTrack][i].active){
  267.                         digitalWrite(dataPin, HIGH);
  268.     }else{digitalWrite(dataPin, LOW);}
  269.                 digitalWrite(clockPin, HIGH);
  270.                 digitalWrite(clockPin, LOW);           
  271.         }
  272.   digitalWrite(latchPin, HIGH);
  273. }
  274. void updateBeatsOutput(){
  275.  unsigned long now = millis();
  276.  
  277.    dT = now - origin;
  278.     if (dT >= beatDelta) {
  279.       origin = now;
  280.       dT=0;
  281.       if (idx == 7) idx = 0;
  282.       else idx +=1;
  283.    }
  284.      if ( beatArray[idx].active) {
  285.       if (dT >= duration) {
  286.         active = false;
  287.       } else {
  288.         active = true;
  289.     }
  290.   }
  291.   else {
  292.     active = false;
  293.   }
  294.   if(active){
  295.     freQ -=fa;
  296.   }else{
  297.     freQ = initialFreq;
  298.   }
  299.  
  300.   aSin.setFreq(freQ);
  301. }
  302. void updateRegisters(){
  303.   // Write pulse to load pin
  304.   digitalWrite(load, LOW);
  305.   delayMicroseconds(5);
  306.   digitalWrite(load, HIGH);
  307.   delayMicroseconds(5);
  308.   // Get data from 74HC165
  309.   digitalWrite(clockIn, HIGH);
  310.   digitalWrite(clockEnablePin, LOW);
  311.         uint8_t i;
  312.   unsigned long time = millis();
  313.   String val = "";
  314.         for (i = 0; i < 8; ++i) {
  315.                 digitalWrite(clockIn, HIGH);
  316.     if((digitalRead(dataIn)==1)&&time-beatArray[i].timestamp>INPUT_DELAY_MS){
  317.       beatArray[i].active=!beatArray[i].active;
  318.       beatArray[i].timestamp=time;
  319.     }
  320.    
  321.     val = val + (beatArray[i].active?"1":"0");
  322.                 digitalWrite(clockIn, LOW);
  323.         }
  324.   digitalWrite(clockEnablePin, HIGH);
  325.  
  326. }
  327. void updateLeds(){
  328.   digitalWrite(latchPin, LOW);
  329.   uint8_t i;
  330.         for (i = 0; i < 8; i++)  {
  331.     if(beatArray[i].active){
  332.                         digitalWrite(dataPin, HIGH);
  333.     }else{digitalWrite(dataPin, LOW);}
  334.                 digitalWrite(clockPin, HIGH);
  335.                 digitalWrite(clockPin, LOW);           
  336.         }
  337.   digitalWrite(latchPin, HIGH);
  338. }
  339.  
  340. AudioOutput_t updateAudio() {
  341.   int value=0;
  342.  if(activation[0])value+=aSin.next();
  343.  if(activation[1])value+=bSin.next();
  344.  if(activation[2])value+=cSin.next();
  345.   return MonoOutput::from8Bit(value);  // return an int signal centred around 0
  346. // if(active) {
  347. //     return MonoOutput::from8Bit(aSin.next());  // return an int signal centred around 0
  348. //   } else {
  349. //     return MonoOutput::from8Bit(0);
  350. //   }
  351. }
  352.  
  353.  
  354. void loop() {
  355.   audioHook();  
  356. }
  357.  
  358.  
  359.  
  360.