- //#include <Wire.h>
- //#include <Adafruit_GFX.h>
- //#include <Adafruit_SSD1306.h>
- //#include <MozziGuts.h>
- #include <Oscil.h> // oscillator template
- #include <tables/square_no_alias_2048_int8.h> // sine table for oscillator
- #include <tables/sin2048_int8.h> // sine table for oscillator
- #include <tables/triangle_valve_2_2048_int8.h> // sine table for oscillator
- #include <ResonantFilter.h>
- #define SCREEN_WIDTH 128
- #define SCREEN_HEIGHT 32
- #define OLED_RESET -1
- //#define SCREEN_ADDRESS 0x3C
- #define INPUT_DELAY_MS 250
- #include <tables/cos512_int8.h> // for filter modulation
- typedef struct{
- unsigned long timestamp;
- bool active;
- }timedBeat;
- typedef struct{
- int duration;
- int freqModifier;
- int frequency;
- int initialFrequency;
- }trackSettings;
- //Pin connected to latch pin (ST_CP) of 74HC595
- const int latchPin = 8;
- //Pin connected to clock pin (SH_CP) of 74HC595
- const int clockPin = 12;
- ////Pin connected to Data in (DS) of 74HC595
- const int dataPin = 11;
- // use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file of table #included above
- //Oscil<SIN2048_NUM_CELLS, AUDIO_RATE> bSin(SIN2048_DATA);
- // use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file of table #included above
- Oscil<SQUARE_NO_ALIAS_2048_NUM_CELLS, AUDIO_RATE> aSin(SQUARE_NO_ALIAS_2048_DATA);
- //Oscil<TRIANGLE_VALVE_2_2048_NUM_CELLS, AUDIO_RATE> aSin(TRIANGLE_VALVE_2_2048_DATA);
- //Oscil<SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);
- //Oscil<SQUARE_NO_ALIAS_2048_NUM_CELLS, AUDIO_RATE> bSin(SQUARE_NO_ALIAS_2048_DATA);
- Oscil<SIN2048_NUM_CELLS, AUDIO_RATE> bSin(SIN2048_DATA);
- Oscil<SIN2048_NUM_CELLS, AUDIO_RATE> cSin(SIN2048_DATA);
- //Oscil<TRIANGLE_VALVE_2_2048_NUM_CELLS, AUDIO_RATE> bSin(TRIANGLE_VALVE_2_2048_DATA);
- // use #define for CONTROL_RATE, not a constant
- #define CONTROL_RATE 128 // Hz, powers of 2 are most reliable
- unsigned long origin = 0;
- volatile unsigned long beatDelta = 150;
- volatile unsigned long duration = 100;
- unsigned long dT = 0;
- int initialFreq = 220;
- int if2 = 250;
- int f2 = if2;
- int fa = 5;
- int fb = 34;
- int freQ = initialFreq;
- int idx = 0;
- int aState;
- int aLastState;
- //Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- // PL pin 1
- const int load = 7;
- // CE pin 15
- const int clockEnablePin = 4;
- const int switchPinA = 3;
- const int switchPinB = 2;
- // Q7 pin 7
- const int dataIn = 5;
- // CP pin 2
- const int clockIn = 6;
- int numberToDisplay = 0;
- int currentTrack = 0;
- bool active=false;
- trackSettings voices[]{
- {100,1,82,82},
- {60,3,260,260},
- {60,2,392,392}
- };
- timedBeat tracks[][8]{
- {
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false}
- },{
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false}
- },{
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false}
- }
- };
- bool activation[]{false,false,false};
- timedBeat beatArray[]{
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false},
- {0.0,false}
- };
- timedBeat inputButton={0.0,false};
- uint8_t resonance1 = 180;
- LowPassFilter lpf1;
- Oscil<COS512_NUM_CELLS, CONTROL_RATE> kFilterMod1(COS512_DATA);
- void setup() {
- Serial.begin(9600);
- pinMode(latchPin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(dataPin, OUTPUT);
- pinMode(load, OUTPUT);
- pinMode(clockEnablePin, OUTPUT);
- pinMode(clockIn, OUTPUT);
- pinMode(switchPinA, INPUT);
- pinMode(switchPinB, INPUT);
- pinMode(dataIn, INPUT);
- startMozzi(CONTROL_RATE); // :)
- aSin.setFreq(voices[0].frequency); // set the frequency
- bSin.setFreq(voices[1].frequency);
- cSin.setFreq(voices[2].frequency);
- origin = millis();
- kFilterMod1.setFreq(1.3f);
- //attachInterrupt(1, switchTrack, RISING );
- //attachInterrupt(0, decreaseTempo, RISING );
- }
- void updateControl() {
- // updateRegisters();
- // updateBeatsOutput();
- // updateLeds();
- checkButton();
- updateTracksRegisters();
- updateTracksOutput();
- updateTrackLeds();
- }
- void increaseTempo(){
- Serial.print("A");
- beatDelta +=1;
- duration +=1;
- }
- void decreaseTempo(){
- Serial.print("B");
- beatDelta -=1;
- duration -=1;
- }
- void checkButton(){
- unsigned long time = millis();
- if((digitalRead(switchPinA)==1)&&time-inputButton.timestamp>INPUT_DELAY_MS){
- inputButton.timestamp=time;
- currentTrack+=1;
- if(currentTrack>2)currentTrack=0;
- }
- }
- void switchTrack(){
- currentTrack+=1;
- if(currentTrack>2)currentTrack=0;
- Serial.print("current track : "+currentTrack);
- }
- void updateControls(){
- aState = digitalRead(switchPinA); // Reads the "current" state of the outputA
- // If the previous and the current state of the outputA are different, that means a Pulse has occured
- if (aState != aLastState){
- // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
- if (digitalRead(switchPinB) != aState) {
- beatDelta +=1;
- duration +=1;
- } else {
- beatDelta -=1;
- duration -=1;
- }
- }
- aLastState = aState;
- }
- void updateTracksOutput(){
- unsigned long now = millis();
- dT = now - origin;
- if (dT >= beatDelta) {
- origin = now;
- dT=0;
- if (idx == 7) idx = 0;
- else idx +=1;
- }
- int j =0;
- for(j=0;j<3;j++){
- if(tracks[j][idx].active){
- if (tracks[j][idx].active) {
- if (dT >= voices[j].duration) {
- activation[j] = false;
- } else {
- activation[j] = true;
- }
- }
- else {
- activation[j] = false;
- }
- if(activation[j]){
- voices[j].frequency-=voices[j].freqModifier;
- }else{
- voices[j].frequency=voices[j].initialFrequency;
- }}
- }
- aSin.setFreq(voices[0].frequency);
- bSin.setFreq(voices[1].frequency);
- cSin.setFreq(voices[2].frequency);
- }
- void updateTracksRegisters(){
- // Write pulse to load pin
- digitalWrite(load, LOW);
- delayMicroseconds(5);
- digitalWrite(load, HIGH);
- delayMicroseconds(5);
- // Get data from 74HC165
- digitalWrite(clockIn, HIGH);
- digitalWrite(clockEnablePin, LOW);
- uint8_t i;
- unsigned long time = millis();
- String val = "";
- for (i = 0; i < 8; ++i) {
- digitalWrite(clockIn, HIGH);
- if((digitalRead(dataIn)==1)&&time-tracks[currentTrack][i].timestamp>INPUT_DELAY_MS){
- tracks[currentTrack][i].active=!tracks[currentTrack][i].active;
- tracks[currentTrack][i].timestamp=time;
- }
- val = val + (tracks[currentTrack][i].active?"1":"0");
- digitalWrite(clockIn, LOW);
- }
- digitalWrite(clockEnablePin, HIGH);
- }
- void updateTrackLeds(){
- digitalWrite(latchPin, LOW);
- uint8_t i;
- for (i = 0; i < 8; i++) {
- if(tracks[currentTrack][i].active){
- digitalWrite(dataPin, HIGH);
- }else{digitalWrite(dataPin, LOW);}
- digitalWrite(clockPin, HIGH);
- digitalWrite(clockPin, LOW);
- }
- digitalWrite(latchPin, HIGH);
- }
- void updateBeatsOutput(){
- unsigned long now = millis();
- dT = now - origin;
- if (dT >= beatDelta) {
- origin = now;
- dT=0;
- if (idx == 7) idx = 0;
- else idx +=1;
- }
- if ( beatArray[idx].active) {
- if (dT >= duration) {
- active = false;
- } else {
- active = true;
- }
- }
- else {
- active = false;
- }
- if(active){
- freQ -=fa;
- }else{
- freQ = initialFreq;
- }
- aSin.setFreq(freQ);
- }
- void updateRegisters(){
- // Write pulse to load pin
- digitalWrite(load, LOW);
- delayMicroseconds(5);
- digitalWrite(load, HIGH);
- delayMicroseconds(5);
- // Get data from 74HC165
- digitalWrite(clockIn, HIGH);
- digitalWrite(clockEnablePin, LOW);
- uint8_t i;
- unsigned long time = millis();
- String val = "";
- for (i = 0; i < 8; ++i) {
- digitalWrite(clockIn, HIGH);
- if((digitalRead(dataIn)==1)&&time-beatArray[i].timestamp>INPUT_DELAY_MS){
- beatArray[i].active=!beatArray[i].active;
- beatArray[i].timestamp=time;
- }
- val = val + (beatArray[i].active?"1":"0");
- digitalWrite(clockIn, LOW);
- }
- digitalWrite(clockEnablePin, HIGH);
- }
- void updateLeds(){
- digitalWrite(latchPin, LOW);
- uint8_t i;
- for (i = 0; i < 8; i++) {
- if(beatArray[i].active){
- digitalWrite(dataPin, HIGH);
- }else{digitalWrite(dataPin, LOW);}
- digitalWrite(clockPin, HIGH);
- digitalWrite(clockPin, LOW);
- }
- digitalWrite(latchPin, HIGH);
- }
- AudioOutput_t updateAudio() {
- int value=0;
- if(activation[0])value+=aSin.next();
- if(activation[1])value+=bSin.next();
- if(activation[2])value+=cSin.next();
- return MonoOutput::from8Bit(value); // return an int signal centred around 0
- // if(active) {
- // return MonoOutput::from8Bit(aSin.next()); // return an int signal centred around 0
- // } else {
- // return MonoOutput::from8Bit(0);
- // }
- }
- void loop() {
- audioHook();
- }
Undefined
By: Guest | Date: Oct 29 2024 18:06 | Format: None | Expires: never | Size: 8.92 KB | Hits: 58
Latest pastes
6 hours ago
13 hours ago
13 hours ago
1 days ago
1 days ago