How to play random sound file when clicking an item within a scrollable list (controlP5 library)?

edited April 2018 in Library Questions

I'm using the 'scrollableList' from the controlP5 library. I wish to simply play a random sound file each time I click an element on the scrollable list, without pausing.

I have tried a few things. I am having limited success with some code from @akenaton that I found in this thread: https://forum.processing.org/two/discussion/8949/how-do-i-play-a-random-audio-sample

I merged that code with the scrollable list. So far, I'm only managing to pause the audio on clicking the menu. I am probably missing something obvious.

As ever, any tips much appreciated :)

import ddf.minim.*;
import controlP5.*;
import java.util.*;
AudioPlayer player;
Minim minim;
ControlP5 cp5;

boolean playeurInit = false;// que leplayer n'est pas lancé
boolean stop = true;

String[] table = {"train1.wav", "train2.wav"};

int randomWav; 
String wav; 

void setup() {
  size(400, 400);

  minim = new Minim(this);
  int randomWav = int(random(table.length));
  String son = table[randomWav];
  //println(son);
  player = minim.loadFile(son);
  player.pause();   

  cp5 = new ControlP5(this);
  List l = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h");
  cp5.addScrollableList("dropdown").setPosition(10, 10).setSize(200, 100).setBarHeight(20).setItemHeight(20).addItems(l);   
}


void draw() {
  background(240);
  if (!stop) {
    if (player.isPlaying() == false) {
          randomWav = int(random(table.length));
          println(randomWav);
          wav = table[randomWav];
          player = minim.loadFile(wav);
          player.play();
          player.loop();//you can change that!
          playeurInit = true;
     }
  }
}


void dropdown(int n) {
  if(stop == true)
  {
    stop = false;
  }else{
    stop = true;
    player.play();
    if (player.isPlaying() == true ) 
    {
      player.pause();
      //player.play();
    }
  }

  CColor c = new CColor();
  c.setBackground(color(255,0,0));
  cp5.get(ScrollableList.class, "dropdown").getItem(n).put("color", c);
}


void stop(){
  player.close();
  minim.stop();
  super.stop();
}

Answers

  • edited April 2018

    Check the code below. No need to call play() and loop(). You need only the former.

    Kf

    import ddf.minim.*;
    import controlP5.*;
    import java.util.*;
    AudioPlayer player;
    Minim minim;
    ControlP5 cp5;
    
    boolean playeurInit = false;// que leplayer n'est pas lancé
    boolean tgr = false;
    
    String[] table = {"button-3.mp3", "button-7.mp3"};
    
    int randomWav; 
    String wav; 
    
    void setup() {
      size(400, 400);
    
      minim = new Minim(this);
      int randomWav = int(random(table.length));
      String son = table[randomWav];
      //println(son);
      player = minim.loadFile(son);
      player.pause();   
    
      cp5 = new ControlP5(this);
      List l = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h");
      cp5.addScrollableList("dropdown").setPosition(10, 10).setSize(200, 100).setBarHeight(20).setItemHeight(20).addItems(l);   
    }
    
    
    void draw() {
      background(240);
      if(frameCount%60==0) println(tgr,player.isPlaying());
      if (tgr) {
        tgr=false;
        if (player.isPlaying() == false) {
              randomWav = int(random(table.length));
              println(randomWav);
              wav = table[randomWav];
              player = minim.loadFile(wav);
              player.play();
              //player.loop();//you can change that!
              playeurInit = true;
         }
      }
    }
    
    
    void dropdown(int n) {
      if(tgr == false)
      {    
        tgr = true;
        //player.play();
        //if (player.isPlaying() == true ) 
        //{
        //  player.pause();
        //  //player.play();
        //}
      }
    
      CColor c = new CColor();
      c.setBackground(color(255,0,0));
      cp5.get(ScrollableList.class, "dropdown").getItem(n).put("color", c);
    }
    
  • Thanks, @kfrajer. I am running the code now but it does not randomise each of my two wav files?

  • edited April 2018

    I simplified my scenario to just using a keypress 'L' to randomise. But in doing so I run into more trouble. The previous sound file seems to always keep playing when the new randomly chosen one starts. So, if I press 'L' many times many samples play once. I wish for each new sample to stop the previous.

    Here's the gist of that code:

    void keyPressed()
    {
      if ( key == 'l' ) { 
        int randomWav = int(random(table.length));
        wav = table[randomWav];
        player = minim.loadFile(wav);
        player.pause();
        player.rewind();
      }
      player.play();
    }
    
  • I tested the code with two very short "noises". They are random. Maybe you didn't try enough times? J/k... it plays only the first one or the second one? Just to have an idea, how long are each file when you play them?

    Kf

  • edited April 2018

    These are 3minute long atmos files. I also need one to stop right when the next is triggered. I tried many times yes, they don't randomise and I'm sure I have correct samples loaded.

  • edited April 2018

    Modified from: https://forum.processing.org/two/discussion/21316/how-do-i-play-the-same-soundfile-multiple-times-simultaneously

    Tested.

    Kf

    import ddf.minim.*;
    AudioPlayer[] mplayer;
    Minim minim;
    
    //String[] songs={"cr1.mp3", "co2.mp3", "dj3.mp3"};
    String[] songs={"aa.mp3", "bb.mp3", "cc.mp3"};
    int idx=0;
    
    void setup() {
      // randomSeed(int(random(1, 10000)));
      size(200, 200);
      background(255);
      frameRate(60);
      minim = new Minim(this);
      mplayer=new AudioPlayer[3];
    
      for (int i=0; i<songs.length; i++) {
        mplayer[i] = minim.loadFile(songs[i]);
        mplayer[i].play();
        mplayer[i].pause();
      }
    }
    
    void draw() {
    }
    
    
    void mouseReleased() {
      mplayer[idx].pause();
      idx=(int)random(songs.length);
      mplayer[idx].rewind();
      mplayer[idx].play();
      println("YES... lucky one is "+idx);
    }
    

    Keywords: kf_keyword minim multiple sound files

  • Excellent stuff. Thank you very much, @krajer. And it fits into the scrollable list code nicely too :)

    import ddf.minim.*;
    import controlP5.*;
    import java.util.*;
    AudioPlayer[] mplayer;
    Minim minim;
    ControlP5 cp5;
    
    String[] songs={"train1.wav", "malechant.wav"};
    int idx=0;
    
    
    void setup() {
      size(400, 400);
    
      minim = new Minim(this);
      mplayer=new AudioPlayer[2];
    
      for (int i=0; i<songs.length; i++) {
        mplayer[i] = minim.loadFile(songs[i]);
        mplayer[i].play();
        mplayer[i].pause();
      }
    
      cp5 = new ControlP5(this);
      List l = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h");
      cp5.addScrollableList("dropdown").setPosition(10, 10).setSize(200, 100).setBarHeight(20).setItemHeight(20).addItems(l);   
    }
    
    
    void draw() {
      background(240);
    }
    
    
    void dropdown(int n) {
      mplayer[idx].pause();
      idx=(int)random(songs.length);
      mplayer[idx].rewind();
      mplayer[idx].play();
      println("YES... lucky one is "+idx);
    
      CColor c = new CColor();
      c.setBackground(color(255,0,0));
      cp5.get(ScrollableList.class, "dropdown").getItem(n).put("color", c);
    }
    
Sign In or Register to comment.