stringprocessingauto-updateminim

Create a self updating string based on files in a folder in Processing


Alright, so I was messing around with a simple fft visualization in Processing and thought it would be fun to have more than just one song playing every time. In the end I added 3 songs manually and on mouse click change between the songs randomly using a predefined string. I wanted to add the rest of my computers music, but every time I would want to add a new song to my sketch I would have to copy and paste it's name into the string in my sketch. Seems like a lot of unnecessary work

Is there a way to have processing scan a folder, recognize how many files are inside, and copy all of the file names into the string? I found a library called sDrop for processing 1.1 which lets you drag and drop files into the sketch directly. However, that doesn't seem to exist anymore in version 2+ of Processing.

Here is the simple version of my current working code to play the music:

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

AudioPlayer player;
Minim minim;

String [] songs = {
  "song1.mp3",
  "song2.mp3",
  "song3.mp3",
};

int index;


void setup() {
  size(100, 100, P3D);
  index = int(random(songs.length));  

  minim = new Minim(this);
  player = minim.loadFile(songs[index]);
  player.play();
}

void draw() {
}


void mouseClicked() {
  index = int(random(songs.length)); 

  player.pause();
  player = minim.loadFile(songs[index]);
  player.play();
}

If anyone has suggestions or could guide me towards a good tutorial that would be great. Thanks!


Solution

  • Assuming you're using this in Java mode, then you can use the Java API: https://docs.oracle.com/javase/8/docs/api/

    The Java API contains a File class that contains several methods for reading the contents of a directory: https://docs.oracle.com/javase/8/docs/api/java/io/File.html

    Something like this:

    ArrayList<String> songs = new ArrayList<String>();
    File directory = new File("path/to/song/directory/");
    for(File f : directory.listFiles()){
       if(!f.isDirectory()){
          songs.add(f.getAbsolutePath());
       }
    }
    

    Googling "java list files of directory" will yield you a ton of results.