# SelectInput() fails

**URL:** <https://discourse.processing.org/t/selectinput-fails/38735>\
**Category:** Libraries\
**Created:** [September 13, 2022, 1:13pm UTC](https://discourse.processing.org/t/selectinput-fails/38735 "2022-09-13T13:13:54Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![blippr](https://avatars.discourse-cdn.com/v4/letter/b/eb8c5e/32.png) [@blippr](https://discourse.processing.org/u/blippr)\
**Post date:** [September 13, 2022, 1:13pm UTC](https://discourse.processing.org/t/selectinput-fails/38735/1 "2022-09-13T13:13:54Z")

</div>

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:

```auto
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:

> **[Simple Arduino audio samples](http://highlowtech.org/?p=1963)**
>
> This tutorial explains how to do simple playback of short (~4 second), low-bitrate (8 KHz) audio samples from Arduino using only a speaker.

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:

> **[GitHub - damellis/EncodeAudio: Processing sketch for translating audio files...](https://github.com/damellis/EncodeAudio)**
>
> Processing sketch for translating audio files into numeric values. - GitHub - damellis/EncodeAudio: Processing sketch for translating audio files into numeric values.

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

Can I please get some help on this?

Cheers

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [September 13, 2022, 1:21pm UTC](https://discourse.processing.org/t/selectinput-fails/38735/2 "2022-09-13T13:21:26Z")

</div>

Hi @blippr,

please read the reference for the function …

> **[Reference](https://processing.org/reference/selectInput_.html)**
>
> Open a platform-specific file chooser dialog to select a file for input. After the selection is made, the selected File will be passed to the 'callback' function. If the dialog is closed or canceled, …

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![blippr](https://avatars.discourse-cdn.com/v4/letter/b/eb8c5e/32.png) [@blippr](https://discourse.processing.org/u/blippr)\
**Post date:** [September 13, 2022, 1:52pm UTC](https://discourse.processing.org/t/selectinput-fails/38735/3 "2022-09-13T13:52:10Z")

</div>

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…

```auto
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:

```auto
...
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![K\_R](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@K\_R](https://discourse.processing.org/u/K_R)\
**Post date:** [April 3, 2023, 12:44pm UTC](https://discourse.processing.org/t/selectinput-fails/38735/4 "2023-04-03T12:44:58Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 3, 2023, 12:47pm UTC](https://discourse.processing.org/t/selectinput-fails/38735/5 "2023-04-03T12:47:35Z")

</div>

Lieber Klaus,

kannst Du Dein Programm zeigen?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 3, 2023, 12:55pm UTC](https://discourse.processing.org/t/selectinput-fails/38735/6 "2023-04-03T12:55:11Z")

</div>

here is a simple version:

```auto

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());
  }
}

```

---

<div class="post-metadata">

**Author:** ![K\_R](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@K\_R](https://discourse.processing.org/u/K_R)\
**Post date:** [April 3, 2023, 12:58pm UTC](https://discourse.processing.org/t/selectinput-fails/38735/7 "2023-04-03T12:58:02Z")

</div>

Naja es ist der von “blippr” oben genannte Code von GitHub: [EncodeAudio/EncodeAudio.pde at master · damellis/EncodeAudio · GitHub](https://github.com/damellis/EncodeAudio/blob/master/EncodeAudio.pde)  
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](https://github.com/damellis/EncodeAudio/blob/master/EncodeAudio.pde)  
Or should I post this one again? Thanks for the quick reply!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 3, 2023, 1:04pm UTC](https://discourse.processing.org/t/selectinput-fails/38735/8 "2023-04-03T13:04:12Z")

</div>

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)

```auto

// 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
//

```

---

<div class="post-metadata">

**Author:** ![K\_R](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@K\_R](https://discourse.processing.org/u/K_R)\
**Post date:** [April 3, 2023, 1:24pm UTC](https://discourse.processing.org/t/selectinput-fails/38735/9 "2023-04-03T13:24:11Z")

</div>

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](http://highlowtech.org/?p=1963) fehlerfrei. Ich benötige aber eine gewandelte MP3 eines “grimmigen Hundes” um Katzen vom Grundstück fern zu halten 🐕  
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](http://highlowtech.org/?p=1963) without errors. But I need a converted MP3 of a “fierce dog” to keep cats away from the property 🐕  
LG Klaus

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 3, 2023, 1:54pm UTC](https://discourse.processing.org/t/selectinput-fails/38735/10 "2023-04-03T13:54:09Z")

</div>

> [@blippr](#):
>
> Script still does not prompt for a file to load

I was reading this and therefore I posted my Sketches.

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 3, 2023, 3:26pm UTC](https://discourse.processing.org/t/selectinput-fails/38735/11 "2023-04-03T15:26:19Z")

</div>

I can’t help you with this

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 3, 2023, 11:41pm UTC](https://discourse.processing.org/t/selectinput-fails/38735/12 "2023-04-03T23:41:07Z")

</div>

Hello,

> [@K\_R](#):
>
> [EncodeAudio/EncodeAudio.pde at master · damellis/EncodeAudio · GitHub](https://github.com/damellis/EncodeAudio/blob/master/EncodeAudio.pde)

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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/c/c/cccae93eeef45bf0b99754d5849275d825ae14b1.png)

`:)`

---

<div class="post-metadata">

**Author:** ![K\_R](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@K\_R](https://discourse.processing.org/u/K_R)\
**Post date:** [April 4, 2023, 8:06am UTC](https://discourse.processing.org/t/selectinput-fails/38735/13 "2023-04-04T08:06:13Z")

</div>

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](http://www.DeepL.com/Translator) (free version)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 4, 2023, 10:27am UTC](https://discourse.processing.org/t/selectinput-fails/38735/14 "2023-04-04T10:27:53Z")

</div>

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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/5/0/50ff917d02fff1e296435f34bfa648ab503bec6a.png)

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

See the comment from PhiLo about the ID3 tags:

> **[Getting a song to play with the help of minim - Processing Forum](https://forum.processing.org/one/topic/getting-a-song-to-play-with-the-help-of-minim.html)**
>
> Processing Forum

`:)`

---

<div class="post-metadata">

**Author:** ![K\_R](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@K\_R](https://discourse.processing.org/u/K_R)\
**Post date:** [April 5, 2023, 5:15pm UTC](https://discourse.processing.org/t/selectinput-fails/38735/15 "2023-04-05T17:15:53Z")

</div>

> [@glv](#):
>
> If you search the _Issues_ for the link above you will find a program that works.

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

> <https://github.com/damellis/EncodeAudio/issues/2>
>
> wont run with current Processing as selectInput("string") was changed long time …back

LG Klaus
