Audio Importer

Processing and minim when working with a file that reside outside the sketch code it need to be loaded into the sketch before it can be used. When loading an audio file into a sketch a defined route for the like has to be specified in the setup. This is will only work in the way because the file needs to be loaded into the buffer/ram where it can then be relayed to the sketch before the sketch starts drawing. The problem for me is that I need to initial load a file then when it finishes playing load another one and play that. This poses several problems like reloading and playing a second time.

A solution for this could be directly importing the file itself using a browse function each time. This is where the user can directly locate the file and load it into the sketch solving the issue partly of being able to select a song from a specific file.

The code below is in its simplest form the importer….

import ddf.minim.spi.*
Minim minim;
AudioPlayer player;
ArrayList<Songs> s;
int k;
String filename;
boolean isSelected = false;

void setup() {
  s = new ArrayList();
  minim = new Minim(this);
}
void draw() {
   text("Import File", buttonX+35, buttonY+30);
}
void mouseClicked() {
  if {
    selectInput("Import music file", "fileSelected");
  }
}
void fileSelected(File selection) {
    filename = selection.getAbsolutePath();
    s.add(new Songs(player, filename, "Filename"));
    isSelected = true;
}
class Songs {
  AudioPlayer song; 
  String directory;
  String songName;
  Songs(AudioPlayer song, String directory, String songName) {
    song=minim.loadFile(directory);    
    this.song=song;
    this.songName=songName;
    song.play();
  }
}

The important part of this code is the use of a class (Songs) to assemble the directory location and the song name that is then imputed into the Minim audioplayer. Using a class works well as it can be called and run at any time with new values entered.

class Songs {
  AudioPlayer song; 
  String directory;
  String songName;
  Songs(AudioPlayer song, String directory, String songName) {
    song=minim.loadFile(directory);    
    this.song=song;
    this.songName=songName;
}

You may notice that in the class it finds the string directory & songName before running the AudioPlayer through the Song array. This is so that the location is stored before its running as an error will occur if not. This is then ran through the song load file an played. Using an array list means that it can be updated, as the array values are variable.

In my installation project this would be useful in replaying the audio player with a different audio file each time. It could be used as a way for an audience to select a personal song using an interface. Secondly it could take the class method and adapt it to be able to generate input independently in the sketch so that there was less relied on the audiences input.

 

Leave a Reply