Combining separate sketches in one processing file

edited May 2016 in Library Questions

Hello everyone. I have a question. We are doing a new media art postgraduate project,our lecturer wants to get us involved with processing. I have seven different sketches. They are all separately. I want to combine them like a slideshow or with a mouse click in one pde file, because its a story . But i have "if mousepressed" comment some of my sketches. Maybe a button can solve my problem. But i have no idea how to do this because I'm a beginner. I am attaching my sketches. Can you please help me? Thank you all warmly.

sketch1:

import ddf.minim.*;
import ddf.minim.ugens.*;

Minim minim;
AudioOutput out;


class SineInstrument implements Instrument
{
  Oscil wave;
  Line  ampEnv;

  SineInstrument( float frequency )
  {

    wave   = new Oscil( frequency, 0, Waves.SINE );
    ampEnv = new Line();
    ampEnv.patch( wave.amplitude );
  }

  void noteOn( float duration )
  {
    ampEnv.activate( duration, 0.5f, 0 );

    wave.patch( out );
  }

  void noteOff()
  {
    wave.unpatch( out );
  }
}


float x, y; // X and Y coordinates of text
float hr, vr;  // horizontal and vertical radius of the text
void setup() {


size(640, 360, P3D);
  fill(25);
  textFont(createFont("Georgia", 36));
  textAlign(CENTER, CENTER);
vr = (textAscent() + textDescent()) / 2;
  noStroke();
  x = width / 2;
  y = height / 2;
    minim = new Minim(this);


  out = minim.getLineOut();


  out.playNote( 0.0, 0.9, new SineInstrument( 97.99 ) );
  out.playNote( 1.0, 0.9, new SineInstrument( 123.47 ) );

  // we can use the Frequency class to create frequencies from pitch names
  out.playNote( 2.0, 2.9, new SineInstrument( Frequency.ofPitch( "C3" ).asHz() ) );
  out.playNote( 3.0, 1.9, new SineInstrument( Frequency.ofPitch( "E3" ).asHz() ) );
  out.playNote( 4.0, 0.9, new SineInstrument( Frequency.ofPitch( "G3" ).asHz() ) );
}


void draw() {
  lights();
  background(0);

  // Change height of the camera with mouseY
  camera(10.0, mouseY, 300.0, // eyeX, eyeY, eyeZ
         0.0, 0.0, 0.0, // centerX, centerY, centerZ
         0.0, 1.0, 0.0); // upX, upY, upZ

  noStroke();
  box(120);
  stroke(0);
  line(0, 0, 0, 0, 0, 0);
  line(0, 0, 0, 0, 0, 0);
  line(0, 0, 0, 0, 0, 0);
 text("KARANLIK BİR ODA...", width-mouseX, height-mouseY);

}

sketch 2:

import ddf.minim.*;
import ddf.minim.ugens.*;

Minim minim;
AudioOutput out;
Oscil       wave;
Oscil       mod;

PFont font;
String[] words = {"uyanıyorum...", "uyanıyorum...", "uyanıyorum...", "uyanıyorum...", "uyanıyorum...", " "};

int whichWord = 0;

void setup() {
  minim = new Minim(this);

  // use the getLineOut method of the Minim object to get an AudioOutput object
  out = minim.getLineOut();

  // create a triangle wave Oscil, set to 440 Hz, at 1.0 amplitude
  // in this case, the amplitude we construct the Oscil with 
  // doesn't matter because we will be patching something to
  // its amplitude input.
  wave = new Oscil( 440, 1.0f, Waves.TRIANGLE );

  // create a sine wave Oscil for modulating the amplitude of wave
  mod  = new Oscil( 2, 0.4f, Waves.SINE );

  // connect up the modulator
  mod.patch( wave.amplitude );

  // patch wave to the output
  wave.patch( out );
  size(640, 360);
  font = createFont("Times New Roman", 50);
  textFont(font);
  textAlign(CENTER);
  frameRate(20);
  fill(204);
}

void draw() {
  background(0);
  text(words[whichWord], width/2, 200);
  whichWord++;
  if (whichWord == words.length) {
    whichWord = 0;
  }
}

