I've been working on a bridge to implement sphinx 4 speech recognition into Processing. I know there have been countless others wanting this or trying to figure out how to do it, so I've decided to offer the code since the project has been complete.
The use was to pull in twitter feeds based on key words and speak the tweets that it had found in order to replicate a conversation between two computers. However, there are more applications that could be used with this, so feel free to work on the code and make something.
Hi Guys,
so I'm preparing an exhibition in two days. Cutting it close I know. I've spent most of my time implementing Sphinx4 speech recognition into processing, which I want to share with everyone soon enough. I'm having some difficulty on the graphics here.
I've used a bit of code from a previous poster for a wanderer. What I've done is pass a variable _l in the event Listener that has the keyword that was recognized by the recognizer engine. That value is going to be one of 4 things "love" "hate" "happy" "sad". I'm trying to get the particles that are associated with that keyword to move to a coordinate, if that text is true in the _l.readString() in the void EventListener.
I've tried every which way to get it sorted. This is where I'm at with it so far. So everything runs but I just can't get the if statement to work and get the balls to move to a coordinate.
Help is most appreciated!
Twitter myTwitter;
Listen reading;
Particle ball;
Particle arriver;
int max_width = 800;
int max_height = 800;
String s = "";
ArrayList finding = new ArrayList();
ArrayList finding2 = new ArrayList();
ArrayList finding3 = new ArrayList();
ArrayList finding4 = new ArrayList();
void loadTwitter() {
myTwitter = new Twitter("yourTwitterUserName", "yourTwitterPassword");
try {
String searchKeys = "love OR hate OR sad OR happy";
// Search twitter for tweets matching the query
println("Searching Twitter for " + searchKeys);
Query tquery = new Query(searchKeys);
tquery.setRpp(100);
QueryResult result = myTwitter.search(tquery);
for (int i = 0; i < 4; i++) // Loop though 4 pages of search results
{
ArrayList tweets = (ArrayList) result.getTweets();
println("tweets found : " + tweets.size());
// Set the next page for the search results
tquery.setPage(tquery.getPage() + 1);
// Create a wanderer class for each of the search results and add it to the array
for (int k = 0; k < tweets.size(); k++) {
int colCount = 0;
int colRed = 0;
int colGreen = 0;
int colBlue = 0;
Particle ball = new Particle(new PVector(width/2,height/2),random(max_width), random(max_height) );
// Get the tweet text
String tweetText = tweets.get(k).toString();
ball.setTweet(tweetText);
// Set the tweet text in the class
// convert the text to lowercase (for searching)
tweetText = tweetText.toLowerCase();
if (tweetText.contains("love"))
{
colRed += 216;
colGreen += 20;
colBlue += 20;
colCount++;
finding.add(ball);
}
if (tweetText.contains("hate"))
{
colRed += 129;
colGreen += 160;
colBlue += 162;
colCount++;
finding2.add(ball);
}
reading = new Listen(this,"sample.config.xml");
frameRate(15);
loadTwitter();
}
void draw() {
background(0);
for (int m = 0; m < finding.size(); m++) {
Particle ball = (Particle) finding.get(m);
ball.stayInsideCanvas();
ball.move();
fill(ball.getColor());
ellipse(ball.getX()+300, ball.getY()+300, 20, 20);
ball.arrive(new PVector(300,300));
}
for (int m = 0; m < finding2.size(); m++) {
Particle ball = (Particle) finding2.get(m);
ball.stayInsideCanvas();
ball.move();
fill(ball.getColor());
ellipse(ball.getX()+300, ball.getY()+300, 20, 20);
}
for (int m = 0; m < finding3.size(); m++) {
Particle ball = (Particle) finding3.get(m);
ball.stayInsideCanvas();
ball.move();
fill(ball.getColor());
ellipse(ball.getX()+300, ball.getY()+300, 20, 20);
}
for (int m = 0; m < finding4.size(); m++) {
Particle ball = (Particle) finding4.get(m);
ball.stayInsideCanvas();
ball.move();
fill(ball.getColor());
ellipse(ball.getX()+300, ball.getY()+300, 20, 20);
}
}
void listenEvent(Listen _l) {
int k = -5;
int minDistance = 3;
background(0);
s = _l.readString();//returns the tweet value of "love" "hate" "angry" "sad" etc
for (int c = 0; c < finding.size(); c++) {
if (s.contains("love")){ //if the Listen passed result = word love
if (finding.contains("love")){
ball.arrive(new PVector(0,0));
}
}
}
for (int c = 0; c < finding2.size(); c++) {
if (s.contains("hate")){
if (finding.contains("hate")){
ball.arrive(new PVector(0,0));
}
}
}
for (int c = 0; c < finding3.size(); c++) {
if (s.contains("sad")){
if (finding.contains("sad")){
ball.arrive(new PVector(300,300));
}
}
}
}
class Particle {
PVector loc;
PVector vel;
PVector acc;
float r;
float maxforce; // Maximum steering force
float maxspeed; // Maximum speed
float x;
float y;
float wander_theta;
float wander_radius;
Particle(PVector l, float _x, float _y) {
acc = new PVector(0,0);
vel = new PVector(random(-1,1),random(-1,1));
loc = l.get();
r = 2.0;
x = _x;
y = _y;
wander_theta = random(TWO_PI);
wander_radius = random(max_wander_radius);
}
float getY()
{
return y;
}
public void stayInsideCanvas()
{
x %= width;
y %= height;
}
// We accumulate a new acceleration each time based on three rules
void flock(ArrayList particles) {
PVector sep = separate(particles); // Separation
PVector ali = align(particles); // Alignment
PVector coh = cohesion(particles); // Cohesion
// Arbitrarily weight these forces
sep.mult(1.5);
ali.mult(1.0);
coh.mult(1.0);
// Add the force vectors to acceleration
acc.add(sep);
acc.add(ali);
acc.add(coh);
}
// Method to update location
void update() {
// Update velocity
vel.add(acc);
// Limit speed
vel.limit(maxspeed);
loc.add(vel);
// Reset accelertion to 0 each cycle
acc.mult(0);
}
// A method that calculates a steering vector towards a target
// Takes a second argument, if true, it slows down as it approaches the target
PVector steer(PVector target, boolean slowdown) {
//maxspeed = ms;
//maxforce = mf;
PVector steer; // The steering vector
PVector desired = target.sub(target,loc); // A vector pointing from the location to the target
float d = desired.mag(); // Distance from the target is the magnitude of the vector
// If the distance is greater than 0, calc steering (otherwise return zero vector)
if (d > 0) {
// Normalize desired
desired.normalize();
// Two options for desired vector magnitude (1 -- based on distance, 2 -- maxspeed)
if ((slowdown) && (d < 100.0)) desired.mult(maxspeed*(d/100.0)); // This damping is somewhat arbitrary
else desired.mult(maxspeed);
// Steering = Desired minus Velocity
steer = target.sub(desired,vel);
steer.limit(maxforce); // Limit to maximum steering force
}
else {
steer = new PVector(0,0);
}
return steer;
}
// Wraparound
void borders() {
if (loc.x < -r) loc.x = width+r;
if (loc.y < -r) loc.y = height+r;
if (loc.x > width+r) loc.x = -r;
if (loc.y > height+r) loc.y = -r;
}
// Separation
// Method checks for nearby boids and steers away
PVector separate (ArrayList particles) {
float desiredseparation = 20.0;
PVector steer = new PVector(0,0,0);
int count = 0;
// For every boid in the system, check if it's too close
for (int i = 0 ; i < particles.size(); i++) {
Particle other = (Particle) particles.get(i);
float d = PVector.dist(loc,other.loc);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if ((d > 0) && (d < desiredseparation)) {
// Calculate vector pointing away from neighbor
PVector diff = PVector.sub(loc,other.loc);
diff.normalize();
diff.div(d); // Weight by distance
steer.add(diff);
count++; // Keep track of how many
}
}
// Average -- divide by how many
if (count > 0) {
steer.div((float)count);
}
// As long as the vector is greater than 0
if (steer.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
steer.normalize();
steer.mult(maxspeed);
steer.sub(vel);
steer.limit(maxforce);
}
return steer;
}
// Alignment
// For every nearby boid in the system, calculate the average velocity
PVector align (ArrayList particles) {
float neighbordist = 25.0;
PVector steer = new PVector(0,0,0);
int count = 0;
for (int i = 0 ; i < particles.size(); i++) {
Particle other = (Particle) particles.get(i);
float d = PVector.dist(loc,other.loc);
if ((d > 0) && (d < neighbordist)) {
steer.add(other.vel);
count++;
}
}
if (count > 0) {
steer.div((float)count);
}
// As long as the vector is greater than 0
if (steer.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
steer.normalize();
steer.mult(maxspeed);
steer.sub(vel);
steer.limit(maxforce);
}
return steer;
}
// Cohesion
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
PVector cohesion (ArrayList particles) {
float neighbordist = 25.0;
PVector sum = new PVector(0,0); // Start with empty vector to accumulate all locations
int count = 0;
for (int i = 0 ; i < particles.size(); i++) {
Particle other = (Particle) particles.get(i);
float d = loc.dist(other.loc);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.loc); // Add location
count++;
}
}
if (count > 0) {
sum.div((float)count);
return steer(sum,false); // Steer towards the location
}
return sum;
}
color getColor()
{
return col;
}
public void setTweet(String text)
{
tweet = text;
}