Playing random sounds from folder as an Urn (no repeat untill all the files are done)

#1

Hi there,
I’m pretty new to processing.
For an audio installation, I need a processing sketch to play random sounds from the data folder every 5 seconds, indefinitely.
For this example I have 10 mp3 files, but I’ll need to use it with at least 100 sounds in the future.
The files are numbered from 0 to 9.

My problem is that I need the program to play all the files and not repeating any of them untill it played all of the 10 files.
And more important, when it has played the 10 files in a unique random order, I need it to do it again with an other unique order of random number, and so on.
In Max/MSP this randomness is called an ‘urn’, but I can’t find similar things with processing.

Maybe I should generate a list of 10 numbers from 0 to 9 in a random order, then play my files following that order, and repeating that every 10 files. But I have no idea how to do it.

I hope it make sense.

Thanks for your help !

Here is my code.


import ddf.minim.*;
AudioPlayer player;
Minim minim;
int rand;
long lastTime = 0;
PFont f;

void setup() {
  size(200,200);
  minim = new Minim(this);  
}
 
 
void draw() {

if ( millis() - lastTime > 5000 ) {
   lastTime = millis();
    
    rand = int(random(0, 9));
    player = minim.loadFile(nf(rand) + ".mp3");
    player.play();
    print(rand);
 
  }
}
1 Like

#2

one way could be to shuffle a IntList

import ddf.minim.*;
AudioPlayer player;
Minim minim;

int rand;
long lastTime = 0;
PFont f;

int max = 10;
IntList songnums = new IntList();
int songidx = 0;

void setup() {
  size(200, 200);
  minim = new Minim(this); 
  for ( int i = 0; i<max; i++) songnums.append(i);
  songnums.shuffle();
  println(songnums);
}

void draw() {
  if ( millis() - lastTime > 5000 ) {
    lastTime = millis();
    rand = songnums.get(songidx);
    if ( songidx++ > max ) songidx = 0;
    String songfile = nf(rand) + ".mp3";
    println("load: "+songfile);
    player = minim.loadFile(songfile);
    player.play();
  }
}

void keyPressed() {
  if ( key == 'r' ) {       // reshuffle
    songnums.shuffle();
    println(songnums);
  }
}

0 Likes

#3

thanks kll !
The IntList works great ! But it stopped a the end of the first list.
But how could I program a ‘reshuffle’, for example, every time it has played a number a files thats equal to int max -1 ?

Thanks a lot !

0 Likes

#4

sorry my fault, i not test as i not have the song files

    rand = songnums.get(songidx);
    songidx++;
    if ( songidx >= max ) songidx = 0;

should not have a
ArrayIndexOutOfBoundsException: Array index out of range: x


for the reshuffle i made a key [r]
but you can do a

songnums.shuffle();

whenever you like.

an counter depending reshuffle would be

int count,countmax = 12;
//...
    if ( count >= countmax ) { 
      count = 0;
      songidx = 0;
      songnums.shuffle(); 
      println(songnums);
    }

0 Likes