mapping pan to dial--stuck...
in
Contributed Library Questions
•
1 year ago
Hi,
Having real difficulties with this- i would like to map the left right movement of the ellipse to the top knob in the control window. Have hit a dead end...any help much apprecitated.
- import beads.*;
- import controlP5.*;
- import processing.opengl.*;
- ControlP5 cp5;
- Glide gainGlide;
- AudioContext ac;
- float x;
- float y;
- float d;
- float dx;
- float dy;
- String sourceFile;
- SamplePlayer sp;
- Gain g;
- Glide gainValue;
- Reverb r;
- Button buttons;
- float rad=50;
- Panner p;
- boolean dragging = false;
- void setup()
- {
- size(800, 600);
- smooth();
- buttons= new Button(width/2, height/2);
- ac = new AudioContext(); // create our AudioContext
- sourceFile = sketchPath("") + "Drum_Loop_01.wav";
- try {
- sp = new SamplePlayer(ac, new Sample(sourceFile));
- }
- catch(Exception e)
- {
- println("Exception while attempting to load sample!");
- e.printStackTrace();
- exit();
- }
- sp.setKillOnEnd(false);
- gainValue = new Glide(ac, 0.0, 20);
- g = new Gain(ac, 1, gainValue);
- g.addInput(sp);
- r = new Reverb(ac, 1);
- r.setSize(0.7);
- r.setDamping(0.5);
- r.addInput(g);
- p = new Panner(ac);
- p.addInput(g);
- p.addInput(r);
- ac.out.addInput(p);
- ac.start();
- gainGlide = new Glide(ac, 0.0, 50);
- g = new Gain(ac, 1, gainGlide);
- x = width/2;
- y = height/2;
- background(0);
- cp5 = new ControlP5(this);
- ControlWindow cw = cp5.addControlWindow("controlP5window", 208, 396, 800, 250, 30);
- Knob k = cp5.addKnob("rad", 0, width, 40, 90, 30);
- k.setWindow(cw);
- Knob k1 = cp5.addKnob("pan", 0, width, 40, 50, 30);
- k1.setWindow(cw);
- }
- void draw() {
- background(10);
- dx=mouseX-width/2;
- dy=mouseY-height/2;
- float d = dist(0, 0, dx, dy);
- rad=constrain (rad, 0, 120);
- gainGlide.setValue(mouseX / (float)width);
- fill(0, 0, 255, 150);
- noStroke();
- ellipse(x, y, 50, 50);
- buttons.display();
- //buttons.resize();
- if (mousePressed) {
- if (dist(mouseX, mouseY, x, y) > 25) {
- if (!dragging) rad=d*2;
- }
- else {
- dragging = true;
- }
- }
- float roomSize = map(rad, 50, 200, 0, 1);
- r.setSize(roomSize );
- rad=constrain (rad, 50, 120);
- if (!mousePressed) dragging = false;
- if (dragging) {
- if (mouseX > 0 && mouseX < width-1) {
- x = mouseX;
- buttons.x = mouseX;
- p.setPos(map(mouseX, 0, width-1, -1, 1));
- }
- }
- }
- void keyPressed() {
- if (key==' ') {
- gainValue.setValue(1);
- sp.setToLoopStart();
- sp.start();
- }
- }
- class Button {
- float x, y;
- float h, s, b;
- float transparency;
- Button(float _cx, float _cy) {
- x = _cx;
- y = _cy;
- h=150;
- s=10;
- b=100;
- transparency=20;
- }
- void display() {
- stroke(0.5);
- fill(255, 0, 55, transparency);
- stroke(50, 0, 255, 50);
- strokeWeight(2);
- ellipse (x, y, rad, rad);
- }
- }
1