We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey, I'm working on a new piece where a random number generator is connected to sound files of a lady I know screaming the numbers. So when the generator hits 1 it plays her screaming 1 and so on. I'm having trouble because I want the code to be in "void draw", however the audio files play over each other when I place it into that section.
I imagine that I need a piece of code to tell the next audio file to wait until the end of the last before it plays but I am an absolute beginner at coding, so I have no idea how to write it.
I have attached what I have done so far for reference, there is only one audio file in so far as I felt it was best to start simply and then add more audio files as I go.
import ddf.minim.*;
Minim house;
AudioPlayer player1;
int christine;
void setup(){
size(100,100);
}
void draw(){
house = new Minim(this);
player1 = house.loadFile("u.wav");
christine = (int)random(10);
println("christine is " + christine);
player1.play();
player1.isPlaying();
for (int =
}
Answers
.isPlaying() tells you when its's over
your goal is to wait that long and then make the new random number and start a new song
you can fill an array in setup() and use it in draw()
it's better to post code here as text (and not as an image)
there is a sticky post here that explain how it's done
Hey @Chrisir,
Thanks for that, I tried my best to edit the code so that it was posted properly, I don't think I got it quite right though.
As for the .isPlaying(), where should it tell me that it is over? I thought that it should return with a true or a false. So my idea was to use a conditional, if(), to say that if it was playing then it should pause the random number. However, as soon found out the if() only responds to integers. Sorry for being so dense but do you reckon you could help a little more?
AudioPlayer[] players = new AudioPlayer[2];
int idx;
Minim m = new Minim(this);
players[0] = m.loadFile("u.wav");
players[1] = m.loadFile("v.wav");
players[0].play();
if (!players[idx].isPlaying()) players[idx = (idx + 1) % players.length].play();
That's incorrect!
if ()
expects 1boolean
as the result of the check expression: :-BAnd we use the logical NOT operator
!
in order to negate the evaluated expression: ~O)https://Processing.org/reference/logicalNOT.html
Ok thanks, I think that should set me on my way! Just one last question, if I want the program to loop then should I place a loop() into the draw? Will it then run and restart once it has played all the files?
OK, I have tried what you said but it comes up with the error, "players cannot be resolved to a variable". Any ideas? Have attached the code.
no.........
draw() runs on and on anyway......
with gotoloops solution he is starting of with the first song again when the last song is over....
this is because he used % here:
this is the same as
Variable players needs to be "global". So both setup() & draw() can "see" it.
OK thank you. Now when I play the code, the sound files play after one another, but are not connected to a random number generator and do not loop. I need it so that they are both connected to the random number generator (as they were in my first attached code), play sequentially and will loop. How would this be achieved? I tried to add an if() to say that if the random number generator hit 1 then it should play players[1] but that did not work.
Problem is I don't understand what the values from the random() mean.
For example, what should happen if we got a
5
? What's the value range?Also how many times another value is picked? Or is it once only within setup()?
Should a
5
return then the soundfile of her screaming 'five' would play. Would you mind explaining what you mean by value range? The values should be picked continuously from playing the code, which makes me feel like it should be in draw().The sequence of values available to pick up. For example from
0
to9
we got 10 diff. values.By that I suppose there are more than 2 sound files for your sketch, right?
That's what I'm having problem to understand: