Shuffle Song Code

I’m trying to practice doing different things using minim. I wanted to play around with shuffling different songs using minim and I found this example. I was having trouble posting a comment to the user that posted this so I’m asking it here (code credit: kfrajer). Can anyone explain this code? I understand most of it such as importing minim, having a stringlist so you can have multiply songs, and things like player: play, stop, and pause. However I’m having a hard time understanding what the rest of the code means, I have looked up examples and specific words but I was having trouble understanding how those things are used in this code. I am trying to understand each line of code and what it does so any explanation will be of great help. Thanks!!

import ddf.minim.*;
AudioPlayer player;
Minim minim;
 
boolean playeurInit = false;// que leplayer n'est pas lancé
boolean stop = true;
 
StringList tableau;
int current=0;
 
void setup() {
  size(100, 100);
  minim = new Minim(this);
 
  //ADD sound files to your list
  tableau=new StringList();
  tableau.append("SONG1.mp3");
  tableau.append("SONG2.mp3");
   tableau.append("SONG3.mp3");
 
  shufflePlayList();
  String son = tableau.get(current++);
  println(son);
  player = minim.loadFile(son);
  player.pause();
 
  background(0);
}
 
void draw() {
 
  if (!stop) {
 
    if (player.isPlaying() == false) {
 
      //CHECK we haven't exhausted the current shuffled(random) play list
      //If we have played all the songs, then re-shuffle the list
      if (current==tableau.size()-1) {
        shufflePlayList();
      }
 
      String son = tableau.get(current++);
      player = minim.loadFile(son);
      player.play();      
      playeurInit = true;
    }
  }
}
 
void mouseReleased() {
  if (stop == true) {
    stop = false;
  } else {
    stop = true;
    if (player.isPlaying() == true ) {
      player.pause();
    }
  }
}
 
void stop() {
  player.close();
  minim.stop();
  super.stop();
}
 
void shufflePlayList() {
  current=0;
  tableau.shuffle();
}

why you not link to
where the code comes from?


and yes, that is older forum ( like READ ONLY )

very good: you did write

(code credit: kfrajer)

but if you would like to contact him ( here at this forum ) might use like

@user

so a msg. ( @ Notification ) pops up in his user panel.

about:

understanding what the rest of the code means

you might quote one command,

tableau.shuffle();

link to the reference you checked on
https://processing.org/reference/StringList_shuffle_.html

and ask further questions what you not understand inthere…

_but i think a general _
" teach me about each line of that code "
is just TOO much to ask for here.

I didn’t know you could message users I thought you can only post comments on there posts which I had trouble doing because of the limit of comments per day. I’m still learning how everything works on here thanks for letting me know!

Hello,

I am working with your code to trigger some sounds using motion sensors.
However, in the last sound an error about array bounds comes.
ArrayIndexOutOfBoundsException: Array index out of range: 5

Someone could help me?
here´s the code:

import javafx.scene.layout.Background;

import processing.serial.*;
import processing.sound.*;
import cc.arduino.*;
import ddf.minim.*;

Minim minim;

AudioPlayer player;
boolean playeurInit = false;
boolean stop = true;

StringList viagens;
int current=0;

Arduino arduino;

//definir as portas de cada sensor
int sensorPin1 = 8;
int sensorPin2 = 9;



void setup() {
  size(1200, 700);
  background(random(255));
  frameRate(10);

  // Modify this line, by changing the "0" to the index of the serial
  // port corresponding to your Arduino board (as it appears in the list
  // printed by the line above).
  arduino = new Arduino(this, Arduino.list()[2], 57600);

  // Alternatively, use the name of the serial port corresponding to your
  // Arduino (in double-quotes), as in the following line.
  //arduino = new Arduino(this, "/dev/tty.usbmodem621", 57600);

  // Set the Arduino digital pins as inputs.
  //for (int i = 0; i <= 13; i++)

  arduino.pinMode(sensorPin2, Arduino.INPUT);
  arduino.pinMode(sensorPin1, Arduino.INPUT);

  // Create an array of empty soundfiles
  minim = new Minim(this);
  
  //ADD sound files to your list
  viagens=new StringList();
  viagens.append("1.mp3");
  viagens.append("2.mp3");
  viagens.append("3.mp3");
  viagens.append("4.mp3");
  viagens.append("5.mp3");
 
  
 
shufflePlayList();

  String son = viagens.get(current++);
  println(son);
  player = minim.loadFile(son);
  player.pause();
background(0);

}
void draw() {
  //read a digital value from the sensor pin
  int val = arduino.digitalRead(sensorPin2);
  int val1 = arduino.digitalRead(sensorPin1);


  //change the background accordingly
  
  if (val == Arduino.HIGH) {
    // se não está a tocar, começa a tocar
    if (!player.isPlaying())
    {
       
        String son = viagens.get(current++);
      player = minim.loadFile(son);
      player.play();      
      playeurInit = true;
    } 
    // caso contrário faz-se rewind
 
    background(100,0,0);
  } 
  // se não houver sinal o background fica a negro
  else background(0);
  
  if  (val1 == Arduino.HIGH) {
    player.pause();
    
    background(51);
  } else background(0);
}
void shufflePlayList() {
  current=0;
  viagens.shuffle();
}