Advertisement
xeromino

audiograph

Apr 29th, 2016
509
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Processing Sound Library, Example 6
  3.  *
  4.  * This sketch shows how to use the Amplitude class to analyze a
  5.  * stream of sound. In this case a sample is analyzed. The smoothFactor
  6.  * variable determines how much the signal will be smoothed on a scale
  7.  * from 0 - 1.
  8.  */
  9.  
  10. import processing.sound.*;
  11.  
  12. // Declare the processing sound variables
  13. SoundFile sample;
  14. Amplitude rms;
  15. Boolean on = false;
  16. String title = "4seasons";
  17.  
  18. // Declare a scaling factor
  19. float scale = 4.0;
  20.  
  21. // Declare a smooth factor
  22. float smoothFactor = 0.25;
  23.  
  24. // Used for smoothing
  25. float sum;
  26.  
  27. void setup() {
  28.   size(800, 800, P2D);
  29.   smooth(8);
  30.   background(238);
  31.  
  32.   //Load and play a soundfile and loop it
  33.   sample = new SoundFile(this, title+".mp3");
  34.   sample.loop();
  35.  
  36.   // Create and patch the rms tracker
  37.   rms = new Amplitude(this);
  38.   rms.input(sample);
  39. }      
  40.  
  41. void draw() {
  42.   strokeWeight(1);
  43.   noFill();
  44.   if (on) {
  45.     stroke(238, 10);
  46.   } else {
  47.     stroke(35, 10);
  48.   }
  49.  
  50.   if (frameCount==4000) {
  51.     save(title + "_" + frameCount +".png");  //on =! on;
  52.     noLoop();
  53.   }
  54.   // Smooth the rms data by smoothing factor
  55.   sum += (rms.analyze() - sum) * smoothFactor;  
  56.  
  57.   // rms.analyze() return a value between 0 and 1. It's
  58.   // scaled to height/2 and then multiplied by a scale factor
  59.   float rmsScaled = sum * (height/2) * scale;
  60.  
  61.   // Draw an ellipse at a size based on the audio analysis
  62.   ellipse(width/2, height/2, rmsScaled, rmsScaled);
  63.  
  64.   println(frameCount);
  65. }
  66.  
  67. void keyPressed() {
  68.   save(title + "_" + frameCount +".png");
  69. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement