Advertisement
JoshuaDavis

working with HBundle to create drawable to behavior bridges

Feb 22nd, 2015
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | None | 0 0
  1. import ddf.minim.*;
  2. import ddf.minim.analysis.*;
  3.  
  4. Minim minim;
  5. AudioPlayer ap;
  6. BeatDetect bd;
  7. BeatListener bl;
  8.  
  9. float[] fNums = new float[13];
  10. int minVal = 0;
  11. int maxVal = 1000;
  12.  
  13. int particleMin = 180;
  14. int particleMax = 220;
  15.  
  16. color[] clr = { #333333,#494949,#5F5F5F,#707070,#7D7D7D,#888888,#949494,#A2A2A2,#B1B1B1,#C3C3C3,#D6D6D6,#EBEBEB,#FFFFFF };
  17.  
  18. HSphere baseSphere;
  19. HDrawablePool pool;
  20.  
  21. void setup() {
  22.     size(700,700,P3D);
  23.     H.init(this).background(#202020).use3D(true);
  24.     smooth();
  25.  
  26. /*
  27.  
  28. check here # 1 - you'll need an .mp3 edit line 33 with your MP3
  29.  
  30. */
  31.  
  32.     minim = new Minim(this);   
  33.     ap = minim.loadFile("seeya.mp3", 2048);
  34.     ap.play();
  35.     bd = new BeatDetect(ap.bufferSize(), ap.sampleRate());
  36.     bd.setSensitivity(0);  
  37.     bl = new BeatListener(bd, ap);
  38.  
  39.     H.add( baseSphere = new HSphere() )
  40.         .size(190)
  41.         .strokeWeight(0)
  42.         .noStroke()
  43.         .fill(#666666)
  44.         .anchorAt(H.CENTER)
  45.         .loc(width/2, height/2)
  46.     ;
  47.  
  48.     pool = new HDrawablePool(100);
  49.     pool.autoAddToStage()
  50.         .add(new HSphere())
  51.  
  52.         .onCreate(
  53.             new HCallback() {
  54.                 public void run(Object obj) {
  55.                     HSphere d = (HSphere) obj;
  56.  
  57.                     int ranSize = 10 + ( (int)random(3)*7 );
  58.  
  59.                     d
  60.                         .size(ranSize)
  61.                         .strokeWeight(0)
  62.                         .noStroke()
  63.                         .fill(#FF3300)
  64.                         .anchorAt(H.CENTER)
  65.                     ;
  66.  
  67.  
  68. /*
  69.  
  70. check here # 2 - pool will always keep track of drawables but not other behaviors associated with a drawable
  71.  
  72. so here we have an HOrbiter3D - I want to use audio reaction to change .radius() to the music
  73.  
  74. this way the drawable "d" HSphere will bounce up and down to the music
  75.  
  76. */
  77.  
  78.  
  79.                     HOrbiter3D orb = new HOrbiter3D(width/2, height/2, 0)
  80.                         .target(d)
  81.                         .zSpeed(random(-1.5, 1.5))
  82.                         .ySpeed(random(-0.5, 0.5))
  83.                         .radius(195)
  84.                         .zAngle( (int)random(360) )
  85.                         .yAngle( (int)random(360) )
  86.                     ;
  87.  
  88. /*
  89.  
  90. check here # 3 - we can make a link to an attached behavior with HBundle
  91.  
  92. this will pass "f" which frequence range d is assoicated with and "o" the HOrbiter3D behavior
  93.  
  94. */
  95.  
  96.                     d.extras( new HBundle().num("f", (int)random(fNums.length)).obj("o", orb) );
  97.                 }
  98.             }
  99.         )
  100.  
  101.         .requestAll()
  102.     ;
  103. }
  104.  
  105. void draw() {
  106.     sphereDetail(20);
  107.  
  108.     pointLight(100, 0, 0,  width/2, height, 200); // red
  109.     pointLight(51, 153, 153,  width/2, -50, 150); // teal
  110.     pointLight(204, 204, 204,  width/2, (height/2) - 50, 500); // white
  111.  
  112.     H.drawStage();
  113.  
  114.     for (int i = 0; i < fNums.length; ++i) {
  115.         if ( bd.isOnset( (i+1)*2 ) ) {
  116.             fNums[i]  = maxVal;
  117.         }
  118.  
  119.         fNums[i] = constrain(fNums[i] * 0.9, particleMin, particleMax);
  120.  
  121. /*
  122.  
  123. check here # 4 - so this for loop cycles through pool... and then we can lookup HBundle
  124.  
  125.     HBundle obj1 = d.extras();
  126.  
  127. so "d" can look up extras() to find the bridge between "d" and the object associated behavior
  128.  
  129.     HOrbiter3D o = (HOrbiter3D) obj1.obj("o");
  130.  
  131. hooray we're now talking to "d"'s' related HOrbiter3D behavior
  132.  
  133. I can then dynamically change the radius for each "d"
  134.  
  135.     o.radius( fNums[ whichFreq ] );
  136.  
  137. this mean that "d" changes it's orbit behavior based on the fluctuating radius which is changing based on the audio
  138.  
  139. */
  140.  
  141.         for(HDrawable d : pool) {
  142.             HBundle obj1 = d.extras();
  143.             HOrbiter3D o = (HOrbiter3D) obj1.obj("o");
  144.  
  145.             int whichFreq = (int)obj1.num("f");
  146.  
  147.             d.fill( clr[ whichFreq ] );
  148.             o.radius( fNums[ whichFreq ] );
  149.         }
  150.     }
  151. }
  152.  
  153. void stop() {
  154.     ap.close();
  155.     minim.stop();
  156.     super.stop();
  157. }
  158.  
  159. class BeatListener implements AudioListener {
  160.     private BeatDetect bd;
  161.     private AudioPlayer source;
  162.  
  163.     BeatListener(BeatDetect bd, AudioPlayer source) {
  164.         this.source = source;
  165.         this.source.addListener(this);
  166.         this.bd = bd;
  167.     }
  168.  
  169.     void samples(float[] samps) {
  170.         bd.detect(source.mix);
  171.     }
  172.  
  173.     void samples(float[] sampsL, float[] sampsR) {
  174.         bd.detect(source.mix);
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement