int n=200; int soundLength = 20000; boolean toggle=false; BSound beep1,beep2; int lastToggleTime; void setup() { size(400,400); beep1 = new BSound(soundLength); beep2 = new BSound(soundLength); generateSound(); // repeat(beep1); lastToggleTime = millis(); } void drawSquare() { /* noStroke(); float x = (int)(random(1.3)); float y = (int)(random(1.3)); fill(100,100,200,50); rect(400,10,20,20); */ } void loop() { int currentTime = millis(); if (currentTime-lastToggleTime > (int)((float)soundLength/8000.0)*1000) { lastToggleTime=millis(); if (toggle) toggle=false; else toggle=true; if (toggle) { stop(beep1); play(beep1); background(0); } else { stop(beep2); play(beep2); background(90); } drawSquare(); generateSound(); } } void generateSound() { float p0 = random(1.0) * 1.0f; float p1 = random(1.0) * 1.0f; float p2 = random(1.0) * 1.0f; float p3 = random(1.0) * 1.0f; float freq1 = random(1.0) * 60.0f; float freq2 = random(1.0) * 60.0f; float freq3 = random(1.0) * 60.0f; int funca = (int)random(2.9f); int funcb = (int)random(2.9f); int funcc = (int)random(2.9f); for (int i = 0; i < soundLength; i++) { // fill the sound with a sine wave int v=0; float a = function(funca,i/freq1, p0, p1); float b = function(funcb,i/freq2, p2, p3); // Fade out: float vol = cos(((float)i/(float)soundLength)*(PI/2.0f)); v = int(127 * function(funcc,i/freq3, a, b) * vol); if (toggle) beep2.samples[i] = v; else beep1.samples[i] = v; } } float function(int func, float p, float a, float b) { if (func==0) return func01(p,a,b); else if (func==1) return func02(p,a,b); else if (func==2) return func02(p,a,b); return(0.0f); } float func01(float p, float a, float b) { return sin((p*a)+(b*PI*0.1f)); } float func02(float p, float a, float b) { float v=func01(p,a,b)*2000.0f; if (v<-1.0f) v=-1.0f; if (v>1.0f) v=1.0f; return v; } float func03(float p, float a, float b) { return (float)((p%100)-50)/50.0f; }