• fullscreen
  • generativeComposition.pde
  • instrument.pde
  • import ddf.minim.*;
    import ddf.minim.ugens.*;
    
    ////////////////////////////////////
    String notesLow[], notesHigh[];
    
    int pitchLow = 1;
    int pitchHigh = 3;
    
    float[][] vals;
    int nn = 30;
    
    String notes[]= {
      "A","F","E","D","C"
    };
    
    int period = 120;
    ////////////////////////////////////
    
    
    
    float amp = 0.2;
    int num = 4;
    float dur = 20;
    
    Minim minim;
    AudioOutput out;
    Waveform disWave;
    
    
    ////////////////////////////////////
    
    
    
    void setup()
    {
      size( 600, 320, P2D );
      
      textFont(loadFont("53Maya-8.vlw"));
      textMode(SCREEN);
    
    
      makeScale();
    
      // initialize the minim and out objects
      minim = new Minim( this );
      out = minim.getLineOut( Minim.STEREO, 1024 );
    
      vals = new float[nn][out.bufferSize()];
    
      colorMode(HSB);
    
      background(0);
        addNotesNow();
    }
    
    ////////////////////////////////////
    
    
    void makeScale() {
    
      if (random(50)<40) {
        disWave = Waves.SINE;
      }
      else {
        disWave = Waves.TRIANGLE;
      }
    
      notesLow = new String[notes.length];
      notesHigh = new String[notes.length];
    
      for (int i = 0 ; i < notes.length;i++) {
        notesLow[i] = notes[i]+""+pitchLow;
        notesHigh[i] = notes[i]+""+pitchHigh;
      }
    }
    
    ////////////////////////////////////
    
    
    void draw() {
      noStroke();
      fill(noise(frameCount/200.0)*255, 25, 25, random(25, 75));
      rect(0, 0, width, height);
    
    
      if (frameCount%period==0) {
        addNotesNow();
      }
    
    ////////////////////////////////////
    
    
      // 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
    
    
        int cnt = 0;
    
        for (int j = 2 ; j < nn;j++) {
    
          if (j==2) {
            resetMatrix();
    
            translate(map(i, 0, out.bufferSize(), 0, width), height/2);
            rotate(radians(frameCount*8.0));
          }
    
    
          for (int z = 200 ; z < 10000;z+=2000) {
    
            vals[j][i] += ((out.left.get(i)*noise((i+j+frameCount)/30.0)*z)-vals[j][i])/(float)(j+z/18.0+2.0);
    
            translate(0, vals[j][i]);
            rotate(i/(out.bufferSize()+0.0));
    
    
            stroke(noise(0, (frameCount+i+j+z)/200.0)*255, 35, 200, 15);
            //  stroke( 255, 15);
    
            point(0, 0);
          }
        }
      }
    
    ////////////////////////////////////
    
      
      pushStyle();
      resetMatrix();
      strokeWeight(10);
      stroke(0);
      noFill();
      rect(0,0,width,height);
      popStyle();
      
      fill(255,50);
      textAlign(RIGHT);
      text("Random Generative Composition by Kof "+nf(month(),2)+"/"+nf(day(),2)+"/"+year(),width-10,height-8);
    }
    
    void keyPressed() {
    
      addNotesNow();
    }
    
    void addNotesNow() {
      
        pitchLow = (int)random(1, 4);
        pitchHigh = (int)random(5, 7);
    
        makeScale();
      
      for (int i = 0 ; i < num ; i++ ) {
        out.playNote( i*(dur/(num+0.0)), (dur/(num+0.0)+random(0.0, 1.0)), new ToneInstrument( notesLow[(int)random(notesLow.length)]+" ", amp, disWave, out ) );
        out.playNote( i*(dur/(num+0.0)), (dur/(num+0.0)+random(0.0, 1.0)), new ToneInstrument( notesHigh[(int)random(notesHigh.length)]+" ", amp, disWave, out ) );
      }
    }
    
    // stop is run when the user presses stop
    void stop()
    {
      // close the AudioOutput
      out.close();
      // stop the minim object
      minim.stop();
      // stop the processing object
      super.stop();
    }
    
    
    // Every instrument must implement the Instrument interface so 
    // playNote() can call the instrument's methods.
    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( 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

    tweaks (0)

    about this sketch

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

    license

    advertisement

    Kryštof Pešek (Kof)

    Melancholic Variations

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

    Minim-BETA Unit Generators (UGens) are wonderful code to play with. (thanks to Damien Di Fede)

    This is a pseudo-random melancholy generator, better speakers or headphones are well-advised, enjoy.

    Ale
    21 Aug 2012
    Full of poetry and quite inspiring. :-)
    Barrie Hemming
    25 Aug 2012
    Another great looking sketch
    Thanks much. This is a strange study, and it is probably the sound, which although is constructed logical way, but enforce to pass through completely different perception channels. Coding for a joy of melancholy from this bare understanding of all surroundings simply makes me feel somehow happy.
    claire kang
    25 Nov 2013
    so gorgeous
    You need to login/register to comment.