How can I stop Minim BitCrush creates noise out of speaker?

edited April 2018 in Library Questions

I have worked on a piece of code that generates notes and beats from tweets. And, I am struggling with a beat part where I use an example code called bitCrushBeatExample as a drum sound for this sound project. I found out that after running this for a long time, like an hour, the speaker produces noise and keeps itself open for signal. Please click here to see the video. You can hear the noise at the 10th second.

As I tried several ways to turn it off, right now the bitCrush.unpatch(out) works for me. I use bitCrush.patch(out) again when the program run into a scene to generate sound again. However, the noise from the speaker will come back and play along with the other note and beat sounds.

Here is a rewritten shorten code. This code will runs fine without noise when the note is off after the 16 sec. But, I put it here because the actual code (a very long one) is similar to this.

// bitCrushBeatExample

import ddf.minim.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*; // for BandPass

Minim minim;

Summer sum;

BitCrush bitCrush;

Line bitRateLine;

AudioOutput out;
AudioPlayer player;
AudioSample player_copy;
Timer time;

int scene=0;
int round=0;
boolean soundOn= true;
void setup()
{
  // initialize the drawing window
  size( 512, 200, P2D );

  // initialize the minim and out objects
  minim = new Minim( this );
  out = minim.getLineOut( Minim.MONO );

  player=minim.loadFile("dialupSound.mp3");
  player_copy=minim.loadSample("dialupSound.mp3");

  // make our summer and bit crush ugens
  sum = new Summer();                                                                          
  bitCrush = new BitCrush(16.f, out.sampleRate());

  // we're going to do 4 measures of 120 bpm, so that's 8 seconds.
  // we'll just ramp from half the sample rate to 100  Hz  
  bitRateLine = new Line(8.f, out.sampleRate()*0.25f, 100 );

  // connect the line to the bit crush resolution
  bitRateLine.patch( bitCrush.bitRate );
  // set up our signal chain
  sum.patch( bitCrush ).patch( out );

  // pause time when adding a bunch of notes at once
  out.pauseNotes();

  // we set the tempo of the output so that the time and duration arguments
  // of playNote now are expressed in beats
  out.setTempo( 60.f );

  println(round+"------------time is reset-----------");
  time = new Timer (24000);
  scene=1;
}

// draw is run many times
void draw()
{
  textAlign(CENTER);
  background( 0 );
  fill(255);
  switch(scene) {
  case 1: 
    text("This is scene 1", width/2, (height/2)-30);
    text("click anywhere to go to scene 2", width/2, height/2);
    break;
  case 2:
    text("This is scene 2", width/2, (height/2)-30);
    text ("play.isPlaying(), at the end will go to scene 3 then, play_loadSample", width/2, height/2);
    text("no mouse click", width/2, (height/2)+30);
    if (!player.isPlaying()) {
      if ( !soundOn) {
        bitCrush.patch(out);
        playDrum();
        soundOn=true;
      }
      player=minim.loadFile("dialupSound_effect.mp3");
      player.play();
      scene++;
      time.reset();
    }
    break;
  case 3:
    text("This is scene 3", width/2, (height/2)-30);
    text ("click here to pause/play beat", (width/2)-100, height/2);
    text ("click here to go to scene 1", (width/2)+100, height/2);
    // draw using a white stroke
    stroke( 255 );
    // draw the waveforms
    for ( int i = 0; i < out.bufferSize() - 1; i++ )
    {
      // find the x position of each buffer value
      float x1  =  map( i, 0, out.bufferSize(), 0, width );
      float x2  =  map( i+1, 0, out.bufferSize(), 0, width );
      // draw a line from one buffer position to the next for both channels
      line( x1, 50 + out.left.get(i)*50, x2, 50 + out.left.get(i+1)*50);
      line( x1, 150 + out.right.get(i)*50, x2, 150 + out.right.get(i+1)*50);
    }  
    if ( time.isDone()) {
      //out.pauseNotes();
      playDrum();
      time.reset();
      round++;
      println(round+"------------time is reset-----------");
    }
    text(time.getCurrentTime(), 20, 30);
    time.update();
    break;
  }
}


void playDrum() {
  float kickDur = 0.8;
  float snareDur = 0.2;
  out.pauseNotes();
  for (int i = 0; i < 4; i++)
  {
    out.setNoteOffset( i * 4 );
    // we set the note offset so that each loop we are queuing up a new measure
    out.playNote( 0, kickDur, new KickInstrument( sum ) );
    println("note 1");
    out.playNote( 1, snareDur, new SnareInstrument( sum ) );
    out.playNote( 1.5, kickDur, new KickInstrument( sum ) );
    println("note 2");
    out.playNote( 2.5, kickDur, new KickInstrument( sum ) );
    println("note 3");
    out.playNote( 3, snareDur, new SnareInstrument( sum ) );
    out.playNote( 3.5, snareDur, new SnareInstrument( sum ) );
    out.playNote( 2.5, kickDur, new KickInstrument( sum ) );
    println("note 4");
    // every other measure give a little kick at the end
    if ( i % 2 == 1 )
    {
      out.playNote( 3.75, 0.1, new KickInstrument( sum ) );
      println("little kick");
    }
  }

  // activate the line and unpause the output!
  bitRateLine.activate();
  out.resumeNotes();
  //out.pauseNotes();
}

void mousePressed() {
  switch(scene) {
  case 1:
    player.play();
    scene++;
    break;
  case 3:
    if (mouseX <= width/2) {
      if (soundOn) { // play and puase 
        println("pause");
        bitCrush.unpatch(out);
        soundOn=false;
      } else {
        println("play");
        time.reset();
        bitCrush.patch(out);
        playDrum();
        soundOn=true;
      }
    } else { // go back to scene 1 (home)
      println("go back");
      player=minim.loadFile("dialupSound.mp3");
      player.pause();
      player.rewind();
      bitCrush.unpatch(out);
      out.close();
      soundOn=false;
      scene=1;
      time.reset();
    }
    break;
  }
}

void stop()
{
  // always close Minim audio classes when you are done with them
  out.close();
  player.close();
  minim.stop();
  super.stop();
}

class KickInstrument implements Instrument {}
class SnareInstrument implements Instrument {}  
class Timer {}

To explain a bit more: this piece of code has 3 scenes. At the 3 scene the sound should be produced. So, when going back to scene 1 the sound should be removed. In this whole project, there are other classes as well, which are SineInstrument, Gain, but they are not listed below. The SineInstrument has no problem at this time. I use out.pauseNote(); to stop it. In all Kick, and Sine class of instrument at void notenf(){} I add the any oscill class a .reset(); into the function.

like this: void noteOn(float dur) { // patch our oscil to the summer we were given and start the line sineOsc.reset(); freqLine.activate(); sineOsc.patch(gain2).patch(out); }

Is there a way to I stop the noise when it is not playing anything!? Is it possible that if you change .setTempo() it might be the one causing this problem? (In my code, tempo has been changed over time.)

Sign In or Register to comment.