Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Re : Do you know whether it is possible or not to put minim and video together in one sketch?

$
0
0
Ok, your attempts show improvement. So that's good!

Some suggestions:
  • Set the number of objects in the array.
  • Initialize the array in setup (this was causing your null pointer exception).
  • Everything minim-related can be done ONCE. Each object can use the global result via dampedVolume.
Adapted Code
  1. import ddf.minim.*;
  2. import ddf.minim.analysis.*;
  3.  
  4. int numBalloons = 10;
  5. Balloon[] balloons = new Balloon[numBalloons];
  6.  
  7. Minim minim;
  8. AudioInput voice;
  9. FFT fft;
  10.  
  11. float dampedVolume;
  12. float damping = 0.6;
  13. float filtering = 100;
  14.  
  15. void setup() {
  16.   size(600, 600);
  17.   smooth();
  18.  
  19.   minim = new Minim(this);
  20.   voice = minim.getLineIn(Minim.STEREO, 2048);
  21.   fft = new FFT(voice.bufferSize(), voice.sampleRate());
  22.  
  23.   for (int i=0; i<numBalloons; i++) {
  24.     balloons[i] = new Balloon(random(width), random(height));
  25.   }
  26. }
  27.  
  28. void draw() {
  29.   background(255);
  30.   analyseSound(); // done once, end result = new dampedVolume value
  31.   for (int i=0; i<balloons.length; i++) {
  32.     balloons[i].display();
  33.   }
  34. }
  35.  
  36. void analyseSound() {
  37.   fft.forward(voice.mix);
  38.   float volume = 0;
  39.   for (int i=0; i<filtering; i++) {
  40.     volume += fft.getBand(i);
  41.   }
  42.   volume *= 0.025;
  43.   dampedVolume = dampedVolume + (volume - dampedVolume)*damping;
  44. }
  45.  
  46. void stop() {
  47.   voice.close();
  48.   minim.stop();
  49.   super.stop();
  50. }
  51.  
  52. class Balloon {
  53.   float x, y;
  54.   color c;
  55.   float scaler;
  56.  
  57.   Balloon(float x, float y) {
  58.     this.x = x;
  59.     this.y = y;
  60.     c = color(random(255), random(255), random(255));
  61.     scaler = random(0.5, 2);
  62.   }
  63.  
  64.   void display() {
  65.     fill(c);
  66.     ellipse(x, y, dampedVolume*scaler, dampedVolume*scaler);
  67.   }
  68. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles