// // ALGONOIZER v0.1 // mai 2013 // // Sources: // http://countercomplex.blogspot.com/2011/10/algorithmic-symphonies-from-one-line-of.html // http://www.bemmu.com/music/index.html // http://wurstcaptures.untergrund.net/music/ // // http://www.marulaberry.co.za/index.php/tutorials/code/arduino-adc/ // // DECLARATIONS *************************************************************** // Constantes #define PIN_PWM 9 // Broche de sortie audio #define PIN_BUT 2 // Broche d'entrée (boutton poussoir) (port D) #define PIN_LFO 3 // Broche de sortie LFO (LED) (port B) #define MODE_NORMAL 0 #define MODE_LFOSETUP 1 #define CTRL_FRQ 0 #define CTRL_PARAM 1 #define CTRL_FUNC 2 #define CTRL_VOL 3 #define CTRL_LFO 4 #define CTRL_BUT 5 #define DEFAULT_LFOSPEED 100 // Période LFO au demarrage (en nb d'interruptions) #ifndef cbi #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) #endif #ifndef sbi #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) #endif // ADC prescaler const unsigned char ADC_PS_16 = (1 << ADPS2); const unsigned char ADC_PS_32 = (1 << ADPS2) | (1 << ADPS0); const unsigned char ADC_PS_64 = (1 << ADPS2) | (1 << ADPS1); const unsigned char ADC_PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Variables programme unsigned long t; // Compteur t int audioOut; // Valeur de l'échantillon audio byte iFunction; // Numero de la fonction audio byte iCtrlNb; // Numero de potentiometre int iParam[] = {0, 0, 0, 0, 0, 0}; int tmpParam[] = {0, 0, 0, 0, 0, 0}; // iParam[0] : freq d'échantillonnage // iParam[1] : fonction audio // iParam[2] : parametre audio // iParam[3] : volume audio // iParam[4] : forme d'onde LFO // iParam[5] : etat du boutton poussoir int iIncrement; // Increment du compteur t int iCount; // Compteur pour ralentir l'incrementation de t byte iMode = MODE_NORMAL; // Variables temps. Mesure = nombre d'interruptions unsigned long interruptCounter; unsigned long timeButtonLast; unsigned long buttonPeriod; // Durée entre 2 appuis unsigned long delayButSetup = 20000; // Temps d'appui pour passer en mode setup unsigned long maxLFOPeriod = 100000; // Temps max entre 2 appuis considéré pour definir le LFO // Variables du LFO int lfoCounter; byte lfoIndex; long lfoOverflow; byte lfoVal; byte lfoWaveForm; int lfoParam[5] = {-1, -1, -1, 0, -1}; // lfoParam[i] = -1 : paramètre i non modifié par le LFO // lfoParam[i] = 0..255 : paramètre i modifié par le LFO // entre iParam[i] et lfoParam[i] // Formes d'onde pour le LFO static const uint8_t waveTable[6][256] PROGMEM = { // #0 - OFF { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 }, // #1 - SQUARE { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // #2 - SINUS { 128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 162, 165, 167, 170, 173, 176, 179, 182, 185, 188, 190, 193, 196, 198, 201, 203, 206, 208, 211, 213, 215, 218, 220, 222, 224, 226, 228, 230, 232, 234, 235, 237, 238, 240, 241, 243, 244, 245, 246, 248, 249, 250, 250, 251, 252, 253, 253, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, 252, 251, 250, 250, 249, 248, 246, 245, 244, 243, 241, 240, 238, 237, 235, 234, 232, 230, 228, 226, 224, 222, 220, 218, 215, 213, 211, 208, 206, 203, 201, 198, 196, 193, 190, 188, 185, 182, 179, 176, 173, 170, 167, 165, 162, 158, 155, 152, 149, 146, 143, 140, 137, 134, 131, 128, 124, 121, 118, 115, 112, 109, 106, 103, 100, 97, 93, 90, 88, 85, 82, 79, 76, 73, 70, 67, 65, 62, 59, 57, 54, 52, 49, 47, 44, 42, 40, 37, 35, 33, 31, 29, 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11, 10, 9, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9, 10, 11, 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35, 37, 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76, 79, 82, 85, 88, 90, 93, 97, 100, 103, 106, 109, 112, 115, 118, 121, 124 }, // #3 - TRIANGLE { 128, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255, 253, 251, 249, 247, 245, 243, 241, 239, 237, 235, 233, 231, 229, 227, 225, 223, 221, 219, 217, 215, 213, 211, 209, 207, 205, 203, 201, 199, 197, 195, 193, 191, 189, 187, 185, 183, 181, 179, 177, 175, 173, 171, 169, 167, 165, 163, 161, 159, 157, 155, 153, 151, 149, 147, 145, 143, 141, 139, 137, 135, 133, 131, 129, 128, 126, 124, 122, 120, 118, 116, 114, 112, 110, 108, 106, 104, 102, 100, 98, 96, 94, 92, 90, 88, 86, 84, 82, 80, 78, 76, 74, 72, 70, 68, 66, 64, 62, 60, 58, 56, 54, 52, 50, 48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126 }, // #4 - RAMP UP { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254 }, // #5 - RAMP DOWN { 255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 237, 236, 235, 234, 233, 232, 231, 230, 229, 228, 227, 226, 225, 224, 223, 222, 221, 220, 219, 218, 217, 216, 215, 214, 213, 212, 211, 210, 209, 208, 207, 206, 205, 204, 203, 202, 201, 200, 199, 198, 197, 196, 195, 194, 193, 192, 191, 190, 189, 188, 187, 186, 185, 184, 183, 182, 181, 180, 179, 178, 177, 176, 175, 174, 173, 172, 171, 170, 169, 168, 167, 166, 165, 164, 163, 162, 161, 160, 159, 158, 157, 156, 155, 154, 153, 152, 151, 150, 149, 148, 147, 146, 145, 144, 143, 142, 141, 140, 139, 138, 137, 136, 135, 134, 133, 132, 131, 130, 129, 128, 128, 127, 126, 125, 124, 123, 122, 121, 120, 119, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 } }; // ROUTINE D'INITIALISATION *************************************************** void setup() { // Declaration des entrees/sorties pinMode(13, OUTPUT); pinMode(PIN_PWM, OUTPUT); pinMode(PIN_LFO, OUTPUT); pinMode(PIN_BUT, INPUT); digitalWrite(PIN_BUT, HIGH); // pullup // Initialisation du LFO lfoWaveForm = 0; lfoOverflow = DEFAULT_LFOSPEED; // Initialisation des registres // Fast ADC ADCSRA &= ~ADC_PS_128; // Suppression du prescaler de 128 defini par defaut ADCSRA |= ADC_PS_16; // Choix du nouveau prescaler : // ADC_PS_16, ADC_PS_32, ADC_PS_64 ou ADC_PS_128 // 8-bit Fast PWM (TIMER1), sans prescaler (CS10) TCCR1A= _BV(COM1A1) | _BV(WGM10); TCCR1B = _BV(CS10) | _BV(WGM12); // Activation de l'interruption de debordement pour TIMER1 TIMSK1 = _BV(TOIE1); // Phase-Correct PWM sur TIMER2 (canal B) TCCR2A = _BV(COM2B1) | _BV(WGM20); TCCR2B = _BV(CS22); } // ROUTINE D'INTERRUPTION ***************************************************** ISR(TIMER1_OVF_vect) { // TIMER1 deborde a une freq de 62.5kHz (ou 7.8kHz) // Incremente les compteurs interruptCounter++; lfoCounter++; // Traitement alterne du LFO et de l'audio // => frequence d'echantillonnage audio /2 (31kHz) if (interruptCounter % 2) { // Calcul du LFO if (lfoCounter >= lfoOverflow) { if (lfoWaveForm > 0) lfoVal = pgm_read_byte(&waveTable[lfoWaveForm][lfoIndex]); else lfoVal = 0; if (iMode == MODE_NORMAL) OCR2B = lfoVal; lfoIndex++; lfoCounter = 0; } return; } // Prepare et envoie l'échantillon audio vers la sortie PWM byte out = audioOut >> 8; uint16_t oscenv = out * (tmpParam[CTRL_VOL]>>2); out = (oscenv >> 8); OCR1A = out; // Incremente le compteur t if (iIncrement < 0) { iCount++; if (iCount >= -iIncrement){ iCount = 0; t++; } } else { t = t + max(1, iIncrement); } // Calcul de l'échantillon switch (iFunction) { case 0: audioOut = t>>6^t&0x25|t+(t^t>>(tmpParam[CTRL_PARAM]>>6))-t*((t%24?2:6)&t>>11)^t<<1&(t&0x256?t>>4:t>>10); break; case 1: audioOut = (t*(t>>5|t>>8))>>(t>>(tmpParam[CTRL_PARAM]>>6)); break; case 2: audioOut = t*(t>>(tmpParam[CTRL_PARAM]>>6)&t>>8&123&t>>3); break; case 3: audioOut = t*(t>>8*(t>>(tmpParam[CTRL_PARAM]>>6)|t>>8)&(20|(t>>19)*5>>t|t>>3)); break; case 4: audioOut = (~t/100|(t*(tmpParam[CTRL_PARAM]>>6)))^(t*3&(t>>5)); break; case 5: audioOut = t>>(tmpParam[CTRL_PARAM]>>6)|t>>(tmpParam[3]>>6); break; case 6: audioOut = t * ((t>>(tmpParam[CTRL_PARAM]>>6)|t>>(tmpParam[3]>>6))&74&t>>15); break; case 7: audioOut = (t*(5+tmpParam[CTRL_PARAM]>>6)&t>>7); break; case 8: audioOut = t & tmpParam[CTRL_PARAM]; break; case 9: audioOut = ((((5&((3 *(23*(4^t)))+t))*(9*((15>>((9&((t&12)^15))>>5))*2)))*((((t*(t*8))>>10)&t)>>(tmpParam[CTRL_PARAM]>>6)))^15); break; case 10: audioOut = ((t*5&t>>7)|(t*3&t>>(tmpParam[CTRL_PARAM]>>6))); break; case 11: audioOut = (t|t>>(1+(tmpParam[CTRL_PARAM]>>6))); break; case 12: audioOut = t>>(tmpParam[CTRL_PARAM]>>6)^t&0x25|t + (t^t>>(1+(tmpParam[3]>>6))) - t*((t%24?2:6)&t>>11)^t<<1&(t&0x256?t>>4:t>>10); break; case 13: audioOut = ((t*t)>>((((8*t)&(t>>t))>>((11>>t)&(8|8)))+(((t+8)*((((t|11)*(t+11))+((t&8)>>(t+11)))*(((11>>8)|(t|8))>>((t>>8)|(11&8)))))+((t>>11)-(8|11))))); break; case 14: audioOut = t&t>>(1+(tmpParam[CTRL_PARAM]>>6)); break; case 15: audioOut = (t^(t>>(1+(tmpParam[CTRL_PARAM]>>6)))) + (t^t>>(1+(tmpParam[3]>>6))); break; default: break; } } // BOUCLE PRINCIPALE ********************************************************** void loop() { unsigned long timeNow = interruptCounter; unsigned long butDuration; int ctrlVal; byte stateButton; // Lecture des potentiometres (1 par boucle) ctrlVal = analogRead(iCtrlNb); if (iMode == MODE_NORMAL) { iParam[iCtrlNb] = ctrlVal; } else { // MODE_LFOSETUP if (abs(iParam[iCtrlNb] - ctrlVal) > 10) { // Le potentiometre iCtrlNb a été modifié lfoParam[iCtrlNb] = ctrlVal; } } iCtrlNb++; if (iCtrlNb >= CTRL_BUT) { // Lecture boutton stateButton = PIND & (1 << PIN_BUT); if (iParam[CTRL_BUT] != stateButton) { // Changement d'etat iParam[CTRL_BUT] = stateButton; if (iParam[CTRL_BUT] == 0) { // boutton enfoncé if (timeButtonLast > 0) { // calcul du temps entre 2 pressions if (timeNow > timeButtonLast) buttonPeriod = timeNow - timeButtonLast; else buttonPeriod = timeNow + (429496729 - timeButtonLast); // Définit la durée du LFO if (buttonPeriod < maxLFOPeriod) { lfoOverflow = buttonPeriod >> 8; timeButtonLast = 0; } else timeButtonLast = timeNow; } else timeButtonLast = timeNow; } else { // bouton relaché // repasse en mode normal iMode = MODE_NORMAL; } } else { // pas de changement d'état if (iParam[CTRL_BUT] == 0) { // boutton enfoncé if (timeNow > timeButtonLast) butDuration = timeNow - timeButtonLast; else butDuration = timeNow + (429496729 - timeButtonLast); if (butDuration > delayButSetup) { // Annule la mesure de la periode du LFO timeButtonLast = 0; // Change de mode if (iMode == MODE_NORMAL) { iMode = MODE_LFOSETUP; OCR2B = 255; resetLFOParams(); } } } } // Remise à zéro du compteur de potentiometres iCtrlNb = 0; } // Modulation (LFO) for (int i=0; i<5; i++) { if (iMode == MODE_NORMAL) { if (lfoWaveForm > 0) { if (lfoParam[i] == -1) tmpParam[i] = iParam[i]; else tmpParam[i] = map(lfoVal, 0, 255, min(lfoParam[i], iParam[i]), max(lfoParam[i], iParam[i])); } else tmpParam[i] = iParam[i]; } else tmpParam[i] = iParam[i]; } // Affectation la fonction audio if ( (tmpParam[CTRL_FUNC] >> 6) != iFunction) iFunction = (tmpParam[CTRL_FUNC] >> 6); // Affectation de la forme d'onde du LFO byte newLFOWaveForm = map(tmpParam[CTRL_LFO],0,1023,0,5); if ( newLFOWaveForm != lfoWaveForm) lfoWaveForm = newLFOWaveForm; // Mise à jour de l'incrément iIncrement = tmpParam[CTRL_FRQ]>>1 - 4; } // ROUTINES DIVERSES ********************************************************** void resetLFOParams() { for (int i=0; i<5; i++) lfoParam[i] = -1; }