Skip to content

Instantly share code, notes, and snippets.

@memish
Created September 29, 2018 18:40
Show Gist options
  • Select an option

  • Save memish/77180d916888aed613aeca1a7a67112d to your computer and use it in GitHub Desktop.

Select an option

Save memish/77180d916888aed613aeca1a7a67112d to your computer and use it in GitHub Desktop.
/**
* Processing Sound Library, Example 1
*
* Five sine waves are layered to construct a cluster of frequencies.
* This method is called additive synthesis. Use the mouse position
* inside the display window to detune the cluster.
*/
import processing.sound.*;
import ddf.minim.analysis.*;
import ddf.minim.*;
SinOsc[] sineWaves; // Array of sines
float[] sineFreq; // Array of frequencies
int numSines = 5; // Number of oscillators to use
void setup() {
size(640, 360);
background(255);
sineWaves = new SinOsc[numSines]; // Initialize the oscillators
sineFreq = new float[numSines]; // Initialize array for Frequencies
for (int i = 0; i < numSines; i++) {
// Calculate the amplitude for each oscillator
float sineVolume = (1.0 / numSines) / (i + 1);
// Create the oscillators
sineWaves[i] = new SinOsc(this);
// Start Oscillators
sineWaves[i].play();
// Set the amplitudes for all oscillators
sineWaves[i].amp(sineVolume);
}
}
void draw() {
//Map mouseY from 0 to 1
float yoffset = map(mouseY, 0, height, 0, 1);
//Map mouseY logarithmically to 150 - 1150 to create a base frequency range
float frequency = pow(1000, yoffset) + 150;
//Use mouseX mapped from -0.5 to 0.5 as a detune argument
float detune = map(mouseX, 0, width, -0.5, 0.5);
for (int i = 0; i < numSines; i++) {
sineFreq[i] = frequency * (i + 1 * detune);
// Set the frequencies for all oscillators
sineWaves[i].freq(sineFreq[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment