We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
random () (Read 2258 times)
random ()
May 26th, 2010, 8:59pm
 
Hi, i'm new to programming and was wondering if anyone can help me with this little bit. Im not sure how to write a random function to call between different images when a case is true. would i have to make an array for the two different types of videos and write in a random() to call from each array? If so how?thanks in advance

Code:
port processing.video.*;

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.signals.*;

Minim minim;
AudioInput in;
FFT fft;

float loudestFreqAmp = 0;
float loudestFreq = 0;
//int timerCounter = 0;

boolean blown; // can be true or false

Movie idle, idle2, idle3, light, light2, light3;
void setup()
{
 size(550, 400, P2D);
 frameRate(30);

 minim = new Minim(this);
 minim.debugOn();
 background(255);
 idle = new Movie(this, "idle.mov");
 idle2 = new Movie(this, "idle2.mov");
 idle3 = new Movie(this, "idle3.mov");
 light = new Movie(this, "light1.mov");
 light = new Movie(this, "light2.mov");
 light = new Movie(this, "light3.mov");
 //  myMovie2 = new Movie(this, "light1.mov");
 idle.loop();
 light
 .loop();
 noStroke();
 // get a line in from Minim, default bit depth is 16
 in = minim.getLineIn(Minim.STEREO, 1024);
 fft = new FFT(in.bufferSize(), in.sampleRate());
 blown = false;
}

void movieEvent(Movie light) {
 light.read();
}

void draw()
{
 // analyzing audio, is it being blown?

 // step 1 - find loudest sample
 float m = 0;
 for(int i = 0; i < in.bufferSize() - 1; i++) {
   if ( abs(in.mix.get(i)) > m ) {
m = abs(in.mix.get(i));
   }
 }
 println(m);

 // step 2 - is loudest sample loud enough to be being blown
 if((blown==false) && (m > 0.1)) {
   blown=true;
   light.jump(0);
 }
//  else {
//    blown=false;
//  }

 // step 3 - draw the appropriate animation
 if(blown==true) {
   //play a blowing video
   
   // randomely choose one of three
   //if(random()
   image(light,0,0); // blowing video 1
   //image(light2,0,0); // blowing video 2
   //image(light3,0,0); // blowing video 3
   
   
   float currTime = light.time();
   float duration = light.duration();
   if(currTime > duration * 0.9) {
// we have reached the end of the movie file
// so stop blowing animation
blown=false;
idle.jump(0);
   }
 }
 else {
   //play an idle video
   image(idle,0,0);
   //image(idle2,0,0); // idle 2
   //image(idle3,0,0); // idle 3
 }

}

void stop()
{
 // always close Minim audio classes when you are done with them
 in.close();
 minim.stop();

 super.stop();

Re: random ()
Reply #1 - May 26th, 2010, 9:43pm
 
Hi,

I would put all the Movies in an array

Quote:
  Movie[] movies = new Movie[6];


initialize it in the setup method
Quote:
  movies[0] =  new Movie(this, "idle.mov");
  movies[1] =  new Movie(this, "idle1.mov");



and then randomly choose an index from that array when you need to use it

Quote:
 int rnd = int(random(0, movies.length));
 movies[rnd].loop();





Re: random ()
Reply #2 - May 26th, 2010, 10:06pm
 
thanks. i really appreciate it.

can you elaborate more on the randomly choosing an index part. lets say i want to play either movies 3,4, or 5, how would that look?
Re: random ()
Reply #3 - May 26th, 2010, 10:17pm
 
you will need to change the values of the high and low parameters given to random()

see http://processing.org/reference/random_.html
Re: random ()
Reply #4 - May 26th, 2010, 10:25pm
 
in this case int random(3,6); not (3,5) because it is up to the last number but doesnt include it.
Re: random ()
Reply #5 - May 26th, 2010, 10:33pm
 
i tried this but it didnt work out so well. am i approaching this wrong?

Code:
 int rnd = int(random(0,2, movies.length));
movies[rnd].loop();
Re: random ()
Reply #6 - May 26th, 2010, 10:40pm
 
if you want a random index between 0 and 2 use
random(0,3);
Re: random ()
Reply #7 - May 26th, 2010, 10:41pm
 
Well, just use the given code, random() has only two parameters, movies.length is providing the second one, the length of the array.
Re: random ()
Reply #8 - May 26th, 2010, 10:52pm
 
so what about the
Code:
movies.length 


part?

when i try to enter this all in i get

"the method random (float,float)... is not applicable for the arguments (int, int,int)".


It seems like the only thing not working is that part. i get the concept of the high and low stuff, i just dont get where it goes. thanks for your patients guys. still struggling to get the vernacular down for this programming language stuff *as you can tell* hah. I know this must be unbearably simple to you all.

heres an updated look

Code:
import processing.video.*;

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.signals.*;

Minim minim;
AudioInput in;
FFT fft;

float loudestFreqAmp = 0;
float loudestFreq = 0;
//int timerCounter = 0;

boolean blown; // can be true or false

//Movie idle, idle2, idle3, light, light2, light3;
Movie[] movies = new Movie[6];
void setup()
{
size(550, 400, P2D);
frameRate(30);

minim = new Minim(this);
minim.debugOn();
background(255);
movies[0] = new Movie(this, "idle.mov");
movies[1] = new Movie(this, "idle2.mov");
movies[2] = new Movie(this, "idle3.mov");
movies[3] = new Movie(this, "light1.mov");
movies[4] = new Movie(this, "light2.mov");
movies[5] = new Movie(this, "light3.mov");
// myMovie2 = new Movie(this, "light1.mov");
movies[0].loop();
movies[1].loop();
movies[2].loop();
movies[3].loop();
movies[4].loop();
movies[5].loop();

noStroke();
// get a line in from Minim, default bit depth is 16
in = minim.getLineIn(Minim.STEREO, 1024);
fft = new FFT(in.bufferSize(), in.sampleRate());
blown = false;
}

void movieEvent(Movie light) {
light.read();
}

void draw()
{
// analyzing audio, is it being blown?

// step 1 - find loudest sample
float m = 0;
for(int i = 0; i < in.bufferSize() - 1; i++) {
if ( abs(in.mix.get(i)) > m ) {
m = abs(in.mix.get(i));
}
}
println(m);

// step 2 - is loudest sample loud enough to be being blown
if((blown==false) && (m > 0.1)) {
blown=true;
movies[3].jump(0);
movies[4].jump(0);
movies[5].jump(0);
}
// else {
// blown=false;
// }

// step 3 - draw the appropriate animation
if(blown==true) {
//play a blowing video
int rnd = int(random(3,5, movies.length));
movies[rnd].loop();


// randomely choose one of three
//if(random()
//image(movies[3],0,0); // blowing video 1
//image(light2,0,0); // blowing video 2
//image(light3,0,0); // blowing video 3


float currTime = movies[3].time();
float duration = movies[3].duration();
if(currTime > duration * 0.9) {
// we have reached the end of the movie file
// so stop blowing animation
blown=false;
movies[0].jump(0);
movies[1].jump(0);
movies[2].jump(0);
}
}
else {
int rnd = int(random(0,2, movies.length));
movies[rnd].loop();

//play an idle video
//image(movies[0],0,0);
//image(idle2,0,0); // idle 2
//image(idle3,0,0); // idle 3
}

}

void stop()
{
// always close Minim audio classes when you are done with them
in.close();
minim.stop();

super.stop();
}



Re: random ()
Reply #9 - May 26th, 2010, 11:05pm
 
you probably didnt read, what we wrote. as PhiLho said.
random has only 2 paramters (float float) you entered 3 (int, int, int) thats what the error message tells you.
So change it to random(2,movies.length); or whatever startIndex you need.
Re: random ()
Reply #10 - May 26th, 2010, 11:12pm
 
oooooh. i read it, i just didnt understand it. ok. hah thanks for the elaboration. im pretty dense like that. but now all is well.

cheers
Re: random ()
Reply #11 - May 27th, 2010, 9:25am
 
hmm. all looks well to me but for some reason im not getting any video playback. i did get a "not enough memory" message once but usually its just a blank screen. the video files are only 1.5 mb or so each. could the playback have something to do with the
Code:
.jump(); 

?
Code:
import processing.video.*;

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.signals.*;

Minim minim;
AudioInput in;
FFT fft;

float loudestFreqAmp = 0;
float loudestFreq = 0;

boolean blown; // can be true or false

Movie[] movies = new Movie[6];

void setup()
{
size(550, 400, P2D);
frameRate(30);

minim = new Minim(this);
minim.debugOn();
background(255);
movies[0] = new Movie(this, "idle.mov");
movies[1] = new Movie(this, "idle2.mov");
movies[2] = new Movie(this, "idle3.mov");
movies[3] = new Movie(this, "light1.mov");
movies[4] = new Movie(this, "light2.mov");
movies[5] = new Movie(this, "light3.mov");

movies[0].loop();
movies[1].loop();
movies[2].loop();
movies[3].loop();
movies[4].loop();
movies[5].loop();

noStroke();
// get a line in from Minim, default bit depth is 16
in = minim.getLineIn(Minim.STEREO, 1024);
fft = new FFT(in.bufferSize(), in.sampleRate());
blown = false;
}

//void movieEvent(Movie light) {
//light.read();
//}

void draw()
{
// analyzing audio, is it being blown?

// step 1 - find loudest sample
float m = 0;
for(int i = 0; i < in.bufferSize() - 1; i++) {
if ( abs(in.mix.get(i)) > m ) {
m = abs(in.mix.get(i));
}
}
println(m);

// step 2 - is loudest sample loud enough to be being blown
if((blown==false) && (m > 0.1)) {
blown=true;
movies[3].jump(0);
movies[4].jump(0);
movies[5].jump(0);
}

// step 3 - draw the appropriate animation
if(blown==true) {
//play a blowing video
int rnd = int(random(3,6));
movies[rnd].loop();


float currTime = movies[rnd].time();
float duration = movies[rnd].duration();
if(currTime > duration * 0.9) {
// we have reached the end of the movie file
// so stop blowing animation
blown=false;
movies[0].jump(0);
movies[1].jump(0);
movies[2].jump(0);
}
}
else {
int rnd = int(random(0,3));
movies[rnd].loop();

}

}

void stop()
{

in.close();
minim.stop();

super.stop();
}


Page Index Toggle Pages: 1