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

Hallo!
an dieser Stelle hänge ich auch. Das File auswählen klappt dank dem Tipp. Aber dann weitere Fehler.
Ist das überhaupt Processing Code??? Bei so vielen Fehlern?? Was läuft da falsch? Kann jemand helfen? LG Klaus

Hello!
I am also stuck at this point. The file selection works thanks to the tip. But then more errors.
Is this processing code at all??? With so many errors?? What is going wrong? Can someone help? LG Klaus
(Translated with DeepL)

1 Like

Lieber Klaus,

kannst Du Dein Programm zeigen?

here is a simple version:


PImage selectedImage;

void setup() {
  size (600, 600);
  selectInput("Select an image to process:", "fileSelected");
}

void draw() {
  if (selectedImage!=null) {
    image(selectedImage, 2, 2);
  }
}

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

Naja es ist der von “blippr” oben genannte Code von GitHub: EncodeAudio/EncodeAudio.pde at master · damellis/EncodeAudio · GitHub
Oder soll ich den hier nochmals einstellen? Danke fĂĽr die schnelle Antwort!

Well it is the code from GitHub mentioned by “blippr” above: EncodeAudio/EncodeAudio.pde at master · damellis/EncodeAudio · GitHub
Or should I post this one again? Thanks for the quick reply!

thanks

here is a more complex version where you can hit “L” or “l” to load

(Bei mir muss man teilweise mit der Maus ins Window klicken, damit er den key L annimmt)



// states for sketch as a whole
final int stateWaitForLoad = 0;
final int stateNormal      = 1;
final int stateCancel      = 2;
int state = stateWaitForLoad;

String loadPath   = "";
String loadedFile = "";

String[] arrayLoadedFileContent;

void setup() {
  size (1000, 600);
  background(111);
  loadFileSelectDialog();
}

void draw() {

  background(111);

  switch(state) {

  case stateWaitForLoad:
    text("waiting", 23, 23);
    break;

  case stateNormal:
    text(loadedFile
      + "\nHit 'l' ('L') for load"
      + "\n"
      + "\n"
      + content(), 23, 23);
    break;

  case stateCancel:
    // cancel
    text("User hit cancel. Hit 'l' ('L') for load", 23, 23);
    break;

  default:
    // error
    break;
  }
}

// -----------------------------------------------------------

void keyPressed() {
  if (state==stateNormal || state==stateCancel) {
    if (key=='l'||key=='L') {
      loadFileSelectDialog();
      state = stateWaitForLoad;
    }//if
  } else if (state == stateWaitForLoad) {
    // ignore
  }
}

void loadFileSelectDialog() {
  // init the dialog for loading
  state = stateWaitForLoad;
  loadPath="";
  File start1 = new File(sketchPath("")+"/*.abc");
  selectInput("Select a file to load:", "fileSelectedForLoad", start1);
}

void fileSelectedForLoad(File selection) {
  // this gets called when the dialog is ended
  if (selection == null) {
    // Error
    println("Window was closed or the user hit cancel.");
    state=stateCancel;
  } else {
    loadPath   = selection.getAbsolutePath();
    loadedFile = selection.getAbsolutePath();
    arrayLoadedFileContent = loadStrings(loadedFile);
    println("User selected " + selection.getAbsolutePath());
    state = stateNormal;
  }
}

//---------------------------------------------------------------------------
// Tool content()

String content() {

  // abort ?
  if (arrayLoadedFileContent==null)
    return "";

  // file not empty? ------------------------
  if (arrayLoadedFileContent.length>0) {
    String result="";
    for (String s1 : arrayLoadedFileContent) {
      result+= s1
        + "\n";
    }//for
    return result; // success
  }//if

  // return / fail
  return "";
}//func
//

Hallo,
kann es sein dass wir uns missverstehen.
Liegt evtl. an dem Betreff der natĂĽrlich nicht mehr zum Problem passt.
Das was Du sendest sind doch Varianten der Dateiauswahl.
Die ist aber nicht das Problem. Das habe ich bereits gelöst.
Aber die weitere ProgrammausfĂĽhrung geht nicht.

Eigentlich soll das Programm ein MP3 in eine Zahlenfolge umwandeln. Diese kann dann ein Arduino (ohne weiteres Zubehör) abspielen. Das klappt auch mit dem Demoprogramm von hier: High-Low Tech – Simple Arduino audio samples fehlerfrei. Ich benötige aber eine gewandelte MP3 eines “grimmigen Hundes” um Katzen vom Grundstück fern zu halten :dog:
LG Klaus

Hello,
It is possible that we misunderstand each other.
It may be that the subject no longer fits the problem.
What you are sending are variants of the file selection.
But this is not the problem. I have already solved that.
But the further program execution does not work.

Actually the program should convert a MP3 into a number sequence. This can then play an Arduino (without further accessories). This also works with the demo program from here: High-Low Tech – Simple Arduino audio samples without errors. But I need a converted MP3 of a “fierce dog” to keep cats away from the property :dog:
LG Klaus

1 Like

I was reading this and therefore I posted my Sketches.

So it’s depending on the MP3 file whether it works or not?

I can’t help you with this

Hello,

If you search the Issues for the link above you will find a program that works.

I tried it with several MP3s that generated an output to the clipboard… some did not work.

I pasted the clipboard generated to Notepad:

:)

Hallo, vielen Dank an Alle für die Hilfe! Ich muss das jetzt zunächst alles austesten.

Aber Frage an glv. Der Code von Github funktioniert bei Dir ohne jegliche Anpassung?? Das habe ich eigentlich auch erwartet aber bei mir geht es eben nicht.
Kann es an meinem Processing liegen?? Ich nutze Linux Mint und die dort angebotene Processing-IDE 4.2. Minim Library habe ich nachinstalliert. Fehlt evtl. noch was?
Melde mich wieder nach weiteren Versuchen. Vielen Dank.
LG Klaus
Hello, thank you all for the help! I have to test it all out now for now.

But question to glv. the code from Github works for you without any customization??? That’s what I expected but it doesn’t work for me.
Can it be because of my processing? I use Linux Mint and the Processing IDE 4.2 offered there. I have installed Minim Library. Is there anything missing?
I will get back to you after further attempts. Thank you very much.
LG Klaus

Translated with DeepL Translate: The world's most accurate translator (free version)

Hello @K_R,

The code that I referenced in the Issues works as is with Processing 3.5.4 and 4.1.2 on a W10 PC.

Output:

I can then paste the data that is in the clipboard.

See the comment from PhiLo about the ID3 tags:

:)

Das war der entscheidende Hinweis!! Vielen Dank glv! Damit klappt alles.
That was the crucial hint!! Thank you very much! With this everything works.

LG Klaus

1 Like