Error playing SinOsc objects from an ArrayList

I’m trying to create an ArrayList of SinOsc objects (Which works), but then the usual functions to play and stop sound aren’t recognised?
The end goal is to be able to click to create a new object and hit a key to remove it.

import processing.sound.*;
ArrayList<SinOsc> noise;

void setup(){
  noise = new ArrayList<SinOsc>();
  
  size(600,600);
}


void draw(){
  background(255);
}

void mousePressed () {
  noise.add(new SinOsc(this));
  noise.play(random(100,300));
}

void keyPressed () {
  if (key == 'q') {
   for (int i = noise.size()-1; i> -1; i--){
    noise.stop(i);
    noise.remove(i);
   }
  }
}

I honestly don’t have too much experience with using sounds like this, but I can see that with the play() and stop() functions you are trying to apply them to the ArrayList instead of the sounds inside them. Try this instead:

noise.get(0).play(random(100,300)); //or any index

instead of:

noise.play(random(100,300));

Do the same change here as well:
noise.stop(i);

Make sure you get the SinOsc object inside your arraylist instead of the whole arraylist, and that should be fine :smiley:

Hopefully that helps, if you have more questions I would be glad to help!

EnhancedLoop7

1 Like

Yes! that absolutely worked! I see how I was looking at it before, trying to play the whole array itself rather than just one of the objects from the array.
HOWEVER
The arraylist works perfectly, i can add new objects and remove them. Great exactly as I wanted.
My next issue is with the sound itself!
I’ve got it looping in draw where it will add a new noise object to the array, if the array gets bigger than a certain number the object at index 0 is removed, but the sound is not and will continuously add more sound to the sketch. the stop() function doesn’t seem to work here.
Thoughts?

import processing.sound.*;
ArrayList<SinOsc> noise;

void setup(){
  noise = new ArrayList<SinOsc>();
  
  size(600,600);
}


void draw(){
  background(255);
  println(noise.size());
  float freqY = map(mouseY,height,0,0,1000);
  float posX = map(mouseX,0,width,-1,1);
  noise.add(new SinOsc(this));
  noise.get(0).play(freqY,1,0,posX);
  
  if (noise.size() > 2){
    noise.remove(0);
    noise.get(0).stop();
    
  }
   
}
1 Like

Hmm, again I’m doing all this just from looking at your code, but I would personally switch your statements in that if statement:

 if (noise.size() > 2){
    noise.get(0).stop();
    noise.remove(0);
  }

instead of:

 if (noise.size() > 2){
    noise.remove(0);
    noise.get(0).stop();
    
  }

Maybe you can try stopping it first before removing it. I imagine it like a singer performing on stage :smiley: You want them to finish singing or at least stop them from singing before you drop the curtain :joy:

And in technical terms, I mean, look at it like this:
“noise” arraylist looks like: [sound 1, sound 2]
you remove the sound at index 0
“noise” arraylist now looks like: [sound 2]
you try to stop the sound at index 0
Now you’re stopping sound 2
sound1: still playing but doesn’t exist anymore technically
Program: doesn’t know what to do anymore :joy:

Hopefully that little skit makes sense :smiley:

Hopefully that works? Thanks for keeping me posted,

EnhancedLoop7

2 Likes

Hmm, I was caught with a “Could not run the sketch” error.
This might be to do with the library itself :frowning:
What happened was it play the number of sound objects that I stated, 2 in this case.
No continuous adding of more sound or anything, but the frequency of the sounds wasn’t responding with the mapped out mouseY variable.

Also thank you very much EnhancedLoop7 for your help :slight_smile:

1 Like

It’s not a problem, and yes maybe it could be an issue with the library, I did read somewhere here that it was getting some maintenance, maybe you can take a look at the minim library that came with the older versions of processing? It is still there, maybe that will work out?

Also, did you try out my suggestion? Maybe switching the statements might help out.

Thanks again for keeping me posted,

EnhancedLoop7

1 Like

Oh yes sorry, that wasn’t very clear! I got the error message AFTER trying out your suggestion.
Definitely going to have a look into other libraries such as minim.
Also going through examples of sound sketches in processings Java Examples.

Oh okay okay, well I have your example up on my computer and I’ll be looking at it for you and I’ll keep you posted on if there is anything else I find :smiley:

Till then, I agree with you on trying out a different library, and such!

EnhancedLoop7

1 Like

Maybe you can take a look at this?

EnhancedLoop7

1 Like

What I got from that was mainly that I should make sure my Java is up to date?
Not exactly sure what he was talking about as a whole, bit too advanced talk for me.

But I went ahead and did exactly that, and yeah it turns out my Java was in need of an update (I’m running macOS). One other thing I noticed is that the processing sound library had an update I needed to install, so I’ve gone and done that also.

What I see happening now is that it will create one instance of an object and just play the one sound which is the first noise object it adds to the array, but it is responding to the changing mouse height and so the frequency follows. Then as the code runs and it reaches that object number limit for the array, the sound just completely stops.

It’s not what I expect to happen. What I expect to happen is that it basically should create a trail of sounds at certain frequencies based off of the mouseY position, and then the end of the line fades as the objects are removed from the array. basically following the mouse as it moves.

There’s also a new error, which I have no clue as to what is happening! :worried:

import processing.sound.*;
ArrayList<SinOsc> noise;

void setup(){
  noise = new ArrayList<SinOsc>();
  
  size(600,600);
}


void draw(){
  background(255);
  println(noise.size());
  float freqY = map(mouseY,height,0,0,1000);
  float posX = map(mouseX,0,width,-1,1);
  noise.add(new SinOsc(this));
  noise.get(0).play(freqY,1,0,posX);
  
  if (noise.size() > 10){
    noise.get(0).stop();
    noise.remove(0);
    
  }
     }

Hey there, sorry for the delayed response,

I tried out the code and got the same result. I am going to go as far to say that there is probably still some issues with the library? I don’t know necessarily what else you can change about your code. Did you try out minim?

EnhancedLoop7

1 Like

No worries mate. Appreciate your efforts! It’s been a fun little exercise.
I’ll give minim a go, hopefully it’s not too much trouble for me to learn.

I might also try others ways using the sound library and post here later on, cheers!

1 Like