sketch 3:

import ddf.minim.*;
import ddf.minim.ugens.*;

// create all of the variables that will need to be accessed in
// more than one methods (setup(), draw(), stop()).
Minim minim;
AudioOutput out;
Delay myDelay;
int savedTime;
int totalTime = 10000;

int i1=0;
int i2=0;
int i3=0;
int i4=0;
int i5=0;
int i6=0;
int newline = 150;
int flag = 0;
int x = 250;
int y = 250;

int time;
int wait = 100;
boolean tick;


String[] adjective = {
 "düşünceler...", 

};
String[] noun = {
  "içimdeki","başkalarının", 

};

String[] verb = {
  "sesleri", 
};



String[] adverb = {
 "hızlı", "geçen",

};

String[] determiner = {
 "düşünceler", "onlar","benim","değil"
};

void setup()
{
//font = loadFont("Trebuchet MS.ttf");
  size(640, 360);
 //fullScreen();
  background(0);//
  color(255);
  textSize(18);
  smooth();
  strokeWeight(3);
   minim = new Minim(this);
  out = minim.getLineOut();

  // initialize myDelay with continual feedback and audio passthrough
  myDelay = new Delay( 0.4, 0.5, true, true );

  // sawh will create a Sawtooth wave with the requested number of harmonics.
  // like with Waves.randomNHarms for sine waves, 
  // you can create a richer sounding sawtooth this way.
  Waveform saw = Waves.sawh( 15 );
  // create the Blip that will be used
  Oscil myBlip = new Oscil( 245.0, 0.3, saw );

  // Waves.square will create a square wave with an uneven duty-cycle,
  // also known as a pulse wave. a square wave has only two values, 
  // either -1 or 1 and the duty cycle indicates how much of the wave 
  // should -1 and how much 1. in this case, we are asking for a square 
  // wave that is -1 90% of the time, and 1 10% of the time. 
  Waveform square = Waves.square( 0.9 );
  // create an LFO to be used for an amplitude envelope
  Oscil myLFO = new Oscil( 1, 0.3, square );
  // offset the center value of the LFO so that it outputs 0 
  // for the long portion of the duty cycle
  myLFO.offset.setLastValue( 0.3 );

  myLFO.patch( myBlip.amplitude );

  // and the Blip is patched through the delay into the output
  myBlip.patch( myDelay ).patch( out );
}

void draw()
{
  textSize(18);
    i1 = round(random(0,(adjective.length-1)));  // generating a Random element index for the Subject Array
    i5 = round(random(0,(adjective.length-1)));
    i2 = round(random(0,(noun.length-1)));  // generating a Random element index for the Verb Array
    i3 = round(random(0,(verb.length-1)));  // generating a Random element index for the Object Array
    i4 = round(random(0,(adverb.length-1)));
    i6 = round(random(0,(determiner.length-1)));

  if(flag==0) //initial screen when no buttons have been pressed
  {
     initial();
   noLoop();
   textSize(18);
   // text(noun[i3] + " " + verb[i2] + " " + determiner[i6] + " " + noun[i3], (width/1)-250, height); 
   text(adjective[i1] + " " + adjective[i5] + " " +  noun [i2] +" " + verb[i3] + " " + adverb[i4], (width/2-125), height/2);
    textSize(14);
   //text("Left Click - To Randomize" +" " + " --||-'S'- To Save-||-- " + " " +"Right Click - To Stop", (width/2)-600, 60);

  }
  if(flag==1) //when left mouse is clicked
  {
    background (0);
    text(adjective[i1] + " " + adjective[i5] + " " +  noun [i2] +" " + verb[i3] + " " + adverb[i4], (width/2)-125, height/2);
    // text(noun[i3] + " " + verb[i2] + " " + determiner[i6] + " " + noun[i3], (width/1.5)-250, height); 
  }

   //textSize(15);
   //text("Left Click - To Randomize" +" " + " --||-'S'- To Save-||-- " + " " +"Right Click - To Stop", (width+CENTER), 60);

}
void mousePressed()
{
  if(mouseButton==LEFT)
  {
     flag = 1;
     loop();
  }
if(mouseButton==RIGHT)
  {
    flag = -1;
    loop();
  }
  if(mouseButton==CENTER)
  {
    flag=0;
    noLoop();
  }
}

