-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added Wavetable + example, cleaned up
- Loading branch information
1 parent
74114e4
commit 80cd092
Showing
20 changed files
with
949 additions
and
340 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
processing-library/ton/examples_ext/ExampleDSP05Wavetable/ExampleDSP05Wavetable.pde
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import de.hfkbremen.ton.*; | ||
import controlP5.*; | ||
import ddf.minim.*; | ||
import com.jsyn.unitgen.*; | ||
|
||
|
||
final Wavetable mWavetable = new Wavetable(1024); | ||
void settings() { | ||
size(640, 480); | ||
} | ||
void setup() { | ||
DSP.dumpAudioDevices(); | ||
DSP.start(this); | ||
triangle(mWavetable.wavetable()); | ||
} | ||
void draw() { | ||
background(255); | ||
DSP.draw_buffer(g, width, height); | ||
} | ||
void mouseMoved() { | ||
mWavetable.set_frequency(map(mouseX, 0, width, 55, 220)); | ||
mWavetable.set_amplitude(map(mouseY, 0, height, 0.0f, 0.9f)); | ||
} | ||
void keyPressed() { | ||
switch (key) { | ||
case '1': | ||
sine(mWavetable.wavetable()); | ||
break; | ||
case '2': | ||
sawtooth(mWavetable.wavetable()); | ||
break; | ||
case '3': | ||
triangle(mWavetable.wavetable()); | ||
break; | ||
case '4': | ||
square(mWavetable.wavetable()); | ||
break; | ||
case '5': | ||
randomize(mWavetable.wavetable()); | ||
break; | ||
} | ||
} | ||
void audioblock(float[] pOutputSamples) { | ||
for (int i = 0; i < pOutputSamples.length; i++) { | ||
pOutputSamples[i] = mWavetable.process(); | ||
} | ||
} | ||
void randomize(float[] pWavetable) { | ||
for (int i = 0; i < pWavetable.length; i++) { | ||
pWavetable[i] = random(-1, 1); | ||
} | ||
} | ||
static void sine(float[] pWavetable) { | ||
for (int i = 0; i < pWavetable.length; i++) { | ||
pWavetable[i] = PApplet.sin(2.0f * PI * ((float) i / (float) (pWavetable.length))); | ||
} | ||
} | ||
static void sawtooth(float[] pWavetable) { | ||
for (int i = 0; i < pWavetable.length; i++) { | ||
pWavetable[i] = 2.0f * ((float) i / (float) (pWavetable.length - 1)) - 1.0f; | ||
} | ||
} | ||
static void triangle(float[] pWavetable) { | ||
final int q = pWavetable.length / 4; | ||
final float qf = pWavetable.length * 0.25f; | ||
for (int i = 0; i < q; i++) { | ||
pWavetable[i] = i / qf; | ||
pWavetable[i + (q * 1)] = (qf - i) / qf; | ||
pWavetable[i + (q * 2)] = -i / qf; | ||
pWavetable[i + (q * 3)] = -(qf - i) / qf; | ||
} | ||
} | ||
static void square(float[] pWavetable) { | ||
for (int i = 0; i < pWavetable.length / 2; i++) { | ||
pWavetable[i] = 1.0f; | ||
pWavetable[i + pWavetable.length / 2] = -1.0f; | ||
} | ||
} | ||
static class Wavetable { | ||
final float[] mWavetable; | ||
float mFrequency; | ||
float mStepSize; | ||
float mArrayPtr; | ||
float mAmplitude; | ||
Wavetable(int pWavetableSize) { | ||
mWavetable = new float[pWavetableSize]; | ||
mArrayPtr = 0; | ||
mAmplitude = 0.75f; | ||
set_frequency(220); | ||
} | ||
void set_frequency(float pFrequency) { | ||
if (mFrequency != pFrequency) { | ||
mFrequency = pFrequency; | ||
mStepSize = mFrequency * ((float) mWavetable.length / (float) DSP.DEFAULT_SAMPLING_RATE); | ||
} | ||
} | ||
void set_amplitude(float pAmplitude) { | ||
mAmplitude = pAmplitude; | ||
} | ||
float[] wavetable() { | ||
return mWavetable; | ||
} | ||
float process() { | ||
mArrayPtr += mStepSize; | ||
final int i = (int) mArrayPtr; | ||
final float mFrac = mArrayPtr - i; | ||
final int j = i % mWavetable.length; | ||
mArrayPtr = j + mFrac; | ||
return mWavetable[j] * mAmplitude; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.