My program slows down when executed several times - buffer problem?

Apologies for my ignorance but I am trying to run this code and I feel it is less sensitive to the mouse movement when I run it several times. Please try it and let me know. It happens when you press Silence and then Sound again. The more you do it the slower it gets.

Is this a buffer problem?

Any solutions?

Thank you!

[code below]

Answers

  • To format your code in this forum, you select your code and hit ctrl+o. Ensure there is a line above and below your code block. To edit the previous post, click on the gear icon on the top right side of your post

    Kf

  • var osc, osc2, osc3;
    var fft, x;
    var silenceButton;
    var soundButton;
    
    var count = 0;
    var count2 = 0;
    var count3 = 0;
    
    var E = 41.20;
    var G = 49.00;
    var A = 55.00;
    var B = 61.74;
    var D = 73.42;
    var E2 = 82.41;
    var G2 = 98.00;
    var A2 = 110.00;
    var B2 = 123.47;
    var D2 = 146.83;
    var E3 = 164.81;
    var G3 = 196.00;
    
    var numNotes = 11;
    
    function setup() {
      createCanvas(800, 400);
    
      //TRI OSCILLATOR
      osc = new p5.TriOsc(); // set frequency and type
      osc.amp(.3);
      fft = new p5.FFT();
      osc.freq(41.40);
      //osc.start();
    
      //SQUARE OSCILLATOR
      osc2 = new p5.SqrOsc(); // set frequency and type
      osc2.amp(.1);
      fft = new p5.FFT();
      osc2.freq(41.40);
      //osc2.start();
    
      //SIN OSCILLATOR
      osc3 = new p5.SinOsc(); // set frequency and type
      osc3.amp(.3);
      fft = new p5.FFT();
      osc3.freq(41.40);
      //osc3.start();
    }
    
    
    function draw() {
      background(255);
    
      var waveform = fft.waveform(); // analyze the waveform
      beginShape();
      strokeWeight(5);
    
      for (var i = 0; i < waveform.length; i++) {
        var x = map(i, 0, waveform.length, 0, width);
        var y = map(waveform[i], -1, 1, height, 0);
        vertex(x, y);
      }
      endShape();
    
    
      silenceButton = createButton('Silence');
      silenceButton.position(width * 0.8, height * 0.05);
      silenceButton.mousePressed(silence);
    
      soundButton = createButton('Sound');
      soundButton.position(width * 0.2, height * 0.05);
      soundButton.mousePressed(sound);
    
    
      soundButton = createButton('Sound2');
      soundButton.position(width * 0.4, height * 0.05);
      soundButton.mousePressed(sound2);
    
      soundButton = createButton('Sound3');
      soundButton.position(width * 0.6, height * 0.05);
      soundButton.mousePressed(sound3);
    
      pentatonic(mouseX);
    
    
    }
    
    // change oscillator frequency based on mouseX
    function pentatonic(pos) {
    
      if (pos > 0 && pos < width / numNotes) {
        var freq = E;
      }
      if (pos > width / numNotes && pos < 2 * (width / numNotes)) {
        var freq = G;
      }
      if (pos > 2 * (width / numNotes) && pos < 3 * (width / numNotes)) {
        var freq = A;
      }
      if (pos > 3 * (width / numNotes) && pos < 4 * (width / numNotes)) {
        var freq = B;
      }
      if (pos > 4 * (width / numNotes) && pos < 5 * (width / numNotes)) {
        var freq = D;
      }
      if (pos > 5 * (width / numNotes) && pos < 6 * (width / numNotes)) {
        var freq = E2;
      }
      if (pos > 6 * (width / numNotes) && pos < 7 * (width / numNotes)) {
        var freq = G2;
      }
      if (pos > 7 * (width / numNotes) && pos < 8 * (width / numNotes)) {
        var freq = A2;
      }
      if (pos > 8 * (width / numNotes) && pos < 9 * (width / numNotes)) {
        var freq = B2;
      }
      if (pos > 9 * (width / numNotes) && pos < 10 * (width / numNotes)) {
        var freq = D2;
      }
      if (pos > 10 * (width / numNotes) && pos < 11 * (width / numNotes)) {
        var freq = E3;
      }
    
    
      osc.freq(freq);
      osc2.freq(freq);
      osc3.freq(freq);
    
    
      var amp = map(mouseY, 0, height, 0.5, .01);
    
      osc.amp(amp);
      osc2.amp(amp - 0.05);
      osc3.amp(amp);
    }
    
    //Stop the 3 OSC
    function silence() {
      osc.stop();
      osc2.stop();
      osc3.stop();
      count = 0;
      count2 = 0;
      count3 = 0;
    }
    
    //Initiate the 3 OSC
    function sound() {
    
      if (count < 1 && count2 === 0 && count3 === 0) {
        osc.start();
      }
      count = +1;
      print(count);
    }
    
    function sound2() {
      if (count2 < 1 && count === 0 && count3 === 0) {
        osc2.start();
      }
      count2 = count2 + 1;
      print(count2);
    }
    
    function sound3() {
      if (count3 < 1 && count === 0 && count2 === 0) {
        osc3.start();
      }
      count3 = count3 + 1;
      print(count2);
    }
    
Sign In or Register to comment.