Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

How can I loop Sin0sc-generated audio?

$
0
0

I'm a beginner in Processing and I'm using the sound library for the first time. I have a sketch that has multiple dots flashing on and off at different frequencies, and I want a note to play every time a dot "flashes on". Right now I'm using draw() and frameCount() to control the visual frequencies of the dots. However, when I added the sound code in, after a certain number of iterations I get this error message: libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: No free ids Could not run the sketch (Target VM failed to initialize).

So I'm probably running out of memory, but I don't know how. I want to have a bunch of different notes playing at different times and frequencies, but I'm not sure how to do that without taking advantage of the draw() loop and/or arrays. Any suggestions?

I've tried to use the loop() function but I haven't got it to work with synthesized sound, just sound files. I might have just gotten the syntax wrong though.

Here's my draw() function for you to look at. I've got the visual stuff down, it's just the sound part that's tripping me up. I'm probably missing a much less convoluted solution to this, so any help would be appreciated.

void draw() {
  //assigns beats to ellipses
  int counter = 0;
  for (int i = 0; i < ellipse_counter; i++) {
    int position2 = position_values[counter];
    posX[position2] = defaultX[i];
    posY[position2] = defaultY[i];
    counter++;
  }


  //fade-out, executing at all times
  for (int i = 0; i < ellipse_counter; i++) {
    fill(0, 10);
    ellipse(defaultX[i], defaultY[i], 30, 30);
  }
  for (int i = 0; i < ellipse_counter; i++) {
  }

  //re-draw white ellipse, execute if enough time has passed
  for (int i = 0; i < array_counter; i++) {
    if (frameCount % travel_beat[i] == 0) {
      if (what_color[i] == 1) {
        fill(200, 80, 100);
      } else {
        fill(255);
      }
      if (posX[i] != 0 && posY[i] != 0) {
        ellipse(posX[i], posY[i], 30, 30);
        //sound: play note when re-drawing ellipse. This is the part that is weird.
        SinOsc osc;
        Env envelope;
        osc = new SinOsc(this);
        envelope = new Env(this);
        int note = int(map(i, 0, 18, 0, 7));
        float sustain = (travel_beat[i] / 60.0) * 0.5;
        if (travel_beat[i] >= 120) {
          sustain = 1;
        }
        float amp = 0.1 / ellipse_counter;
        osc.play(scale[note], 0.1);
        envelope.play(osc, 0.02, sustain, amp, sustain - 0.02);
      }
    }
  }
}

If anything needs to be clarified, I'd be happy to do so.


Viewing all articles
Browse latest Browse all 2896

Trending Articles