damellis / EncodeAudio

Processing sketch for translating audio files into numeric values.
http://hlt.media.mit.edu/?p=1963
35 stars 23 forks source link

selectInput uses old (pre 2012) style #2

Open MauiJerry opened 6 years ago

MauiJerry commented 6 years ago

wont run with current Processing as selectInput("string") was changed long time back

YoGitpp commented 1 year ago

You are right. Try this :

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

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

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

    try {
      Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

      minim = new Minim(this);
      sample = minim.loadSample(selection.getAbsolutePath());

      float[] samples = sample.getChannel(sample.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();
}