0
votes

I'm trying to make a Processing application which, right now, will play a note according to what keys are pressed -- in the future I'll replace the keys with an Arduino integer input. When I play just one note (sine wave), it sounds fine, but when I play more than one, the the sound gets really choppy and not at all like a combination of the notes being played. How would I go about solving this issue? Also, I've found that when I release the key, the sine wave becomes centered at a different location than its original. Why would that be?

    import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.ugens.*;
import processing.serial.*;

Minim minim;
AudioOutput out;
SineWave sinea;
SineWave sineb;
SineWave sinec;

void setup()
{
  size(displayWidth, displayHeight, P3D);

  minim = new Minim(this);
  out = minim.getLineOut(Minim.MONO);
  sinea = new SineWave(440.00,1,out.sampleRate());
  sineb = new SineWave(493.88,1,out.sampleRate());
  sinec = new SineWave(523.25,1,out.sampleRate());
  sinea.portamento(200);
  sineb.portamento(200);
  sinec.portamento(200);
  out.addSignal(sinea);
  out.addSignal(sineb);
  out.addSignal(sinec);
  sinea.setFreq(0);
  sineb.setFreq(0);
  sinec.setFreq(0);
  smooth();
  stroke(255);
  strokeWeight(3);
}

void draw() {
  background(0);
  for(int i = 0; i < out.bufferSize() - 1; i++)
  {
    float x1 = map(i, 0, out.bufferSize(), 0, width);
    float x2 = map(i+1, 0, out.bufferSize(), 0, width);
    line(x1, height/2 + out.right.get(i)*100, x2, height/2 + out.mix.get(i+1)*100);
  }
}


void keyPressed() {
  switch (key) {
    case 'a':
      sinea.setFreq(440);
      break;
    case 'b':
      sineb.setFreq(493.88);
      break;
    case'c':
      sinec.setFreq(523.25);
      break;
  }
}

void keyReleased() {
  switch (key) {
    case 'a':
      sinea.setFreq(0);
      break;
    case 'b':
      sineb.setFreq(0);
      break;
    case'c':
      sinec.setFreq(0);
      break;
  }
}

void stop() {
  out.close();
  minim.stop();
  super.stop();
}
1

1 Answers

0
votes

Sadly, when I find these sort of issues and I start digging into the libraries, I find not so nice busy loops and/or sections that go for too long with interrupts disabled.

The implementation typically works in a sort of "demo" mode, but falls apart when one tries to use it for real in a more serious way.

Most likely you are victim of the same scenario.

I suppose playing 2 waves only yields somewhat intermediate results ...

The options you have are:

  • dig through the library and profile/inspect the code, to locate where most of the time is spent - might be daunting, but highly educational

  • do your own implementation - same as above, easier or harder depending on your coding skills and understanding of the problem and underlying implementation.