# Error playing SinOsc objects from an ArrayList

**URL:** <https://discourse.processing.org/t/error-playing-sinosc-objects-from-an-arraylist/2867>\
**Category:** Coding Questions\
**Created:** [August 22, 2018, 11:31pm UTC](https://discourse.processing.org/t/error-playing-sinosc-objects-from-an-arraylist/2867 "2018-08-22T23:31:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![lanelps](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lanelps/32/1195_2.png) [@lanelps](https://discourse.processing.org/u/lanelps)\
**Post date:** [August 22, 2018, 11:31pm UTC](https://discourse.processing.org/t/error-playing-sinosc-objects-from-an-arraylist/2867/1 "2018-08-22T23:31:06Z")

</div>

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.

```auto
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);
   }
  }
}

```

---

<div class="post-metadata">

**Author:** ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)\
**Post date:** [August 22, 2018, 11:47pm UTC](https://discourse.processing.org/t/error-playing-sinosc-objects-from-an-arraylist/2867/2 "2018-08-22T23:47:38Z")

</div>

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 😃

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

EnhancedLoop7

---

<div class="post-metadata">

**Author:** ![lanelps](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lanelps/32/1195_2.png) [@lanelps](https://discourse.processing.org/u/lanelps)\
**Post date:** [August 23, 2018, 12:32am UTC](https://discourse.processing.org/t/error-playing-sinosc-objects-from-an-arraylist/2867/3 "2018-08-23T00:32:03Z")

</div>

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?

```auto
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();
    
  }
   
}

```

---

<div class="post-metadata">

**Author:** ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)\
**Post date:** [August 23, 2018, 12:41am UTC](https://discourse.processing.org/t/error-playing-sinosc-objects-from-an-arraylist/2867/4 "2018-08-23T00:41:33Z")

</div>

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

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

```

instead of:

```auto
 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 😃 You want them to finish singing or at least stop them from singing before you drop the curtain 😂

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_ 😂

Hopefully that little skit makes sense 😃

Hopefully that works? Thanks for keeping me posted,

EnhancedLoop7

---

<div class="post-metadata">

**Author:** ![lanelps](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lanelps/32/1195_2.png) [@lanelps](https://discourse.processing.org/u/lanelps)\
**Post date:** [August 23, 2018, 12:49am UTC](https://discourse.processing.org/t/error-playing-sinosc-objects-from-an-arraylist/2867/5 "2018-08-23T00:49:50Z")

</div>

Hmm, I was caught with a “Could not run the sketch” error.  
This might be to do with the library itself ☹  
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 🙂

 ![16%20PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5ce224b678646aea7a48e2a100b560aa0919b144.png)

---

<div class="post-metadata">

**Author:** ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)\
**Post date:** [August 23, 2018, 12:54am UTC](https://discourse.processing.org/t/error-playing-sinosc-objects-from-an-arraylist/2867/6 "2018-08-23T00:54:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lanelps](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lanelps/32/1195_2.png) [@lanelps](https://discourse.processing.org/u/lanelps)\
**Post date:** [August 23, 2018, 1:01am UTC](https://discourse.processing.org/t/error-playing-sinosc-objects-from-an-arraylist/2867/7 "2018-08-23T01:01:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)\
**Post date:** [August 23, 2018, 1:07am UTC](https://discourse.processing.org/t/error-playing-sinosc-objects-from-an-arraylist/2867/8 "2018-08-23T01:07:03Z")

</div>

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 😃

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

EnhancedLoop7

---

<div class="post-metadata">

**Author:** ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)\
**Post date:** [August 23, 2018, 1:27am UTC](https://discourse.processing.org/t/error-playing-sinosc-objects-from-an-arraylist/2867/9 "2018-08-23T01:27:01Z")

</div>

Maybe you can take a look at this?

> <https://stackoverflow.com/questions/34762790/failed-to-write-core-dump-minidumps-are-not-enabled-by-default-on-client-versio>

EnhancedLoop7

---

<div class="post-metadata">

**Author:** ![lanelps](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lanelps/32/1195_2.png) [@lanelps](https://discourse.processing.org/u/lanelps)\
**Post date:** [August 23, 2018, 4:24am UTC](https://discourse.processing.org/t/error-playing-sinosc-objects-from-an-arraylist/2867/10 "2018-08-23T04:24:09Z")

</div>

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! 😟

 ![20%20PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/db7a266aa2ca80071a47c6b0f63778d8c3b3dcc1.png)

```auto
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);
    
  }
     }

```

---

<div class="post-metadata">

**Author:** ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)\
**Post date:** [August 23, 2018, 7:39pm UTC](https://discourse.processing.org/t/error-playing-sinosc-objects-from-an-arraylist/2867/11 "2018-08-23T19:39:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lanelps](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lanelps/32/1195_2.png) [@lanelps](https://discourse.processing.org/u/lanelps)\
**Post date:** [August 24, 2018, 12:11am UTC](https://discourse.processing.org/t/error-playing-sinosc-objects-from-an-arraylist/2867/12 "2018-08-24T00:11:29Z")

</div>

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!
