1

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!

Philippe H
  • 13
  • 3

2 Answers2

0

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.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Awesome thanks! it took me a while to realize I needed to use \\ in the path to directory name instead of \. So "C:\\Users\\username\\etc\\etc" instead of "C:\Users\username\etc\etc". But once I did that the code works perfectly. could you tell me what the !f stands for? I understood it as "not" but it doesn't make sense to me in this context. Also thanks for showing me what an API is. I'm overwhelmed at the massiveness of it but that's going to help so much in the future! – Philippe H Nov 24 '14 at 18:35
  • 1
    A single \ inside of a String is an escape character. For example, "\n" stands for "newline character". So when you need to type an actual \ and not an escape character, you have to escape the \, which makes it "\\". And the f variable is an instance of File, which has an isDirectory() method that returns a boolean. I'm using !f.isDirectory() to basically say "if this file is NOT a directory, then it must be a song". – Kevin Workman Nov 24 '14 at 18:45
  • Thanks for clearing that up. I think it makes sense to me now. At least until the next time it breaks :) – Philippe H Nov 24 '14 at 19:09
0

Just to add to Kevin's Workman's answer:

  1. Try to use File.separator instead of "/" or "\". It does the same thing, but it figures out the right based on the OS you're using for you, so you can move you sketch on other computers and still have it working.
  2. Check out Daniel Shiffman's that comes with Processing in Examples > Topics > File IO > DirectoryList
George Profenza
  • 50,687
  • 19
  • 144
  • 218