0

I am trying to play a random amount of random audio files over PIFM using node.js

// A function that will let me call and play one random audio file. 

function audio1(){

var child_process = require("child_process") //calls child process 

//These are my 9 audio wavs that will play one after the other on pi2fm
var ran1 = child_process.execSync("sudo -E /Path/to/pi2fm audio1.wav 103.50"); 
var ran2 = child_process.execSync("sudo -E /Path/to/pi2fm audio2.wav 103.50");
var ran3 = child_process.execSync("sudo -E /Path/to/pi2fm audio3.wav 103.50");
var ran4 = child_process.execSync("sudo -E /Path/to/pi2fm audio4.wav 103.50");
var ran5 = child_process.execSync("sudo -E /Path/to/pi2fm audio5.wav 103.50");
var ran6 = child_process.execSync("sudo -E /Path/to/pi2fm audio6.wav 103.50");
var ran7 = child_process.execSync("sudo -E /Path/to/pi2fm audio7.wav 103.50");
var ran8 = child_process.execSync("sudo -E /Path/to/pi2fm audio8.wav 103.50");
var ran9 = child_process.execSync("sudo -E /Path/to/pi2fm audio9.wav 103.50");

function randomFrom(array) {return array[Math.floor(Math.random() * array.length)];} // this creates a function (randomFrom()) that selects a random variable in array 

var ranAudio = [ran1, ran2, ran3, ran4, ran5, ran6, ran7, ran8 ,ran9]; //this is my array of variable containg execution of audio on Pi2fm


randomFrom(ranAudio).call(); // feeds ranAudio to randomFrom() 

}

//calls audio1();
audio1();

with the above code the files are playing one after another (I don't want this). And then when it gets to randomFrom() where I would expect one of the files to play randomly I am getting the following error:

randomFrom(ranAudio).call();
                     ^

TypeError: randomFrom(...).call is not a function

To clarify, I just want a random amount of audio files (1- 9) to play, however, so far I can not get even one random file to play.

Any suggestions, appreciated etc.

1 Answer 1

0

This should do it:

// A function that will let me call and play one random audio file. 


function audio1(){


var child_process = require("child_process") //calls child process 

//variables ran1 - ran7 are each a path to Pi2fm + audiofile + frequency
var ran1 = ("sudo -E /Path/to/pi2fm audio1.wav 103.50"); 
var ran2 = ("sudo -E /Path/to/pi2fm audio2.wav 103.50");
var ran3 = ("sudo -E /Path/to/pi2fm audio3.wav 103.50");
var ran4 = ("sudo -E /Path/to/pi2fm audio4.wav 103.50");
var ran5 = ("sudo -E /Path/to/pi2fm audio5.wav 103.50");
var ran6 = ("sudo -E /Path/to/pi2fm audio6.wav 103.50");
var ran7 = ("sudo -E /Path/to/pi2fm audio7.wav 103.50");
var ran8 = ("sudo -E /Path/to/pi2fm audio8.wav 103.50");
var ran9 = ("sudo -E /Path/to/pi2fm audio9.wav 103.50");


// puts all audio files into an array
var randomAudio  = [ran1, ran2, ran3, ran4, ran5, ran6, ran7];

//creates a variable ranNUM  with a random integer
var ranNum  = Math.floor(Math.random() * 8);
console.log(ranNum);//prints that number just so I can test

var x = 1; //for use in loop

while(x <=  ranNum){ //while x is less than the random Number.

var audioRan = randomAudio[Math.floor(Math.random() * randomAudio.length)];//select a random audio file
var play = child_process.execSync(audioRan);//play that random audio file using child_process.exexSync
x++; //incremenet x by 1 each loop

}

}

audio1();//call function 
1
  • 1
    Please accept your own answer with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year.
    – Ingo
    Feb 21, 2020 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.