We are about to switch to a new forum software. Until then we have removed the registration on this forum.
It looks like p5.Score requires you to pass a specified number of parts to its constructor:
var score = new p5.Score(a, b, c, a, b, c)
But I want to do something like this:
var arr = [];
var array_of_parts = fill_array_with_parts(arr);
**var score = new p5.Score(array_of_parts)**;
score.start();
Where the parts are played in order that they appear in the array. Any ideas?
Answers
Nope... it says: "p5.Part: One or multiple parts, to be played in sequence."
That is, we can pass as many p5.Part arguments as we want to!
However, if those p5.Part objects are stored within an array, we can call apply() in order to pass them as parts[0], parts[1], etc.: https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
const score = new p5.Score.apply(null, myParts);
Warning: Not tested! ~:>
Yeah I tried that but got this error:
"Uncaught TypeError: function apply() { [native code] } is not a constructor"
Think I may just have to extend the constructor to take an array as an argument
You could for - loop over the array and pass only 1 or 5 each time (the last bits you need to do manually though)
Seems like the operator
new
doesn't work straight w/ apply(): #-ohttp://StackOverflow.com/questions/1606797/use-of-apply-with-new-operator-is-this-possible
If I understood correctly the 1st answers from StackOverflow, a possible solution is to call apply() over Function.bind. We also need to concat()
[null]
as the 1st element of the passed array argument:const score = new ( Function.bind.apply(p5.Score, [null].concat(myParts)) );
Of course life would be easier if you manage to convince "p5.Sound" library's developers to include an overloaded constructor for p5.Score class, so it accepts an array parameter too: :P
https://GitHub.com/processing/p5.js-sound/issues
If you do so, don't forget to mention this forum thread there so they have a better idea why you'd want the extra feature. :-\"
Thanks for the clever solution!