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

Re : Using sound in processing?

$
0
0
Yes, Anton was so kind to include the code for this already with comments //

I've made the required changes yellow in the adapted code below.

Some forum-related remarks. I've moved this thread to Core Library Questions because it uses Minim. You can post code in a more user-friendly manner as done below. Then it has line numbers and people can use the Copy Code button. For this and other information check out:
https://forum.processing.org/topic/where-to-place-threads-and-other-forum-practices

Adapted Code
  1. /* Created by Anton Pugh */
  2. /* Music Visualizer that samples the short-time fft of a song and */
  3. /* plots the sampled points (|fft| => brightness of point).       */
  4. // Note: If your machine has OpenGL capabilities, uncomment the
  5. // following line and change 'P3D' to 'OPENGL' on line 34
  6. //import processing.opengl.*;
  7. import ddf.minim.analysis.*;
  8. import ddf.minim.*;
  9. //import fullscreen.*;
  10. /* MAX_SPOTS is the maximum number of points the sketch can plot  */
  11. /* NUM_ITERATIONS determines how often fft is sampled into points */
  12. /*   - Higher number => less often
  13. /*   - 1 == as often as possible
  14. /* SPEED determines how fast points rotate                        */
  15. int MAX_SPOTS = 1000000;
  16. int NUM_ITERATIONS = 1;
  17. float SPEED = 4;
  18. // Declare a new Minim object which will allow us to play and
  19. // analyze music. If you want to use the computer sound card
  20. // output, leave code as is, otherwise comment out the AudioInput
  21. // line and uncomment the AudioPlayer line. Additionally, you
  22. // will need to change how the sketch gets data, so look in
  23. // the setup loop for song = minim.loadFile
  24. Minim minim;
  25. AudioInput song;
  26. //AudioPlayer song;
  27. FFT fft;
  28. //SoftFullScreen fs;
  29. Spot[] spots = new Spot[MAX_SPOTS];
  30. int numSpots, index, colorSwitch;
  31. color spotColor, barCol1, barCol2;
  32. float band, rBar, gBar, bBar;
  33. void setup() {
  34.   // Set size of window and rendering mode
  35.   // Note: If your machine has OpenGL capabilities, change
  36.   // 'P3D' to 'OPENGL' and uncomment the first line
  37.   size(800, 500, P3D);
  38.   minim = new Minim(this);
  39.   //fs = new SoftFullScreen(this);
  40.   //fs.enter();
  41.   //
  42.   // MODIFY STRING TO CHANGE THE SONG THAT IS LOADED
  43.   // (SONG MUST BE IN soundWaterfall/data/)
  44.   //song = minim.loadFile("annArborPart2.mp3", 512);
  45.   //song.loop();
  46.   song = minim.getLineIn(minim.MONO, 2048);
  47.   fft = new FFT(song.bufferSize(), song.sampleRate());
  48.   fft.window(FFT.HAMMING);
  49.   numSpots = 0;
  50.   barCol1 = color(0, 0, 128, 255);
  51.   barCol2 = color(0, 0, 128, 150);
  52.   index = 0;
  53.   colorSwitch = 0;
  54.   rBar = 0;
  55.   gBar = 0;
  56.   bBar = 0;
  57. }
  58. void draw() {
  59.   smooth();
  60.   background(0);
  61.   fft.forward(song.mix);
  62.   for (int i = 0; i < 150; i+=2) {
  63.     // GET MAGNITUDE OF FFT AT BAND i
  64.     band = fft.getBand(i);
  65.     spotColor = color(0, 0, min(100*band, 255));
  66.     // Draw a bar for FFT Band i as well as a reflection of the bar
  67.     stroke(barCol1);
  68.     strokeWeight(4);
  69.     line(width/30 + 5*i, 23*height/26, -width/10, width/30 + 5*i, 23*height/26 - 5*band, -width/10);
  70.     stroke(barCol2);
  71.     strokeWeight(3);
  72.     line(width/30 + 5*i, 93*height/104, -width/10, width/30 + 5*i - 2*band, 93*height/104 + 2*band, band/15 - width/10);
  73.     // Make a spot if the magnitude of the band is big enough
  74.     if ((numSpots < MAX_SPOTS) && (index == NUM_ITERATIONS) && (band > 1)) {
  75.       spots[numSpots] = new Spot(5  *width/61 + 5*i, max(2*height/7, 103*height/168 - int(4*band)), spotColor);
  76.       numSpots++;
  77.     }
  78.   }
  79.   // RESET INDEX IF IT'S TIME TO GET ANOTHER FFT SAMPLE
  80.   if (index == NUM_ITERATIONS) {
  81.     index = 0;
  82.   }
  83.   else {
  84.     index++;
  85.   }
  86.   // Update the color and position of all spots
  87.   for (int i=0; i < numSpots; i++) {
  88.     spots[i].update();
  89.     if (index == NUM_ITERATIONS) {
  90.       spots[i].colorChange();
  91.       rBar += spots[i].r;
  92.       gBar += spots[i].g;
  93.       bBar += spots[i].b;
  94.     }
  95.   }
  96.   // Update the color of the bars
  97.   if (index == NUM_ITERATIONS) {
  98.     rBar = rBar/(numSpots - 1);
  99.     gBar = gBar/(numSpots - 1);
  100.     bBar = bBar/(numSpots - 1);
  101.     barCol1 = color(rBar, gBar, bBar, 255);
  102.     barCol2 = color(rBar, gBar, bBar, 150);
  103.     rBar = 0;
  104.     gBar = 0;
  105.     bBar = 0;
  106.   }
  107.   if (numSpots > 80000) {
  108.     numSpots -= 500;
  109.     for (int i=0; i < numSpots; i++) {
  110.       spots[i] = spots[i+499];
  111.     }
  112.   }
  113. }
  114. void stop() {
  115.   song.close();
  116.   minim.stop();
  117.   super.stop();
  118. }
  119. void mousePressed() {
  120.   numSpots = 0;
  121. }
  122. class Spot {
  123.   int x, y, z;
  124.   color c;
  125.   float r, g, b;
  126.   Spot(int tempX, int tempY, color tempC) {
  127.     c = tempC;
  128.     x = tempX;
  129.     y = tempY;
  130.     z = width/15;
  131.     r = red(tempC);
  132.     g = green(tempC);
  133.     b = blue(tempC);
  134.   }
  135.   void update() {
  136.     z -= SPEED/2;
  137.     y += SPEED/20;
  138.     stroke(c);
  139.     strokeWeight(1);
  140.     point(x, y, z);
  141.     //line(y,12*height/21,x,y,z,x);
  142.   }
  143.   void colorChange() {
  144.     if (b > 0 && g == 0) {
  145.       colorSwitch = 0;
  146.     }
  147.     else if (r > 0 && b == 0) {
  148.       colorSwitch = 1;
  149.     }
  150.     else if (g > 0 && r == 0) {
  151.       colorSwitch = 2;
  152.     }
  153.     if (colorSwitch == 0) {
  154.       r++;
  155.       b--;
  156.     }
  157.     else if (colorSwitch == 1) {
  158.       g++;
  159.       r--;
  160.     }
  161.     else if (colorSwitch == 2) {
  162.       b++;
  163.       g--;
  164.     }
  165.     c = color(r, g, b, 255);
  166.   }
  167. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles