Skip to content

Commit

Permalink
added draw wavetable example, minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisppaul committed May 26, 2023
1 parent f0d3478 commit 2161ae7
Show file tree
Hide file tree
Showing 34 changed files with 757 additions and 168 deletions.
Binary file modified lib/wellen.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void setup() {
fSampler = new Sampler();
fSampler.load(mData);
fSampler.set_loop_all();
fSampler.start();
fSampler.play();
DSP.start(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void draw() {
background(255);
DSP.draw_buffers(g, width, height);
fill(0);
float mSize = fSampler.get_data().length;
float mSize = fSampler.get_buffer().length;
mSize /= Wellen.DEFAULT_SAMPLING_RATE;
mSize *= 100.0f;
ellipse(width * 0.5f, height * 0.5f, mSize + 5, mSize + 5);
Expand All @@ -44,7 +44,7 @@ void keyReleased() {
print("+++ recorded " + mLengthRecording + " samples");
println(" or " + nf(mLengthRecordingInSeconds, 0, 2) + " sec.");
fSampler.set_loop_all();
fSampler.start();
fSampler.play();
}

void audioblock(float[] output_signal, float[] pInputSignal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ void draw() {
rect(0, 0, width, height);
/* selection */
fill(0, 31);
float x0 = map(fSampler.get_loop_in(), 0, fSampler.get_data().length, 0, width);
float x1 = map(fSampler.get_loop_out(), 0, fSampler.get_data().length, 0, width);
float x0 = map(fSampler.get_loop_in(), 0, fSampler.get_buffer().length, 0, width);
float x1 = map(fSampler.get_loop_out(), 0, fSampler.get_buffer().length, 0, width);
if (fSampler.get_loop_in() >= 0 && fSampler.get_loop_out() >= 0) {
if (fSampler.get_loop_in() < fSampler.get_loop_out()) {
noStroke();
Expand All @@ -60,9 +60,9 @@ void draw() {
noFill();
stroke(0);
beginShape();
for (int i = 0; i < fSampler.get_data().length; i++) {
float x = map(i, 0, fSampler.get_data().length, 0, width);
float y = map(fSampler.get_data()[i], -1.0f, 1.0f, 0, height);
for (int i = 0; i < fSampler.get_buffer().length; i++) {
float x = map(i, 0, fSampler.get_buffer().length, 0, width);
float y = map(fSampler.get_buffer()[i], -1.0f, 1.0f, 0, height);
vertex(x, y);
}
endShape();
Expand All @@ -73,7 +73,7 @@ void draw() {
}

void mousePressed() {
fSampler.start();
fSampler.play();
fSampler.rewind();
fSampler.enable_loop(true);
}
Expand All @@ -91,7 +91,7 @@ void keyPressed() {
fSampler.set_loop_out_normalized(map(mouseX, BORDER, width - BORDER, 0, 1));
break;
case 'z':
int[] mLoopPoints = Wellen.find_zero_crossings(fSampler.get_data(),
int[] mLoopPoints = Wellen.find_zero_crossings(fSampler.get_buffer(),
fSampler.get_loop_in(),
fSampler.get_loop_out());
if (mLoopPoints[0] > 0 && mLoopPoints[1] > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ void draw() {
line(0, height * 0.5f, width, height * 0.5f);
/* draw samples */
beginShape();
for (int i = 0; i < mSampler.get_data().length; i += 128) {
float x = map(i, 0, mSampler.get_data().length, 0, width);
float y = map(mSampler.get_data()[i], -1.0f, 1.0f, 0, height);
for (int i = 0; i < mSampler.get_buffer().length; i += 128) {
float x = map(i, 0, mSampler.get_buffer().length, 0, width);
float y = map(mSampler.get_buffer()[i], -1.0f, 1.0f, 0, height);
vertex(x, y);
}
endShape();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import wellen.*;
import wellen.dsp.*;

/*
* this example demonstrate how to use a vocoder with SAM. see the vocoder example for further explanation.
*/

Vocoder mVocoder;

Wavetable mVocoderCarrierOsc;

SAM mSAM;

void settings() {
size(640, 480);
}

void setup() {
Wellen.dumpAudioInputAndOutputDevices();
mVocoderCarrierOsc = new Wavetable();
Wavetable.fill(mVocoderCarrierOsc.get_wavetable(), Wellen.WAVEFORM_SAWTOOTH);
mVocoderCarrierOsc.set_frequency(55);
mVocoderCarrierOsc.set_amplitude(1.0f);
mVocoder = new Vocoder(24, 4, Wellen.DEFAULT_SAMPLING_RATE, 1);
mVocoder.set_volume(8);
mSAM = new SAM();
DSP.start(this, 1);
Beat.start(this, 140);
}

void draw() {
background(255);
stroke(0);
final int mBufferSize = DSP.get_buffer_size();
DSP.draw_buffers(g, width, height);
}

void beat(int beatCount) {
mSAM.say("hello");
}

void mouseMoved() {
mVocoder.set_formant_shift(map(mouseX, 0, width, 0.25f, 2.5f));
mVocoder.set_reaction_time(map(mouseY, 0, height, 0.002f, 0.1f));
}

void keyPressed() {
if (key == '1') {
mVocoderCarrierOsc.set_frequency(27.5f);
}
if (key == '2') {
mVocoderCarrierOsc.set_frequency(55.0f);
}
if (key == '3') {
mVocoderCarrierOsc.set_frequency(110.0f);
}
if (key == '4') {
mVocoderCarrierOsc.set_frequency(220.0f);
}
}

void audioblock(float[] output_signal) {
for (int i = 0; i < output_signal.length; i++) {
float mCarrier = mVocoderCarrierOsc.output();
float mModulator = mSAM.output() * 0.5f;
output_signal[i] = mVocoder.process(mCarrier, mModulator);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import wellen.*;
import wellen.dsp.*;

/*
* this example demonstrates how to draw data directly into a *wavetable* buffer. it is a nice way to explore how
* slight changes in the wavetable can change the characterics of the sound ( e.g create a race engine sound ).
*
* move mouse to change amplitude and frequency. press and move mouse to draw into wavetable. press `1` to set
* wavetable to sine shape, `2` to reset wavetable, `3` to disable interpolation and `4` to enable linear
* interpolation.
*/

Wavetable fWavetable;

final int fWavetableSize = 128;

void settings() {
size(640, 480);
}

void setup() {
fWavetable = new Wavetable(fWavetableSize);
fWavetable.set_interpolation(Wellen.WAVESHAPE_INTERPOLATE_LINEAR);
/* similar to `Wavetable.sine(fWavetable.get_wavetable())` */
for (int i = 0; i < fWavetable.get_wavetable().length; i++) {
final float r = TWO_PI * (float) i / fWavetable.get_wavetable().length;
fWavetable.get_wavetable()[i] = sin(r);
}
DSP.start(this);
}

void draw() {
background(255);
noStroke();
fill(32);
for (int i = 0; i < fWavetable.get_wavetable().length; i++) {
float x0 = map(i, 0, fWavetable.get_wavetable().length, 0, width);
float y0 = map(fWavetable.get_wavetable()[i], -1, 1, 0, height);
float x1 = map(i + 1, 0, fWavetable.get_wavetable().length, 0, width);
float y1 = height * 0.5f;
rectMode(CORNERS);
rect(x0, y0, x1, y1);
}
stroke(0);
DSP.draw_buffers(g, width, height);
}

void mouseMoved() {
final float mNewFrequency = map(mouseX, 0, width, 1, 110);
final float mNewAmplitude = map(mouseY, 0, height, 0, 1);
fWavetable.set_frequency(mNewFrequency, Wellen.millis_to_samples(100));
fWavetable.set_amplitude(mNewAmplitude, Wellen.millis_to_samples(10));
}

void mouseDragged() {
int i = (int) map(mouseX, 0, width, 0, fWavetable.get_wavetable().length);
i = constrain(i, 0, fWavetable.get_wavetable().length - 1);
fWavetable.get_wavetable()[i] = map(mouseY, 0, height, -1, 1);
}

void keyPressed() {
switch (key) {
case '1':
Wavetable.sine(fWavetable.get_wavetable());
break;
case '2':
Arrays.fill(fWavetable.get_wavetable(), 0.0f);
break;
case '3':
fWavetable.set_interpolation(Wellen.WAVESHAPE_INTERPOLATE_NONE);
break;
case '4':
fWavetable.set_interpolation(Wellen.WAVESHAPE_INTERPOLATE_LINEAR);
break;
}
}

void audioblock(float[] output_signal) {
for (int i = 0; i < output_signal.length; i++) {
output_signal[i] = fWavetable.output();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ void draw() {
ellipse(width * 0.5f, height * 0.5f, mScale, mScale);
}

void midi_control_change(int channel, int number, int value) {
}

void midi_note_off(int channel, int pitch) {
Tone.instrument(channel);
mNote = pitch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ void setup() {
/* import samples from WAV file */
float[][] mImportSamples = Wellen.importWAV(this, WAV_FILE_NAME);
fSampler = new Sampler();
fSampler.set_data(mImportSamples[0]);
fSampler.set_buffer(mImportSamples[0]);
fSampler.set_loop_all();
DSP.start(this);
}

void draw() {
background(255);
stroke(0);
Wellen.draw_buffer(g, width, height, fSampler.get_data());
Wellen.draw_buffer(g, width, height, fSampler.get_buffer());
DSP.draw_buffers(g, width, height);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import wellen.*;
import wellen.dsp.*;

/*
* this example demonstrates how to send + receive MIDI events.
*/

MidiOut mMidiOut;

MidiIn mMidiIn;

int mNote = 0;

int mVelocity = 0;

void settings() {
size(640, 480);
}

void setup() {
Wellen.dumpMidiInputDevices();
mMidiIn = new MidiIn("Arturia KeyStep 37");
mMidiIn.addListener(new MIDIInput());
mMidiOut = new MidiOut("Arturia KeyStep 37");
}

void draw() {
background(255);
noStroke();
fill(map(mVelocity, 0, 127, 255, 0));
float mScale = Tone.is_playing() ? map(mNote, 24, 96, 5, height * 0.8f) : 5;
ellipse(width * 0.5f, height * 0.5f, mScale, mScale);
}

void mousePressed() {
mMidiOut.sendNoteOn(10, 48, 100);
}

class MIDIInput implements MidiInListener {

void receiveProgramChange(int channel, int number, int value) {
}

void receiveControlChange(int channel, int number, int value) {
}

void clock_tick() {
}

void clock_start() {
}

void clock_continue() {
}

void clock_stop() {
}

void clock_song_position_pointer(int pOffset16th) {
}

void receiveNoteOff(int channel, int pitch) {
Tone.instrument(channel);
mNote = pitch;
mVelocity = 0;
Tone.note_off(mNote);
}

void receiveNoteOn(int channel, int pitch, int velocity) {
Tone.instrument(channel);
mNote = pitch;
mVelocity = velocity;
Tone.note_on(mNote, mVelocity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void setup() {
fSampler = new Sampler();
fSampler.load(mData);
fSampler.set_loop_all();
fSampler.start();
fSampler.play();
fPitchShifter = new PitchShifter();
fPitchShifter.Init(Wellen.DEFAULT_SAMPLING_RATE);
DSP.start(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import wellen.*;
import wellen.dsp.*;


final int NUM_OF_CONTROLLERS = 1;
final int NUM_OF_CONTROLLERS = 2;

final ArrayList<CircleController> mControllers = new ArrayList<CircleController>();

Expand Down Expand Up @@ -88,13 +88,15 @@ class CircleController {
float radius = 100.0f;
float speed = 3.0f;

final Sampler mSampler;
final Sampler fSampler;
CircleController() {
byte[] mData = SampleDataSNARE.data;
mSampler = new Sampler();
mSampler.load(mData);
mSampler.set_loop_all();
mSampler.set_speed(1);
fSampler = new Sampler();
fSampler.load(mData);
fSampler.set_loop_all();
fSampler.set_speed(1);
fSampler.enable_loop(true);
fSampler.play();
}
void draw() {
noFill();
Expand All @@ -105,13 +107,13 @@ final Sampler mSampler;
ellipse(pointer.x, pointer.y, 10, 10);
}
float process() {
return mSampler.output();
return fSampler.output();
}
void update(float pDelta) {
counter += pDelta * speed;
pointer.x = sin(counter) * radius + position.x;
pointer.y = cos(counter) * radius + position.y;
mSampler.set_speed(map(pointer.x, 0, width, 0, 32));
mSampler.set_amplitude(map(pointer.y, 0, height, 0.0f, 0.9f));
fSampler.set_speed(map(pointer.x, 0, width, 0, 32));
fSampler.set_amplitude(map(pointer.y, 0, height, 0.0f, 0.9f));
}
}
Loading

0 comments on commit 2161ae7

Please sign in to comment.