Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Re : Play a random audio file / invoke a function on a random object of a class

$
0
0
Hi, I am a total newbie and have tried a few ways (//as you can see) to get these random files to play back when mousePressed but to no avail.  This is the closest post I have found to what I am trying to do, which is to trigger the files, display the filename at the location and change the background to a random colour.  Can anyone tell me what I am doing wrong?
  1. Minim minim;
  2. AudioSample[]notes = new AudioSample[8];
  3. AudioMetaData meta;
  4. int myRan;

  5. void setup() {
  6.   size(400, 680);
  7.   background(0);
  8.   colorMode(HSB);
  9.   textFont(createFont("Serif", 12));
  10.   minim = new Minim(this);
  11.   notes[0] = minim.loadSample("A2 MONO.wav", 2048);
  12.   notes[1] = minim.loadSample("B2.wav", 2048);
  13.   notes[2] = minim.loadSample("C2.wav", 2048);
  14.   notes[3] = minim.loadSample("D2.wav", 2048);
  15.   notes[4] = minim.loadSample("E2.wav", 2048);
  16.   notes[5] = minim.loadSample("F2.wav", 2048);
  17.   notes[6] = minim.loadSample("G2.wav", 2048);
  18.   notes[7] = minim.loadSample("C3.wav", 2048);
  19. }
  20. int index = 0;
  21. void draw() {
  22.   if (mousePressed)
  23.   { 
  24.     //index = int(random(notes.length));
  25.     background(random(255), 255, 255);
  26.     myRan = (int) random(0, 8);
  27.     notes[myRan].play();
  28.     notes[myRan].cue(0);
  29.     fill(255);
  30.     textSize(random(48, 400));
  31.     text("" + meta.fileName(), mouseX, mouseY);
  32.   }
  33. }
  34. void stop() {
  35.   minim.stop();
  36.   super.stop();
  37. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles