Play a random audio file / invoke a function on a random object of a class
in
Core Library Questions
•
2 years ago
Hello, My goal is to play a random audio file using the trigger function. If I call a.trigger() file a is played but if I would like to call a random file between (a - z) how would i do that ? I have do it this way :
Below is the rest of my code and I am using analog inputs running on the Arduino in the Arduino environment to trigger this sketch in processing.
- String[] audio = { "a", "b", "c" };
int index = int(random(audio.length)); - audio[index].trigger();
Below is the rest of my code and I am using analog inputs running on the Arduino in the Arduino environment to trigger this sketch in processing.
- import processing.serial.*;
import ddf.minim.*;
AudioSample a;
AudioSample b;
AudioSample c;
Minim minim;
int linefeed = 10;
Serial myPort;
int sensorValue = 0;
void setup()
{
println(Serial.list());
myPort = new Serial(this, Serial.list()[1], 9600);
minim = new Minim(this);
a = minim.loadSample("yes.wav", 2048);
b = minim.loadSample("no.wav", 2048);
c = minim.loadSample("maybe.wav", 2048);
}
void draw()
{
String[] audio = { "a", "b", "c" };
int index = int(random(audio.length));
stroke(255);
while (myPort.available() > 0)
{
String inBuffer = myPort.readString();
sensorValue = int(trim(inBuffer));
println(sensorValue);
if (sensorValue != 0 )
{
audio[index].trigger();
sensorValue=0;
}
}
}
1