Audio Problem with .app Processing


#1

Hi Guys,

I’m trying to make a native App for work, where people can drag the sound file into data folder and run the AudioWaveform.app.

The problem is: When I launch for the first time the application it’s works, but when I remove the mp3 file from data folder and I add another one mp3 file the program is not running.

I don’t know how to fix this bug, because all work when I use Processing IDE but when I export the native application it’s work only the first time.

I attach the code below:

import ddf.minim.analysis.*;
import ddf.minim.*;
import com.hamoid.*;
import java.util.Date;

VideoExport videoExport;
Minim minim;
AudioPlayer player;
FFT fft;

String[] filenames;
String nomeFile;
float c = 1.2;
float dist;
int larghezza;
PFont font;


void setup() {
  size(400, 400);
  smooth();

  font = loadFont("Avenir-Black-16.vlw");
  textFont(font);

  larghezza = round(width/60);
  dist = larghezza*2;

  String path = sketchPath("data");
  filenames = listFileNames(path);
  printArray(filenames);

  for (int i = 0; i < filenames.length; i++) {
    if (filenames[i].endsWith("mp3")) { 
      nomeFile = filenames[i];
      minim = new Minim(this);
      player = minim.loadFile(nomeFile);
    } 
  }

  //println("File audio: " + nomeFile);
  //println("File " + ds + " trovato");

  fft = new FFT(player.bufferSize(), player.sampleRate());
  player.play();
}

void draw() {
  background(0);
  stroke(255);
  fft.forward(player.mix);
  for (int i = 0; i < fft.specSize(); i++) {
    fill(255);
    strokeWeight(0.5);
    rect(i*dist, height/2, larghezza, fft.getBand(i) * -c);
    rect(i*dist, height/2, larghezza, fft.getBand(i) * c);
  }

  for (int i = 0; i < filenames.length; i++) {
    text(filenames[i], 15, 20*i+20);
  }
}

// This function returns all the files in a directory as an array of Strings  
String[] listFileNames(String dir) {
  File file = new File(dir);
  String names[] = file.list();
  return names;
}

#2

It works the first time because you load the files and create the minim objects in setup which, by design, only runs once in your code.

The title of your post should be changed to “Detect new files in my apps’ data folder”. There are two ways to do this.

  1. The user loads the files in the data folder. The user then returns to your application and it tells your applciation to update the file list and re-create the minim list based on those read files from the data folder. For this to work, you need to move relevant function in setup to another function: initSoundFilesFromDataFolder(). Then you need to call this function in setup and through the user’s update trigger. The later could be something as simple as when the user presses the u key, u for update. This in turn call the init function. Notice this is not automated. The user needs to still load the files AND update the list.

  2. Maybe your app could listen to the data folder and update when a change occurs. I haven’t done this myself. You can create a separate thread that checks for the file listing in the data folder. You run this every 1 or 2 secs. If files are added, the thread will detected and it will trigger the re-loading of the files. An alternative to this would be to investigate if your OS provides hooks to listen to folder changes. The disadvantage of this is that it is OS specific. You can search about this on stackoverflow or implement the solution using threads.

Kf


#3

Hi Kfrajer,

Thank you for your answer. I fixed the problem.

The problem is the variable path:

String path = sketchPath("data");

changed with

String path = sketchPath("");