class SoundWorm { SoundBox sbox; int x; int y; color hours_color; color minutes_color; color seconds_color; SoundWorm(int x, int y, SoundBox sbox) { this.sbox = sbox; this.x = x; this.y = y; } void randomizeColors() { hours_color = color(random(255), random(255), random(255)); minutes_color = color(random(255), random(255), random(255)); seconds_color = color(random(255), random(255), random(255)); } void display() { //get current time Calendar rightNow = Calendar.getInstance(); int seconds = rightNow.get(Calendar.SECOND); int minutes = rightNow.get(Calendar.MINUTE); int hours = rightNow.get(Calendar.HOUR); boolean isPM = (rightNow.get(Calendar.AM_PM) == Calendar.PM); //set maximum arm attributes int max_arm_width = 2; int max_arm_length = min(width, height) / 3; int max_arm_height = 30; //draw circle pushMatrix(); translate(x, y); noFill(); stroke(0, 0, 0); smooth(); if (isPM) { strokeWeight(10); ellipse(0, 0, 2 * (max_arm_length + max_arm_width + 10), 2 * (max_arm_length + max_arm_width + 10)); } else { strokeWeight(2); ellipse(0, 0, 2 * (max_arm_length + max_arm_width + 10) + 4, 2 * (max_arm_length + max_arm_width + 10) + 4); ellipse(0, 0, 2 * (max_arm_length + max_arm_width + 10) - 4, 2 * (max_arm_length + max_arm_width + 10) - 4); } popMatrix(); int interp; int buffer_length; if (!sbox.isActive()) { interp = 0; buffer_length = Integer.MAX_VALUE; } else { interp = (int) max(0,(((millis()- sbox.audio_stream.bufferStartTime) / (float)sbox.audio_stream.duration) * sbox.audio_stream.size)); buffer_length = sbox.audio_stream.buffer2.length; } // draw seconds arm float seconds_angle = (((float)seconds / 60) * 360) - 90; pushMatrix(); translate(x, y); rotate(radians(seconds_angle)); stroke(seconds_color); strokeWeight(2); beginShape(LINE_STRIP); curveVertex(0, 0); curveVertex(0, 0); int amplitude = 0; int maxx = 0; for ( int xx = 0; xx < max_arm_length; xx++) { if (sbox.isActive()) { if ( xx + interp < buffer_length) { amplitude = (int) ((sbox.audio_stream.buffer2[xx + interp] * max_arm_height) * 2); curveVertex( xx, amplitude); maxx = xx; } else { break; } } else { amplitude = 0; curveVertex( xx, amplitude); maxx = xx; } } curveVertex(maxx, 0); curveVertex(maxx, 0); endShape(); fill(0,0,0); ellipse(maxx, 0, 10 , 10 ); popMatrix(); //draw minutes arm float minutes_angle = (((float)minutes / 60) * 360) - 90; pushMatrix(); translate(x, y); rotate(radians(minutes_angle)); stroke(minutes_color); beginShape(LINE_STRIP); curveVertex(0, 0); curveVertex(0, 0); amplitude = 0; maxx = 0; for ( int xx = 0; xx < (((float)2/3) * max_arm_length); xx++ ) { if (sbox.isActive()) { if ( xx + interp < buffer_length) { amplitude = (int) ((sbox.audio_stream.buffer2[xx + interp] * max_arm_height) * 2); curveVertex( xx, amplitude); maxx = xx; } else { break; } } else { amplitude = 0; curveVertex( xx, amplitude); maxx = xx; } } curveVertex(maxx, 0); curveVertex(maxx, 0); endShape(); fill(0,0,0); ellipse(maxx, 0, 10 , 10 ); popMatrix(); //draw hours arm float hours_angle = (((float)hours / 12) * 360) - 90; pushMatrix(); translate(x, y); rotate(radians(hours_angle)); stroke(hours_color); beginShape(LINE_STRIP); curveVertex(0,0); curveVertex(0, 0); amplitude = 0; maxx = 0; for ( int xx= 0; xx < ((float)1/3) * max_arm_length; xx++ ) { if (sbox.isActive()) { if ( xx + interp < buffer_length) { amplitude = (int) ((sbox.audio_stream.buffer2[xx + interp] * max_arm_height) * 2); curveVertex( xx, amplitude); maxx = xx; } else { break; } } else { amplitude = 0; curveVertex( xx, amplitude); maxx = xx; } } curveVertex(maxx, 0); curveVertex(maxx, 0); endShape(); fill(0,0,0); ellipse(maxx, 0, 10 , 10 ); popMatrix(); } void update() { //moveTowards(mouseX, mouseY); //rotateTowards(mouseX, mouseY); if (x < 0) { x = 0; } if (x > width) { x = width; } if (y < 0) { y = 0; } if (y > height) { y = height; } } void moveTowards(int t_x, int t_y) { if (x < t_x) { x++; } if (x > t_x) { x--; } if (y < t_y) { y++; } if(y > t_y) { y--; } } float rotateTowards(int t_x, int t_y, float angle) { if (t_x == x && t_y == y) { return angle; } float t_angle = degrees(atan2(t_y - y, t_x - x)); if (t_angle < 0) { t_angle = 360 + t_angle; } t_angle -= 180; float angle_dist = abs(t_angle - angle); int rotate_speed = 5; if (angle_dist > rotate_speed) { if (angle > t_angle) { if (angle_dist < 180) { angle -= rotate_speed; } else { angle += rotate_speed; angle %= 360; } } else if (angle < t_angle) { if (angle_dist < 180) { angle += rotate_speed; } else { angle -= rotate_speed; angle %= 360; if (angle < 0) { angle += 360; } } } } return angle; } }