Minim setGain/probably my fault

edited January 2017 in Library Questions

Hello guys!! I'm new to this forum :) !! And I'm new to processing in general.....and in this code I have a problem with minim "setGain()". I'm trying to triggering a piano note every time the play bar is hitting a rectangle with "setGain(0) and play(0)" (the length of rectangles represent the duration of the piano note duration) and muting/stopping the sound when the play bar is hitting the end of a rectangle with "setGain(-200)" (I've tried also with "setVolume()").

The problem is: there are two rectangles for now and both duration always refer to the length of the first rectangle(when "setGain(-200)") , the second sound (combined with the second rectangle) is not happening at all, or, it happing with the length of the first rectangle :((

Is there anybody who can help me 8-| ? Where am I doing wrong :-? ?

p.s. the rectangle are created randomly so you have to click every time on "play" to make a different combination between the two rectangles.

import ddf.minim.*;
Minim minim;
AudioPlayer piano_C3;
AudioPlayer piano_Cdiesis3;
AudioPlayer piano_D3;
AudioPlayer piano_Ddiesis3;
AudioPlayer piano_E3;
AudioPlayer piano_F3;
AudioPlayer piano_Fdiesis3;
AudioPlayer piano_G3;
AudioPlayer piano_Gdiesis3;
AudioPlayer piano_A3;
AudioPlayer piano_Adiesis3;
AudioPlayer piano_B3;
AudioPlayer piano_C4;

float menusp;
float noteX1;
float noteX2;
float noteY1;
float noteY2;
int noteAlpha1;
int noteAlpha2;
int sceltoX1;
int sceltoX2;
int sceltoY1;
int sceltoY2;
int sceltoAlpha1;
int sceltoAlpha2;
float movelineX;
float Ktempo;
float PX1I;
float PX2I;
float fixedNoteY;
float lenght1;
float lenght2;
float lenghtTot;



void setup () {

  size (450, 750);
  rectMode (CENTER);
  minim=new Minim (this);
  piano_C3=minim.loadFile ("C3.mp3");  
  piano_Cdiesis3=minim.loadFile ("Cdiesis3.mp3");
  piano_D3=minim.loadFile ("D3.mp3");
  piano_Ddiesis3=minim.loadFile ("Ddiesis3.mp3");
  piano_E3=minim.loadFile ("E3.mp3");
  piano_F3=minim.loadFile ("F3.mp3");
  piano_Fdiesis3=minim.loadFile ("Fdiesis3.mp3");
  piano_G3=minim.loadFile ("G3.mp3");
  piano_Gdiesis3=minim.loadFile ("Gdiesis3.mp3");
  piano_A3=minim.loadFile ("A3.mp3");
  piano_Adiesis3=minim.loadFile ("Adiesis3.mp3");
  piano_B3=minim.loadFile ("B3.mp3");
  piano_C4=minim.loadFile ("C4.mp3");

  menusp=height-(height/4);  
  fixedNoteY=menusp/12; 
  float[] dataY1={(height-fixedNoteY), (height-(fixedNoteY*2)), 
    (height-(fixedNoteY*3)), (height-(fixedNoteY*4)), 
    (height-(fixedNoteY*5)), (height-(fixedNoteY*6)), 
    (height-(fixedNoteY*7)), (height-(fixedNoteY*8)), 
    (height-(fixedNoteY*9)), (height-(fixedNoteY*10)), 
    (height-(fixedNoteY*11)), (height-(fixedNoteY*12))}; 
  sceltoY1=int(random(dataY1.length));
  noteY1=(dataY1 [sceltoY1]);



  float[] dataY2={(height-fixedNoteY), (height-(fixedNoteY*2)), 
    (height-(fixedNoteY*3)), (height-(fixedNoteY*4)), 
    (height-(fixedNoteY*5)), (height-(fixedNoteY*6)), 
    (height-(fixedNoteY*7)), (height-(fixedNoteY*8)), 
    (height-(fixedNoteY*9)), (height-(fixedNoteY*10)), 
    (height-(fixedNoteY*11)), (height-(fixedNoteY*12))}; 
  sceltoY2=int(random(dataY2.length));
  noteY2=(dataY2 [sceltoY2]);



  int[] dataX1={4, 8, 16};
  sceltoX1=int(random(dataX1.length));
  noteX1=(dataX1 [sceltoX1]);

  int[] dataX2={4, 8, 16}; 
  sceltoX2=int(random(dataX2.length));
  noteX2=(dataX2 [sceltoX2]);


  int[] alpha1 ={255, 0, 255, 0};
  sceltoAlpha1=int(random(alpha1.length));                 
  noteAlpha1=(alpha1[sceltoAlpha1]);

  int[] alpha2 ={255, 0, 255, 0};
  sceltoAlpha2=int(random(alpha2.length));                 
  noteAlpha2=(alpha2[sceltoAlpha2]);

  lenght1=width/noteX1;
  PX1I=0;
  lenght2=(width/noteX2);
  PX2I=(width/16);
}


void draw () {


  background (255);

  //triggering note 1

  if (movelineX==PX1I && noteY1==(height-fixedNoteY) && noteAlpha1!=0 ) {     //C3 
    piano_C3.setGain(0);
    piano_C3.play(0);
  }  
  if (movelineX>lenght1) {                                                         
    piano_C3.setGain(-200);
  } else { 
    piano_C3.setGain(0);
  }
  if (movelineX==PX1I && noteY1==(height-(2*fixedNoteY)) && noteAlpha1!=0 ) {  //Cdiesis3  
    piano_Cdiesis3.setGain(0);
    piano_Cdiesis3.play(0);
  }  
  if (movelineX>lenght1) {                                                         
    piano_Cdiesis3.setGain(-200);
  } else { 
    piano_Cdiesis3.setGain(0);
  }
  if (movelineX==PX1I && noteY1==(height-(3*fixedNoteY)) && noteAlpha1!=0 ) {  //D3  
    piano_D3.setGain(0);
    piano_D3.play(0);
  }  
  if (movelineX>lenght1) {                                                         
    piano_D3.setGain(-200);
  } else { 
    piano_D3.setGain(0);
  }
  if (movelineX==PX1I && noteY1==(height-(4*fixedNoteY)) && noteAlpha1!=0 ) {  //Ddiesis3    
    piano_Ddiesis3.setGain(0);
    piano_Ddiesis3.play(0);
  }
  if (movelineX>lenght1) {                                                         
    piano_Ddiesis3.setGain(-200);
  } else { 
    piano_Ddiesis3.setGain(0);
  }
  if (movelineX==PX1I && noteY1==(height-(5*fixedNoteY)) && noteAlpha1!=0 ) {  //E3 
    piano_E3.setGain(0);
    piano_E3.play(0);
  }
  if (movelineX>lenght1) {                                                         
    piano_E3.setGain(-200);
  } else { 
    piano_E3.setGain(0);
  }
  if (movelineX==PX1I && noteY1==(height-(6*fixedNoteY)) && noteAlpha1!=0 ) {  //F3  
    piano_F3.setGain(0);
    piano_F3.play(0);
  }  
  if (movelineX>lenght1) {                                                         
    piano_F3.setGain(-200);
  } else { 
    piano_F3.setGain(0);
  }
  if (movelineX==PX1I && noteY1==(height-(7*fixedNoteY)) && noteAlpha1!=0 ) {  //Fdiesis3 
    piano_Fdiesis3.setGain(0);
    piano_Fdiesis3.play(0);
  }  
  if (movelineX>lenght1) {                                                         
    piano_Fdiesis3.setGain(-200);
  } else { 
    piano_Fdiesis3.setGain(0);
  }
  if (movelineX==PX1I && noteY1==(height-(8*fixedNoteY)) && noteAlpha1!=0 ) {  //G3  
    piano_G3.setGain(0);
    piano_G3.play(0);
  }  
  if (movelineX>lenght1) {                                                         
    piano_G3.setGain(-200);
  } else { 
    piano_G3.setGain(0);
  }
  if (movelineX==PX1I && noteY1==(height-(9*fixedNoteY)) && noteAlpha1!=0 ) {  //Gdiesis3
    piano_Gdiesis3.setGain(0);
    piano_Gdiesis3.play(0);
  }  
  if (movelineX>lenght1) {                                                         
    piano_Gdiesis3.setGain(-200);
  } else { 
    piano_Gdiesis3.setGain(0);
  }
  if (movelineX==PX1I && noteY1==(height-(10*fixedNoteY)) && noteAlpha1!=0 ) {  //A3    
    piano_A3.setGain(0);
    piano_A3.play(0);
  }  
  if (movelineX>lenght1) {                                                         
    piano_A3.setGain(-200);
  } else { 
    piano_A3.setGain(0);
  }
  if (movelineX==PX1I && noteY1==(height-(11*fixedNoteY)) && noteAlpha1!=0 ) {  //Adiesis3  
    piano_Adiesis3.setGain(0);
    piano_Adiesis3.play(0);
  }  
  if (movelineX>lenght1) {                                                         
    piano_Adiesis3.setGain(-200);
  } else { 
    piano_Adiesis3.setGain(0);
  }
  if (movelineX==PX1I && noteY1==(height-(12*fixedNoteY)) && noteAlpha1!=0 ) {  //B3 
    piano_B3.setGain(0);
    piano_B3.play(0);
  }  
  if (movelineX>lenght1) {                                                         
    piano_B3.setGain(-200);
  } else { 
    piano_B3.setGain(0);
  }
  if (movelineX==PX1I && noteY1==(height-(13*fixedNoteY)) && noteAlpha1!=0 ) {  //C4 
    piano_C4.setGain(0);
    piano_C4.play(0);
  }  
  if (movelineX>lenght1) {                                                         
    piano_C4.setGain(-200);
  } else { 
    piano_C4.setGain(0);
  }


  //triggering note2

  if (movelineX==PX2I && noteY2==(height-fixedNoteY) && noteAlpha2!=0 ) {   //C3 
    piano_C3.setGain(0);
    piano_C3.play(0);
  }  
  if (movelineX>(lenght1+lenght2)) {                                                         
    piano_C3.setGain(-100);
  } 
  if (movelineX==PX2I && noteY2==(height-(2*fixedNoteY)) && noteAlpha2!=0 ) {   //Cdiesis3
    piano_Cdiesis3.setGain(0);
    piano_Cdiesis3.play(0);
  }  
  if (movelineX>(lenght1+lenght2)) {                                                         
    piano_Cdiesis3.setGain(-100);
  } 
  if (movelineX==PX2I && noteY2==(height-(3*fixedNoteY)) && noteAlpha2!=0 ) {   //D3 
    piano_D3.setGain(0);
    piano_D3.play(0);
  }  
  if (movelineX>(lenght1+lenght2)) {                                                         
    piano_D3.setGain(-100);
  }
  if (movelineX==PX2I && noteY2==(height-(4*fixedNoteY)) && noteAlpha2!=0 ) {   //Ddiesis3  
    piano_Ddiesis3.setGain(0);
    piano_Ddiesis3.play(0);
  }  
  if (movelineX>(lenght1+lenght2)) {                                                         
    piano_Ddiesis3.setGain(-100);
  }
  if (movelineX==PX2I && noteY2==(height-(5*fixedNoteY)) && noteAlpha2!=0 ) {   //E3
    piano_E3.setGain(0);
    piano_E3.play(0);
  }  
  if (movelineX>(lenght1+lenght2)) {                                                         
    piano_E3.setGain(-100);
  }
  if (movelineX==PX2I && noteY2==(height-(6*fixedNoteY)) && noteAlpha2!=0 ) {   //F3  
    piano_F3.setGain(0);
    piano_F3.play(0);
  }  
  if (movelineX>(lenght1+lenght2)) {                                                         
    piano_F3.setGain(-100);
  }
  if (movelineX==PX2I && noteY2==(height-(7*fixedNoteY)) && noteAlpha2!=0 ) {   //Fdiesis3
    piano_Fdiesis3.setGain(0);
    piano_Fdiesis3.play(0);
  }  
  if (movelineX>(lenght1+lenght2)) {                                                         
    piano_Fdiesis3.setGain(-100);
  }
  if (movelineX==PX2I && noteY2==(height-(8*fixedNoteY)) && noteAlpha2!=0 ) {   //G3 
    piano_G3.setGain(0);
    piano_G3.play(0);
  }  
  if (movelineX>(lenght1+lenght2)) {                                                         
    piano_G3.setGain(-100);
  }
  if (movelineX==PX2I && noteY2==(height-(9*fixedNoteY)) && noteAlpha2!=0 ) {   //Gdiesis3  
    piano_Gdiesis3.setGain(0);
    piano_Gdiesis3.play(0);
  }  
  if (movelineX>(lenght1+lenght2)) {                                                         
    piano_Gdiesis3.setGain(-100);
  }
  if (movelineX==PX2I && noteY2==(height-(10*fixedNoteY)) && noteAlpha2!=0 ) {   //A3   
    piano_A3.setGain(0);
    piano_A3.play(0);
  }  
  if (movelineX>(lenght1+lenght2)) {                                                         
    piano_A3.setGain(-100);
  }
  if (movelineX==PX2I && noteY2==(height-(11*fixedNoteY)) && noteAlpha2!=0 ) {   //Adiesis3
    piano_Adiesis3.setGain(0);
    piano_Adiesis3.play(0);
  }  
  if (movelineX>(lenght1+lenght2)) {                                                         
    piano_Adiesis3.setGain(-100);
  }
  if (movelineX==PX2I && noteY2==(height-(12*fixedNoteY)) && noteAlpha2!=0 ) {   //B3
    piano_B3.setGain(0);
    piano_B3.play(0);
  }  
  if (movelineX>(lenght1+lenght2)) {                                                         
    piano_B3.setGain(-100);
  }
  if (movelineX==PX2I && noteY2==(height-(13*fixedNoteY)) && noteAlpha2!=0 ) {   //C4   
    piano_C4.setGain(0);
    piano_C4.play(0);
  }  
  if (movelineX>(lenght1+lenght2)) {                                                         
    piano_C4.setGain(-100);
  }


  // orizontal lines
  stroke (1); 
  strokeWeight (1);
  for (float ia = 0; ia < 750; ia = ia+(menusp/12)) {
    line (0, ia, 450, ia);
  }


  // vertical lines
  for (float ib = 0; ib < 450; ib = ib+(width/16)) {
    line (ib, 0, ib, 750);
  }




  //note 1
  fill (24, 0, 191, noteAlpha1);
  if (noteAlpha1==0) {
    stroke (0, 0, 0, 0);
  } else {
    stroke (1);
  }

  rectMode (CORNER); // modalità corner rect (x1,y1,x2,y2)
  rect (PX1I, noteY1, lenght1, fixedNoteY);   
  rectMode (CENTER); // ritorna la modalità centro

  //note 2
  fill (255, 0, 1, noteAlpha2);
  if (noteAlpha2==0) {
    stroke (0, 0, 0, 0);
  } else {
    stroke (1);
  }

  rectMode (CORNER);
  rect (PX2I, noteY2, lenght2, fixedNoteY);   
  rectMode (CENTER); // ritorna la modalità centro





  // play bar
  strokeWeight (5); 
  stroke (#FF05DE);
  Ktempo=1;
  movelineX=movelineX+Ktempo;
  if (movelineX>=width) {
    movelineX=0;
    piano_C3.setGain(0);
    piano_Cdiesis3.setGain(0);
    piano_D3.setGain(0);
    piano_Ddiesis3.setGain(0);
    piano_E3.setGain(0);
    piano_F3.setGain(0);
    piano_Fdiesis3.setGain(0);
    piano_G3.setGain(0);
    piano_Gdiesis3.setGain(0);
    piano_A3.setGain(0);
    piano_Adiesis3.setGain(0);
    piano_B3.setGain(0);
    piano_C4.setGain(0);
  }

  line(0+movelineX, 0, 0+movelineX, height);
  strokeWeight (1);


  stroke (1); 
  strokeWeight (1);
  noStroke ();
  fill (0, 220, 141);
  rect (width/2, (height/8), width, ((height/4)+2));
}

Answers

Sign In or Register to comment.