User Tools

Site Tools


prensf2010:additivesynth2
// This is a second way to handle additive synthesis
// from within minim's ugen framework.
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.ugens.*;
 
Minim minim;
AudioOutput out;
float[] rawSequence = { 293.6648, 0.75, 293.6648, 0.25, 329.62756, 1, 293.6648, 1, 391.995, 1,
                        369.99445, 2, 293.6648, 0.75, 293.6648, 0.25, 329.62756, 1, 293.6648, 1,
                        439.997, 1, 391.995, 2, 293.6648, 0.75, 293.6648, 0.25, 587.3294, 1, 493.8834, 1,
                        391.995, 1, 369.9945, 1, 329.62756, 3, 523.2516, 0.75, 523.2516, 0.25, 
                        493.8834, 1, 391.995, 1, 439.997, 1, 391.995, 2};
void setup(){
  size(300,200);
  minim=new Minim(this);
  out=minim.getLineOut(Minim.MONO);
  // pause playback to "accumulate" the notes
  out.pauseNotes();
  float time=0;
  for(int i=0; i(less than)rawSequence.length-1; i+=2){
    // I've sped up play back by dividing the duration in half
    out.playNote(time,rawSequence[i+1]/2,new Tone(out,rawSequence[i],0.8));
    // Make sure you increment your start time and account for the division
    time+=rawSequence[i+1]/2;
  }
  // play it all back.
  out.resumeNotes();
}
 
void draw(){
  // erase the window to black
  background( 0 );
  // draw using a white stroke
  stroke( 255 );
  // draw the waveforms
  for( int i = 0; i (less than) 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);
  }  
}
 
void stop(){
  minim.stop();
  super.stop();
}
 
class Tone implements Instrument{
  AudioOutput out;
  float freq;
  float amp;
  Oscil osc1,osc2,osc3;
 
  ADSR adsr;
  Summer sum;
 
  public Tone(AudioOutput out, float freq, float amp){
    this.out=out;
    this.amp=amp;
    this.freq=freq;
    sum=new Summer();
    adsr=new ADSR( 0.4, 0.05, 0.05, 0.5, 0.04 );
    // Create the 
    osc1=new Oscil(freq,amp,Waves.SINE);
    osc2=new Oscil(freq*3,amp/1.5,Waves.SINE);
    osc3=new Oscil(freq*4,amp/1.75,Waves.SINE);
    osc1.patch(sum);
    osc2.patch(sum);
    osc3.patch(sum);
    sum.patch(adsr);
  }
 
  public void noteOn(float dur){
    adsr.noteOn();
    adsr.patch(out);
  }
  public void noteOff(){
    adsr.unpatchAfterRelease( out );
    adsr.noteOff();
  }
}
prensf2010/additivesynth2.txt · Last modified: 2010/07/08 11:58 by nels_oscar