Advertisement
xeromino

audio

Mar 12th, 2017
201
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.  
  16. // Declare a scaling factor
  17. float scale = 4.0;
  18.  
  19. // Declare a smooth factor
  20. float smoothFactor = 0.25;
  21.  
  22. // Used for smoothing
  23. float sum;
  24. float x, y;
  25.  
  26. void setup() {
  27.   size(800, 600, P2D);
  28.   smooth(8);
  29.   background(238);
  30.   //Load and play a soundfile and loop it
  31.   sample = new SoundFile(this, "sail.mp3");
  32.   sample.loop();
  33.  
  34.   // Create and patch the rms tracker
  35.   rms = new Amplitude(this);
  36.   rms.input(sample);
  37. }      
  38.  
  39. void draw() {
  40.   // Smooth the rms data by smoothing factor (value will mostly vary between 0 and 0.25)
  41.   sum += (rms.analyze() - sum) * smoothFactor;
  42.   //println(sum);
  43.   //outlines();
  44.   //lines();
  45.   //if (frameCount%4==0) clouds();
  46.   //diag();
  47. }
  48.  
  49. void outlines() {
  50.   float sz = map(sum*scale, 0, 1, 5, height/2);
  51.   stroke(34, 25);
  52.   noFill();
  53.   if (sum>0.01) ellipse(width/2, height/2, sz, sz);
  54. }
  55.  
  56. void lines() {
  57.   float maxH = height/6;
  58.   rectMode(CENTER);
  59.   float h = map(sum*scale, 0, 1, 0, maxH/2);
  60.   fill(34);
  61.   noStroke();
  62.   rect(x, y+maxH/2, 1, h);
  63.   x++;
  64.  
  65.   if (x > width) {
  66.     x = 0;
  67.     y = y + maxH;
  68.     //hue = random(360);
  69.   }
  70.  
  71.   if (y > height-maxH) y = 0;
  72. }
  73.  
  74. void clouds() {
  75.   rectMode(CENTER);
  76.   float cellSize = 20;
  77.   float sz = map(sum*2, 0, 1, 0.25, 3)*cellSize;
  78.   stroke(238);
  79.   fill(34);
  80.   //if (sum>0.4) fill(225,76,69);
  81.   if (sum>0.01) {
  82.     //noStroke();
  83.     ellipse(cellSize/2 + x, cellSize/2 + y, sz, sz);
  84.     x += cellSize;
  85.   }
  86.   if (x > width) {
  87.     x = 0;
  88.     y = y + cellSize;
  89.   }
  90. }
  91.  
  92. void diag() {
  93.   stroke(34, 25);
  94.   y = map(sum*scale, 0, 1, height, 0);
  95.   if (sum>0.05) line(0, y, width, height-y);
  96. }
  97.  
  98. void keyPressed() {
  99.   saveFrame("image-###.png");
  100. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement