processing / processing-sound

Audio library for Processing built with JSyn
https://processing.org/reference/libraries/sound/
GNU Lesser General Public License v2.1
149 stars 50 forks source link

stuttering / noise when using amp(float volume) continously #98

Open trackme518 opened 11 months ago

trackme518 commented 11 months ago

Hi, I am getting noticeable glitches / noise when using amp(float volume) continuously. Example with custom Envelope function - code below. Tested on Linux Mint.

import processing.sound.*;
AmpplitudeEnvelope env;
Track track;

void setup() {
  frameRate(1000);
  JSONObject json = new JSONObject();
  json.setFloat("attack", 0.8);
  json.setFloat("sustain", 0.9);
  json.setFloat("release", 1.2);
  json.setFloat("level", 0.8);

  track = new Track( new SoundFile(this, "zvonkohra.wav") );
  track.sample.loop();

  env = new AmpplitudeEnvelope(json);
}

void draw() {
  env.update();
}

void mousePressed() {
  env.reset();
}

class Track {
  SoundFile sample;
  Track(SoundFile fil) {
    sample =  fil;
  }
}
//custom envelopes class - i want to operate on existing samples + let them play
class AmpplitudeEnvelope {
  float attack; //in seconds
  float sustain;
  float release;
  float level;//between 0-1
  Track audiotrack = null; //reference to existing track with SoundFile object
  long started = 0;//when we start attack
  int state = 0;//0=attack,1=sustain,2=release,3=ended
  float amplitude = 0.0;

  AmpplitudeEnvelope(JSONObject json) {
    this.attack = json.getFloat("attack");
    this.sustain = json.getFloat("sustain");
    this.release = json.getFloat("release");
    this.level = json.getFloat("level");
    this.audiotrack = track;

  }

  void reset() {
    started = millis();
    amplitude = 0.0; //start silent
    println("envelope triggered");
  }

  void update() {

    float delta = (millis()-started)/1000.0;
    //println(delta);
    if ( delta<attack) { //attack
      state = 0;
      amplitude = map( delta, 0, attack, 0, level);//where we are at current curve for volume change
    } else if ( delta>attack && delta<attack+sustain) { //sustain
      state = 1;
      amplitude = level;
    } else if ( delta>attack+sustain && delta<attack+sustain+release ) { //release
      state = 2;
      amplitude = map( (delta-attack-sustain), 0, release, level, 0);//where we are at current curve for volume change
    } else if ( delta>attack+sustain+release ) {
      state = 3; //finished
      amplitude = 0.0;
    }
    //----
    if (this.audiotrack!=null) {
      if ( this.audiotrack.sample != null) {
        this.audiotrack.sample.amp(amplitude);
        //println(this.amplitude);
      }
    }
    //----
  }
}
kevinstadler commented 10 months ago

I wonder if this is issue is specific to Linux Mint? I just tried your sketch on MacOS and it sounds perfectly fine. I think it should also be possible to call the built-in Env envelope functionality on a looping SoundFile, I wonder if that would make a difference for performance/stuttering.