// mouse sampler: a simple minim synthesizer // Adam Parrish import ddf.minim.*; import ddf.minim.signals.*; import ddf.minim.effects.*; AudioOutput out; MouseSampler synth; // MouseSampler class defined below LowPassSP lowpass; float[] samples; // used by MouseSampler object // change these to change the resolution of the sampler int sampleCount = 16; int vertRes = 16; float freq = 220; // frequency for oscillator float step = 110; // step to change when changing frequency void setup() { size(512, 512); Minim.start(this); samples = new float[sampleCount]; out = Minim.getLineOut(Minim.STEREO, 512); synth = new MouseSampler(freq, 0.5, 44100); lowpass = new LowPassSP(200, 44100); // lowpass to make it a bit more pleasant... out.addSignal(synth); out.addEffect(lowpass); } void draw() { background(40); // draw the current samples stroke(255); for (int i = 0; i < samples.length; i++) { pushMatrix(); translate(i * (width/sampleCount), height/2); rect(0, 0, width/sampleCount, height * samples[i]); popMatrix(); } // overwrite samples when mouse is pressed if (mousePressed) { if (mouseX > 0 && mouseX < width) { int index = (int)(((float)mouseX/width) * sampleCount); int val = (int)(((float)mouseY/height) * vertRes); samples[index] = (val / (float)vertRes) - 0.5f; } } } // press 'm' to mute/unmute void keyPressed() { if (key == 'm') { if (out.isMuted()) { out.unmute(); } else { out.mute(); } } if (keyCode == UP) { freq += step; } else if (keyCode == DOWN) { freq -= step; } freq = constrain(freq, 110, 2420); synth.setFreq(freq); } void stop() { out.close(); super.stop(); } // MouseSampler extends Oscillator, a Minim built-in class that provides the basic // functionality (like mute(), unmute(), etc.). The only method you need to override is // value() - see below. class MouseSampler extends Oscillator { MouseSampler(float freq, float amp, float sampleRate) { super(freq, amp, sampleRate); } // defines the sample to return for the current position in the waveform - "step" is // the position within the current period (e.g., this will range from 0 to 0.00227 for // a frequency of 440Hz) - multiply by frequency() to get the real value public float value(float step) { int index = (int)(frequency() * step * (float)sampleCount); return samples[index % sampleCount]; // wrap in case index is greater than sampleCount } }