Created
October 29, 2014 00:41
-
-
Save bitchwhocodes/45a2997376c71bb26857 to your computer and use it in GitHub Desktop.
SkrillTRex- Processing and Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.serial.*; | |
import cc.arduino.*; | |
import ddf.minim.*; | |
import processing.video.*; | |
// Number of columns and rows in our system | |
int cols, rows; | |
// Variable to hold onto Capture object | |
Capture video; | |
PImage output; | |
float avg_r, avg_g, avg_b; | |
PImage img = createImage(640, 360, RGB); | |
//Referencing code from a few different sketches on OpenProcessing.org | |
// http://openprocessing.org/search/?q=video+cam+pixel | |
int filter = 20; | |
int filter_index = 6; | |
int[] filters = { | |
1, 2, 4, 5, 8, 10, 20, 40 | |
}; | |
Minim minim; | |
Arduino arduino; | |
AudioPlayer player;// BACKGROUND SOUND | |
AudioPlayer bass; // DROP THE BASS | |
AudioPlayer horn;// FOG HORN SOUND FROM BUTTON!! | |
Serial myPort; // Create object from Serial class | |
String[] samples; | |
int val; | |
Boolean enableRex = false;// Enable when you want the trex to be active | |
void setup() | |
{ | |
size(640, 360); | |
frameRate(60); | |
background(0); | |
noStroke(); | |
filter = filters[filter_index]; | |
video = new Capture(this, Capture.list()[0]); | |
video.start(); | |
// Set up the Arduino | |
arduino = new Arduino(this, Arduino.list()[0], 57600); | |
// Get Input from pin 2 | |
arduino.pinMode(2, arduino.INPUT); | |
// List of Dubstep samples we can play | |
samples = new String[12]; | |
for (int i=0; i<12; i++) { | |
samples[i] = "loop"+str(i)+".wav"; | |
} | |
// we pass this to Minim so that it can load files from the data directory | |
minim = new Minim(this); | |
bass = minim.loadFile(samples[4]); | |
// loadFile will look in all the same places as loadImage does. | |
// this means you can find files that are in the data folder and the | |
// sketch folder. you can also pass an absolute path, or a URL. | |
player = minim.loadFile("marcus_kellis_theme.mp3"); | |
player.setGain(-10); | |
horn = minim.loadFile("horn.mp3"); | |
player.play(); | |
loadRandomFile(); | |
} | |
// Load a random file for hte bass dropping. | |
void loadRandomFile() { | |
// TREX not enabled - exit! | |
if (!enableRex)return; | |
int index = int(random(samples.length)); | |
String file = samples[index]; | |
if (bass!=null) { | |
bass.rewind(); | |
} | |
// Loop this sucker | |
bass.pause(); | |
bass = minim.loadFile(file); | |
bass.loop(999); | |
} | |
void draw() | |
{ | |
stroke(0); | |
//Reading of the Flex Sensor on A0 | |
int val = arduino.analogRead(0); | |
// map from 541 to 567 calibrated to -60 to 0 | |
float percent = map(val, 567, 541, -60, 0); | |
// Minim does not support Volume on my computer. You need to check if it does. | |
// It does support Gain tho. | |
bass.setGain(percent); | |
// Arcade button mapped to A2 | |
if (arduino.digitalRead(2) == Arduino.HIGH) { | |
horn.rewind(); | |
horn.play(); | |
} | |
// Video and drawing to canvas starts | |
if (video.available()) { | |
video.read(); | |
video.loadPixels(); | |
for (int x = 0; x < video.width; x+=filter) { | |
for (int y = 0; y < video.height; y+=filter ) { | |
avg_r = avg_g = avg_b = 255.0; | |
for (int r = x; r < x+filter; r++) { | |
for (int c = y; c < y+filter; c++ ) { | |
int loc = r + c*video.width; | |
avg_r += red (video.pixels[loc]); | |
avg_g += green (video.pixels[loc]); | |
avg_b += blue (video.pixels[loc]); | |
} | |
} | |
color col = color(avg_r/(filter*filter), avg_g/(filter*filter), avg_b/(filter*filter)); | |
fill( col ); | |
int size2 = int(random(filter+30)); | |
int size1 = int(random(filter+5)); | |
// draw triangle, ellipse, rect based on random sizes. | |
// Could change many things here to get different visuals. | |
triangle(x, y, x+(size2/2), y+size2, x+size2, y); | |
ellipse(x, y, size1, size1); | |
rect(x, y, random(size2), random(size2)); | |
} | |
} | |
} | |
// draw the waveforms | |
// the values returned by left.get() and right.get() will be between -1 and 1, | |
// so we need to scale them up to see the waveform | |
// note that if the file is MONO, left.get() and right.get() will return the same value | |
//IF TREX is Enabled, and the gain is higher than a -40 let's draw the waveform | |
if (enableRex && percent > -40) { | |
for (int i = 0; i < bass.bufferSize () - 1; i++) | |
{ | |
float x1 = map( i, 0, bass.bufferSize(), 0, width ); | |
float x2 = map( i+1, 0, bass.bufferSize(), 0, width ); | |
//line( x1, 50 + bass.left.get(i)*50, x2, 50 + bass.left.get(i+1)*50 ); | |
stroke(255); | |
strokeWeight(10); | |
line( x1, 150 + bass.right.get(i)*200, x2, 150 + bass.right.get(i+1)*200); | |
strokeWeight(0); | |
} | |
} | |
} | |
void keyPressed() | |
{ | |
// save out an image of the canvas | |
if (keyCode == 32) { | |
String name = "output"+ (millis())+(minute())+(day())+random(255)+".png"; | |
save(name); | |
} | |
// can change the size of the grid | |
if ( keyCode == 38 ) { | |
filter_index++; | |
} else if ( keyCode == 40 ) { | |
filter_index--; | |
} | |
if ( filter_index < 0 ) { | |
filter_index = 0; | |
} else if ( filter_index > 7 ) { | |
filter_index = 7; | |
} | |
filter = filters[filter_index]; | |
// Load in a random file | |
if ( key == 's' ) { | |
loadRandomFile(); | |
} | |
//Enable the Trex otherwise we will get values we don't need to address when you | |
// are putting the puppet on your hand. | |
if (key== 't') { | |
enableRex = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment