Random mp3 files

I’m trying to make a random generator of mp3 files on Processing but I don’t know how to do it.
I’ve tried to create a class (and before I tried it with an Array) with my audio files but I can’t get it to run.

import processing.sound.*;
processing_merci.Merci.files;
SoundFile file;
ArrayList sound_merci = new ArrayList();
void setup() {
size (800, 800);
background(255);
file.play();
}
void draw() {
}

and here’s my class:

import processing.sound.*;
class Merci {
// définir les attributs
SoundFile files;
ArrayList files = new ArrayList();
Merci (){
files = new SoundFile(this, “merci_Victor.mp3”);
file = new SoundFile(this, “merci_Ayten.mp3”);
file = new SoundFile(this, “merci_JulieT.mp3”);
file = new SoundFile(this, “merci_Lenard.mp3”);
file = new SoundFile(this, “merci_Lisa.mp3”);
file = new SoundFile(this, “merci_Noé.mp3”);
file = new SoundFile(this, “merci_Pierre.mp3”);
file = new SoundFile(this, “merci_Rita.mp3”);
file = new SoundFile(this, “merci_Prune.mp3”);
file = new SoundFile(this, “merci_Sarah.mp3”);
file = new SoundFile(this, “merci_Thomas.mp3”);
file = new SoundFile(this, “merci_Vlad.mp3”);
}
}

:grimacing:

so you want to play a random mp3 file from the collection?

1 Like

yes exactly ! I would like a random file from my collection to be played with each mouse click. :slight_smile:

I’ve done it before but I’m not sure if it will work to you. Give me a minute, I’ll send you the code

1 Like

so sweet thank you very much !!

int ALL = 9; //audio library length
int Psong = int(random(ALL)); //playing song
float SInfo[][] = new float[ALL][1]; //[X][] - song ID, [][X] - 0: allowed (-1 banned, 1 allowed)

import ddf.minim.*;
Minim minim;

AudioPlayer songs[] = new AudioPlayer[ALL];

void setup() {
  minim = new Minim(this);
  for(int i = 0; i < ALL; i++) {
    songs[i] = minim.loadFile("SONGS/"+i+".mp3");
    SInfo[i][0] = 1;
  }
}

void draw() {
  if(!songs[Psong].isPlaying()) {
    Psong++;
    Psong = Psong % ALL;
    songs[Psong].rewind();
    songs[Psong].play();
  }
}

//void nextSong() {
//  Psong++;
//  for(int i = 0; i < ALL*2 && SInfo[i][0] != 1; i++) {
//    Psong++;
//    Psong = Psong % ALL;
//  }
//}

the songs are name 0.mp3, 1.mp3 … 9.mp3

edit: this is the first version. I’ll try to find a better one but don’t get your hopes up.

edit2: I’ve done this when I didn’t know much ArrayLists. It would be easier to do it with them probably but I’m too lazy to try

I’m going to watch and try it all thanks a lot

I found the better version. The banning of songs actually works. There is also a “hidden song” which only plays if the MODE == 1. Have fun!

int ALL = 9; //audio library length
float SInfo[][] = new float[ALL][1]; //[X][] - song ID, [][X] - 0: allowed (-1 banned, 1 allowed)
int MODE = 1;
int Psong = (MODE == 0)? (int)random(ALL) : 9; //playing song

import ddf.minim.*;
Minim minim;
AudioPlayer songs[] = new AudioPlayer[ALL+1];

void setup() {
  minim = new Minim(this);
  for (int i = 0; i < ALL; i++) {
    songs[i] = minim.loadFile("SONGS/"+i+".mp3");
    SInfo[i][0] = 1;
  }
  songs[9] = minim.loadFile("SONGS/BL.mp3");
}

void draw() {
  if (!songs[Psong].isPlaying()) {
    Psong++;
    Psong = Psong % ALL;
    if (MODE == 1) {
      Psong = ALL;
    }
    songs[Psong].rewind();
    songs[Psong].play();
  }
}

If you have any way for me to send you the whole file (with songs) then tell me if you want. I can’t post it here since it is .zip well yeah

Thank you I’m trying to play it but I arrive to an error NullPointerException :zipper_mouth_face:

it’s because you dont have the files

Oh that would be really nice
I could send you by mail ? or another plateform?

image

what do you suggest? I have a few throw-away gmails so we can do that

What is throw-away gmails ? Is ather gmail boy to stay anonymous ?

but yes that’s a good idea I thing :slight_smile:

yes. I use it to sign up for some websites that spam notifications so I don’t have the issue on my personal mail. It’s also good for anonymity. EDIT: you should have receved my DM

What I would do is to create a CSV file which contains the names and locations of my songs. Then read the CSV using table() methods into an array. Then randomly access the array index. Load and play. This allows you to change the list of songs without updating code. I’ve had trouble using the sound() library which is easy to use, but minim works well. I’ll write the code and send it to you if you like

2 Likes