We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I am trying to read several files using multiple audioplayers with minim library.
There are 4 .wav files corresponding to a zone, when one plays the others stop.
I am stuck with the index and would very much appreciate some help.
Thanks a lot in advance !
best, laurent
import ddf.minim.*;
Minim minim;
String [] fileNames = {"MOUSE", "WORDS", "FLOW", "VORTEX"};
int numSounds = fileNames.length-1;
int Border = 50;
/**AudioPlayer sounds1;
AudioPlayer sounds2;
AudioPlayer sounds3;
AudioPlayer sounds4;*/
AudioPlayer [] sounds = new AudioPlayer[numSounds];
int idx;
void setup () {
size(1080, 1080, P2D);
minim = new Minim (this);
/*sounds1 = minim.loadFile("MOUSE.wav");
sounds2 = minim.loadFile("WORDS.wav");
sounds3 = minim.loadFile("FLOW.wav");
sounds4 = minim.loadFile("VORTEX.wav");*/
for (byte idx = 0; idx != numSounds;
sounds[idx] = minim.loadFile (fileNames [idx++] +".wav"));
}
void draw() {
background (255);
fill(255, 0, 0);
rect(25, 25, 50, 50);
fill(0, 0, 255);
rect(125, 25, 50, 50);
}
void mouseMoved() {
if (mouseX > Border && mouseX < 350) {
sounds [(int)(idx)].play();
println("sound0:"+sounds);
} else if (mouseX < 750) {
} else {
}
if (mouseX < Border && mouseX > height-Border) {
}
}
void stop() {
for ( byte idx = 0; idx != numSounds; sounds[idx++].close() );
minim.stop();
super.stop();
}
Answers
ctrl-t in the processing editor will indent your code nicely. makes it much easier to read.
oh god...
try not to use code that you don't understand. inline post incrementing, pointless byte types, merging loop contents into the for clauses are all terrible ideas.
i was about to suggest printing out the filenames that you are actually trying to load but with this convoluted mess it's hard to add any debug.
Thank you so much koogs and sorry for my mess! I fixed the code and cleaned it a bit, but still haven't found a way to play one file per zone...
idx in line 30, where are you setting that?
Dear Koogs, thanks for your help. I fixed my sound problem with a friend. Now I try to switch from a String array to an other according to frameCount but don't manage to do so... Here below is a part of the code. Thanks alot in advance.
Dear koogs, Thanks a lot for your help as usual. Well this code bellow is working, but I am sure there is a clean way to code it incrementing an idx?! I am just a poor beginner ;))
@lolonulu - One way of refactoring your mouseMoved if statements is to use the same approach that you use in
setup
and instop
: looping. Create a function called e.g.soundSelect()
and have it rewind-and-play the selected, and pause everything else.The advantage to this approach is that, no matter how many sounds you add to your list, your sound selections "just work" -- any the sound that is playing (no matter how many new sounds there are in the list) gets paused automatically when the next sound is selected.
Thank you so much Jeremy this is excatly what I was searching for! Still my sound players are part of a larger project and I still have issue with playing sounds smoothly ! (The code is quite long sorry) if you can help me with this issue that would be fantastic! I have 5 vertical zones, I'like the sounds playing independantly... Thank you very much!
What do you mean? What, specifically, is the problem, and what are you trying to change?
Thanks for your help. Well as you see in the code I declare the sound together with the 'visitors' inside the setVisitorPos function and when I compile it sounds as if the sounds loop very fast but doesn't play smoothly... I thought it's coming from too many particles, but no. Then the problem is certainly between lines 124 and 141 where I set the y location for my visitors interaction over the 5 blocks of text...
I solved other problem, but still the sound doesn't play smoothly at all, if you have a bit of time to help me it would be great ! Thank you vermuch in advance, best
Hey Jeremy, Sorry to bother again, but I can't solve this sound problem alone, it's between line 133 and line 144 where I should declare my sounds zone ! Thanks a lot in advance. best, laurent
Hello, I would like to change language in my class according to frameCount (2 min = latin, 2 min = arabic, 2min = tifinagh). How should I proceed? Of course it's urgent! Thank you very much for your help !
@lolonulu -- it sounds like you need a timer. See for example this recent thread:
https://forum.processing.org/two/discussion/comment/81162/#Comment_81162
2 minutes = 2* 60 seconds * 1000 milliseconds. So when millis() > 120000 then two minutes has passed. Although you probably want to test first with 3 seconds while you work on your code.
hi jeremydouglass, thank you very much for your help. I tried to put a timer in setup with an if / else structure and frameCount to switch between the 3 instances of my class but it didn't compile! Can we switch this way? (when I change the language in my class it works...)
dear @jeremydouglas, in the example you posted the time structure is inside the void draw can it be outside the void draw? thks a lot!!! :)
Setup is run only once at the very beginning of the sketch. So checking "millis" during setup probably won't do what you want -- the clock is at zero! You need to check during the main loop. What are you trying to time?
"I'm about to draw() -- what time is it? If it is this time, draw x. If it is this time, draw y"
Isn't that what you want to do? So put your check in draw.
Can it be outside? Yes, time checking with millis() can be done inside built-in interaction methods -- like keyPressed or mousePressed -- and inside functions or class methods that have been called from draw() (so they are still "inside draw" in a sense). You can also do timing in a separate thread if you like using thread(), or using Java threading.
Thank you very much @jeremydouglas this is very clear to me now! Since I declare different languages in my class in setup I need to create a function to switch from one to another and then declare it inside void draw? I'll post the code tomorrow to be more concrete. I want to switch language according to time...
Dear @jeremydouglas if I post the code to morrow would be able to check it out please ? Tomorrow for me is in 10 hours from now! Thks ;))
Yes @lolonulu. Look at my example again. Your suggested approach with a timer function called from draw might be used by a simple
switch
statement in draw:Thank you very much @jeremydouglas. I actualy started to write a switch case, but it doesn't compile : java.lang.RuntimeException at processing.opengl.PSurfaceJOGL$2.run(PSurfaceJOGL.java:443) at java.lang.Thread.run(Thread.java:745) Do I need to make a double array with my phrases instances in setup?!
Here is the code:
It actually does compile - you found a Runtime Exception, that means there was no compiler error.