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.
IndexProgramming Questions & HelpSyntax Questions › Loading images for simple playback with sound
Page Index Toggle Pages: 1
Loading images for simple playback with sound (Read 450 times)
Loading images for simple playback with sound
Nov 18th, 2008, 5:40pm
 
Hi
I have tried to take something I created in Mobile Processing over to Processing, in order to try and embed in a page. I think I have the syntax right for the sound events in the data file, but I cannot seem to create the right syntax for an array for all the 9 images. Can anyone help point out where I am going wrong?

Code:

import pitaru.sonia_v2_9.*;
Particle[] Particles = new Particle[100];
Particle[] Particles2 = new Particle[100];
Particle[] Particles3 = new Particle[100];
int particle_num = 100;
int Pcnt = 0;

// The number of frames in the animation
int numFrames = 9;
// Frame number initialize
int frame = 0;
// Image File Variables
PImage[] img = createImage[numFrames];
// Sound File Variables
Sample[] mySample = new Sample[numFrames];
// Indicator showing whether it is a new clip
int START = 1;
int a;
int i;

void setup()
{
 frameRate(10);
 // Imageload automatically
 for(int i=1; i<=numFrames; i++) {
   String imageName = "girlstones" + i + ".png";
  // println(imageName);
   img[i-1] = loadImage(imageName);
 }
size(200,200);
colorMode(HSB,100);
 for (int i = 0; i< particle_num; i++) {
   Particles[i] = new Particle(new Vector3D((int)(width/2*1.0),(int)(height/2*1.0), (int)(0f*1.0)));
 }
 for (int i = 0; i< particle_num; i++) {
   Particles2[i] = new Particle(new Vector3D((int)(width/2*1.0) -1,(int)(height/2*1.0), (int)(0f*1.0)));
 }
 for (int i = 0; i< particle_num; i++) {
   Particles3[i] = new Particle(new Vector3D((int)(width/2*1.0) + 1,(int)(height/2*1.0), (int)(0f * 1.0)));
 }
 frameRate(30);

 // Sound File load automatically
 Sonia.start(this, 44100);
 for(int i=1; i<=numFrames; i++) {
   String soundName = "girlstones" + i + ".mp3";
   mySample[i-1] = new Sample(soundName, this);
 }
}

void draw()
{    
 frameRate(100);
 if (START == 1) {     // play only new 1.0 not the same 1.0 again and again  
   mySample[frame].play();
 }
 background(img[frame]);
 
 switch (frame) {  
 case 3:  
 if (Pcnt < particle_num) {
     Particles[Pcnt].run();
     Particles2[Pcnt].run();
     Particles3[Pcnt].run();
     Pcnt++;
   }  
   if (Pcnt == particle_num)
     Pcnt--;

   for (i=0 ; i < Pcnt; i++) {
     Particles[i].run();
     Particles2[i].run();
     Particles3[i].run();
     if (Particles[i].dead()) {
       Particles[i] = new Particle(new Vector3D((int)(width/2*1.0),(int)(height/2*1.0), (int)(0f * 1.0)));
     }
     if (Particles2[i].dead()) {
       Particles2[i] = new Particle(new Vector3D((int)(width/2 * 1.0)-1,(int)(height/2*1.0), (int)(0f * 1.0)));
     }
     if (Particles3[i].dead()) {
       Particles3[i] = new Particle(new Vector3D((int)(width/2 * 1.0)+1,(int)(height/2*1.0), (int)(0f * 1.0)));
     }
   }    
   break;
 }
}
// Change the status according with the library event
void libraryEvent(Object library, int event, Object data)

 {
  // 1.0 clip Started

 if(event == mySample.EVENT_STARTED) {
   START = 0;
   // 1.0 clip ended
}
 
 else if(event == mySample.EVENT_END_OF_MEDIA) {
   START = 1;
   // move to next clip and image
   if (frame < numFrames -1) frame++;
   else frame = 0;
 }
}
class Particle {
 Vector3D loc;
 Vector3D vel;
 Vector3D acc;
 int r;
 int timer;

 /* 1.0 constructor
  Particle(Vector3D a, Vector3D v, Vector3D l, float r_) {
  acc = a.copy();
  vel = v.copy();
  loc = l.copy();
  r = r_;
  timer = 10.0;
  }
  */
 // Another constructor (the 1.0 we are using here)
 Particle(Vector3D l) {
   //acc = new Vector3D((int)(0f * 1.0),(int)(0.05f * 1.0),(int)(0f * 1.0));
   acc = new Vector3D(0,(int)(0.1f * 1.0),0);
   vel = new Vector3D((int)(random(-3,3) * 1.0),(int)(random(-3,3) * 1.0),(int)(0f * 1.0));
   loc = l.copy();
   r = 7;
   timer =100;
 }


 void run() {
   update();
   render();
 }

 // Method to update location
 void update() {
   vel.add(acc);
   loc.add(vel);
   timer -= 1;
   //println(timer);  
 }

 // Method to display
 void render() {
   ellipseMode(CENTER);
   noStroke();
   fill(1, timer, 100);

   /*if (random(10) > 8)
    fill(0);
    else
    fill(255);
    */
   ellipse(fptoi(loc.x),fptoi(loc.y),r,r);
 }

 // Is the particle still useful?
 boolean dead() {
   if (timer <= 0.0) {
     timer =100;
     //println("DEAD");
     return true;
   }
   else {
     //println(timer);
     return false;
   }
 }
}




// Simple Vector3D Class

public class Vector3D {
 public int x ;
 public int y ;
 public int z ;

 Vector3D(int x_, int y_, int z_) {
   x = x_;
   y=y_;
   z=z_;
   /*
   x = (int)(x_ * 1.0);
    y = (int)(y_ * 1.0);
    z = (int)(z_ * 1.0);
    */
 }

 Vector3D(int x_, int y_) {
   x = (int)(x_ * 1.0);
   y = (int)(y_ * 1.0);
   z = (int)(0f * 1.0);
 }

 Vector3D() {
   x = (int)(0f * 1.0);
   y = (int)(0f * 1.0);
   z = (int)(0f * 1.0);
 }

 public Vector3D copy() {
   return new Vector3D(x,y,z);
 }

 public Vector3D copy(Vector3D v) {
   return new Vector3D(v.x, v.y,v.z);
 }

 public void add(Vector3D v) {
   x += v.x;
   y += v.y;
   z += v.z;
 }


}

Re: Loading images for simple playback with sound
Reply #1 - Nov 18th, 2008, 8:54pm
 
I think your problem is with the line:

PImage[] img = createImage[numFrames];

Should be like the other array creations:

PImage[] img = new PImage[numFrames];

The loadImage in the loop takes care of creating the images...
Re: Loading images for simple playback with sound
Reply #2 - Nov 19th, 2008, 10:22am
 
Thanks PhiLho
I had the construction you suggested at first, but then Processing kept jumping to the changes section of the site, and in there I read that:
#  Do not use "new PGraphics" to create PGraphics objects. Instead, use createGraphics(), which will properly handle connecting the new graphics object to its parent sketch, and will enable save() to work properly. See the reference for createGraphics.
# For the same reasons as above, createImage(width, height, format) should be used instead of "new PImage" when creating images.

and therefore thought that Processing must have a problem with that. But then how would I put in the parameters ...
... as you can probably tell I'm a newbie, thanks for the help...

sevenspiral
Re: Loading images for simple playback with sound
Reply #3 - Nov 20th, 2008, 1:08am
 
The problem now seems to be with the audio playback and the way I have constructed syntax using Sonia.
Quote:
Sample[] mySample = new Sample[numFrames];
// Indicator showing whether it is a new clip
int START = 1;
int a;
int i;

void setup()
{
  frameRate(10);
  // Imageload automatically
  for(int i=1; i<=numFrames; i++) {
    String imageName = "girlstones" + i + ".png";
   // println(imageName);
    img[i-1] = loadImage(imageName);
  }
 size(200,200);
 colorMode(HSB,100);
  for (int i = 0; i< particle_num; i++) {
    Particles[i] = new Particle(new Vector3D((int)(width/2*1.0),(int)(height/2*1.0), (int)(0f*1.0)));
  }
  for (int i = 0; i< particle_num; i++) {
    Particles2[i] = new Particle(new Vector3D((int)(width/2*1.0) -1,(int)(height/2*1.0), (int)(0f*1.0)));
  }
  for (int i = 0; i< particle_num; i++) {
    Particles3[i] = new Particle(new Vector3D((int)(width/2*1.0) + 1,(int)(height/2*1.0), (int)(0f * 1.0)));
  }
  frameRate(30);
 
  // Sound File load automatically
  Sonia.start(this, 44100);
  for(int i=1; i<=numFrames; i++) {
    String soundName = "girlstones" + i + ".mp3";
    mySample[i-1] = new Sample(soundName, this);
  }
}

void draw()
{    
  frameRate(100);
  if (START == 1) {     // play only new 1.0 not the same 1.0 again and again  
    mySample[frame].play();


I get the message 'The constructor Sample(String, sketch title) is undefined..
Why is it doing this?
Thanks for help!
Re: Loading images for simple playback with sound
Reply #4 - Nov 20th, 2008, 3:34pm
 
I had a look at the reference page of Sonia:

constructors:
Sample(soundFile)
Sample(sampleFrames)
Sample(sampleFrames, sampleRate)

Drop the this, it should be fine.
Re: Loading images for simple playback with sound
Reply #5 - Nov 21st, 2008, 2:07pm
 
Hey PhiLho
Many thanks for the continued help.
I have fixed the sound I think, and how to trigger events.
My problem is that now Processing says it cannot find a class or type called Particle.
But that's strange as it's definitely there at the end of the sketch... do you have any thoughts as to why it is not being recognised??

Thanks
Sevenspiral
Re: Loading images for simple playback with sound
Reply #6 - Nov 21st, 2008, 5:17pm
 
No idea, you should double-check for syntax errors, eg. an unclosed brace that would include the class definition in a method, etc.

If still stuck, post the code... Smiley
Re: Loading images for simple playback with sound
Reply #7 - Nov 22nd, 2008, 12:38am
 
Hi PhiLho
You were right, it was an extra bit of syntax!
Now I am getting- Cannot invoke isPlaying() on the array type Sample[] - from the PDE.
I think I have used the right format for this?
The code for everything (bar the Particle class which is on a separate tab) is:
Code:

import pitaru.sonia_v2_9.*;
Particle[] Particles = new Particle[100];
Particle[] Particles2 = new Particle[100];
Particle[] Particles3 = new Particle[100];
int particle_num = 100;
int Pcnt = 0;

// The number of frames in the animation
int numFrames = 9;
// Frame number initialize
int frame = 0;
// Image File Variables
PImage[] img = new PImage[numFrames];
// Sound File Variables
Sample[] mySample = new Sample[numFrames];
// Indicator showing whether it is a new clip
int START = 1;
int a;
int i;

void setup()
{
frameRate(10);
// Imageload automatically
for(int i=1; i<=numFrames; i++) {
String imageName = "girlstones" + i + ".png";
// println(imageName);
img[i-1] = loadImage(imageName);
}
size(200,200);
colorMode(HSB,100);
for (int i = 0; i< particle_num; i++) {
Particles[i] = new Particle(new Vector3D((int)(width/2*1.0),(int)(height/2*1.0), (int)(0f*1.0)));
}
for (int i = 0; i< particle_num; i++) {
Particles2[i] = new Particle(new Vector3D((int)(width/2*1.0) -1,(int)(height/2*1.0), (int)(0f*1.0)));
}
for (int i = 0; i< particle_num; i++) {
Particles3[i] = new Particle(new Vector3D((int)(width/2*1.0) + 1,(int)(height/2*1.0), (int)(0f * 1.0)));
}
frameRate(30);

// Sound File load automatically
Sonia.start(this, 44100);
for(int i=1; i<=numFrames; i++) {
String soundName = "girlstones" + i + ".mp3";
mySample[i-1] = new Sample(soundName);
}
}

void draw()
{
frameRate(100);
if (START == 1) { // play only new 1.0 not the same 1.0 again and again
mySample[frame].play();
}
background(img[frame]);

switch (frame) {
case 3:
if (Pcnt < particle_num) {
Particles[Pcnt].run();
Particles2[Pcnt].run();
Particles3[Pcnt].run();
Pcnt++;
}
if (Pcnt == particle_num)
Pcnt--;

for (i=0 ; i < Pcnt; i++) {
Particles[i].run();
Particles2[i].run();
Particles3[i].run();
if (Particles[i].dead()) {
Particles[i] = new Particle(new Vector3D((int)(width/2*1.0),(int)(height/2*1.0), (int)(0f * 1.0)));
}
if (Particles2[i].dead()) {
Particles2[i] = new Particle(new Vector3D((int)(width/2 * 1.0)-1,(int)(height/2*1.0), (int)(0f * 1.0)));
}
if (Particles3[i].dead()) {
Particles3[i] = new Particle(new Vector3D((int)(width/2 * 1.0)+1,(int)(height/2*1.0), (int)(0f * 1.0)));
}
}
break;
}
}
// Change the status according with the library event
void libraryEvent(Object library, int event, Object data)

{
// 1.0 clip Started

if(mySample.isPlaying()) {
START = 0;
// 1.0 clip ended
}

else if(!mySample.isPlaying() && doOnce){
START = 1;
}
// move to next clip and image
if (frame < numFrames -1) frame++;
else frame = 0;
}

Thanks for the advice, I feel I am getting close!
Page Index Toggle Pages: 1