Skip to content

Commit

Permalink
updated processing library
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisppaul committed Nov 9, 2024
1 parent 07b3d6a commit b9e96f1
Show file tree
Hide file tree
Showing 22 changed files with 358 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ void draw() {
noStroke();
circle(60, 60, fSampler.is_looping() ? 50 : 10);
circle(120, 60, fSampler.interpolate_samples() ? 50 : 10);
if (!fSampler.is_playing()) {
noFill();
stroke(0);
}
circle(180, 60, 50);
}
void mousePressed() {
fSampler.rewind();
fSampler.play();
}
void mouseMoved() {
fSampler.set_speed(map(mouseX, 0, width, -4, 4));
Expand All @@ -65,6 +71,13 @@ void keyPressed() {
case 'I':
fSampler.interpolate_samples(true);
break;
case ' ':
if (fSampler.is_playing()) {
fSampler.pause();
} else {
fSampler.play();
}
break;
}
}
void audioblock(float[] output_signal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void setup() {
fSampler = new Sampler();
fSampler.load(mData);
fSampler.set_loop_all();
fSampler.play();
DSP.start(this);
background(255);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import wellen.dsp.*;
import wellen.analysis.*;
/*
* this example demonstrates how to detect a beat from an input signal.
* @TODO(the algorithm is not working particularuly fine …)
* @TODO(the algorithm is not working particularly fine …)
*/
final int[] fBassPattern = {1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1,};
boolean fBeatDetected = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import wellen.*;
import wellen.dsp.*;

/*
* this example demonstrates how to load and play a sample with Tone.
*/
Sampler mSampler;
void settings() {
size(640, 480);
}
void setup() {
mSampler = Tone.load_sample(SampleDataSNARE.data);
// alternatively load data with `loadBytes("audio.raw")` ( raw format, 32bit IEEE float )
}
void draw() {
background(255);
fill(0);
ellipse(width * 0.5f, height * 0.5f, mSampler.is_playing() ? 100 : 5, mSampler.is_playing() ? 100 : 5);
}
void keyPressed() {
if (key == 'l') {
mSampler.enable_loop(false);
}
if (key == 'L') {
mSampler.enable_loop(true);
mSampler.set_loop_all();
}
}
void mousePressed() {
mSampler.trigger();
}
void mouseReleased() {
mSampler.stop();
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void setup() {
fTracks[i] = new Track(i, TRACK_LENGTH);
/* distribute instruments across stereo field */
Tone.instrument(i).set_pan(map(i, 0, TRACKS - 1, -1, 1));
Tone.instrument(i).set_amplitude(0.5f);
}
fSelected = new StepSelected(fTracks);
if (ENABLE_PERCUSSION) {
Expand Down
Binary file modified processing-library/wellen/library/wellen.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion processing-library/wellen/src/wellen/SAM.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ public float[] say(String pText) {
@Override
public float output() {
if (!mIsDoneSpeaking && mBuffer != null && mBuffer.length > 0) {
float mSamples = mBuffer[(int) mSampleBufferCounter];
final int mSampleIndex = (int) mSampleBufferCounter > mBuffer.length - 1 ? mBuffer.length - 1 : (int) mSampleBufferCounter;
float mSamples = mBuffer[mSampleIndex];
mSampleBufferCounter += mSampleSpeed;
if (mSampleBufferCounter > mBuffer.length - 1) {
mIsDoneSpeaking = true;
Expand Down
15 changes: 15 additions & 0 deletions processing-library/wellen/src/wellen/Tone.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

package wellen;

import processing.core.PApplet;
import wellen.dsp.Sampler;

import java.lang.reflect.Constructor;
import java.util.ArrayList;

Expand Down Expand Up @@ -166,6 +169,18 @@ public static void replace_instrument(Instrument instrument) {
instance().replace_instrument(instrument);
}

public static Sampler load_sample(PApplet p, String sample_filename) {
return instance().load_sample(p, sample_filename);
}

public static Sampler load_sample(byte[] sample_buffer) {
return instance().load_sample(sample_buffer);
}

public static boolean remove_sample(Sampler sampler) {
return instance().remove_sample(sampler);
}

public static void set_engine(ToneEngine tone_engine) {
mInstance = tone_engine;
}
Expand Down
9 changes: 9 additions & 0 deletions processing-library/wellen/src/wellen/ToneEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

package wellen;

import processing.core.PApplet;
import wellen.dsp.Sampler;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
Expand Down Expand Up @@ -132,6 +135,12 @@ public float[] get_buffer_right() {
return null;
}

public abstract Sampler load_sample(PApplet p, String sampleFilename);

public abstract Sampler load_sample(byte[] sampleBuffer);

public abstract boolean remove_sample(Sampler sampler);

private class NoteOffTask extends TimerTask {

final int instrument;
Expand Down
76 changes: 58 additions & 18 deletions processing-library/wellen/src/wellen/ToneEngineDSP.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@
package wellen;

import processing.core.PApplet;
import wellen.dsp.DSPNodeOutput;
import wellen.dsp.DSPNodeOutputSignal;
import wellen.dsp.EffectStereo;
import wellen.dsp.Gain;
import wellen.dsp.Reverb;
import wellen.dsp.Signal;
import wellen.dsp.*;

import java.util.ArrayList;
import java.util.Map;

import static wellen.Wellen.NO_AUDIO_DEVICE;
import static wellen.Wellen.clamp;
Expand Down Expand Up @@ -92,18 +88,18 @@ public ToneEngineDSP() {

public static ToneEngineDSP create_without_audio_output(int number_of_instruments) {
return new ToneEngineDSP(Wellen.DEFAULT_SAMPLING_RATE,
Wellen.DEFAULT_AUDIOBLOCK_SIZE,
Wellen.NO_AUDIO_DEVICE,
2,
number_of_instruments);
Wellen.DEFAULT_AUDIOBLOCK_SIZE,
Wellen.NO_AUDIO_DEVICE,
2,
number_of_instruments);
}

public static ToneEngineDSP no_output() {
return new ToneEngineDSP(Wellen.DEFAULT_SAMPLING_RATE,
Wellen.DEFAULT_AUDIOBLOCK_SIZE,
Wellen.DEFAULT_AUDIO_DEVICE,
Wellen.NO_CHANNELS,
Wellen.DEFAULT_NUMBER_OF_INSTRUMENTS);
Wellen.DEFAULT_AUDIOBLOCK_SIZE,
Wellen.DEFAULT_AUDIO_DEVICE,
Wellen.NO_CHANNELS,
Wellen.DEFAULT_NUMBER_OF_INSTRUMENTS);
}

@Override
Expand Down Expand Up @@ -184,7 +180,7 @@ public void replace_instrument(Instrument instrument) {
fInstruments.set(instrument.ID(), (InstrumentDSP) instrument);
} else {
System.err.println("+++ WARNING @" + getClass().getSimpleName() + ".replace_instrument(Instrument) / " +
"instrument must " + "be" + " of type `InstrumentInternal`");
"instrument must " + "be" + " of type `InstrumentInternal`");
}
}

Expand All @@ -206,7 +202,7 @@ public void audioblock(float[][] output_signal, float[][] input_signal) {
audioblock(output_signal[0], output_signal[1]);
} else {
System.err.println("+++ WARNING @" + getClass().getSimpleName() + ".audioblock / multiple output " +
"channels are not " + "supported.");
"channels are not " + "supported.");
}
if (fAudioblockCallback != null) {
fAudioblockCallback.audioblock(output_signal);
Expand Down Expand Up @@ -269,8 +265,10 @@ public Signal output_signal() {
public void audioblock(float[] signal) {
for (int i = 0; i < signal.length; i++) {
signal[i] = getNextInstrumentSampleMono();
signal[i] += getNextSampleBankMono();
}


if (fReverbEnabled) {
fReverb.process(signal, signal, signal, signal);
}
Expand All @@ -282,7 +280,9 @@ public void audioblock(float[] signal) {

public void audioblock(float[] signal_left, float[] signal_right) {
for (int i = 0; i < signal_left.length; i++) {
Signal mSignalSum = getNextInstrumentSampleStereo();
final Signal mSignalSum = getNextInstrumentSampleStereo();
mSignalSum.add(getNextSampleBankStereo());

signal_left[i] = mSignalSum.left();
signal_right[i] = mSignalSum.right();
}
Expand Down Expand Up @@ -324,6 +324,25 @@ private int getInstrumentID() {
return Math.max(fCurrentInstrumentID, 0) % fInstruments.size();
}

private float getNextSampleBankMono() {
float mSignal = 0;
for (Sampler s : fSampleBank) {
mSignal += s.output();
}
mSignal = clamp(mSignal);
return mSignal;
}

private Signal getNextSampleBankStereo() {
final Signal mSignalSum = new Signal();
for (Sampler s : fSampleBank) {
final float mSignal = clamp(s.output());
mSignalSum.left_add(mSignal);
mSignalSum.right_add(mSignal);
}
return mSignalSum;
}

private float getNextInstrumentSampleMono() {
float mSignal = 0;
for (InstrumentDSP mInstrument : fInstruments) {
Expand Down Expand Up @@ -359,8 +378,29 @@ private Signal getNextInstrumentSampleStereo() {
}
return mSignalSum;
}
public interface AudioOutputCallback {

private final ArrayList<Sampler> fSampleBank = new ArrayList<>();

public interface AudioOutputCallback {
void audioblock(float[][] output_signals);
}

public Sampler load_sample(PApplet p, String sampleFilename) {
byte[] mRawSampleData = p.loadBytes(sampleFilename); // ( raw format, 32bit IEEE float )
Sampler mSampler = new Sampler();
mSampler.load(mRawSampleData);
fSampleBank.add(mSampler);
return mSampler;
}

public Sampler load_sample(byte[] sampleBuffer) {
Sampler mSampler = new Sampler();
mSampler.load(sampleBuffer);
fSampleBank.add(mSampler);
return mSampler;
}

public boolean remove_sample(Sampler sampler) {
return fSampleBank.remove(sampler);
}
}
20 changes: 20 additions & 0 deletions processing-library/wellen/src/wellen/ToneEngineMIDI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

package wellen;

import processing.core.PApplet;
import wellen.dsp.Sampler;

import java.util.ArrayList;

import static processing.core.PApplet.constrain;
Expand Down Expand Up @@ -134,6 +137,23 @@ public void replace_instrument(Instrument pInstrument) {
}
}

@Override
public Sampler load_sample(PApplet p, String sampleFilename) {
System.out.println("+++ @" + getClass().getSimpleName() + ".load_sample(PApplet, String) / not implemented");
return null;
}

@Override
public Sampler load_sample(byte[] sampleBuffer) {
System.out.println("+++ @" + getClass().getSimpleName() + ".load_sample(byte[]) / not implemented");
return null;
}

@Override
public boolean remove_sample(Sampler sampler) {
return false;
}

private void prepareExitHandler() {
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
Expand Down
20 changes: 19 additions & 1 deletion processing-library/wellen/src/wellen/ToneEngineOSC.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import netP5.NetAddress;
import oscP5.OscMessage;
import oscP5.OscP5;
import processing.core.PApplet;
import wellen.dsp.Sampler;

import java.util.ArrayList;

Expand Down Expand Up @@ -132,7 +134,23 @@ public void replace_instrument(Instrument pInstrument) {
mInstruments.set(pInstrument.ID(), (InstrumentOSC) pInstrument);
} else {
System.err.println("+++ WARNING @" + getClass().getSimpleName() + ".replace_instrument(Instrument) / " +
"instrument must be" + " of type `" + InstrumentOSC.class.getSimpleName() + "`");
"instrument must be" + " of type `" + InstrumentOSC.class.getSimpleName() + "`");
}
}

public Sampler load_sample(PApplet p, String sampleFilename) {
System.out.println("+++ WARNING @" + getClass().getSimpleName() + ".load_sample(PApplet, String) / " +
"not implemented");
return null;
}

public Sampler load_sample(byte[] sampleBuffer) {
System.out.println("+++ WARNING @" + getClass().getSimpleName() + ".load_sample(byte[]) / " +
"not implemented");
return null;
}

public boolean remove_sample(Sampler sampler) {
return false;
}
}
16 changes: 16 additions & 0 deletions processing-library/wellen/src/wellen/Wellen.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,22 @@ public static void dumpAudioInputAndOutputDevices() {
queryAudioInputAndOutputDevices(null, true, false);
}

public static boolean isMidiDeviceAvailable(String pDeviceName) {
final String[] mInputNames = MidiIn.availableInputs();
for (String mInputName : mInputNames) {
if (mInputName.equals(pDeviceName)) {
return true;
}
}
final String[] mOutputNames = MidiOut.availableOutputs();
for (String mOutputName : mOutputNames) {
if (mOutputName.equals(pDeviceName)) {
return true;
}
}
return false;
}

public static String[] dumpMidiInputDevices() {
final String[] mInputNames = MidiIn.availableInputs();
System.out.println("+-------------------------------------------------------+");
Expand Down
Loading

0 comments on commit b9e96f1

Please sign in to comment.