Processing code
/* This code is created by Shengzhi Wu (www.wushengzhi.xyz) for Responsive Mobile Environment class project Aura Carbon
It used a PowerMate dial to trigger various pieces of conversations and communicate with a Particle Photon to change light effect of a 24 NeoPixel
All you need to change is to find the correct serial port, it is in line 67, just change the number
You can use line 72 to print all the availiable port, and find the one starts with usb
the PowerMate uses a U and D key to trigger the changes, so you need to use PowerMate app and change it to mimick press key "U" and "D"
*/
import processing.serial.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
Minim minim;
AudioPlayer[] audio;
String[][] lines;
String[][] time;
String[][] speaker;
String[][] dialog;
float a =0;
float ringChange;
float speed =1;
float ringSize =1;
BeatDetect beat;
int totalNum =4;
int index =0;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
char state ='0';
PFont font;
int normal = 22;
int selected = 21;
void setup() {
font = loadFont("Futura-Medium-48.vlw");
//size(1000, 1000);
fullScreen();
lines = new String[totalNum][];
time = new String[totalNum][];
speaker = new String[totalNum][];
dialog = new String[totalNum][];
audio = new AudioPlayer[totalNum];
minim = new Minim(this);
for (int j=0; j<totalNum; j++) {
lines[j] = loadStrings("dialog" + (1+j)+ ".txt");
time[j] = new String[lines[j].length];
speaker[j] =new String[lines[j].length];
dialog[j] = new String[lines[j].length];
println("there are " + lines[j].length + " lines");
for (int i = 0; i < lines[j].length; i++) {
String[] words = split(lines[j][i], ' ');
speaker[j][i] = words[0];
time[j][i] = words[1];
//println(words[1]);
String remove = words[0] + " " + words[1] + " ";
String[] dialogs = split(lines[j][i], remove);
dialog[j][i] = dialogs[1];
//println(dialogs[1]);
}
audio[j] = minim.loadFile("aura" + (j+1)+ ".mp3");
}
// Load a soundfile from the /data folder of the sketch and play it back
//audio[0].play();
beat = new BeatDetect();
String portName = Serial.list()[13]; //change the 0 to a 1 or 2 etc. to match your usb port
myPort = new Serial(this, portName, 9600);
println(portName);
printArray(Serial.list());
//println(lines[0].length);
// println(dialog[0].length);
//printArray(time[0]);
textFont(font, 32);
noCursor();
}
void draw() {
//scale(1.2);
background(0);
pushMatrix();
translate(60,-150);
int playTime = (int)audio[index].position()/1000;
//println((int)audio[index].position()/1000, (int)audio[index].length()/1000);
if (playTime >= (int)audio[index].length()/1000) {
if (state != '0') {
myPort.write('0');
state = '0';
//println("tirgger rest");
}
displayGuide();
speed = 0.8;
} else {
for (int i = 1; i < lines[index].length; i++) {
if (playTime >int(time[index][i-1]) && playTime < int(time[index][i])+1) {
displayUsersDialog(speaker[index][i], dialog[index][i]);
}
}
}
breathing();
drawRing();
popMatrix();
drawDialRing();
//text("yes", 80, height/2, 200, 240);
//println(frameRate);
//displayGuide();
}
void displayUsersDialog(String _speaker, String s) {
//print(_speaker);
//println(s);
textSize(normal);
textAlign(LEFT, TOP);
fill(255);
if (int(_speaker) ==1) {
fill(#FF82F8);
s = "Aura:" + "\n" + "\n"+ s;
text(s, width/2 -600, height/2-100, 350, 340);
ringSize =1;
if ( beat.isOnset() ) {
speed = 10;
} else {
speed = 2;
}
if (state != '2') {
myPort.write('2');
state = '2';
println("tirgger on");
}
} else if (int(_speaker) ==2)
{
fill(255);
//println("it is U");
s = "User:" + "\n" + "\n"+ s;
text(s, width/2 +250, height/2 -100, 350, 340);
speed = 0.8;
ringSize = 0.8f;
if (state != '1') {
myPort.write('1');
state = '1';
println("tirgger off");
}
} else if (int(_speaker) ==3)
{
fill(255);
//println("it is U");
s = "Restaurant:" + "\n" + "\n"+ s;
text(s, width/2 +250, height/2 -100, 350, 340);
speed = 0.8;
ringSize = 0.8f;
if (state != '1') {
myPort.write('1');
state = '1';
println("tirgger off");
}
} else if (int(_speaker) ==4)
{
fill(255);
//println("it is U");
s = "Mom:" + "\n" + "\n"+ s;
text(s, width/2 +250, height/2 -100, 350, 340);
speed = 0.8;
ringSize = 0.8f;
if (state != '1') {
myPort.write('1');
state = '1';
println("tirgger off");
}
}
}
void drawRing() {
noFill();
beat.detect(audio[index].mix);
stroke(255);
ellipse(width/2, height/2, 330 + ringChange, 330 + ringChange);
}
void breathing() {
ringChange = sin(a) * 20 * ringSize;
a += 0.05 * speed;
;
}
//void mousePressed() {
// postRequest();
//}
void keyPressed() {
println(index);
if (keyCode == 'U') {
ChangeIndexPlus();
println("U is pressed");
}
if (keyCode == 'D') {
println("D is pressed");
ChangeIndexMinus();
}
if (keyCode == 'P') {
println("P is pressed");
if (audio[index].isPlaying()) {
audio[index].pause();
} else {
audio[index].play();
}
}
}
void ChangeIndexPlus() {
audio[index].pause();
audio[index].rewind();
if (index < totalNum-1) {
index ++;
} else {
index =totalNum -1;
}
audio[index].play();
}
void ChangeIndexMinus() {
audio[index].pause();
audio[index].rewind();
if (index > 0) {
index --;
} else {
index =0;
}
audio[index].play();
}
void displayGuide() {
fill(255, ringChange *5 + 150);
textSize(normal);
textAlign(CENTER);
text("Please turn the dial to trigger another dialogue.", width-280, height -350);
}
void drawDialRing() {
pushMatrix();
translate(-500, -90);
scale(1.2);
textSize(normal - 5);
textAlign(RIGHT, CENTER);
//ellipse(width-180, height -200, 180, 180);
if (index==0) {
fill(255);
textSize(selected);
ellipse(width-280, height -150, 8, 8);
} else {
fill(255, 120);
textSize(normal- 3);
}
text("Learning voice", width-300, height -150);
if (index==1) {
fill(255);
textSize(selected);
ellipse(width-290, height -200, 8, 8);
} else {
fill(255, 120);
textSize(normal- 3);
}
text("Accessing data", width-310, height -200);
if (index==2) {
fill(255);
textSize(selected);
ellipse(width-280, height -250, 8, 8);
} else {
fill(255, 120);
textSize(normal- 3);
}
text("Booking restaurant", width-300, height -250);
if (index==3) {
fill(255);
textSize(selected);
ellipse(width-260, height -300, 8, 8);
} else {
fill(255, 120);
textSize(normal- 3);
}
text("Call from mom", width-280, height -300);
popMatrix();
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .