Importing a list of songs

edited August 2014 in How To...

Hi,

For an audio-visualising app I am looking for a way to import music from a folder or just get a list or array in the sketch itself. Is this possible and how can I do this? I am using processing 2.2 on a windows 8 computer.

Answers

  • edited August 2014

    This thread below got a full example on how to get a File[] from some user-chosen folder:
    http://forum.processing.org/two/discussion/6004/how-to-make-a-movie-array

    Basically we use FilenameFilter interface + listFiles() method in order to only get the extensions we want to:

    import java.io.FilenameFilter;
    
    static final FilenameFilter pictFilter = new FilenameFilter() {
      final String[] EXTS = {
        ".png", ".jpg", ".jpeg", ".gif"
      };
    
      @ Override boolean accept(final File dir, String name) {
        name = name.toLowerCase();
        for (final String ext: EXTS)  if (name.endsWith(ext))  return true;
        return false;
      }
    };
    

    Just replace the image extensions w/ audio 1s. Of course, rename pictFilter to something like sndFilter! (*)

  • edited August 2014

    @GoToLoop: You don't have to install that library? The webpage is a bit confusing.... :P

    And Do you have to use alll the code you used in the thread about the movie array? all the voids and everything?

  • Answer ✓

    You don't have to install that library?

    That example doesn't use any 3rd-party libraries. Only standard Java's!

    ... have to use alll the code you used in the thread about the movie array?

    That example is about displaying pictures from a folder. You gotta adapt it for your particular needs!

  • Thanks a lot!!!

  • Okay... @GoToLoop, I've tried your sketch just the way it is without changing things. I get a pop up screen when clicking on the screen. I can only select folders? And when I select one I still get a black screen. How does this work? :p Sorry for being such a nuisance

  • edited August 2014

    I can only select folders?

    That's true! It uses selectFolder() rather than selectInput():
    http://processing.org/reference/selectFolder_.html
    http://processing.org/reference/selectInput_.html

    And when I select one I still get a black screen.

    As you can see inside new FilenameFilter():

    final String[] EXTS = {
      ".png", ".jpg", ".jpeg", ".gif"
    };
    

    It only reads file names w/ those extensions! So we gotta choose a folder w/ at least 1 of those type of files.
    listFiles() invokes that accept() customized method in order to filter them out.
    As I've already mentioned, you gotta replace those w/ the 1s you're gonna need!

  • I've been through your code, but where can I find the part to what has happened with the selected file? The screen stays black

  • edited August 2014

    This is the code which invokes the folder select pop-up window:

    void mouseClicked() {
      selectFolder("Select a folder to process:", "folderSelected");
    }
    

    Once it's done and closed, it invokes folderSelected():
    Which in turn calls up getFolderContent():

    protected static final File[] getFolderContent(final File dir) {
      return dir.listFiles(pictFilter);
    }
    

    Which returns a File[] filter out by listFiles() + pictFilter!

    P.S.: You don't need to use neither selectFolder() nor selectInput() if you got a fixed path where your files are!

Sign In or Register to comment.