User Tools

Site Tools


cs276:addsynth
/**
 * 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();
}
cs276/addsynth.txt · Last modified: 2007/11/29 12:58 by scarl