How do I play a random audio sample?

Hi there. So I'm still quite new to the coding game and I've been working on a visual code which is going really well. I'm now trying to add some sound. Using Minim audio samples I've made it so a sound plays when you click. However, I would like it to be so that when I click it plays a random 1 of, say, 5 audio samples. How do I go about doing this?

Many thanks in advance for any help

Answers

  • @AlfieChillar: you can do that easily with minim (or other audio lib). Example (change the name of mp3 to yours!):::

    import ddf.minim.*;
    AudioPlayer player;//initialiser l'AudioPlayer
    Minim minim;//initialiser minim
    
    String[] tableau = {
      "my1.mp3", "my2.mp3"
    };
    
    void setup() {
    
      size(100, 100);
      minim = new Minim(this);
      int zardSon = int(random(tableau.length));
     String son = tableau[zardSon];
     player = minim.loadFile(son);
     player.pause();
     background(0);
    
    }
    
    void draw() {
    
    
    if (player.isPlaying() == false) {
          int zardSon = int(random(tableau.length));
          String son = tableau[zardSon];
         player = minim.loadFile(son);
          player.play();
    
     }
    
    }
    
      void stop(){
    
        player.close();//kill player
        minim.stop();//kill minim
        super.stop();//kill superClass audio Minim
      }
    
  • This is so almost perfect! Thank you so much! But how do I now change it so that it plays an audio file on mouseClick instead of back to back on start up?

  • @AlfieChillar:: in order to do that (i am not sure to understand exactly what you want but i think you can easily adapt...) 1) add some boolean "boolean initPlayer = false" 2) in draw() before if (player.isPlaying() == false) add your conditionnal: if(mousePressed== true){ dont forget to close it with } at the end!!! 3) change the value of the boolean:: initPlayer = true;

    Then add your mousePressed() method (after draw, of course)==

    if (mousePressed == false && initPlayer == true){
      if (player.isPlaying() == true ) {
      player.pause();
      }
    }
    }
    

    ---So you can launch or stop the random sound...

  • I still can't quite get that bit working, but I am closer. Just to try and re-describe what I am aiming for: When you click the mouse it plays a single audio sample, but as oppose to setting just one sample, I would like it to randomly pick one from a few I have.

  • @AlfieChillar== so, as long as you pressed the mouse the sound will be played...

        import ddf.minim.*;
        AudioPlayer player;
        Minim minim;
        boolean playeurInit = false;
    
        String[] tableau = {
          "my1.mp3", "my2.mp3"
        };
    
        void setup() {
    
          size(100, 100);
          minim = new Minim(this);
          int zardSon = int(random(tableau.length));
         String son = tableau[zardSon];
         player = minim.loadFile(son);
         player.pause();
    
          background(0);
    
        }
    
    
        void draw() {
    
    
        if (mousePressed == true) {
    
        if (player.isPlaying() == false) {
              int zardSon = int(random(tableau.length));
              String son = tableau[zardSon];
             player = minim.loadFile(son);
              player.play();
              player.loop();
              playeurInit = true;
         }
    
    
        if (mousePressed == false && playeurInit == true){
          if (player.isPlaying() == true ) {
          player.pause();
          }
        }
        }
    
          void stop(){
    
            player.close();//le player est tué
            minim.stop();//minim est tué
    
            super.stop();//on arrete la superClasse audio Minim
          }
    
  • or if you prefer only clicking:

    import ddf.minim.*;
    AudioPlayer player;
    Minim minim;
    
    boolean playeurInit = false;// que leplayer n'est pas lancé
    boolean stop = true;
    
    String[] tableau = {
      "Om.mp3", "groove.mp3"
    };
    
    
    void setup() {
    
    
    
      size(100, 100);
      minim = new Minim(this);
      int zardSon = int(random(tableau.length));
     String son = tableau[zardSon];
     println(son);
     player = minim.loadFile(son);
     player.pause();
    
      background(0);
    
    }
    
    
    
    
    
    void draw() {
    
    if (!stop) {
    
    if (player.isPlaying() == false) {
          int zardSon = int(random(tableau.length));
          String son = tableau[zardSon];
         player = minim.loadFile(son);
          player.play();
          player.loop();//you can change that!
          playeurInit = true;
     }
    
    }
    
    //if (mousePressed == false && playeurInit == true){
    //  if (player.isPlaying() == true ) {
    //  player.pause();
    //  }
    //}
    }
    
    void mouseReleased(){
      if(stop == true){
        stop = false;
      }else{
        stop = true;
        if (player.isPlaying() == true ) {
     player.pause();
     }
      }
    }
    
    
      void stop(){
    
        player.close();
        minim.stop();
        super.stop();
      }
    
  • These are awesome! Thank you so much :) Do you know how I could make it so it just plays one single file when I click? As opposed to starting a string of them?

  • only one??? not random??? ---- each time in the code you see "son" (String son...) you change the random method to the name of your file; as for an example instead of String son = tableau[zardSon]; player = minim.loadFile(son); you put String son = "mymp3.mp3"; player = minim.loadFile("mymp3.mp3") or you put only one mp3 in the array and leave the code as it is!

  • But then it will play only one set file. I want it to play one file but for that file to be different/random each time you click.

  • if you want that the random method NEVER returns a file that was already played you have to add another String[] playedFiles array (same length as the first one) and add to it the played files;( playedFiles.add[son]); then, also in the random method you add a conditionnal "if the file is present into the playedFiles, random again; it' s need a for(...){} loop with an int var which is = to the number of played file; for that you create a global int nbrePlayed files initialized to 0 at the beginning, and incremented each time a new file is played....Of course you must be aware that at some moment ALL the files have to be played, this will happen when your global nbrePlayedFiles == the length of your array of mp3...

  • Hello ! Thanks a lot for this useful explanation. I'm not really good with english but I think your last explanation (akenaton) is exactly what I want to do with my file. But I don't really understand your explanation, do you have the exempe with the code? Thanks a lot !

  • A slightly different approach using StringList and its associated method shuffle() to generate a random list where all its items is played at least once before a new shuffled list is generated.

    Kf

    import ddf.minim.*;
    AudioPlayer player;
    Minim minim;
    
    boolean playeurInit = false;// que leplayer n'est pas lancé
    boolean stop = true;
    
    StringList tableau;
    int current=0;
    
    void setup() {
      size(100, 100);
      minim = new Minim(this);
    
      //ADD sound files to your list
      tableau=new StringList();
      tableau.append("Om.mp3");
      tableau.append("groove.mp3");
    
      shufflePlayList();
      String son = tableau.get(current++);
      println(son);
      player = minim.loadFile(son);
      player.pause();
    
      background(0);
    }
    
    void draw() {
    
      if (!stop) {
    
        if (player.isPlaying() == false) {
    
          //CHECK we haven't exhausted the current shuffled(random) play list
          //If we have played all the songs, then re-shuffle the list
          if (current==tableau.size()-1) {
            shufflePlayList();
          }
    
          String son = tableau.get(current++);
          player = minim.loadFile(son);
          player.play();      
          playeurInit = true;
        }
      }
    }
    
    void mouseReleased() {
      if (stop == true) {
        stop = false;
      } else {
        stop = true;
        if (player.isPlaying() == true ) {
          player.pause();
        }
      }
    }
    
    void stop() {
      player.close();
      minim.stop();
      super.stop();
    }
    
    void shufflePlayList() {
      current=0;
      tableau.shuffle();
    }
    
Sign In or Register to comment.