void sentence() //defines the sentence
{
  text(noun[i3] + " " + verb[i2] + " " + determiner[i6] + " " + noun[i3], (width/2)-125, height); 
   text(adjective[i1] + " " + adjective[i5] + " " + verb[i2] + " " + noun[i3] + " " + adverb[i4], (width/2)-125, height); 
}

void initial() //
{
 background(0);
  textSize(25);
  for(int a = 0; a < adjective.length; a++)
  {

 // text(adjective[a] + " " + verb [a] + " " + noun[a] + " " + adverb[a], (width/2)-250 , newline);
    newline = newline + 50;
  }

}
 void keyPressed() { //hit the "s" key and save my image
  if (key=='s')
  {
    saveImage();
  }
}

void saveImage() {
  saveFrame("keep_going_##.png"); //when running on the web it will put the PNG in a new browser tab
}

sketch 4:

import ddf.minim.*;
import ddf.minim.ugens.*;

// create all of the variables that will need to be accessed in
// more than one methods (setup(), draw(), stop()).
Minim minim;
AudioOutput out;

// the Oscil we use for modulating frequency.
Oscil fm;
String msg;
PFont font;
  PFont font1;
void setup(){
   minim = new Minim( this );
  out   = minim.getLineOut();

  // make the Oscil we will hear.
  // arguments are frequency, amplitude, and waveform
  Oscil wave = new Oscil( 200, 0.8, Waves.TRIANGLE );
  // make the Oscil we will use to modulate the frequency of wave.
  // the frequency of this Oscil will determine how quickly the
  // frequency of wave changes and the amplitude determines how much.
  // since we are using the output of fm directly to set the frequency 
  // of wave, you can think of the amplitude as being expressed in Hz.
  fm   = new Oscil( 10, 2, Waves.SINE );
  // set the offset of fm so that it generates values centered around 200 Hz
  fm.offset.setLastValue( 200 );
  // patch it to the frequency of wave so it controls it
  fm.patch( wave.frequency );
  // and patch wave to the output
  wave.patch( out );

  int i;
  //frameRate(30);
  size(640, 360); //determina el tamaño de la pantalla
  size(640, 360);
  //smooth sirve para mejorar el aspecto de los gráficos, la calidad del renderizado 2,3,4 o 8
  smooth();
  frameRate(50);
  //font cargamos la tipografía desde Tools
//PFont es una variable tipográfica 
 font = createFont("aileronultralight", 30);
 font1 = createFont("aileronthin", 10);
  //msg escribimos el mensaje
  msg = "[ yine aynı baş ağrısı ] ";

}

void draw(){
  background(0);

  pushMatrix();
  textFont(font1,65);
  fill(255,0,0,random(0,255));
  translate(width/2, height/2);
  textAlign(CENTER);
  text(msg, 0, 0);
  popMatrix();

  pushMatrix();
  textFont(font1,random(50,100));
  fill(random(0,255),255,255,random(0,255));
  translate(width/2,height/2);
  textAlign(CENTER);
  text(msg,0,0);
  popMatrix();

}

sketch 5:

import ddf.minim.*;

Minim minim;
AudioPlayer player;
PFont myfont;

void setup()
{
  size(640,360);

  myfont = createFont("Serif",80);
  minim = new Minim(this);

  // 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("Footsteps_on_Cement-Tim_Fryer-870410055.mp3");

  // play the file from start to finish.
  // if you want to play the file again, 
  // you need to call rewind() first.
  player.play();
}

void draw()

{
  if (mousePressed)
  {
    background (0);
  translate(width/2, height/2);
  fill(255,255,255);

  textFont (myfont);
  textSize(48);
  textAlign(CENTER,CENTER);
  text("yakınlaşan ayak seslerim", 0, 0);
  }

  else
  {
    background(255);
    translate(width/2, height/2);
    fill(0);

    textFont (myfont);
    textSize(24);
    textAlign(CENTER, CENTER);
    text("uzaklaşan içimdeki ses", 0, 0);
  }


}   

sketch 6:

PFont myfont;


void setup()
{
size(500,500);

myfont = createFont("san serif", 80);
}

void draw()
{
   if (mousePressed)
  {
    background (255);
  translate(width/2, height/2);
  fill(255,255,255);
   textFont(myfont);
  fill(0);
  textSize(40);
  textAlign(CENTER, CENTER);
  text("nüchtern", 0, 0);

  }
  else
  {
  background(random(255),random(255),random(255));
  translate( 0,height/2);
  rotate(millis()/500.0);
  fill(random(255),random(255),random(255));
 float a = sin(millis()/400.0);
  textFont(myfont);
  textSize(90);
  textAlign(LEFT, CENTER);
  text("BERAUSCHT", 0, a*100);
}
}

sketch 7:

import ddf.minim.*;

Minim minim;
AudioPlayer player;

PFont typo;

void setup()
{
  size (640, 360);
  typo = createFont("BrushScriptStd", 80);
   minim = new Minim(this);

  // 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("jarboe-kris-force-safe-song-the-path.mp3");

  // play the file from start to finish.
  // if you want to play the file again, 
  // you need to call rewind() first.
  player.play();
  //println(PFont.list());
}

void draw()
{

  //background(255,0,255);
  translate(width/2, height/2);

  if (mousePressed == false)
  {
    background(255, 255, 255);

    fill(0);

    pushMatrix();
    rotate(millis()/100.0);
    textFont(typo);
    textSize(30);
    textAlign(CENTER, CENTER);
    text("bu dönme hissi hayal mi?", 0, 0);
    popMatrix();

    pushMatrix();
    textFont(typo);
    textSize(40);
    fill(255, 255, 255, 150);
    float x = sin(millis()/2500);
    float y = cos(millis()/2500);
    //rotate(millis()/500.0);
    //text("Hello", 220*x, 220*y);
    popMatrix();



    pushMatrix();
    textFont(typo);
    textSize(20);
    fill(0);
    rotate(millis()/600.0);
    fill(0);
    text("başka gerçek",150,150);
    text("olabilir", -150,-150);
    text("gerçek gibi", -150,150);
    text("ayrım yapamıyorum", 150,-150);
    popMatrix();



  } else if ( mousePressed == true)
  {
    background(0);
    textFont(typo);
    textSize(20);
    textAlign(CENTER, CENTER);
    fill(255);
    text("Gerçek gerçeklik normal,", 0, 13);
    text("sıradan bir insanın algılamasına denk düşerken, ", 0, 40);
  text("ikinci gerçeklik sağlıklı bir insanın anlayamayacağı,", 0, 70);
  text("çoğu kez belli bir sisteme dayalı bir gerçekliktir.", 0, 100);  
}
}
Tagged:

Answers

  • hm....

    when you join them you'll have one setup with only size in it

    rename the othersetups to setup1 to setup7 --- removeall calls to size but one

    same with draw : you need one draw with switch(state) (see reference) and name your old draws draw1 to draw7

    now only one keypressed is allowed say if (key==' ') {state++; switch(state)case 0: setup1(); break; case 1: setup2(); ........ there

    now as you guessed state tells you which sub sketch to run

    so in draw

    switch(state) {

    case 0:

    draw1();

    break;

    case 1:

    draw2()

    break;

    ........

    ok?

    best, Chrisir ;-)

  • thank you very much Chrisir, you explain very clear, but hope i can make it, i have sounds for each pde sketch as well, what about them? can i put them too?

  • can you please make an example two of my sketches, if i can see how its done maybe i can join seven sketches together...its like chinese i cant find the solution. im sorry for the trouble :(

  • Sure.

    best load them in setup()

    The Main idea is that you need a meta structure with setup and draw and states to manage your 7 old sketches.

    Those are them added / embedded below renamed to setup1 to 7 and draw1 to 7

Sign In or Register to comment.