Hello,
I managed to deform some letters and to scale them reacting to a sound file using Geomerative and minim libraries, but how can I keep the letters deformed?! Why once the sound position is 100px away the points get back to their original positions?!
Thanks a lot in advance for your help.
Best,
L
import processing.pdf.*;
import geomerative.*;
import ddf.minim.analysis.*;
import ddf.minim.*;
String soundNameFR = "FR_01";
Minim minim;
AudioPlayer soundsFR;
FFT fftFR;
float bandHeightFR;
int x, y;
String [] message={"music is like a mountain path"};
color textColor=0;
RFont f;
RShape gShape;
float fontSize=110;
int splitGlyph = 120;
RPoint[][] pointsP;
float r = random(5, 20);
WordAttractor attractorW;
int tChildCount;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
size(1920, 1080);
RG.init(this);
f = new RFont("FreeSans.ttf", int( fontSize), LEFT);
minim=new Minim(this);
soundsFR=minim.loadFile("FR_01.wav", 512);
soundsFR.play();
fftFR= new FFT(soundsFR.bufferSize(), soundsFR.sampleRate());
pointsP = new RPoint[message.length][];
for (int i =0; i<message.length; i++) {
//We create a RGroup to which we apply the font
RGroup myGroupFR = f.toGroup(message[i]);
// We set this groupe to a ploygons group
myGroupFR = myGroupFR.toPolygonGroup();
// We call all the points stored inside the group
pointsP[i] = myGroupFR.getPoints();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void draw() {
background(0);
smooth();
soundFFTAnalyse();
pushMatrix();
for (int l=0; l<message.length; l++) {
translate(200, height/2);
pointsP= new RPoint [message[l].length()][splitGlyph];
RShape [] gShape = new RShape [message.length];
//get grouped lettershapes from RFont
gShape [l] = f.toShape(message[l]);
//get the PShape[] containing all the single letters as RShapes. Each letter = children
RShape[]letterShapes = gShape[l].children;
//Number of childrens --> number of letters
tChildCount = gShape[l].children.length;
//int tChildrenCount = gShape[1].children.length;
for (int k = 0; k < tChildCount; k++) {
RShape tShape = letterShapes[k];
float posX=map(soundsFR.position(), 200, soundsFR.length(), 200, width-200);
float posY=bandHeightFR/8;
// Calculate distance between attractor position and the center of each character
float d= dist(posX, 0, gShape[l].children[k].getCenter().x, 0);
//Scale each character according to its distance with mouseX
float sx= map(d, 0, 100, 3, 1);
float r= map(d, 0, 100, 0, -TWO_PI);
if (d<100) {
gShape[l].children[k].scale(sx, gShape[l].children[k].getCenter());
//gShape[l].children[k].rotate(r, gShape[l].children[k].getCenter());
}
for (int j=0; j<splitGlyph; j++) {
float frac=(1.0/splitGlyph);
pointsP[k][j]=gShape[l].children[k].getPoint(j*frac);
RPoint topLeft = tShape.getTopLeft();
RPoint bottomRight = tShape.getBottomRight();
attractorW= new WordAttractor(posX-250, -280+posY, pointsP[k][j]);
attractorW.attract();
stroke(255);
pushMatrix();
translate( pointsP[k][j].x, pointsP[k][j].y);
beginShape();
noFill();
strokeWeight(1);
float angle = TWO_PI/18;
rotate(j/angle+mouseX/500*noise(pointsP[k][j].x));
bezier(-5*(noise(5)), 5, -10*(noise(2)), 5, -5*noise(10), -5, 5, -5);
endShape();
popMatrix();
}
}
}
popMatrix();
}
void soundFFTAnalyse() {
AudioPlayer fr = soundsFR;
fftFR.forward(fr.mix);
for (int i=0; i<fftFR.specSize(); i++) {
float bandDBFR = 10*log(fftFR.getBand(i)/fftFR.timeSize());
bandHeightFR = map(bandDBFR*4, 0, -220, 0, height);
constrain(bandHeightFR, 0, 1000);
noStroke();
fill(255, 0, 0);
ellipse(fr.position()/10, bandHeightFR/20, 5, 5);
}
}
class WordAttractor {
float force_radious = 200;
float maxForce = 50;
RPoint position;
RPoint points;
float pX;
float pY;
WordAttractor(float x, float y, RPoint p) {
points =p;
position = new RPoint(x, y);
}
void attract() {
float d= points.dist(position);
if (d < force_radious) {
RPoint desired = new RPoint(points);
desired.sub(position);
desired.normalize();
desired.scale(map(d, 0, force_radious, maxForce, 0));
points.add(desired);
}
}
}