Processing Sound, How to release loaded file after playback is stopped?

Hi there!
Thanks a lot to everyone writing/debugging/contributing with amazing code :slight_smile:
I’m making a sound player app to browse some folders using Guido.
If the selected file is a .wav it loads it with

soundfile = new SoundFile(this, fileToLoad);

if the file is a .flac, I call a decoder function to convert it onto a .wav in a temp directory.

Decoder.decode(fileToLoad, tempFile);

Where fileToLoad is the selected file from the list, and tempFile is my temporary .wav file to write over.
The result is then loaded onto soundfile:

soundfile = new SoundFile(this, tempFile);

My problem is the following:
If I want to play two .flac files in a consecutive order, soundfile, doesn’t release the old one, so the decoder function can’t write over it.

And my question is:
Is there a way to tell soundfile to release the file?

The function I’m using to open fies:

public void openFile(String fileToLoad) {

  if (soundfile!=null) {
    soundfile.stop();
    soundfile=null;
  }
  println(soundfile);
  String fileExtension = fileToLoad.toLowerCase().substring(fileToLoad.length()-4);
  if (fileExtension.equals(".wav")) {
    soundfile = new SoundFile(this, fileToLoad);
  } else if (fileExtension.equals("flac")) {
    try {
      Files.deleteIfExists(Paths.get(tempFile));
    }
    catch (Exception e) {
      System.out.println("Problem with deleting file");
      e.printStackTrace();
    }
    try {
      Decoder.decode(fileToLoad, tempFile);
    } 
    catch (Exception e) {
      System.out.println("Problem decoding file");
      e.printStackTrace();
    }
    soundfile = new SoundFile(this, tempFile);
  } else if (!(fileExtension.equals("flac")&&!fileExtension.equals(".wav"))) {
    println("unsupported file format");
  }
  return;
}
1 Like

PS: I’m using Sound instead of Minim, because it accepts 24bit and 48000hz .wav files.

Based on this, maybe calling close() in addition to stop() could release the resources. Not tested.

Kf

SoundFile doesn’t have a function called close() unfortunately :frowning:
And I can’t seem to find a way to un-create a soundfile…

So I did not read your last post. My solution was related to minim and no relevant to your issue.

You should create an issue in github’s repo.

I checked the source code and I can see the SoundFile extends from AudioSample. Loading the file is done by calling JSyn but I am not sure about the mechanism to release the file. You might do a search online or maybe check stackoverflow.

An alternate solution is to create a variable temporal file instead of relying on the same file name to store the file. This means you will need to clean up your temporal folder from time to time. Processing creates temporal files for new sketches. You can find this code useful for your case.

Kf

1 Like

I’ll dive a bit deeper and see if I can get around to JSyn.
In the meantime I got my app to work with Beads, which is a great library!
However I can’t get it to load 48khz .wav files or 24 bit ones.

Thanks for the guidance!