package org.nyu.mocap.anmotion.processing;
import processing.core.*;
import processing.xml.*;
import ddf.minim.*;
import ddf.minim.signals.*;
import javax.sound.sampled.*;
import java.applet.*;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import java.awt.Image;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;
public class GameSound15 extends PApplet {
//Chris Ball (ball2000) (cc) 2010 (Creative Commons License)
//GameSynth by Chris Ball
//0.1.5b
int b =0;
PFont fontA;
String theText = "game sounds \nby Chris Ball \nv0.1.5b \n use the \nkeyboard,y'all";
String text2 = "";
GameSynth synth = new GameSynth(this);
public void setup()
{
fontA= loadFont("CourierNew36.vlw");
size(200,200,P2D);
//synth
}
public void draw()
{
synth.update();
background(0);
stroke(color(128,256,0));
textFont(fontA,16);
text(theText+"\n\n"+text2,20,20);
}
public void keyPressed(){
theText = "game sounds \nby Chris Ball \nv0.1.5b ";
if(key==' '){
synth.jump(b=round(random(10)));
printsc("jump "+b);
}
if(key=='q'){
synth.boom(b=0);
printsc("boom "+b);
}
if(key=='a'){
synth.boom(b=round(random(10)));
printsc("boom "+b);
}
if(key=='w'){
synth.bang(b=round(random(10)));
printsc("bang "+b);
}
if(key=='s'){
synth.bump(b=round(random(10)));
printsc("bump "+b);
}
if(key=='d'){
synth.blip(b=round(random(10)));
printsc("blip "+b);
}
if(key=='r'){
synth.zapB(b=round(random(10)));
printsc("zapB "+b);
}
if(key=='f'){
synth.zap(b=round(random(10)));
printsc("zap "+b);
}
if(key=='g'){
synth.spacey(b=round(random(10)));
printsc("spacey "+b);
}
if(key=='h'){
synth.gulp(b=round(random(10)));
printsc("gulp "+b);
}
if(key=='j'){
synth.chime(b=round(random(10)));
printsc("chime "+b);
}
if(key=='k'){
synth.gong(b=round(random(10)));
printsc("gong "+b);
}
if(key=='l'){
synth.boing1(b=round(random(10)));
printsc("boing1 "+b);
}
if(key==';'){
synth.boing2(b=round(random(10)));
printsc("boing2 "+b);
}
if(key=='\''){
synth.clank(b=round(random(20)));
printsc("clank "+b);
}
if(key=='z'){
synth.boinky(b=round(random(10)));
printsc("boinky "+b);
}
if(key=='x'){
synth.car(b=round(random(10)));
printsc("car "+b);
}
if(key=='c'){
synth.weirdCar(b=round(random(20)));
printsc("weirdCar "+b);
}
if(key=='v'){
synth.bzaap(b=round(random(20)));
printsc("bzaap "+b);
} if(key=='b'){
synth.bzaap(b=2);
printsc("bzaap "+b);
} if(key=='b'){
synth.blipBzap(b=round(random(20)));
printsc("blipBzap "+b);
} if(key=='p'){
synth.blipSong(b=round(random(20)));
printsc("blipSong "+b);
} if(key=='1'){
synth.classicBlip(b=round(random(20)));
printsc("classicBlip "+b);
} if(key=='2'){
synth.classicBump(b=round(random(20)));
printsc("classicBump "+b);
} if(key=='3'){
synth.classicDown(b=round(random(20)));
printsc("classicDown "+b);
} if(key=='4'){
synth.classicUp(b=round(random(20)));
printsc("classicUp "+b);
}// classicDrop}// spaceInvaderGun
if(key=='5'){
synth.classicClik(b=round(random(20)));
printsc("classicClik "+b);
}
if(key=='6'){
synth.classicGun(b=round(random(20)));
printsc("classicGun "+b);
}
if(key=='7'){
synth.splat(b=round(random(20)));
printsc("splat "+b);
}
if(key=='8'){
synth.classicPowerUp(b=round(random(20)));
printsc("classicPowerUp "+b);
}
if(key=='9'){
synth.classicPowerDn(b=round(random(20)));
printsc("classicPowerDn "+b);
}
}
public void printsc(String thString){
text2=thString;
}
public void stop(){
synth.stop();
super.stop();
}
class CompanderFilter implements AudioEffect
{
float[] level ={0.0f,0.0f};
float[] comp={0.25f,0.25f};
float[] filt1={0.0f,0.0f};
float[] filt2={0.0f,0.0f};
float[] filt3={0.0f,0.0f};
float[] op;
public void process(float[] samp,int ch)
{
op = new float[samp.length];
int i = samp.length;
for (int j = 0; j < i; j++)
{
level[ch] = ((19000*level[ch]) +abs(samp[j]))/19001;
comp[ch] = (((0.1f+level[ch])/(0.05f+(level[ch]*2.f))))/2.f;
filt1[ch] = ((9.0f*filt1[ch])+(samp[j]))/10.f;// lo-Pass filter
filt2[ch] = ((6100.0f*filt2[ch])+filt1[ch])/6102.f;// hi-pass Filter
filt3[ch] = ((610.0f*filt3[ch])+filt2[ch])/611.f;
op[j] = (filt1[ch]+filt3[ch]-(filt2[ch])*2.f)*comp[ch]*8.0f;
}
// we have to copy the values back into samp for this to work
arraycopy(op, samp);
}
public void process(float[] mono){
// int p=0;
process(mono,0);
}
public void process(float[] left, float[] right)
{
process(left,0);
process(right,1);
}
}
public class GameSynth{
Minim minim;
AudioOutput out;
SineWave sine;
SineWave sineX;
SquareZap square;
SquareHat square2;
SquareSpike square3;
SquarePhase bzerk;
SquareBoink boink;
bitNoise bnoise;
bitNoiseBP oldnoise;
AudioSample wave;
float[] samples = new float[1024*8];
float amp = 0.f;
float freq = 100.f;
float noiseAmp = 0.f;
float pan = -0.0f;
float panner = 0.0f;
int wavecount = 0;
int synthFrame=0;
float decay = 1.1f;
float portamento = 0;float lastport=0;
CompanderFilter comp;
public GameSynth(PApplet thing){
minim = new Minim(thing);
out = minim.getLineOut(Minim.STEREO);
comp = new CompanderFilter();
bnoise = new bitNoise(180, 0.0f, 44100);
bnoise.setAmp(noiseAmp);
oldnoise = new bitNoiseBP(180, 0.0f, 44100);
oldnoise.setAmp(0.0f);
sineX = new SineWave(180, 1.0f, 44100);
sineX.generate(samples);
float ampl=0.0f;
for(int i =0;i<samples.length;i++){
if(ampl>0.f && i>20){
ampl -= 0.0003f;
}
else if(i<20){
ampl +=0.03f;
}
samples[i] = (ampl*samples[i]);
samples[i] = (ampl*samples[i]);
}
AudioFormat format = new AudioFormat( 44100, // sample rate
16, // sample size in bits
2, // channels
true, // signed
true // bigEndian
);
wave = minim.createSample( samples, // the samples
format, // the format
1024 // the output buffer size
);
//sineX= null;
sine = new SineWave(120, 0.0f, 44100);
sine.setFreq(120);
sine.portamento(20);
square = new SquareZap(120, 0.0f, 44100);
square2 = new SquareHat(120, 0.0f, 44100);
square3 = new SquareSpike(120, 0.0f, 44100);
bzerk = new SquarePhase(120, 0.0f, 44100);
boink = new SquareBoink(120, 0.0f, 44100);
square.setFreq(120);
// square.portamento(20);
square.setAmp(0.f);
square2.setAmp(0.f);
sine.setAmp(0.f);
bnoise.setAmp(0.f);
bnoise.noPortamento();
out.addSignal(sineX);
out.disableSignal(sineX);
out.addSignal(bnoise);
// out.disableSignal(bnoise);
out.addSignal(sine);
// out.disableSignal(sine);
out.addSignal(square);
out.addSignal(square2);
out.addSignal(square3);
out.addSignal(bzerk);
out.addSignal(boink);
out.addSignal(oldnoise);
// boink.setAmp(0.3);
out.addEffect(comp);
println("It always says that!! Ignore it!!");
gameSoundStartup(3);
}
public void update(){
synthFrame++;
amp/=decay;
//noiseAmp/=1.1;
sine.setFreq(freq);sine.setAmp(amp/2.f);
sine.portamento(round(lastport+10));
lastport = portamento;
portamento /=1.4f;
// bnoise.setAmp(constrain(noiseAmp,0.0,0.9));
//bnoise.setHarmonic(synthFrame%5+2);
}
public void stop(){
out.close();
wave.stop();
minim.stop();
}
public void bang(int thBoom){
bnoise.setAmp(0.4f);
bnoise.resetFreq(thBoom*555+130);
//noiseAmp=1.2-((5+thBoom)/(10+thBoom));
wave.trigger();
bnoise.setEnv(0.001f,0.4f);
bnoise.setLFO(6,0.0f);
bnoise.setFreq(thBoom*555+40);
bnoise.setFreqLFO(0,0);
bnoise.trigger();
freq=50+thBoom;amp=0.2f;decay=1.4f+(0.1f*thBoom);
}
public void boom(int thBoom){
bnoise.setAmp(0.8f);
bnoise.setFreq(thBoom*555+40);
//noiseAmp=1.2-((5+thBoom)/(10+thBoom));
wave.trigger();
bnoise.setEnv(0.001f,1.4f);
bnoise.setLFO(6,0.2f);
bnoise.setFreq(thBoom*555+40);
bnoise.setFreqLFO(0,0);
bnoise.trigger();
freq=50;amp=0.6f;decay=1.1f+(0.1f*thBoom);
}
public void bump(int thBump){
freq = (constrain(thBump*20,60,320));
amp = constrain(thBump/5.f,0.05f,0.1f);
decay = 1.7f;
wave.trigger();
}
public void blip(int thBlip){
freq = (constrain(thBlip*20+160,160,640));
amp = constrain(thBlip/5.f,0.05f,0.1f);
decay = 1.8f;
wave.trigger();
}
public void zap(int thZap){
// square.portamento(1);
square.setAmp(0.15f);
square.freqy = (constrain(thZap*100+360,360,1440));
square.amp = (constrain((thZap%3)*2.f,0.8f,0.8f));
//square.portamento(390);
//square.setFreq(80);
// decay = 1.8;
}
public void zapB(int thZap){
// square.portamento(1);
// bzerk.reset();
bzerk.setAmp(0.25f);
bzerk.SETFreq(60);
bzerk.setLFO(5+3*thZap,0.1f);
bzerk.setAmpADSR(0.0001f,0.43f,0.18f,0.3f,
0.0f,0.9f,0.01f,0.01f);
// bzerk.setFreqLFO(99,0.1,60,constrain(thZap*10+90,90,300)+400);
bzerk.setFreqADSR(0.001f,0.3f,0.01f,0.03f,300.f+(50.f*thZap),200.f+(50.f*thZap),60.f,60.f);
// bzerk.setFreqADSR(0.01,0.4,0.01,0.5,300.,250.,200.,100.);
bzerk.trigger();
//square.freqy = (constrain(thZap*100+360,360,1440));
//bzerk.amp = (constrain((thZap%3)*2.,0.8,0.8));
//square.portamento(390);
//square.setFreq(80);
// decay = 1.8;
}
public void spacey(int thSpace){
square2.SETFreq(constrain(thSpace*20+90,90,400));
square2.setLFO(0.5f+(0.3f*thSpace),0.3f+(0.1f*thSpace%0.3f));
square2.setAmp(0.05f);
square2.setFreqLFO(8,0.8f,constrain(thSpace*20+90,90,300)+100,constrain(thSpace*20+90,90,300)+600);
square2.setFreqADSR(0.01f,0.9f,0.09f,0.9f,300.f,square2.frequency()-(thSpace*20),square2.frequency()+thSpace*30,100.f);
square2.setEnv(1.0001f,2.5f);
square2.trigger();
}
public void gulp(int thSpace){
square3.SETFreq(constrain(thSpace*20+90,90,300));
// square3.setLFO(55,0.9);
square3.setAmp(0.1f);
square3.setFreqLFO(22,0.9f,constrain(thSpace*5+190,90,400)+100,constrain(thSpace*20+190,90,400)+200);
square3.setEnv(0.53f,0.001f+(0.01f*thSpace/3));
square3.trigger();
}
public void gong(int thSpace){
square3.SETFreq(constrain(thSpace*20+90,90,300));
square3.setLFO(29,0.9f);
square3.setAmp(0.15f);
square3.setFreqLFO(99,0.9f,constrain(thSpace*3+50,60,300)+30,constrain(thSpace*13+30,60,300)+100);
square3.setEnv(0.001f,0.9f);
square3.trigger();
}
public void boing1(int thSpace){
square3.SETFreq(constrain(thSpace*20+90,90,300));
square3.setLFO(229,1.4f);
square3.setAmp(0.15f);
square3.setFreqLFO(6,1.1f,constrain(thSpace*3+50,60,300)+100,constrain(thSpace*13+30,60,300)+200);
square3.setEnv(0.001f,0.9f);
square3.trigger();
}
public void boing2(int thSpace){
square3.SETFreq(constrain(thSpace*20+90,90,300));
square3.setLFO(28,0.1f);
square3.setAmp(0.15f);
square3.setFreqLFO(5,0.9f,constrain(thSpace*3+50,60,300)+100,constrain(thSpace*13+30,60,300)+200);
square3.setEnv(0.001f,0.6f);
square3.trigger();
}
public void chime(int thSpace){
square2.resetFreq(constrain(thSpace*80+90,180,600));
square2.setLFO(125,0.2f);
square2.setAmp(0.2f);
square2.setFreqLFO(500.5f,1.9f,constrain(thSpace*3+50,60,300)+100,constrain(thSpace*80+90,180,800));
square2.setEnv(0.0001f,0.25f);
square2.trigger();
}
public void gameSoundStartup(int thSpace){
square2.resetFreq(constrain(thSpace*80+90,180,600));
//square2.setLFO(125,0.2);
square2.setAmp(0.1f);
// square2.setFreqLFO(500.5,1.9,constrain(thSpace*3+50,60,300)+100,constrain(thSpace*80+90,180,800));
square2.setEnv(0.0001f,0.25f);
float[] v =
{
0,100,200,400,800,0,1600
};
square2.setBlip(v,240,1);
//square2.trigger();
}
public void clank(int thSpace){
square2.SETFreq(constrain(thSpace*70+30,90,200));
square2.setLFO(555+thSpace*49.1f,1.5f);
square2.setAmp(0.06f);
square2.setFreqLFO(1.5f,0.01f,constrain(thSpace*3+50,60,100)+100,constrain(thSpace*13+30,60,100)+200);
square2.setEnv(0.004f,0.4f);
square2.trigger();
}
public void boinky(int thSpace){
boink.SETFreq(constrain(thSpace*20+30,90,200));
boink.setLFO(111,0.1f);
boink.setAmp(0.4f);
boink.setFreqLFO(8,0.2f);
boink.setEnv(0.001f,0.4f);
boink.setFreqADSR(0.005f,0.2f,0.08f,0.9f,constrain(thSpace*14+30,90,300),
constrain(thSpace*40+30,90,600),
constrain(thSpace*11+30,60,300),constrain(thSpace*10+10,40,100));
boink.trigger();
}
public void car(int thSpace){
boink.SETFreq(constrain(thSpace*20+30,90,200));
//boink.reset();
boink.setLFO(20+10*thSpace,0.3f);
boink.setAmp(0.6f);
// boink.setFreqLFO(8,0.0);
// boink.setEnv(0.001,0.4);
boink.setFreqADSR(0.005f,0.09f,2.08f,0.5f,constrain(thSpace*8+30,90,100)*2,
constrain(thSpace*10+30,90,230)*2,
constrain(thSpace*14+30,60,350)*2,constrain(thSpace*10+10,40,80)*2);
boink.setAmpADSR(2.8f,0.3f,0.01f,7.8f,
0.00f,0.8f,0.1f,0.0f);
boink.trigger();
}
public void jump(int thSpace){
// boink.reset();
boink.SETFreq(constrain(thSpace*20+30,90,200));
boink.setLFO(0.1f,0.1f);
boink.setAmp(0.3f);
// boink.setFreqLFO(8,0.2);
boink.setEnv(0.01f,0.3f);
boink.setFreqADSR(0.25f,0.25f,0.0f,0.4f,70+(thSpace*5),150+(thSpace*10),225+(thSpace*20),300+(thSpace*40));
boink.trigger();
}
public void weirdCar(int thSpace){
bzerk.SETFreq(constrain(thSpace*20+30,90,200));
// bzerk.reset();
bzerk.setLFO(1+3.1f*thSpace,0.4f);
bzerk.setAmp(0.6f);
// boink.setFreqLFO(8,0.2);
// boink.setEnv(0.001,0.4);
bzerk.setFreqADSR(0.005f,0.09f,2.08f,0.5f,constrain(thSpace*8+30,90,200)*2,
constrain(thSpace*10+30,90,330)*2,
constrain(thSpace*14+30,60,600)*2,constrain(thSpace*10+10,40,180)*2);
bzerk.setAmpADSR(2.8f,0.3f,0.01f,7.8f,
0.00f,0.8f,0.1f,0.0f);
bzerk.trigger();
}
public void bzaap(int thSpace){
bzerk.SETFreq(constrain(thSpace*3+35,70,140));
// bzerk.reset();
bzerk.setLFO(2.f*(35.01f+3*thSpace),1.1f);
bzerk.setAmp(0.3f);
boink.setFreqLFO(8,0.2f);
// boink.setEnv(0.001,0.4);
bzerk.setFreqADSR(0.08f,0.8f,1.0f,0.51f,
constrain(thSpace*3+140,140,340)-9,
constrain(thSpace*3+140,140,340)+0.5f,
constrain(thSpace*3+140,140,340)-0.1f,
constrain(thSpace*3+140,140,340)-9
);
//constrain(thSpace*8+30,90,200)*2,
// constrain(thSpace*10+30,90,330)*2,
//constrain(thSpace*14+30,60,600)*2,constrain(thSpace*10+10,40,180)*2);
bzerk.setAmpADSR(0.06f,0.8f,1.3f,0.02f,
0.00f,0.9f,0.4f,0.9f);
bzerk.trigger();
// freq=35;
//amp=0.11;
//decay=1.1;
//wave.trigger();
}
public void blipBzap(int thSpace){
bzerk.SETFreq(constrain(thSpace*3+35,70,140));
// bzerk.reset();
bzerk.setLFO(2.f*(5.01f+1.3f*thSpace),0.2f);
bzerk.setAmp(0.7f);
bzerk.setFreqLFO(8,0.05f);
// boink.setEnv(0.001,0.4);
bzerk.setAmpADSR(0.002f,0.1f,0.01f,0.062f,
0.00f,0.4f,0.4f,0.1f);
// bzerk.trigger();
float h=(20*thSpace);
float[] v = {100+h,200+h,155+h,134+h,
100+h,200+h,155+h,134+h,
100+h/2,200+h/2,155+h/2,134+h/2,
100,200,155,134,
};
bzerk.setBlip(v,0.023f,4);
// void setBlip(float[] thblips,float thblipRate, int thblipLoops)
}
public void blipSong(int thSpace){
square3.SETFreq(constrain(thSpace*3+35,70,140));
// bzerk.reset();
square3.setLFO(8.f+(0.01f+0.2f*thSpace),0.08f);
square3.setAmp(0.4f);
square3.setFreqLFO(8,0.1f);
// boink.setEnv(0.001,0.4);
square3.setAmpADSR(0.002f,0.56f,0.2f,0.30f,
0.00f,0.4f,0.1f,0.0f);
// bzerk.trigger();
float U=1+(thSpace*0.1f);
float[] p = {100*U,100*U,150*U,150*U,
168*U,168*U,150*U,0*U,
133*U,133*U,127*U,127*U,
113*U,113*U,100*U,0*U
};
square3.setBlip(p,180,1);
// void setBlip(float[] thblips,float thblipRate, int thblipLoops)
}
public void classicBump(int thSpace){
boink.resetFreq(50+thSpace*3);
boink.setAmp(0.2f);
boink.setAmpADSR(0.0f,0.0f,0.1f,0.0f);
boink.trigger();
}
public void classicBlip(int thSpace){
square3.resetFreq(250+thSpace*30);
square3.setAmp(0.24f);
square3.setAmpADSR(0.0f,0.0f,0.05f,0.0f);
square3.trigger();
}
public void classicDown(int thSpace){
boink.resetFreq(50+thSpace*3);
boink.setAmp(0.2f);
boink.setAmpADSR(0.0f,0.0f,1.0f,0.0f);
boink.sweepDown();
boink.trigger();
}
public void classicUp(int thSpace){
boink.resetFreq(50+thSpace*3);
boink.setAmp(0.2f);
boink.setAmpADSR(0.0f,0.0f,1.0f,0.0f);
boink.sweepUp();
boink.trigger();
}
public void classicClik(int thSpace){
oldnoise.resetFreq(800+(200*thSpace));
oldnoise.setAmp(0.2f);
//oldnoise.setLFO(800,0.9);
oldnoise.setAmpADSR(0.001f,0.05f,0.0f,0.05f,
0.0f,1.0f,0.4f,0.0f
);
oldnoise.trigger();
//wave.setAmp(0.05);
//wave.trigger();
}
public void classicGun(int thSpace){
oldnoise.resetFreq(300+(5*thSpace));
oldnoise.setAmp(0.8f);
//oldnoise.setLFO(800,0.9);
oldnoise.setAmpADSR(0.001f,0.05f,0.0f,0.05f,
0.0f,1.0f,0.4f,0.0f
);
float[] v = {100+10*thSpace,100+5*thSpace};
oldnoise.setBlip(v,0.01f,1);
boink.resetFreq(150+thSpace*3);
boink.setAmp(0.15f);
boink.setAmpADSR(0.0f,0.1f,0.05f,0.1f);
boink.setFreqADSR(0.0f,0.15f,0.0f,0.2f,
850+thSpace*20,100+thSpace*10,80+thSpace*5,50+thSpace*2
);
float[] q = {130+thSpace*10,90+thSpace*5};
boink.setBlip(q,0.01f,3);
wave.trigger();
//boink.trigger();
}
public void splat(int thSpace){
oldnoise.resetFreq(40+(40*thSpace));
oldnoise.setAmp(0.2f);
//oldnoise.setLFO(800,0.9);
oldnoise.setAmpADSR(0.01f,0.02f,0.0f,0.02f);
float[] v = {200+80*thSpace,150+40*thSpace};
oldnoise.setBlip(v,0.01f,1);
bzerk.resetFreq(150+thSpace*3);
bzerk.setAmp(0.05f);
bzerk.setAmpADSR(0.0f,0.05f,0.05f,0.1f);
bzerk.setFreqADSR(0.0f,0.05f,0.0f,0.2f,
850+thSpace*20,100+thSpace*15,80+thSpace*10,50+thSpace*10
);
float[] q = {80+thSpace*5,50+thSpace*2};
bzerk.setBlip(q,0.01f,3);
}
public void classicPowerUp(int thSpace){
boink.resetFreq(250+thSpace*20);
boink.setAmp(0.1f);
boink.setAmpADSR(0.0f,0.05f,0.05f,0.1f);
//boink.setFreqADSR(0.0,0.05,0.0,0.2,
float h = PApplet.parseFloat(thSpace)/7.f +0.25f;
float[] t = {
200,230,260,400,
200,230,260,400,
200,230,260,400,
230,260,280,460,
230,260,280,460,
230,260,280,460,
260,290,310,490,
260,290,310,490,
260,290,310,490,1
};
arrayMult(t,h);
boink.setBlip(t,0.025f,1);
}
public void classicPowerDn(int thSpace){
boink.resetFreq(250+thSpace*20);
boink.setAmp(0.1f);
boink.setAmpADSR(0.0f,0.05f,0.05f,0.1f);
//boink.setFreqADSR(0.0,0.05,0.0,0.2,
float h = PApplet.parseFloat(thSpace)/7.f +0.25f;
float[] t = {
260,290,310,490,
260,290,310,490,
260,290,310,490,
230,260,280,460,
230,260,280,460,
230,260,280,460,
200,230,260,400,
200,230,260,400,
200,230,260,400,1
};
arrayMult(t,h);
boink.setBlip(t,0.025f,1);
}
}//be cool, stay in class
// arrayMult utility
public void arrayMult(float[] target,float thMult){
for(int i=0;i<target.length;i++){
target[i] = thMult*target[i];
}
}
public void arrayAdd(float[] target,float thAdd){
for(int i=0;i<target.length;i++){
target[i] = thAdd+target[i];
}
}
public class OscillatorLFO extends Oscillator{
float tremLFO = 1.0f;
float gain = 1.0f;
float tremRate = 0.00f;
float tremCount = 0.0f;
float tremDepth=0.0f;
float freqLFO = 0.0f;
float freqRate = 0.0f;
float freqCount = 0.0f;
float freqDepth=0.0f;
float val;
int trigger=0;
int cycling=0;
float attack=0.01f;
float release = 0.5f;
float att = 0.0f;
float rel = 0.5f;
float freqEnv=0.f;
boolean enveloped = false;
float detune=0.0f;
float theLast =0.0f;
float upperFreq = 200;
float lowerFreq=80;
float freqRange=120;
float[] blips;
float blipRate =0.f;
int blipLoops = 0;
int numBlips =0;
boolean blipped =false;
float blipPeriod = 0.f;
float blipCount=0.f;
int currBlip = 0;
OscillatorLFO(float frequency, float amplitude, float sampleRate){
super(frequency, amplitude, sampleRate);
this.noPortamento();
}
protected float value(float step){
return(0.0f);
}
public void updateLFO(){
this.noPortamento();
tremCount = (tremCount + tremRate)%360.f;
freqCount = (freqCount + freqRate)%360.f;
// if(tremCount>360){tremCount-=360;}
tremLFO = abs(sin(radians(tremCount))*tremDepth+1.0f)/2.0f;
// if(freqRate!=0){
freqLFO = abs(sin(radians(freqCount))*freqDepth+1.0f)/2.0f;
// *freqRange)+lowerFreq,20,100000);
// }else{freqLFO=lowerFreq;}
// setFreq(freqLFO);
// val = super.value(step);
// this.setAmp(tremLFO);
if(trigger==1){
//println("ARtrig");
cycling = 1;
att = 0;
trigger=0;
}
if(cycling ==1){
if(att<1.0f){
att += (0.0001f/attack);
rel=att;
}
else{
cycling=0;
rel=att;
}
}
else{
// rel -= (0.0001/release);
rel /= (1+(0.0001f/release)*(1+(rel/5)));
if(rel<0){
rel=0.0f;
}
}
if(enveloped){
gain = rel;
}
// if(freqRate != 0.){
this.setFreq(lowerFreq+(freqRange*freqLFO)+freqEnv);
//}
//if((gain+tremLFO)>0.9){tremLFO -= abs(tremLFO-gain);}
//if((gain+tremLFO)<0.){tremLFO += abs(gain+tremLFO);}
}//updateLFO
public void setLFO(float rate, float depth){
tremRate=rate/100;
tremDepth = depth;
}
public void setFreqLFO(float rate, float depth, float lower,float upper){
freqRate=rate/100;
freqDepth = depth;
upperFreq = upper;
lowerFreq = lower;
if(lowerFreq<40){
lowerFreq=40;
}
if(upperFreq>7000){
upperFreq=7000;
}
freqRange = upperFreq-lowerFreq;
}
public void setFreqLFO(float rate, float depth){
SETFreq(this.frequency());
freqRate=rate/100;
freqDepth = depth;
upperFreq = lowerFreq*1.65f;
lowerFreq = lowerFreq*0.95f;
freqRange = upperFreq-lowerFreq;
}
public void trigger(){
trigger = 1;
enveloped = true;
}
public void setEnv(float thAttack,float thRelease){
attack = thAttack;
release=thRelease;
enveloped=true;
}
public void SETFreq(float thFreq){
lowerFreq=thFreq;
freqRange=0;
freqDepth=0;
this.setFreq(thFreq);
}
public void resetFreq(float thFreq){
tremDepth=0.0f;
tremCount=0.0f;
tremRate=0.0f;
freqCount=0.0f;
freqDepth=0.0f;
freqRate=0.0f;
// this.stopBlip();
// ampEnveloped=false;
// freqEnveloped=false;
cycling =0;
blipped=false;
enveloped=false;
this.noPortamento();
lowerFreq=thFreq;
freqRange=0;
freqDepth=0;
this.setFreq(thFreq);
}
}//class
public class OscillatorLFOADSR extends OscillatorLFO{
float freqA = 0.0f;
float freqD = 0.0f;
float freqS = 0.0f;
float freqR=0.0f;
float fenv=100;
float freqMin=100.f;
float freqMax=200;
float freqSus=180;
float startFreq=100;
float susCount=0;
float envCount=0;
int fcycling=0;
int ftrigger=0;
boolean freqEnveloped=false;
float ampA = 0.0f;
float ampD = 0.0f;
float ampS = 0.0f;
float ampR=0.0f;
float ampEnv=100;
float ampMin=0.f;
float ampMax=1.f;
float ampSus=0.7f;
float startAmp=0.0f;
float ampSusCount=0;
float ampEnvCount=0;
int acycling=0;
int atrigger=0;
boolean ampEnveloped = false;
OscillatorLFOADSR(float frequency, float amplitude, float sampleRate){
super(frequency, amplitude, sampleRate);
this.SETFreq(frequency);
}
protected float value(float step){
return(0.0f);
}
public void updateLFO(){
blupdate();
super.updateLFO();
if(freqEnveloped){
// freqRate=0;
if(ftrigger==1){
fenv = startFreq;
// this.setFreq(fenv);
fcycling=1;
ftrigger=0;
}
if(fcycling==1){
envCount = envCount+0.00005f;
// fenv = fenv +(0.005/freqA);
fenv = fenv+((freqMax-startFreq)/(freqA/0.00005f));
if(envCount>freqA){
fenv=freqMax;
fcycling=2;
envCount=0;
}
}
if(fcycling==2){
envCount = envCount+0.00005f;
fenv = fenv+((freqSus-freqMax)/(freqD/0.00005f));
// fenv = fenv -(0.005/freqD);
if(envCount>freqD){
fenv=freqSus;
fcycling=3;
envCount=0.f;
}
}
if(fcycling==3){
susCount = susCount +0.00005f;
if(susCount>=freqS){
// //println(fenv);
// println(freqEnv);
// println(freqLFO);
// println(freqLFO+freqEnv);
// println(this.frequency());
susCount=0;
fcycling=4;
envCount=0.f;
}
}
if(fcycling==4){
envCount = envCount+0.00005f;
fenv = fenv+((freqMin-freqSus)/(freqR/0.00005f));
// fenv = fenv -(0.005/freqR);
if(envCount>freqR){
// fenv=freqMin;
fcycling=0;
envCount=0;
}
}
//this.setFreq(fenv);
freqEnv=fenv;
}
if(ampEnveloped){
if(atrigger==1){
ampEnv = startAmp;
gain=constrain(ampEnv,0.0f,0.95f);
acycling=1;
atrigger=0;
}
if(acycling==1){
ampEnvCount = ampEnvCount+0.00005f;
ampEnv = ampEnv+((ampMax-startAmp)/(ampA/0.00005f));
if(ampEnvCount>ampA){
ampEnv=ampMax;
acycling=2;
ampEnvCount=0;
}
}
if(acycling==2){
ampEnvCount = ampEnvCount+0.00005f;
ampEnv = ampEnv+((ampSus-ampMax)/(ampD/0.00005f));
if(ampEnvCount>ampD){
ampEnv=ampSus;
acycling=3;
ampEnvCount=0.f;
}
}
if(acycling==3){
ampSusCount = ampSusCount +0.00005f;
if(ampSusCount>=ampS){
ampSusCount=0;
acycling=4;
ampEnvCount=0.f;
}
}
if(acycling==4){
ampEnvCount = ampEnvCount+0.00005f;
ampEnv = ampEnv+((ampMin-ampSus)/(ampR/0.00005f));
if(ampEnvCount>ampR || ampEnv<0.00000001f){
ampEnv=0.0f;
acycling=0;
ampEnvCount=0;
ampSusCount=0;
}
}
gain=constrain(ampEnv,0.0f,0.95f);
}
} /// update
public void trigger(){
if(freqEnveloped){
ftrigger=1;
fenv=startFreq;
susCount=0;
envCount=0.f;
}
if(ampEnveloped){
acycling=0;
ampSusCount=0;
ampEnvCount=0.f;
gain=startAmp;
atrigger=1;
}
super.trigger();
}
public void sweepDown(){
float totaltime = ampA+ampD+ampS+ampR;
float ctrFreq=lowerFreq;
freqA=totaltime/3;
freqD=totaltime/3;
freqS=0.0001f;
freqR=totaltime/2;
startFreq=ctrFreq*0.3f;
freqMax=ctrFreq*0.1f;
freqSus=0;
freqMin=ctrFreq* -(0.3f);
//println((startFreq));
freqEnveloped=true;
}
public void sweepUp(){
float totaltime = ampA+ampD+ampS+ampR;
float ctrFreq=lowerFreq;
freqA=totaltime/3;
freqD=totaltime/3;
freqS=0.0001f;
freqR=totaltime/2;
startFreq=ctrFreq*(-0.3f);
freqMax=ctrFreq*(-0.1f);
freqSus=0;
freqMin=ctrFreq* (0.3f);
//println((startFreq));
freqEnveloped=true;
}
public void setFreqADSR(float atta, float deca, float susa, float rela,float startf, float maxf,float sustain,float minf){
freqA=abs(atta)+0.0001f;
freqD=abs(deca)+0.0001f;
freqS=abs(susa)+0.0001f;
freqR=abs(rela)+0.0001f;
freqMin=minf-lowerFreq;
freqMax=maxf-lowerFreq;
freqSus=sustain-lowerFreq;
startFreq=startf-lowerFreq;
//println((startFreq));
freqEnveloped=true;
this.noPortamento();
//freqDepth=0;
//if(freqDepth==0.){
// lowerFreq=0.;
//}
}//setFreq
public void setAmpADSR(float atta, float deca, float susa, float rela,float starta, float maxa,float sustaina,float mina){
ampA=abs(atta)+0.0001f;
ampD=abs(deca)+0.0001f;
ampS=abs(susa)+0.0001f;
ampR=abs(rela)+0.0001f;
ampMin=mina/3;
ampMax=maxa/3;
ampSus=sustaina/3;
startAmp=starta/3;
//this.setAmp(ampMax);
ampEnveloped=true;
//this.noPortamento();
}//setAmp
public void setAmpADSR(float atta, float deca, float susa, float rela){
ampA=abs(atta)+0.0001f;
ampD=abs(deca)+0.0001f;
ampS=abs(susa)+0.0001f;
ampR=abs(rela)+0.0001f;
ampMin=0.0f;
ampMax=1.0f;
ampSus=0.9f;
startAmp=0.0f;
//this.setAmp(ampMax);
ampEnveloped=true;
//this.noPortamento();
}//setAmp
public void setEnv(float atk, float rels){
ampEnveloped=false;
enveloped=true;
super.setEnv(atk,rels);
}//setEnv(float,float)
public void setBlip(float[] thblips,float thblipRate, int thblipLoops){
// float[] blips;float blipRate =0.; int blipLoops = 0; int numBlips =0; boolean blipped =false;
// float blipPeriod = 0.;float blipCount=0.;int currBlip = 0;
numBlips = thblips.length;
blips = new float[numBlips];
for(int i=0;i<numBlips;i++){
blips[i]=thblips[i];
}
blipRate = thblipRate;
blipLoops=thblipLoops;
currBlip = 0;
if(blipRate>10.1f){//it must be a tempo, eh?
blipPeriod = 44100/((blipRate/60.f));//bpm?
}
else{//it must be in seconds, eh?
blipPeriod = 44100*blipRate;
}
blipCount=blipPeriod+0.3f;
blipped=true;
this.SETFreq(blips[0]);
blupdate();
}
public void blupdate(){
if(blipped){
blipCount++;//wait, does this happen every sample?
if(blipCount>=blipPeriod){
blipCount-=blipPeriod;
if(blips[currBlip] >40.0f ){// 0.0 == a rest. 1.0 = stacatto rest
lowerFreq=blips[currBlip];upperFreq=blips[currBlip]+freqRange;
this.trigger();
}else{
if(blips[currBlip]==1.0f){ // a 'hard rest'
if(acycling!=0){
ampEnv=ampSus;acycling=4;
}
}
}
currBlip++;
// currBlip%=numBlips;
if(currBlip==numBlips){
currBlip=0;
blipLoops--;
if(blipLoops<=0){
blipped=false;
}
}
}
}
}
public void blipStop(){
if(acycling!=0){
ampEnv=ampSus;acycling=4;
}
blipped=false;
}
public void blipPlay(){
// blipped=true;
blipCount=blipPeriod+0.3f;
blipped=true;
//blupdate();
}
public void resetFreq(float thFreq){
tremDepth=0.0f;
tremCount=0.0f;
tremRate=0.0f;
freqCount=0.0f;
freqDepth=0.0f;
freqRate=0.0f;
blipStop();
ampEnveloped=false;
freqEnveloped=false;
cycling =0;
blipped=false;
enveloped=false;
this.noPortamento();
lowerFreq=thFreq;freqEnv=0;
freqRange=0;
freqDepth=0;
this.setFreq(thFreq);
}
}//class
public class bitNoise extends OscillatorLFOADSR
{
float filterMush = 0.0f;
float nse = 0.0f;
public bitNoise(float frequency, float amplitude, float sampleRate)
{
super(frequency, amplitude, sampleRate);
}
protected float value(float step)
{
updateLFO();
if(round((step*100))%64==0){
nse = (random(10.f)-5.0f)/10.f;
}
filterMush = (nse*tremLFO*gain);
return (filterMush);
}
}
public class bitNoise2 extends OscillatorLFOADSR
{
float filterMush = 0.0f;
float nse = 0.0f;
public bitNoise2(float frequency, float amplitude, float sampleRate)
{
super(frequency, amplitude, sampleRate);
}
protected float value(float step)
{
updateLFO();
if(round((step))%4==0){
nse = (random(10.f)-5.0f)/10.f;
}
filterMush = (nse*tremLFO*gain);
return (filterMush);
}
}
public class bitNoiseBP extends OscillatorLFOADSR
{
float filterMush = 0.0f;
float nse = 0.0f;
float freq=200.0f;
float lowPass=0.0f;
float hiPass =0.0f;
//int numPass = 4;
//lowPass.length;
float sine=0.0f;
public bitNoiseBP(float frequency, float amplitude, float sampleRate)
{
super(frequency, amplitude, sampleRate);
}
protected float value(float step)
{
freq = 44100/this.frequency();
updateLFO();
//if(((step))%0.1==0){
nse = (random(100.f)-50.0f)/50.f;
//}
sine= (sin(step)*0.02f);
lowPass = (lowPass*(freq*1.03f) +nse +sine )/ (freq*1.03f+1);
hiPass = ((hiPass*(freq*0.97f) +nse+sine )/ (freq*0.97f+1));
filterMush = (hiPass-lowPass)*80.f ;
return (filterMush*tremLFO*gain);
}
}
public class SquareMush extends OscillatorLFOADSR
{
float filterMush = 0.0f;
float a =0.0f; float b =0.0f;
float detuna=0.0f;float last =0.0f;
public SquareMush(float frequency, float amplitude, float sampleRate)
{
//filterMush = 0.0;
// a=0.0;
super(frequency, amplitude, sampleRate);
//detuna = 0.0;last=0.0;
}
protected float value(float step)
{
if(step>0.5f){a=0.5f;}else{a=(-0.5f);}
detuna =detuna+((step-last)*1.8f);
last=step;
if((detuna)>0.55f ||(detuna>0.0f && detuna<0.05f)){b=0.3f;}else{b=(-0.3f);}
filterMush = (2*filterMush + (a+b))/3.f;
return (filterMush);
}
}
public class SquareZap extends OscillatorLFOADSR
{
float filterMush = 0.0f;
float a =0.0f; float b =0.0f;
float detuna=0.0f;float last =0.0f;
float freqy;
float amp;float zamp;
public SquareZap(float frequency, float amplitude, float sampleRate)
{
//filterMush = 0.0;
// a=0.0;
super(frequency, amplitude, sampleRate);
freqy=frequency;amp =amplitude;
this.noPortamento();
//detuna = 0.0;last=0.0;
}
protected float value(float step)
{
if(step>0.5f){a=0.5f;}else{a=(-0.5f);}
detuna =detuna+((step-last)*1.8f);
last=step;
if((detuna)>0.55f ||(detuna>0.0f && detuna<0.05f)){b=0.3f;}else{b=(-0.3f);}
filterMush = (4*filterMush + ((a+b)*amp))/5.f;
if(freqy>80){freqy *=0.9996f;}else{amp *=0.9f;}
if(amp>0.0f){amp -= 0.0001f;}
//if(zamp>0.0){zamp -= 0.00095;}else{zamp=amp;}
this.setFreq(freqy);//this.setAmp(amp);
return (filterMush);
}
}
public class SquareHat extends OscillatorLFOADSR
{
float filterMush = 0.0f;
float a =0.0f; float b =0.0f;
float detuna=0.0f;float last =0.0f;
public SquareHat(float frequency, float amplitude, float sampleRate)
{
super(frequency, amplitude, sampleRate);
}
protected float value(float step)
{
updateLFO();
if(step>0.5f){a=0.5f;}else{a=(-0.5f);}
detuna =detuna+((step-last)*1.8f);//detuna %=1.0;
last=step;
if((detuna)>0.75f ||(detuna>0.25f && detuna<0.5f)){b=0.5f;}else{b=(-0.5f);}
filterMush = (5*filterMush + ((a+b)*tremLFO*gain))/6.f;
return (filterMush);
}
}
public class SquareTriangle extends OscillatorLFOADSR
{
float filterMush = 0.0f;
float a =0.0f; float b =0.0f;
float detuna=0.0f;float last =0.0f;
public SquareTriangle(float frequency, float amplitude, float sampleRate)
{
//filterMush = 0.0;
// a=0.0;
super(frequency, amplitude, sampleRate);
//detuna = 0.0;last=0.0;
}
protected float value(float step)
{
if(step>0.5f){a=0.5f;}else{a=(-0.5f);}
detuna =detuna+((step-last)*1.8f);//detuna %=1.0;
// if(detuna>=1.0){detuna = (detuna-1.0);}
last=step;
if((detuna)>0.65f ||(detuna>0.04f && detuna<0.15f)){b=0.5f;}else{b=(-0.5f);}
filterMush = (7*filterMush + a+b)/8.f;
// if(detuna>=1.0){detuna = (detuna-1.0);}
return (filterMush);
}
}
public class SquareSpike extends OscillatorLFOADSR
{
float filterMush = 0.0f;
float a =0.0f; float b =0.0f;
float detuna=0.0f;float last =0.0f;
public SquareSpike(float frequency, float amplitude, float sampleRate)
{
//filterMush = 0.0;
// a=0.0;
super(frequency, amplitude, sampleRate);
//detuna = 0.0;last=0.0;
}
protected float value(float step)
{
updateLFO();
// step +=(1/freqLFO);
if(step>0.5f){a=0.5f;}else{a=(-0.5f);}
detuna =detuna+((step-last)*1.1f);//detuna %=1.0;
// if(detuna>=1.0){detuna = (detuna-1.0);}
last=step;
if((detuna)>0.85f ||(detuna>0.35f && detuna<0.4f)){b=0.5f;}else{b=(-0.5f);}
filterMush = (5*filterMush + ((a+b)*tremLFO*gain))/6.f;
// if(detuna>=1.0){detuna = (detuna-1.0);}
// this.setFreq(freqLFO);
return (filterMush);
}
}
public class SquareBzerker extends OscillatorLFOADSR
{
float filterMush = 0.0f;
float subSonic=0.0f;
float a =0.0f; float b =0.0f;float c =0.0f;float d=0.0f;float e=0.0f;
float detuna=0.0f;float last =0.0f;
public SquareBzerker(float frequency, float amplitude, float sampleRate)
{
//filterMush = 0.0;
// a=0.0;
super(frequency, amplitude, sampleRate);
//detuna = 0.0;last=0.0;
}
protected float value(float step)
{
updateLFO();
// step +=(1/freqLFO);
if(step>0.5f){a=0.5f;}else{a=(-0.5f);}
detuna =((detuna)+((step-last)*(0.1f+tremLFO-gain)));//detuna %=1.0;
// if(detuna>=1.0){detuna = (detuna-1.0);}
last=step;
if((detuna)>0.95f ||(detuna>0.25f && detuna<0.3f)){b=0.5f;}else{b=(-0.15f);}
d=(abs(a-b));
c = (a-b)*(d+0.01f)/(d+0.0000001f);
e = (c)*((d+0.01f)/(d+0.0000001f))+(abs(d-0.1f));
filterMush = (3*filterMush + ((e)*tremLFO*gain))/6.f;
subSonic =(2300*subSonic + filterMush)/2301;
// this.setFreq(100+100*detuna);
// if(detuna>=1.0){detuna = (detuna-1.0);}
// this.setFreq(freqLFO);
return ( filterMush -(subSonic));
}
}
public class SquareBoink extends OscillatorLFOADSR
{
float filterMush = 0.0f;
float a =0.0f; float b =0.0f;
float detuna=0.0f;float last =0.0f;
public SquareBoink(float frequency, float amplitude, float sampleRate)
{
//filterMush = 0.0;
// a=0.0;
super(frequency, amplitude, sampleRate);
//detuna = 0.0;last=0.0;
}
protected float value(float step)
{
if(step>0.5f){a=0.5f;}else{a=(-0.5f);}
detuna =detuna+((step-last)*1.8f);
last=step;
if((detuna)>0.55f ||(detuna>0.0f && detuna<0.05f)){b=0.3f;}else{b=(-0.3f);}
filterMush = (2*filterMush + a+b)/3.f;
updateLFO();
return (filterMush*tremLFO*gain);
}
}
public class SquarePhase extends OscillatorLFOADSR
{
float filterMush = 0.0f;
float a =0.0f; float b =0.0f;
float detuna=0.0f;float last =0.0f;
float DCleak =0.0f;
public SquarePhase(float frequency, float amplitude, float sampleRate)
{
//filterMush = 0.0;
// a=0.0;
super(frequency, amplitude, sampleRate);
//detuna = 0.0;last=0.0;
}
protected float value(float step)
{
if((step>0.5f)&&(step<0.85f) ){a=0.5f;}else{a=(-0.2f);}
detune =detune+(this.frequency()/44100);
detune %= 0.9991f-(gain/50.f);
// theLast=step;
if((detune > 0.5f)&&(detune < 0.85f) ){b=-0.5f;}else{b=(0.2f);}
filterMush = ((2*filterMush + a+b)/3.f)*tremLFO;
DCleak = ((2000*DCleak) +filterMush)/2001;
updateLFO();
return ((filterMush-DCleak)*gain*3.0f);
}
}
static public void main(String args[]) {
PApplet.main(new String[] { "--present", "--bgcolor=#666666", "--stop-color=#cccccc", "GameSound15" });
}
}
|