Close
Sign in to view the code!
Sign in to send to cube!
disco mania
Created by alex on May 05, 2017
Twitter
Facebook
dust off your bell bottoms, son. This is the sound of tomorrow and damn doe...
Comments:
Guitarmon8
said
Cool concept! A little harsh on the eyes though!
Up next
The Drawer
Update Demo
Sign up, make your own!
#include <math.h> #define MICROPHONE 12 #define GAIN_CONTROL 11 #define MAX_POINTS 20 #define SPEED 0.22 //maxBrightness is the brightness limit for each pixel. All color data will be scaled down //so that the largest value is maxBrightness int maxBrightness=50; /********************************* * FFTJoy variables * * *******************************/ #define M 4 float real[(int)pow(2,M)]; float imaginary[(int)pow(2,M)]; float maxValue=0; float sample; float maxAvg=0; float smoothedAverage; float weight=0.3; int frameCount=0; Cube cube=Cube(); byte spectrum[24]={255,255,100, 255,225,80, 255,195,60, 255,165,40, 255,135,20, 255,105,0, 255,75,0, 255,45,0}; void FFTJoy(); short FFT(short int dir,int m,float *x,float *y); void setup() { cube.begin(); } void loop() { FFTJoy(); frameCount++; cube.show(); } /******************************************** * FFT JOY functions * *****************************************/ void FFTJoy(){ //don't have to modify this stuff for(int i=0;i<pow(2,M);i++) { real[i]=analogRead(MICROPHONE)-2048; delayMicroseconds(212); //this sets our 'sample rate'. I went through a bunch of trial and error to //find a good sample rate to put a soprano's vocal range in the spectrum of the cube imaginary[i]=0; } FFT(1, M, real, imaginary); for(int i=0;i<pow(2,M);i++) { imaginary[i]=sqrt(pow(imaginary[i],2)+pow(real[i],2)); if(imaginary[i]>maxValue) maxValue=imaginary[i]; } if(maxValue>100) maxValue--; //graphics code starts cube.background(black); for(int i=0;i<8;i++) { imaginary[i]=cube.size*imaginary[i]/maxValue; int y; for(y=0;y<=imaginary[i];y++) { // Color col=cube.colorMap(y+25,0,50); Color col=Color(spectrum[y*3],spectrum[y*3+1],spectrum[y*3+2]); cube.setVoxel(i,y,cube.size-1,col); cube.setVoxel(0,y,i,col); cube.setVoxel(cube.size-1-i,y,0,col); cube.setVoxel(cube.size-1,y,cube.size-1-i,col); } } float avg; for(int i=0;i<pow(2,M);i++) avg+=real[i]; avg/=pow(2,M); if(avg>maxAvg) maxAvg=avg; // maxAvg-=0.01; if(maxAvg>10) maxAvg--; smoothedAverage=weight*avg+(1-weight)*smoothedAverage; float radius=6*smoothedAverage/maxAvg; // cube.shell(cube.center, 3*smoothedAverage/maxAvg,blue); Color shellCol=Color(100,100,100); for(int x=0;x<cube.size;x++) for(int y=0;y<cube.size;y++) for(int z=0;z<cube.size;z++) { float rad=sqrt(pow(x-3.5,2)+pow(y-3.5,2)+pow(z-3.5,2)); Color currentColor=Color((float)shellCol.red*(radius-rad)/radius, (float)shellCol.green*pow((radius-rad)/radius,2), (float)shellCol.blue*pow((radius-rad)/radius,2)); if((random(200)>192)&&(rad<4)) currentColor=Color(255,255,255); if(rad<radius) cube.setVoxel(x,y,z,currentColor); } sample++; if(sample>=pow(2,M)) sample-=pow(2,M); } short FFT(short int dir,int m,float *x,float *y) { int n,i,i1,j,k,i2,l,l1,l2; float c1,c2,tx,ty,t1,t2,u1,u2,z; /* Calculate the number of points */ n = 1; for (i=0;i<m;i++) n *= 2; /* Do the bit reversal */ i2 = n >> 1; j = 0; for (i=0;i<n-1;i++) { if (i < j) { tx = x[i]; ty = y[i]; x[i] = x[j]; y[i] = y[j]; x[j] = tx; y[j] = ty; } k = i2; while (k <= j) { j -= k; k >>= 1; } j += k; } /* Compute the FFT */ c1 = -1.0; c2 = 0.0; l2 = 1; for (l=0;l<m;l++) { l1 = l2; l2 <<= 1; u1 = 1.0; u2 = 0.0; for (j=0;j<l1;j++) { for (i=j;i<n;i+=l2) { i1 = i + l1; t1 = u1 * x[i1] - u2 * y[i1]; t2 = u1 * y[i1] + u2 * x[i1]; x[i1] = x[i] - t1; y[i1] = y[i] - t2; x[i] += t1; y[i] += t2; } z = u1 * c1 - u2 * c2; u2 = u1 * c2 + u2 * c1; u1 = z; } c2 = sqrt((1.0 - c1) / 2.0); if (dir == 1) c2 = -c2; c1 = sqrt((1.0 + c1) / 2.0); } /* Scaling for forward transform */ if (dir == 1) { for (i=0;i<n;i++) { x[i] /= n; y[i] /= n; } } return(0); }
Cool concept! A little harsh on the eyes though!