Hello, i am writing this processing script with the use of minim, the concept to have a general sound that controls the scale of an ellipse with amplitude, and then when a mouse control rectangle gets inside the ellipse, an other sound will play. i create the shapes with a class object, all good. The problem is that the sound of the objects sometimes they play sometimes not, without any error.
Thanks in advance
Here is the code:
import ddf.minim.analysis.*;
import ddf.minim.*;
Minim minim;
AudioPlayer player;
AudioPlayer[] mplayer;
FFT fft;
int screenScaleX = 10;
int screenScaleY = 10;
// Constants
// KEEP OUT!
static final int LEFT_SCREEN_WIDTH = 39;
static final int LEFT_SCREEN_HEIGHT = 9;
static final int RIGHT_SCREEN_WIDTH = 38;
static final int RIGHT_SCREEN_HEIGHT = 13;
static final int RIGHT_SCREEN_POSITION = LEFT_SCREEN_WIDTH;
int ShapeNum= 3;
int lX, lY;
String[] music = {"lava.mp3", "glacier.mp3", "ice_crack.mp3", "waterfall.mp3"};
ArrayList<ShapeMusic> ShapeAr;
float x_pos, y_pos;
void settings() {
size((LEFT_SCREEN_WIDTH + RIGHT_SCREEN_WIDTH) * screenScaleX, RIGHT_SCREEN_HEIGHT * screenScaleY);
}
void setup()
{
colorMode(HSB);
minim = new Minim(this);
player = minim.loadFile("ocean.mp3", 1024);
fft = new FFT( player.bufferSize(), 32 );
ShapeAr = new ArrayList<ShapeMusic>();
player.setGain(-40);
mplayer=new AudioPlayer[music.length];
for (int i=0; i<music.length; i++) {
mplayer[i] = minim.loadFile(music[i]);
println("loading : " + music[i]);
}
for (int i=0; i< ShapeNum; i++) {
int rand = int(random(music.length));
AudioPlayer randM=mplayer[rand];
println("Object Song: "+music[rand]);
x_pos=random(LEFT_SCREEN_WIDTH*screenScaleX,width);
y_pos=random(height);
ShapeAr.add(new ShapeMusic(x_pos, y_pos, 0, randM));
}
}
/////PUT ALL INSIDE CLASS< IT CREATES RANDOM POSITIONS EVERY FRAME FOR EVERY OBJECT
///I DONT THINK THAT ENYTHINK HAS TO BE OUTSIDE THE CLASS, HAVE ONE OBJECT
void draw()
{
background(0);
stroke(255);
fill(0);
rect(LEFT_SCREEN_WIDTH*screenScaleX,0,width,height);
noStroke();
fft.forward( player.mix );
for (int i = ShapeAr.size()-1; i >= 0; i--) {
ShapeMusic shapeNew = ShapeAr.get(i);
//shapeNew((player.right.level()*500), GivenFft, GivenFft);
shapeNew.r =player.right.level()*100*screenScaleX;
shapeNew.display();
shapeNew.playSound(mouseX, mouseY);
player.play();
fill(200);
rect(mouseX, mouseY, 4, 4);
}
}
class ShapeMusic {
AudioPlayer soundInside;
Minim minimInside;
//Circle shape vars Amplitude
float x_pos_sh, y_pos_sh, r;
float GivenFft;
float MappedGivenFft;
float angle;
float x_pos, y_pos;
float x_vert, y_vert;
//Rect or Line shape var FFT
float FftShapesNumber, Shape2_x_2, Shape2_y_2;
AudioPlayer ObjectSound;
AudioPlayer mySound;
ShapeMusic(float x_pos_input, float y_pos_input, float ShapeSize, AudioPlayer tempSound ) {
r = ShapeSize;
x_pos_sh= x_pos_input;
y_pos_sh= y_pos_input;
//Shape2_x_2=inputX;
//Shape2_y_2=inputY;
//FftShapesNumber=shapeNumbers;
ObjectSound=tempSound;
}
void display() {
for (int y = 0; y < fft.specSize(); y++)
{
fill(0, 0, 255);
//noStroke();
// draw the line for frequency band i, scaling it up a bit so we can see it
GivenFft=fft.getBand(y)*10 ;
angle = map(y, 0, fft.specSize(), 0, 360);
MappedGivenFft = map(GivenFft, 0, 100, 0, 500);
x_vert= MappedGivenFft*cos(angle);
y_vert= MappedGivenFft*sin(angle);
stroke(y, 255, 255);
//INTERESTING EFFECT
point(10*x_vert+x_pos_sh, 10*y_vert+y_pos_sh);
//TESTS
//point(20*x_vert+x_pos_sh,20*y_vert+y_pos_sh);
//point(10*sin(angle*y)+x_pos_sh,10*cos(angle*y)+y_pos_sh);
//line(10*sin(angle*y)+x_pos_sh,10*cos(angle*y)+y_pos_sh,20*sin(angle*y)+x_pos_sh,20*cos(angle*y)+y_pos_sh);
//line(10*sin(angle*y)+x_pos_sh,10*cos(angle*y)+y_pos_sh, 10-MappedGivenFft*sin(angle*y)+x_pos_sh,10-MappedGivenFft*cos(angle*y)+y_pos_sh);
//float mapp2= map(GivenFft,0,256,height,0);
//rect(w*y,mapp2,w-5,height-y);
}
fill(0, 255, 255);
noStroke();
ellipse(x_pos_sh, y_pos_sh, r, r);
}
void playSound(int lx, int ly) {
if ((lx>x_pos_sh-r)&&(lx<x_pos_sh+r)&&(ly>x_pos_sh-r)&&(ly<ly+r)) {
ObjectSound.play();
println("Sound Played");
}else{
ObjectSound.pause();
//println("Sound Stoped");
}
}
}