/** * controlP5basics * * key combinations: * ALT+h hide and show controllers * ALT+s save controllers to an xml file * ALT+l load controllers from an xml file * ALT+k bring the key menu to the front. use the arrow keys .. * hold the ALT key and move controllers by dragging them. */ import processing.opengl.*; import controlP5.*; import ddf.minim.*; import ddf.minim.analysis.*; import ddf.minim.signals.*; import ddf.minim.effects.*; //declare classes Minim minim; ControlP5 gui; AudioOutput out; WaveformRenderer waveform; WaveformRenderer small_waveform; PFont Arial; BandPass bpf; FFT fftLin; //make array to hold my sinewave objects ArrayList sineArray; //make array to hold my triangle wave objects ArrayList triArray; //make array to hold my pulsewave objects ArrayList pulseArray; //variables for controls public float Amp = 0.025; public float Pan = 0; public float Freq = 440; public float Rate = .5; public float Gain = 1; public float bP_lower = 100; public float bP_upper = 2000; //placeholder waves for updates SineWave curSin; TriangleWave curTri; PulseWave curPulse; //Indexes public int signalIndex = 0; public int sinIndex = 0; public int triIndex = 0; public int pulseIndex = 0; //Misc Variables public int rightCol = 400; public boolean passBandOn = false; float height3; void setup(){ size(700,600, OPENGL); minim = new Minim(this); gui = new ControlP5(this); sineArray = new ArrayList(); triArray = new ArrayList(); pulseArray = new ArrayList(); curTri = null; curSin = null; curPulse = null; //Settings for sound output (TYPE/BUFFERSIZE/SAMPLE RATE/BIT DEPTH) -- ALL VALUES HERE ARE DEFAULT out = minim.getLineOut(Minim.STEREO,1024,44100,16); waveform = new WaveformRenderer((height/4)-height/8, 150); small_waveform = new WaveformRenderer(8.5*height/10, 50); out.addListener(waveform); fftLin = new FFT(out.bufferSize(), out.sampleRate()); bpf = new BandPass(440, 20, out.sampleRate()); out.printControls(); //////////////////////////////////////////////// //TEST WAVE sineArray.add (new SineWave(Freq, Amp, 44100) ); SineWave curWave = (SineWave) sineArray.get(0); curWave.portamento(200); out.addSignal(curWave); sinIndex = sineArray.size()-1; //////////////////////////////////////////////// //////////////////////////////////////////////// //GUI Arial = createFont("Arial", 12); textFont (Arial); //gui.addNumberbox("bpm", 120, 15, 345, 20, 15); gui.addSlider("Freq",40,5000,200,305,10,260); gui.addSlider("Amp",0,0.1,250,305,10,260); gui.addSlider("Rate",1,30,300,305,10,260); gui.addSlider("Gain",-80,13,350,305,10,260); gui.addSlider("bP_lower",40,5000,100,305,10,260); gui.addSlider("bP_upper",40,5000,150,305,10,260); gui.addButton("addSineWave",1,10,310,75,15); gui.addButton("addTriangleWave",1,10,330,75,15); gui.addButton("addPulseWave",1,10,350,75,15); gui.addButton("stepUp",1,10,370,75,15); gui.addButton("stepDown",1,10,390,75,15); //gui.addToggle("Mute",false,10,420,75,30); } void draw(){ //DRAW STAGE background(0); stroke(255); line(0,30,width,30); line(0,300,width,300); //DRAW WAVEFORM waveform.draw(); //small_waveform.draw(); //spectrum, not working // for(int i = 0; i < width; i++) // { // line(i, height, i, height - fftLin.getBand(i)*2); // } // GOOD MORE OR LESS STABLE CODE FROM THIS POINT ON setSignal(); displayText(); setGain(); passBandFilter(); } //////////////////////////////////////////////// /////////////////////////////////////////////// //////////////////////////////////////////////// Functions /////////////////////////////////////////////// //////////////////////////////////////////////// /////////////////////////////////////////////// void keyPressed() { if ( key == '1' ) addSineWave(); if ( key == '2' ) addTriangleWave(); if ( key == '3' ) addPulseWave(); if ( key == 's' ) stepDown(); if ( key == 'd' ) stepUp(); if ( key == 'm' ) setMute(); // if ( key == 'r' ) RemoveSignal(); } //////////////////////////////////////////////// /////////////////////////////////////////////// //////////////////////////////////////////////// UPDATES /////////////////////////////////////////////// //////////////////////////////////////////////// /////////////////////////////////////////////// void setSignal(){ if (out.getSignal(signalIndex).getClass().getName().equals("ddf.minim.signals.SineWave")){ curSin = (SineWave)out.getSignal(signalIndex); curSin.setFreq(Freq); curSin.setAmp(Amp); // cureSine.setPan(sinPan); text("The current Frequency is " + curSin.frequency() + ".", rightCol, 315); } if (out.getSignal(signalIndex).getClass().getName().equals("ddf.minim.signals.TriangleWave")){ curTri = (TriangleWave)out.getSignal(signalIndex); curTri.setFreq(Freq); curTri.setAmp(Amp); //triSelected.setPan(triPan); text("The current Frequency is " + curTri.frequency() + ".", rightCol, 315); } if (out.getSignal(signalIndex).getClass().getName().equals("ddf.minim.signals.PulseWave")){ curPulse = (PulseWave)out.getSignal(signalIndex); curPulse.setFreq(Freq); curPulse.setAmp(Amp); //curPulse.setPan(pulsePan); curPulse.setPulseWidth(Rate); text("The current Frequency is " + curPulse.frequency() + ".", rightCol, 315); } } void stepUp(){ if (signalIndex< out.signalCount()-1){ upArrayIndex(); signalIndex ++; updateSignal(); } } void stepDown(){ if (signalIndex > 0){ downArrayIndex(); signalIndex --; updateSignal(); } } void upArrayIndex(){ if (out.getSignal(signalIndex).getClass().getName().equals("ddf.minim.signals.SineWave") && sinIndex < sineArray.size()-1) { sinIndex ++; } if (out.getSignal(signalIndex).getClass().getName().equals("ddf.minim.signals.TriangleWave") && triIndex < triArray.size()-1) { triIndex ++; } if (out.getSignal(signalIndex).getClass().getName().equals("ddf.minim.signals.PulseWave") && pulseIndex < pulseArray.size()-1) { pulseIndex ++; } } void downArrayIndex(){ if (out.getSignal(signalIndex).getClass().getName().equals("ddf.minim.signals.SineWave") && sinIndex>0) { sinIndex --; } if (out.getSignal(signalIndex).getClass().getName().equals("ddf.minim.signals.TriangleWave") && triIndex>0) { triIndex --; } if (out.getSignal(signalIndex).getClass().getName().equals("ddf.minim.signals.PulseWave") && pulseIndex>0) { pulseIndex --; } } void updateSignal(){ if (out.getSignal(signalIndex).getClass().getName().equals("ddf.minim.signals.SineWave")){ updateSin(); } if (out.getSignal(signalIndex).getClass().getName().equals("ddf.minim.signals.TriangleWave")){ updateTri(); } if (out.getSignal(signalIndex).getClass().getName().equals("ddf.minim.signals.PulseWave")){ updatePulse(); } } void updateSin(){ SineWave tempSin = (SineWave)out.getSignal(signalIndex); float curFreq = tempSin.frequency(); gui.controller("Freq").setValue(curFreq); Freq = curFreq; float curAmp = tempSin.amplitude(); gui.controller("Amp").setValue(curAmp); Amp = curAmp; tempSin.setAudioListener(small_waveform); } void updateTri(){ TriangleWave tempTri = (TriangleWave) out.getSignal(signalIndex); float curFreq = tempTri.frequency(); gui.controller("Freq").setValue(curFreq); Freq = curFreq; float curAmp = tempTri.amplitude(); gui.controller("Amp").setValue(curAmp); Amp = curAmp; tempTri.setAudioListener(small_waveform); } void updatePulse(){ PulseWave tempPulse = (PulseWave) out.getSignal(signalIndex); float curFreq = tempPulse.frequency(); gui.controller("Freq").setValue(curFreq); Freq = curFreq; float curAmp = tempPulse.amplitude(); gui.controller("Amp").setValue(curAmp); Amp = curAmp; float curRate = tempPulse.getPulseWidth(); gui.controller("Rate").setValue(curRate); tempPulse.setPulseWidth(curRate); tempPulse.setAudioListener(small_waveform); } //////////////////////////////////////////////// //////////////////////////////////////////////// //////////////////////////////////////////////// ADD WAVES //////////////////////////////////////////////// //////////////////////////////////////////////// //////////////////////////////////////////////// void addSineWave(){ int lastItem = sineArray.size(); int signalItem = out.signalCount(); //my wave (FREQ/AMPLITUDE/SAMPLE RATE) sineArray.add (new SineWave(440, 0.025,44100) ); SineWave curWave = (SineWave) sineArray.get(lastItem); curWave.portamento(200); out.addSignal(curWave); sinIndex = lastItem; signalIndex = signalItem; updateSin(); } void addTriangleWave(){ int lastItem = triArray.size(); int signalItem = out.signalCount(); //my wave (FREQ/AMPLITUDE/SAMPLE RATE) triArray.add (new TriangleWave(440, 0.025,44100) ); TriangleWave curWave = (TriangleWave) triArray.get(lastItem); curWave.portamento(200); out.addSignal(curWave); triIndex = lastItem; signalIndex = signalItem; updateTri(); } void addPulseWave(){ int lastItem = pulseArray.size(); int signalItem = out.signalCount(); //my wave (FREQ/AMPLITUDE/SAMPLE RATE) pulseArray.add (new PulseWave(440, 0.025,44100) ); PulseWave curWave = (PulseWave) pulseArray.get(lastItem); curWave.portamento(200); out.addSignal(curWave); pulseIndex = lastItem; signalIndex = signalItem; updatePulse(); } /* //not working problems with stepping out of array index. What do you do if you want to remove the last wave in an array? void RemoveSignal(){ if(signalIndex > 0){ stepDown(); if (out.getSignal(signalIndex).getClass().getName().equals("ddf.minim.signals.SineWave")){ SineWave curWave = (SineWave) sineArray.get(sinIndex+1); out.removeSignal(curWave); sineArray.remove(sinIndex+1); } if (out.getSignal(signalIndex).getClass().getName().equals("ddf.minim.signals.TriangleWave")){ TriangleWave curWave = (TriangleWave) triArray.get(triIndex+1); out.removeSignal(curWave); triArray.remove(triIndex+1); } if (out.getSignal(signalIndex).getClass().getName().equals("ddf.minim.signals.PulseWave")){ PulseWave curWave = (PulseWave) pulseArray.get(pulseIndex+1); out.removeSignal(curWave); pulseArray.remove(pulseIndex+1); } } } */ //not working void signalEnable(boolean Switch) { if(Switch==true) { out.enableSignal(signalIndex); } else { out.disableSignal(signalIndex); } println("a toggle event."); } //void signalCheck(curWave){ // boolean sinCheck triCheck pulseCheck //test all these statements, then use to abbreviate when checking other signals //i guess its really just about updating these switches based on checking classes //} ///////////////////////////////////////////////// /////////////////////////////////////////////// //////////////////////////////////////////////// PASS BAND FILTER ///////////////////////////////////////////// //////////////////////////////////////////////// /////////////////////////////////////////////// void mousePressed() { if (mouseY<300){ if ( !passBandOn ) { out.addEffect(bpf); passBandOn = true; } else { passBandOn = false; out.removeEffect(bpf); //bpf.setFreq(passBand); // bpf.setBandWidth(bandWidth); } } } void passBandFilter(){ if (passBandOn){ if (mouseY<300){ fill(#00BFFF,50); rect(mouseX - bpf.getBandWidth()/20, 30, bpf.getBandWidth()/10, height/2-30); // map the mouse position to the range [100, 10000], an arbitrary range of passBand frequencies - later make this adjustable float passBand = map(mouseX, 0, width, bP_lower, bP_upper); bpf.setFreq(passBand); float bandWidth = map (mouseY, 0, height/2, 50, 1000); bpf.setBandWidth(bandWidth); // bpf.printCoeff(); } } } ///////////////////////////////////////////////// /////////////////////////////////////////////// //////////////////////////////////////////////// AUDIO OUT CONTROL FUNCTIONS /////////////////////////////////////////////// //////////////////////////////////////////////// /////////////////////////////////////////////// void setGain(){ float val = Gain; out.setGain(val); } void setMute(){ if (!out.isMuted() ) { out.mute(); } else { out.unmute(); } } //void Mute(boolean theFlag){ // if (!out.isMuted() && !theFlag ) // { out.mute(); } // else // { out.unmute(); } //} ///////////////////////////////////////////////// /////////////////////////////////////////////// //////////////////////////////////////////////// MY TEXT /////////////////////////////////////////////// //////////////////////////////////////////////// /////////////////////////////////////////////// void displayText(){ //MY CURRENT SELECTED SIGNAL textFont (Arial,18); text("The Current Signal is " + out.getSignal(signalIndex), 15, 25); textFont (Arial, 12); //information text("The Current Index is " + signalIndex, rightCol, 330); text("The Current SinIndex is " + sinIndex, rightCol, 345); text("The Current BandWidth is " + bpf.getBandWidth(),rightCol, 360); text("The Current PulseIndex is " + pulseIndex,rightCol, 375); text("The Current TriIndex is " + triIndex, rightCol, 390); text("There are " + out.signalCount() + " signals attached to the output.", rightCol, 415); text("There are " + sineArray.size() + "Sine Waves", rightCol, 430); text("There are " + triArray.size() + "Triangle Waves",rightCol, 445); text("There are " + pulseArray.size() + "Pulse Waves",rightCol, 460); if ( passBandOn ) { text("Passband Filter On", rightCol, 475);} else {text("PassBand Filter Off.", rightCol, 475);} if ( out.isMuted() ) {text("The output is muted.", rightCol, 490);} else {text("The output is not muted.", rightCol, 490);} text("The current gain is " + out.getGain() + ".",rightCol, 505); } void stop(){ out.close(); minim.stop(); super.stop(); }