User Tools

Site Tools


prensf2010:additivesynth
Ess Minim
/**
 * Additive Synthesizer
 * 
 * Sound is generated at setup with a sine wave generator. Insert your own array of notes/durations
 * as 'rawSequence' and let it roll.
 *
 * Modified 10/28/2007 by spc 
 * Based on example Synthesizer 2 by R. Luke DuBois
 */
 
import krister.Ess.*;
import java.util.ArrayList;
 
AudioChannel myChannel; // Create channel
SineWave myWave,        // Create sine waveform
         myWaveHarmonic2,
         myWaveHarmonic3;
 
// Music notation - one possibility
int noteDuration = 300; // default note Duration in milliseconds
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}; // Happy birthday
 
void setup() {
  size(100, 100);
  Ess.start(this); // Start Ess
 
  // Create a new AudioChannel
  myChannel = new AudioChannel(); 
  myChannel.initChannel(myChannel.frames(rawSequence.length * noteDuration));
 
  // Create sine wave
  myWave = new SineWave(480, 0.35); 
  myWaveHarmonic2 = new SineWave(840, 0.15);
  myWaveHarmonic3 = new SineWave(1680, 0.15);
 
  // Store notes in AudioChannel
  int time = 0;
  for (int i = 0; i < rawSequence.length; i+=2) {
    myWave.frequency = rawSequence[i];            // Update waveform frequency + harmonics
    myWaveHarmonic2.frequency = rawSequence[i] * 2;
    myWaveHarmonic3.frequency = rawSequence[i] * 3;
    int begin = myChannel.frames(time);           // Starting position within Channel
    int e = int(noteDuration * rawSequence[i+1]); // duration of this note
    int end = myChannel.frames(e);                // Ending position within Channel
    println("("+begin+", "+end+")");
    myWave.generate(myChannel, begin, end);       // Render sine wave + harmonics
    myWaveHarmonic2.generate(myChannel, Ess.ADD, begin, end);
    myWaveHarmonic3.generate(myChannel, Ess.ADD, begin, end);
    time += noteDuration;                         // Increment the Channel output point
  }
 
  // Play the sound!
  myChannel.play(); 
}
 
void draw() { } // Empty draw() keeps the program running
 
void stop() {
  Ess.stop(); // When program stops, stop Ess too
  super.stop();
}
// Audio sythesis example with minim.
// This is a version using the CURENT BETA of Minim
// The beta is NOT the version included with Processing1.0
 
import ddf.minim.*;
import ddf.minim.ugens.*;
import ddf.minim.signals.*;
 
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(){
  minim=new Minim(this);
  // Get the line out that will handle your notes. 
  out=minim.getLineOut(Minim.MONO);
  // Set the start time of each note
  float time=0;
  // and its duration
  float dur=0.3;
  // pause playback on the output line, this will 
  // "store" the notes to the playback buffer.
  out.pauseNotes();
  for(int i=0; i<rawSequence.length; i++){
 
    float freq=rawSequence[i];
    float freq2=freq*2;
    float freq3=freq*3;
 
    out.playNote(time,dur,new Sine(freq,out));  
    out.playNote(time,dur,new Sine(freq2,out));
    out.playNote(time,dur,new Sine(freq3,out));
 
    // Make sure you increment your start time
    time+=dur;
  }
  // play it all back.
  out.resumeNotes();
}
 
void draw(){}
 
void stop(){
  minim.stop();
  super.stop();
}
 
class Sine implements Instrument{
    Oscil osc;
    AudioOutput output;
    Sine(float freq, AudioOutput out){
      output=out;
 
      osc=new Oscil(freq,0.28,Waves.SINE);
    }
    void noteOn(float dur){
      osc.patch(output);
    }
    void noteOff(){
      osc.unpatch(output);
    }
}
prensf2010/additivesynth.txt · Last modified: 2010/07/02 10:12 by nels_oscar