Hi soundburst, I made some changes, basically i put the code at the top of the sketch.
Like you said, all you do is drop the code at the top of the sketch or into a new tab if you prefer.
This is optional but I find it produces the best results.
Adding
void setup(){
size(1970,760,P3D);
...
...
...
frameRate(frate) ;
}
Here is the code I modified slightly you can see I modified the size of the sketch just for testing purposes
- import processing.video.*;
public int pn=0;
public String savename ="k-soundburst-"; // << here is the name of the file
MovieMaker mm;
public boolean firstpass = true;
public int frate = 15; // <<-- here is the frameRate
public boolean inc = true; // always set to true this increments each frame
public boolean addmovframe = true; // this is used to save the quictktime video
public boolean dovid =false; //<< toggle this true or false to save pics
public boolean looponce = false; // if you want to just loop once set to true
public boolean snap = true; // toggle this to take a snap of the first frame
void saveget() {
setupvid();
if (dovid) {
if (inc) {
pn++;
}
save(savename+str(pn)+".png");
}
if (addmovframe) {
mm.addFrame();
}
if (looponce) {
noLoop();
exit();
} // Pressing the ESCape , ' key or ~ key will end the capture and the sketch
if (keyPressed ==true && ( key=='`' || key =='~' || key == ESC)) {
mm.finish();
exit();
}
}
void setupvid() {
if (firstpass)
{
mm = new MovieMaker(this, width, height, savename+".mov", frate,
MovieMaker.ANIMATION, MovieMaker.LOSSLESS);
if (snap) {
save("snap"+savename+str(pn)+".png");
}
firstpass = false;
}
}
void loop() {
saveget(); // toggle comment this to save video or run as normal
}
///----
Trails tr1;
PGraphics tintLayer;
float mouseStrength;
float t=0;
void setup() {
// ---- Parameters to tune ----
int numParticles = 400;
float speed = 3; // How strong the Perlin vector field is
int decayRate = 50; // Trails: 0=last forever, 255=die instantly.
float noiseSize = 0.01; // Higher value -> faster spatial change
float timeDelta = 0.01; // Higher value -> faster temporal change
mouseStrength = 1.0; // How strongly the mouse pushes/pulls
int borders = 100; // How far off-screen a particle can go
strokeWeight(0.5);
// ---- Boilerplate ----
// size(1680,1050);
//size(800,600,P3D);
size(255,255,P3D);
//frameRate(60);
frameRate(frate);
smooth();
tr1 = new Trails(numParticles, speed);
tr1.setNoiseSize(noiseSize, noiseSize);
tr1.setTimeDelta(timeDelta);
tr1.setBorders(borders);
tr1.generateBottom();
background(0);
stroke(255);
colorMode(HSB, 255);
// The tint layer is just black with an alpha channel.
// At each frame, it is copied to the existing particle trails,
// which creates the decay as it gradually is forced to black.
tintLayer = createGraphics(width, height, P2D);
tintLayer.beginDraw();
tintLayer.background(0,decayRate);
tintLayer.endDraw();
}
void draw() {
PVector mouseForce = null;
// If mouse is pressed, exert a constant force.
if(mousePressed) {
if (mouseButton == LEFT) {
float mx = ((float)mouseX / width - 0.5);
float my = ((float)mouseY / height - 0.5);
mouseForce = new PVector(mouseStrength*mx, mouseStrength*my);
}
}
tr1.updatePositions(mouseForce);
// Dim the old display by a certain amount (tintLayer has an
// alpha channel that influences this)
image(tintLayer, 0, 0);
// Draw new lines on top of this.
tr1.drawLines();
tr1.resurrectOutliers();
t += 0.05;
//---
loop();
}
class Trails {
Particle[] particles;
// noiseOffset = whatever the Z coordinate passed to noise(...) is
// to give us a sort-of distinct 2D space of noise
float noiseOffset;
//float delta;
float cx, cy;
// How far off the screen it can go before being dead
int borders;
// Magnitude of the noise
float magnitude;
// Effect of passage of time (0 = static field)
float timeDelta;
float t;
Trails(int count, float magn) {
particles = new Particle[count];
noiseOffset = 5000;
borders = 50;
magnitude = magn;
timeDelta = 0;
cx = 1;
cy = 1;
t = 0;
}
Trails(Trails toCopy) {
// Note that we don't copy the particles; it's expected that
// you'll generate your own.
particles = new Particle[toCopy.particles.length];
noiseOffset = toCopy.noiseOffset;
cx = toCopy.cx;
cy = toCopy.cy;
borders = toCopy.borders;
magnitude = toCopy.magnitude;
timeDelta = toCopy.timeDelta;
t = toCopy.t;
}
void setNoiseSize(float cx, float cy) {
this.cx = cx;
this.cy = cy;
}
void setTimeDelta(float d) {
timeDelta = d;
}
void setBorders(int b) {
borders = b;
}
// Generate a row of particles, evenly spaced, at the bottom of the view.
void generateBottom() {
int count = particles.length;
for(int i = 0; i < count; ++i) {
PVector pos = new PVector(width*i/count, height);
PVector vel = new PVector(0,0);
particles[i] = new Particle(pos, vel);
}
}
void updatePositions() {
updatePositions(null);
}
// Pass any extra forces as an argument
void updatePositions(PVector otherForce) {
PVector[] forces = new PVector[2];
forces[1] = otherForce;
if (otherForce==null) forces[1] = new PVector(0,0);
for(int i = 0; i < particles.length; ++i) {
PVector pos = particles[i].position;
// The force is derived from the Perlin noise, which we
// generate as a 2D vector by choosing from two regions that
// are suitably far away.
// Since noise(...) is in [0,1] it needs some biasing.
float fx = magnitude*(noise(cx*pos.x, cy*pos.y, t) - 0.47);
float fy = magnitude*(noise(cx*pos.x, cy*pos.y, t + noiseOffset) - 0.48);
forces[0] = new PVector(fx, fy);
particles[i].perturb(forces);
}
t += timeDelta;
}
void drawLines() {
//surface.beginDraw();
//surface.background(0);
for(int i = 0; i < particles.length; ++i) {
PVector old = particles[i].oldPosition;
PVector now = particles[i].position;
stroke(particles[i].h, 127, 255);
line(old.x, old.y, now.x, now.y);
}
//surface.endDraw();
}
void resurrectOutliers() {
int x0 = -borders;
int y0 = -borders;
int x1 = width + borders;
int y1 = height + borders;
for(int i = 0; i < particles.length; ++i) {
PVector pos = particles[i].position;
if (pos.x < x0 || pos.x > x1 || pos.y < y0 || pos.y > y1) {
PVector vel = new PVector(0,0);
PVector newPos = new PVector(random(0,width), height);
particles[i] = new Particle(newPos, vel);
}
}
}
}
class Particle {
PVector position;
PVector velocity;
PVector oldPosition;
int h;
Particle(PVector pos, PVector vel) {
// making a copy, hopefully.
oldPosition = pos.get();
velocity = vel;
position = pos;
h = floor(255*noise(t));
}
void perturb(PVector[] forces) {
oldPosition = position.get();
// Update the position with the velocity.
position.add(velocity);
// Update the velocity with the forces acting on the
// particle (which, since we're assuming a constant
// time delta and constant mass, just means that change
// in velocity equals vector sum of the forces)
for(int i = 0; i < forces.length; ++i) {
velocity.add(forces[i]);
}
}
}
Like you said you , just drop the code at the
top of the code and just call
loop() at the end of the draw() procedure and you are good to go. Sorry I should have been more clear when I said top of any program.