Musically interesting fibo waveforms

main
Mike Lynch 2024-02-11 14:01:31 +11:00
parent 7aec076356
commit 0bff8f0db0
1 changed files with 39 additions and 25 deletions

View File

@ -6,12 +6,17 @@
Adafruit_MCP4728 mcp;
#define nsamp 256
#define nsamp 1500
#define dacmax 256
unsigned int phase = 0;
unsigned int fibo = 20;
unsigned int fibo = 3;
unsigned int scale = 0;
unsigned int fibi = 0;
unsigned int pmax = nsamp;
unsigned int fibosort[] = { 4,3,11,8,29,7,21,19,38,76,5,55,199,6,9,12,16,18,24,36,48,72,144,13,22,31,44,62,124,47,141,17,34,68,136,152,15,33,41,123,165,205,58,116,211,89,139,14,23,28,32,42,46,56,63,69,84,92,96,112,126,138,161,168,184,207,224,252,101,151,233,39,87,59,10,20,40,61,88,110,122,155,220,244,248,71,27,51,54,57,102,107,108,114,153,171,204,214,216,228,37,113,79,158,35,77,105,231,26,52,104,232,43,129,181,209,64,94,188,192,25,53,106,109,212,218,121,49,91,147,203,229,30,45,60,66,80,82,90,93,99,120,132,164,176,180,183,186,198,240,246,131,178,67,201,65,143,145,119,133,189,238,73,149,202,111,235,78,83,117,156,166,174,208,234,249,118,236,179,85,95,170,187,190,191,128,97,75,103,142,81,159,162,74,148,226,177,239,70,115,140,154,160,210,217,230,241,253,251,221,247,127,86,172,137,195,213,219,50,100,200,237,157,163,242,98,167,182,196,173,135,255,169,185,193,197,175,134,130,215,146,223,222,227,125,245,194,150,225,206,243,254,250 };
byte waveform[nsamp];
@ -26,7 +31,7 @@ void setup() {
// set compare match register for 1hz increments
//OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
//OCR1A = 7812;// = (16*10^6) / (1*1024) - 1 (must be <65536)
OCR1A = 10000;
OCR1A = 2000;
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
@ -37,6 +42,7 @@ void setup() {
sei();
Serial.begin(115200);
if (!mcp.begin(0x64)) {
@ -50,38 +56,46 @@ void setup() {
for( int i = 0; i < nsamp; i++ ) {
waveform[i] = 1;
}
fibo = fibosort[fibi];
setwave();
}
void setwave(){
waveform[0] = 0;
waveform[1] = 1;
fibo = fibosort[fibi];
Serial.println(fibo);
pmax = nsamp;
for (int i=2; i<nsamp; ++i){
waveform[i] = (waveform[i - 1] + waveform[i - 2]) % fibo;
if( waveform[i - 1] == 0 && waveform[i] == 1 ) {
pmax = i - 1;
break;
}
}
Serial.println(pmax);
float scale = 256 / fibo;
for( int i = 0; i < nsamp; ++i ) {
waveform[i] *= scale;
}
}
ISR(TIMER1_COMPA_vect){//timer1
unsigned int flog;
scale = 10;
fibo = 3 + analogRead(A0) >> 2;
flog = fibo;
while( flog ) {
flog = flog >> 1;
scale -= 1;
}
Serial.println(fibo);
Serial.println(scale);
Serial.println(fibo << scale);
fibi++;
if( fibi == 128 ) {
fibi = 0;
}
Serial.println("Setting wave");
setwave();
phase = 0;
}
void loop() {
int p0 = phase - 1;
int p1 = phase + 2;
if( p0 < 0 ) {
p0 += nsamp;
}
if( p1 < 0 ) {
p1 += nsamp;
}
waveform[phase] = (waveform[p1] + waveform[p0]) % fibo;
mcp.fastWrite(waveform[phase] << scale, 0, 0, 0);
mcp.fastWrite(waveform[phase] << 4, 0, 0, 0);
phase += 1;
if( phase >= nsamp ) {
if( phase == pmax ) {
phase = 0;
}
}