Home > Back-end >  How do I make it so that p5 randomly selects a song from an array?
How do I make it so that p5 randomly selects a song from an array?

Time:12-10

In p5, how do I make it so that for example whenever inData == RED, instead of just redSong1 playing, I can insert an array called redSounds that consists of redSong1 and redSong2 that p5 can randomly choose from to play? I wasn't sure how to format it.

let OFF = 0;
let RED = 6; // HIP HOP
let YELLOW = 5; // HISTORIC EVENTS
let GREEN = 4; // CLASSIC ROCK/ OLDIES
let TEAL = 3; // CLASSICAL
let BLUE = 2; // KHALEEJI FM

let station;
let oldData = OFF; // Start with radio off
let songs;

function preload() {
  radioBackground = loadImage ("radio.png");
  radioFont = loadFont ("digital-7 (italic).ttf");
  
  
  soundFormats("mp3");
  
  // RED (HIP HOP)
  redSong1 = loadSound ("NWA.mp3");
  redSong2 = loadSound ("STILL DRE.mp3");
  redSounds = [redSong1, redSong2] // etc
  
  // YELLOW (HISTORY)
  yellowSong1 = loadSound ("APOLLO 11.mp3");
  let yellowSounds = [yellowSong1,] // etc
  
  // GREEN (CLASSIC ROCK/ OLDIES)
  greenSong1 = loadSound ("QUEEN.mp3")
  let greenSounds = [greenSong1,] // etc
  
  // TEAL (CLASSICAL)
  tealSong1 = loadSound ("NUTCRACKER.mp3")
  let tealSounds = [tealSong1,] // etc
  
  // BLUE (KHALEEJI)
  blueSong1 = loadSound ("MEHAD.mp3");
  let blueSounds = [blueSong1,] // etc
  
}
 
function setup() {
  createCanvas(512, 384);
}
 
function draw() {
  
  // oldData holds the *previous* value of inData
  
  // If user changes station, p5 checks that oldData has changed into inData
  // then prints the change
  if (inData != oldData) {
    // The station changed
     console.log("changed from "   oldData   " to "   inData);
    
    // Stops all songs if inData (new station value) is not equal to oldData
    // (old station value)
    redSong1.stop();
    redSong2.stop();
    yellowSong1.stop();
    greenSong1.stop();
    tealSong1.stop();
    blueSong1.stop(); 
    
    // Change current song to new station
    
    // Start playing the song for new station
    // Change to arrays
    
    if (inData == RED){
      redSong1.play();
    }
    
    if (inData == YELLOW){
      yellowSong1.play();
    }
    
    if (inData == GREEN){
      greenSong1.play();
    }
    
    if (inData == TEAL){
      tealSong1.play(); 
    }
    if (inData == BLUE){
      blueSong1.play(); 
    }
  }
  
   background(radioBackground);
   // fill(255);
   // text("sensor value: "   inData, 30, 50);
  
    if (inData == OFF) { // OFF
      
    noStroke();
    fill (128,172,57); // text color
    textFont(radioFont, 45); // (font name, font size)
    text ("OFF", 225, 148);
    glow(color(128,172,57), 19); // calls glow function
    
      
  } else if (inData == RED) { // HIP HOP
    
    // CHANNEL NAME TEXT
    noStroke();
    fill (128,172,57); // text color
    textFont(radioFont, 45); // (font name, font size)
    text("HIP HOP", 194, 148); // (text, x, y)
    glow(color(128,172,57), 19); // calls glow function
    
    // redSongs();
    
    //redSong1.play();
    
  } else if (inData == YELLOW) { // HISTORY
    
    // CHANNEL NAME TEXT
    noStroke();
    fill (128,172,57); // text color
    textFont(radioFont, 45); // (font name, font size)
    text("HISTORY", 188, 148); // (text, x, y)
    glow(color(128,172,57), 19); // calls glow function
    
  } else if (inData == GREEN) { // CLASSIC ROCK
    
    // CHANNEL NAME TEXT
    noStroke();
    fill (128,172,57); // text color
    textFont(radioFont, 45); // (font name, font size)
    text("CLASSIC ROCK", 142, 148); // (text, x, y)
    glow(color(128,172,57), 19); // calls glow function
    
    // greenSongs(); // replace with randomized array?
    
  } else if (inData == TEAL) { // CLASSICAL
    
    // CHANNEL NAME TEXT
    noStroke();
    fill (128,172,57); // text color
    textFont(radioFont, 45); // (font name, font size)
    text("CLASSICAL", 168, 148); // (text, x, y)
    glow(color(128,172,57), 19); // calls glow function
    
    // tealSongs(); // replace with randomized array?
    
  } else if (inData == BLUE) { // KHALEEJI
    
    // CHANNEL NAME TEXT
    noStroke();
    fill (128,172,57); // text color
    textFont(radioFont, 45); // (font name, font size)
    text("KHALEEJI", 178, 148); // (text, x, y)
    glow(color(128,172,57), 19); // (color, intensity) calls glow function
    
  }
  oldData = inData;
}

So instead of just redSong1.play() as I wrote in the code, I was thinking something like redSounds = \[redSong1, redSong2\], and then when it comes time to play a random song from the redSounds array, it would be something like redSounds.play(random) -- but this, of course, didn't work.

    if (inData == RED){
      redSong1.play();
    }

CodePudding user response:

Call p5.js random() function on the array itself

redsong = random(redSounds)
redsong.play()

CodePudding user response:

You can do:

const playRandom = ((list) => () => list[Math.random() * list.length | 0].play())
([redSong1, redSong2])
    
playRandom();
  • Related