SelectInput() fails

Hi,

I get this error message in the console:

The method selectInput(String, String) in the type PApplet is not applicable for the arguments (String)

when trying to run this script:

import java.awt.datatransfer.*;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import ddf.minim.*;

int SAMPLES = 30000;

Minim minim;
AudioSample sample;

void setup()
{
  size(512, 200);
  
  String file = selectInput("Select audio file to encode.");

  if (file == null) {
    exit();
    return;
  }
  
  try {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  
    minim = new Minim(this);
    sample = minim.loadSample(file);
    
    float[] samples = sample.getChannel(BufferedAudio.LEFT);
    float maxval = 0;
  
    for (int i = 0; i < samples.length; i++) {
      if (abs(samples[i]) > maxval) maxval = samples[i];
    }
    
    int start;
    
    for (start = 0; start < samples.length; start++) {
      if (abs(samples[start]) / maxval > 0.01) break;
    }
  
    String result = "";  
    for (int i = start; i < samples.length && i - start < SAMPLES; i++) {
      result += constrain(int(map(samples[i], -maxval, maxval, 0, 256)), 0, 255) + ", ";
    }
  
    clipboard.setContents(new StringSelection(result), null);
    
    JOptionPane.showMessageDialog(null, "Audio data copied to the clipboard.", "Success!", JOptionPane.INFORMATION_MESSAGE);
  } catch (Exception e) {
    JOptionPane.showMessageDialog(null, "Maybe you didn't pick a valid audio file?\n" + e, "Error!", JOptionPane.ERROR_MESSAGE);
  }
  
  exit();
}

void stop()
{
  sample.close();
  minim.stop();
  super.stop();
}

I am following this guide:

and there is a link to a Processing application but I can not run it, probably since I am on OSX Monterey. The source code above is taken from here:

However, I can not get the scrip to work, as described above.

Can I please get some help on this?

Cheers

Hi @blippr,

please read the reference for the function …

Cheers
— mnse

1 Like

Hi, thank you for replying. I have looked at the reference but still did not figure it out. I had a second go at it now, but still can’t get there.

I assumed the original scrip was fine and it was more of a platform or version problem. But…

String file = selectInput("Select audio file to encode.");

it seems like Processing does not like to create a String this way.

So I tried to merge the code from the reference into the original script, adding this function:

...
void fileSelected(File selection)
...

But I can not get it right, especially since I find it hard to understand how to treat selectInput()

I modify the script like this:

import java.awt.datatransfer.*;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import ddf.minim.*;

int SAMPLES = 30000;

String file;

Minim minim;
AudioSample sample;

void setup()
{
  size(512, 200);
  
  //String file = selectInput("Select audio file to encode.");

   selectInput("Select a file to process:", "fileSelected");

  if (file == null) {
    exit();
    return;
  }
  
  try {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  
    minim = new Minim(this);
    sample = minim.loadSample(file);
    
    float[] samples = sample.getChannel(BufferedAudio.LEFT);
    float maxval = 0;
  
    for (int i = 0; i < samples.length; i++) {
      if (abs(samples[i]) > maxval) maxval = samples[i];
    }
    
    int start;
    
    for (start = 0; start < samples.length; start++) {
      if (abs(samples[start]) / maxval > 0.01) break;
    }
  
    String result = "";  
    for (int i = start; i < samples.length && i - start < SAMPLES; i++) {
      result += constrain(int(map(samples[i], -maxval, maxval, 0, 256)), 0, 255) + ", ";
    }
  
    clipboard.setContents(new StringSelection(result), null);
    
    JOptionPane.showMessageDialog(null, "Audio data copied to the clipboard.", "Success!", JOptionPane.INFORMATION_MESSAGE);
  } catch (Exception e) {
    JOptionPane.showMessageDialog(null, "Maybe you didn't pick a valid audio file?\n" + e, "Error!", JOptionPane.ERROR_MESSAGE);
  }
  
  exit();
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    println("User selected " + selection.getAbsolutePath());
  file = selection.getAbsolutePath();
  }
}

void stop()
{
  sample.close();
  minim.stop();
  super.stop();
}

Script still does not prompt for a file to load but it continues and give this error message:

BufferedAudio cannot be resolved to a variable