• fullscreen
  • classMover.pde
  • classToneInstrument.pde
  • classToneInstrument2.pde
  • classWaveShaperInstrument.pde
  • generativeComposition2.pde
  • makeSomeNoise.pde
  • class Mover 
    {
      PVector location;
      PVector direction;
      PVector target;
      float speed, SPEED;
    
      float topSpeed;
    
      float d;
    
      Mover ()
      {
        location = new PVector(random(width), random(height));
        target = new PVector(random(width), random(height));
        float angle = random (TWO_PI);
        direction = new PVector (cos (angle), sin (angle));
    
    
        d = random (5, 20);
        speed = map (d, 5, 20, 5, 1);
        SPEED = speed;
    
        topSpeed = 7;
      }
      
      void setLocation (float x, float y)
      {
        location.x = x;
        location.y = y;
      }
    
    
      void setTarget (PVector newTarget)
      {
        if (target != null)
        {
          target.x = newTarget.x;
          target.y = newTarget.y;
        } else target = new PVector (newTarget.x, newTarget.y);
        //target.x = constrain (target.x, d/2, width-d/2);
        //target.y = constrain (target.y, d/2, height-d/2);
      }
    
      void move ()
      {
    
        if (target != null) move ( target); else location.add (getVelocity ());
    
        if (speed > SPEED) speed *= 0.97; else if (speed < SPEED) speed *= 1.01;
      }
    
      void move (PVector force)
      {
        float maxDis = dist (0, 0, width, height);
        float dis = dist (force.x, force.y, location.x, location.y);
        float angle = atan2 (force.y - location.y, force.x - location.x);
    
        float forceStrength = map (dis, 0, maxDis, 0.5, 0.1);
    
    
        PVector acceleration = new PVector (cos(angle)*forceStrength, sin (angle)*forceStrength);   
    
        forceStrength *= map (d, 5, 20, 3, 0.2);
    
        speed += forceStrength;
    
        float slowDownarea = map (d, 5, 20, 80, 50);
        if (dis < slowDownarea)
        {
          speed *= 0.85;
          acceleration.normalize();
        }
    
        speed = constrain (speed, 1, topSpeed);
    
        direction.add (acceleration);
        direction.normalize();
        location.add (getVelocity ());
    
        checkTargetReached();
      }
    
      void checkTargetReached ()
      {
        float currentDistanceToTarget = dist(location.x, location.y, target.x, target.y);
        if (currentDistanceToTarget < speed || currentDistanceToTarget < d/2)
        {
          target = null;
        }
      }
    
      PVector getVelocity ()
      {
        PVector velocity = new PVector (direction.x * speed, direction.y * speed);
        return velocity;
      }
    
      void checkEdges ()
      {
        if (location.x < -width/2+d/2)
        {
          location.x = -width/2+d/2;
          direction.x = direction.x * -1;
        } else if (location.x > width/2-d/2) 
        {
          location.x = width/2-d/2;
          direction.x = direction.x * -1;
        }
    
        if (location.y < -height/2 + d/2)
        {
          location.y = -height/2+d/2;
          direction.y = direction.y * -1;
        } else if (location.y > height/2-d/2) 
        {
          location.y = height/2-d/2;
          direction.y = direction.y * -1;
        }
      }
    
    
      void display ()
      {
        noStroke();
        fill (#b1c999);
        ellipse (location.x, location.y, d, d);
    
        if (target != null)
        {
          fill (#e8e2b2);
          ellipse (target.x, target.y, 3, 3);
        }
        strokeWeight (1);
        stroke (255);
        line (location.x, location.y, location.x + direction.x*d/2, location.y + direction.y*d/2);
      }
    }
    
    
    
    /* 
    Code by Kryštof Pešek (Kof) and minim
    http://openprocessing.org/user/3942/
    
    */
    
    class ToneInstrument implements Instrument
    {
      // create all variables that must be used throughout the class
      Oscil toneOsc;
      ADSR adsr;
      AudioOutput out;
      Delay myDelay1;
      Damp  damp;
      
     // BitCrush bitCrush;
      Line crushLine;
      
      // constructors for this intsrument
      ToneInstrument( String note, float amplitude, Waveform wave, AudioOutput output )
      {
        // equate class variables to constructor variables as necessary
        out = output;
        
        //bitCrush = new BitCrush(random(0,10.0));
       // crushLine = new Line(0.1, random(0.1,50.0), random(0.01,40));
       // crushLine.patch(bitCrush.bitRes);
        
        // make any calculations necessary for the new UGen objects
        // this turns a note name into a frequency
        float frequency = Frequency.ofPitch( note ).asHz();
        
        // create new instances of any UGen objects as necessary
        toneOsc = new Oscil( frequency, amplitude, wave );
        adsr = new ADSR( random (0.5, 1), random (0.8, 1), random (0.001, 0.2), random (0.7, 1.0), random (0.05, 0.15) );
        damp = new Damp( random(0.0001,5.0), random(0.001,10.0) );
        
        myDelay1 = new Delay( random(0.0,100.0), amplitude /*random(0.0,100.0)*/, true, false );
        
        
        Summer sum = new Summer();
     
         toneOsc.patch( sum ).patch(damp).patch(myDelay1);
        // patch everything together up to the final output
        toneOsc.patch( adsr );
      }
      
      // every instrument must have a noteOn( float ) method
      void noteOn( float dur )
      {
        // turn on the adsr
        adsr.noteOn();
        // patch the adsr into the output
        //adsr.patch(bitCrush);
        
        // set the damp time from the duration given to the note
        damp.setDampTimeFromDuration( dur );
        // activate the damp
        damp.activate();
        
        
        adsr.patch( out );
      }
      
      void noteOff()
      {
        // turn off the note in the adsr
        adsr.noteOff();
       // adsr.unpatchAfterRelease(bitCrush);
        // but don't unpatch until the release is through
        
        damp.unpatchAfterDamp( out );
        
        adsr.unpatchAfterRelease( out );
      }
    }
    
    /* 
    Code by Kryštof Pešek (Kof) and minim
    http://openprocessing.org/user/3942/
    
    */
    
    class ToneInstrument2 implements Instrument
    {
      // create all variables that must be used throughout the class
      Oscil toneOsc;
      ADSR adsr;
      AudioOutput out;
      Delay myDelay1;
      Damp  damp;
       
      BitCrush bitCrush;
      Line crushLine;
       
      // constructors for this intsrument
      ToneInstrument2( String note, float amplitude, Waveform wave, AudioOutput output )
      {
        // equate class variables to constructor variables as necessary
        out = output;
         
        bitCrush = new BitCrush(random(0,10.0));
        crushLine = new Line(0.1, random(0.1,50.0), random(0.01,40));
        crushLine.patch(bitCrush.bitRes);
         
        // make any calculations necessary for the new UGen objects
        // this turns a note name into a frequency
        float frequency = Frequency.ofPitch( note ).asHz();
         
        // create new instances of any UGen objects as necessary
        toneOsc = new Oscil( frequency, amplitude, wave );
        adsr = new ADSR( 1.0, 0.8, 0.01, 1.0, 0.1 );
        damp = new Damp( random(0.0001,1.0), random(0.001,2.0) );
         
        myDelay1 = new Delay( random(0.0,10.0), random(0.0,10.0), true, false );
         
         
        Summer sum = new Summer();
      
         toneOsc.patch(bitCrush).patch( sum ).patch(myDelay1).patch(damp);
        // patch everything together up to the final output
        toneOsc.patch( adsr );
      }
       
      // every instrument must have a noteOn( float ) method
      void noteOn( float dur )
      {
        // turn on the adsr
        adsr.noteOn();
        // patch the adsr into the output
        adsr.patch(bitCrush);
         
        // set the damp time from the duration given to the note
        damp.setDampTimeFromDuration( dur );
        // activate the damp
        damp.activate();
         
         
        adsr.patch( out );
      }
       
      void noteOff()
      {
        // turn off the note in the adsr
        adsr.noteOff();
       // adsr.unpatchAfterRelease(bitCrush);
        // but don't unpatch until the release is through
         
        damp.unpatchAfterDamp( out );
         
        adsr.unpatchAfterRelease( out );
      }
    }
    
    /* 
    Code by Kryštof Pešek (Kof) and minim
    http://openprocessing.org/user/3942/
    
    */
    
    class WaveShaperInstrument implements Instrument
    {
      // our tone
      Oscil sineOsc;
      // what we'll shape our oscil with
      WaveShaper shaper;
      // a line to change the amount of shaping over time
      Line shaperAmountLine;
      // and a reciprocal to change the output amplitude over time
      Reciprocal reciprocal;
      
      AudioOutput out;
    
      WaveShaperInstrument(float frequency, float amplitude, AudioOutput output)
      {
        out = output;
        sineOsc = new Oscil(frequency, amplitude, Waves.SAW);
        // We've created three different waves to shape the sine with.  Just uncomment
        // one of the "shaper =" lines to hear the different waves.
        // The first is a modified saw wave.  We made this while we were experimenting
        // with the WaveShaper and liked it, so it remains.
        Wavetable shapeA = new Wavetable( Waves.TRIANGLE );
        shapeA.set(0, -1.0);
        shapeA.set(shapeA.size()-1, 1.0);
        // The second argument in WaveShaper
        // is the amount of shaping to be applied, which in our case doesn't 
        // really matter because we are going to drive that with a Line.
        shaper = new WaveShaper(amplitude, 5, shapeA);
        
        // If we want to shape the sine with a saw wave... 
        //shaper = new WaveShaper( amplitude, 5, Waves.SAW );
        
        // We can choose to wrap around the ends of the waveshaping map for interesting
        // effects, and one does this by setting the fourth argument to true.
        //shaper = new WaveShaper( amplitude, 5, Waves.SAW, true );
        
        shaperAmountLine = new Line(5.f, 1.f, 25.f);
        reciprocal = new Reciprocal();
       
        // patch the line into the mapAmplitude of the WaveShaper
        shaperAmountLine.patch( shaper.mapAmplitude );
        // Patch the reciprocal of the line into the outAmplitude.
        // Since the line goes from 1 to 25, the reciprocal goes from 1/1 to 1/25.
        // This creates a pretty good approximation of a drum envelope.
        shaperAmountLine.patch( reciprocal ).patch( shaper.outAmplitude );
        sineOsc.patch( shaper );
      }
     
      void noteOn(float dur)
      {
        // set our line time based on duration
        shaperAmountLine.setLineTime( dur );
        shaperAmountLine.activate();
        shaper.patch( out );
      }
      
      void noteOff()
      {
        shaper.unpatch( out );
      }
    }
    
    /* OpenProcessing Tweak of *@*http://www.openprocessing.org/sketch/67904*@* */
    
    /*----------------------------------
     
     Copyright by Diana Lange 2014
     Don't use without any permission
     
     mail: kontakt@diana-lange.de
     web: diana-lange.de
     facebook: https://www.facebook.com/DianaLangeDesign
     flickr: http://www.flickr.com/photos/dianalange/collections/
     tumblr: http://dianalange.tumblr.com/
     twitter: http://twitter.com/DianaOnTheRoad
     
     Based on "Melodic Variations" by Kof:
     http://openprocessing.org/sketch/67904
     
     -----------------------------------*/
    
    import ddf.minim.*;
    import ddf.minim.ugens.*;
    import ddf.minim.effects.*; // for BandPass
    
    String [] notesLow, notesHigh, notesMid;
    
    int pitchLow = 1;
    int pitchMid = 3;
    int pitchHigh = 6;
    
    float[][] vals;
    int nn = 10;
    
    String [] notes;
    
    int delayCounter = 0;
    
    
    float amp = 0.1;
    int num = 10;
    float dur = 10;
    
    Minim minim;
    AudioOutput out;
    Waveform disWave;
    
    int cnt = 0;
    int melody = 0;
    
    int skip = 4;
    
    ArrayList <Mover> m;
    
    boolean doCopy = true;
    
    void setup()
    {
      size( 600, 600);
      smooth();
    
      frameRate (30);
    
    
      // initialize the minim and out objects
      minim = new Minim( this );
      out = minim.getLineOut( Minim.STEREO, 1024 );
      vals = new float[nn][out.bufferSize()];
    
      colorMode (HSB, 360, 100, 100);
    
      background(47, 82, 24);
      initMover (out.bufferSize());
      addNotesNow();
      makeScale();
    }
    
    
    
    void draw() 
    {
      if (!doCopy) 
      {
        fill (47, 82, 24, 20);
        noStroke();
        rect (0,0,width, height);
      }
    
      translate (width/2, height/2);
      rotate (radians (frameCount % 360)*0.25);
    
      /* make music ----------------------- */
      if (delayCounter > 10 && out.mix.level() < 0.04) 
      {
        delayCounter = 0;
        addNotesNow();
      }
      delayCounter++;
    
      /* show music ----------------------- */
    
      float x, y, r;
      float a = TWO_PI / out.bufferSize();
    
      float basicR = map (out.mix.level(), 0, 0.2, 50, 150);
      float amplitude = map (out.mix.level(), 0, 0.2, 50, 150);
    
      for (int i = 0; i < out.bufferSize(); i++)
      {
        if (frameCount % 4 == 0)
        {
          r = basicR+out.mix.get(i)*amplitude;
          x = cos (a * i) * r;
          y = sin (a * i) * r;
    
          m.get (i).setTarget (new PVector (x, y));
        }
    
        m.get(i).move(); 
      }
    
      if (out.mix.level() >= 0.2)
      {
    
        fill (47, 13, 90, map (out.mix.level(), 0.2, 0.3, 80, 180));
        noStroke();
    
        float d = map (out.mix.level(), 0.2, 0.3, skip, 10);
    
        int steps = (int) map (out.mix.level(), 0.2, 0.3, 5, 11);
    
        for (int i = 0; i < out.bufferSize(); i+=steps)
        {
          r = basicR+out.mix.get(i)*amplitude + 20;
          x = cos (a * i) * r;
          y = sin (a * i) * r;
    
          ellipse ( m.get (i).location.x, m.get (i).location.y, d, d);
        }
      }
      else 
      {
        stroke(47, 13, 90, 120);
        strokeWeight (0.75);
        fill (47, 82, 24, 20);
        beginShape();
    
        for (int i = 0; i < out.bufferSize(); i+=skip)
        {
          r = basicR+out.mix.get(i)*amplitude;
          x = cos (a * i) * r;
          y = sin (a * i) * r;
    
          vertex ( m.get (i).location.x, m.get (i).location.y);
        }
        endShape(CLOSE);
      }
    
      if (doCopy) copy (1, 1, width, height, -1, -1, width+3, height+3);
    }
    
    void keyPressed ()
    {
      if (key == 's') saveFrame ("export/" + timestamp() + ".png");
      else doCopy = !doCopy;
    }
    
    
    void initMover (int n)
    {
      float x, y;
      float a = TWO_PI / n;
      m = new ArrayList();
      for (int i = 0; i < n; i++) 
      {
        x = cos (a * i) * 200;
        y = sin (a * i) * 200;
        m.add (new Mover());
        m.get (m.size()-1).setLocation (x, y);
      }
    }
    
    String timestamp ()
    {
      String s = "";
      s += nf (year(), 4);
      s += nf (month(), 2);
      s += nf (day(), 2);
      s += "_";
      s += nf (hour(), 2);
      s += nf (minute(), 2);
      s += nf (second(), 2);
    
      return s;
    }
    
    
    void addNotesNow() 
    {
      setNotes();
      makeScale();
    
      pitchLow = (int)random(1, 3);
      pitchMid = (int)random(3, 6);
    
      pitchHigh = (int)random(6, 8);
      
      float dice = (int) random (10);
      
      if (dice < 30) dur = 2;
      else if (dice >= 30 && dice < 40) dur = 3;
      else if (dice >= 40 && dice < 50) dur = 4;
      else if (dice >= 50 && dice < 70) dur = 5;
      else if (dice >= 70 && dice < 75) dur = 6;
      else if (dice >= 75 && dice < 80) dur = 7;
      else if (dice >= 80 && dice < 85) dur = 9;
      else if (dice >= 85 && dice < 95) dur = 10;
      else dur = 15;
    
     // dur = (int) random (1, 10);
      out.setVolume(random (-1, 1));
      num = (int) random (10);
    
    
      float [] rDur = { 
        random (0.5, 1), random (0.5, 1), random (0.5, 1)
      };
    
      for (int i = 0 ; i < notes.length ; i++ ) 
      {
        dice = (int) random (3);
        
        if (dice == 0)
        {
         out.playNote( i*(dur*rDur [0]/(notes.length+0.0))+random(-1.0, 1.0), (dur*rDur [0]/(notes.length+0.0)+random(0.0, 1.0)), new ToneInstrument( notesLow[i]+" ", amp, disWave, out ) );
        out.playNote( i*(dur*rDur [1]/(notes.length+0.0))+random(-1.0, 1.0), (dur*rDur [1]/(notes.length+0.0)+random(0.0, 1.0)), new ToneInstrument (notesMid[i]+" ", amp, disWave, out ) );
        out.playNote( i*(dur*rDur [2]/(notes.length+0.0))+random(-1.0, 1.0), (dur*rDur [1]/(notes.length+0.0)+random(0.0, 1.0)), new ToneInstrument( notesHigh[i]+" ", amp, disWave, out ) ); 
        }
        else 
        {
        out.playNote( i*(dur*rDur [0]/(notes.length+0.0))+random(-1.0, 1.0), (dur*rDur [0]/(notes.length+0.0)+random(0.0, 1.0)), new ToneInstrument2( notesLow[i]+" ", amp, disWave, out ) );
        out.playNote( i*(dur*rDur [1]/(notes.length+0.0))+random(-1.0, 1.0), (dur*rDur [1]/(notes.length+0.0)+random(0.0, 1.0)), new ToneInstrument2( notesMid[i]+" ", amp, disWave, out ) );
        out.playNote( i*(dur*rDur [2]/(notes.length+0.0))+random(-1.0, 1.0), (dur*rDur [1]/(notes.length+0.0)+random(0.0, 1.0)), new ToneInstrument2( notesHigh[i]+" ", amp, disWave, out ) );
        }
      }
    
    
      out.setDurationFactor(random (0.1, 2));
    
    
      cnt++;
    
      if (cnt%2==0)melody++;
    
      String s;
      int n1, n2, pos = melody%notes.length;
    
      char c = notes [pos].charAt (0);
    
      switch(c) 
      {
    
      case 'c':
      case 'C':
        s = "C";
        n1 = (int) random (3);
        n2 = (int) random (n1, 7);
    
        out.playNote( 1.f, 4.4f, new WaveShaperInstrument( Frequency.ofPitch(s + str (n1)).asHz(), 0.1, out ) );
        out.playNote( 1.2f, 1.2f, new WaveShaperInstrument( Frequency.ofPitch(s + str (n2)).asHz(), 0.1, out ) );
        break;
    
      case 'D':
      case 'd':
    
        s = "D";
        n1 = (int) random (3);
        n2 = (int) random (n1, 7);
    
        out.playNote( 1.f, 4.4f, new WaveShaperInstrument( Frequency.ofPitch(s + str (n1)).asHz(), 0.1, out ) );
        out.playNote( 1.2f, 1.2f, new WaveShaperInstrument( Frequency.ofPitch(s + str (n2)).asHz(), 0.1, out ) );
        break;
    
      case 'E':
      case 'e':
    
        s = "E";
        n1 = (int) random (3);
        n2 = (int) random (n1, 7);
    
        out.playNote( 1.f, 4.4f, new WaveShaperInstrument( Frequency.ofPitch(s + str (n1)).asHz(), 0.1, out ) );
        out.playNote( 1.2f, 1.2f, new WaveShaperInstrument( Frequency.ofPitch(s + str (n2)).asHz(), 0.1, out ) );
        break;
    
      case 'F':
      case 'f':
    
        s = "F";
        n1 = (int) random (3);
        n2 = (int) random (n1, 7);
    
        out.playNote( 1.f, 4.4f, new WaveShaperInstrument( Frequency.ofPitch(s + str (n1)).asHz(), 0.1, out ) );
        out.playNote( 1.2f, 1.2f, new WaveShaperInstrument( Frequency.ofPitch(s + str (n2)).asHz(), 0.1, out ) );
        break;
    
    
      case 'g':
      case 'G':
    
        s = "G";
    
        n1 = (int) random (3);
        n2 = (int) random (n1, 7);
    
        out.playNote( 1.f, 4.4f, new WaveShaperInstrument( Frequency.ofPitch(s + str (n1)).asHz(), 0.1, out ) );
        out.playNote( 1.2f, 1.2f, new WaveShaperInstrument( Frequency.ofPitch(s + str (n2)).asHz(), 0.1, out ) );
        break;
    
      case 'a':
      case 'A':
    
        s = "A";
        n1 = (int) random (3);
        n2 = (int) random (n1, 7);
    
        out.playNote( 1.f, 4.4f, new WaveShaperInstrument( Frequency.ofPitch(s + str (n1)).asHz(), 0.1, out ) );
        out.playNote( 1.2f, 1.2f, new WaveShaperInstrument( Frequency.ofPitch(s + str (n2)).asHz(), 0.1, out ) );
        break;
    
      case 'h':
      case 'H':
    
        s = "H";
        n1 = (int) random (3);
        n2 = (int) random (n1, 7);
    
        out.playNote( 1.f, 4.4f, new WaveShaperInstrument( Frequency.ofPitch(s + str (n1)).asHz(), 0.1, out ) );
        out.playNote( 1.2f, 1.2f, new WaveShaperInstrument( Frequency.ofPitch(s + str (n2)).asHz(), 0.1, out ) );
        break;
    
    
      default:
    
        s = "C";
        n1 = (int) random (3);
        n2 = (int) random (n1, 7);
    
        out.playNote( 1.f, 4.4f, new WaveShaperInstrument( Frequency.ofPitch(s + str (n1)).asHz(), 0.1, out ) );
        out.playNote( 1.2f, 1.2f, new WaveShaperInstrument( Frequency.ofPitch(s + str (n2)).asHz(), 0.1, out ) );
        break;
      }
    }
    
    void setNotes ()
    {
      noiseSeed (year() + month() + day() + hour() + minute());
      noiseDetail (10, 0.5);
      int notesLength = 1 + (int) noise (frameCount / 50) * 7;
      notes = new String [notesLength];
    
      float noiseVal;
      float noiseScale = random (20, 100);
      noiseDetail ((int) random (10), random (0.4, 0.6));
    
      for (int i = 0; i < notes.length; i++)
      {
        noiseVal = noise (frameCount /noiseScale + i / noiseScale);
        noiseVal = map (noiseVal, 0, 1, 0, notes.length);
    
        notes [i] = getNoteByValue ((int) noiseVal);
      }
    }
    
    String getNoteByValue (int val)
    {
      if (val == 0) return "C";
      else if (val == 1) return "D";
      else if (val == 2) return "E";
      else if (val == 3) return "F";
      else if (val == 4) return "G";
      else if (val == 5) return "A";
      else if (val == 6) return "H";
      else return "C";
    }
    
    void makeScale() {
    
      if (random(50)<40) {
        disWave = Waves.SINE;
      }
      else {
        disWave = Waves.TRIANGLE;
      }
    
      notesLow = new String[notes.length];
      notesMid = new String[notes.length];
    
      notesHigh = new String[notes.length];
    
      for (int i = 0 ; i < notes.length;i++) {
        notesLow[i] = notes[i]+""+pitchLow;
        notesMid[i] = notes[i]+""+pitchMid;
    
        notesHigh[i] = notes[i]+""+pitchHigh;
      }
    }
    
    void stop()
    {
    
      out.close();
      minim.stop();
      super.stop();
    }
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Diana Lange

    Noise Variations

    Add to Faves Me Likey@! 5
    You must login/register to add this sketch to your favorites.

    Tweak of Kof's Melodic Variations (http://openprocessing.org/sketch/67904).

    I don't know much about the AudioOutput class, in fact I never used it until today. So most of the code is still done by Kof (especially the Instruments).

    "Melody" is generated with noise. A lot parmeters are randomised.

    Visualization done with my Mover class (check Appetite: http://openprocessing.org/sketch/131989) and the simple Minim Waveform, just aligned on a circle.

    Controls:
    Any Key == Filter on / off

    Video:
    https://vimeo.com/87939253

    Jerome Herr
    1 Mar 2014
    Impressive!
    You need to login/register to comment.