Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Processing Sound Library, Example 6
- *
- * This sketch shows how to use the Amplitude class to analyze a
- * stream of sound. In this case a sample is analyzed. The smoothFactor
- * variable determines how much the signal will be smoothed on a scale
- * from 0 - 1.
- */
- import processing.sound.*;
- // Declare the processing sound variables
- SoundFile sample;
- Amplitude rms;
- // Declare a scaling factor
- float scale = 4.0;
- // Declare a smooth factor
- float smoothFactor = 0.25;
- // Used for smoothing
- float sum;
- float x, y;
- void setup() {
- size(800, 600, P2D);
- smooth(8);
- background(238);
- //Load and play a soundfile and loop it
- sample = new SoundFile(this, "sail.mp3");
- sample.loop();
- // Create and patch the rms tracker
- rms = new Amplitude(this);
- rms.input(sample);
- }
- void draw() {
- // Smooth the rms data by smoothing factor (value will mostly vary between 0 and 0.25)
- sum += (rms.analyze() - sum) * smoothFactor;
- //println(sum);
- //outlines();
- //lines();
- //if (frameCount%4==0) clouds();
- //diag();
- }
- void outlines() {
- float sz = map(sum*scale, 0, 1, 5, height/2);
- stroke(34, 25);
- noFill();
- if (sum>0.01) ellipse(width/2, height/2, sz, sz);
- }
- void lines() {
- float maxH = height/6;
- rectMode(CENTER);
- float h = map(sum*scale, 0, 1, 0, maxH/2);
- fill(34);
- noStroke();
- rect(x, y+maxH/2, 1, h);
- x++;
- if (x > width) {
- x = 0;
- y = y + maxH;
- //hue = random(360);
- }
- if (y > height-maxH) y = 0;
- }
- void clouds() {
- rectMode(CENTER);
- float cellSize = 20;
- float sz = map(sum*2, 0, 1, 0.25, 3)*cellSize;
- stroke(238);
- fill(34);
- //if (sum>0.4) fill(225,76,69);
- if (sum>0.01) {
- //noStroke();
- ellipse(cellSize/2 + x, cellSize/2 + y, sz, sz);
- x += cellSize;
- }
- if (x > width) {
- x = 0;
- y = y + cellSize;
- }
- }
- void diag() {
- stroke(34, 25);
- y = map(sum*scale, 0, 1, height, 0);
- if (sum>0.05) line(0, y, width, height-y);
- }
- void keyPressed() {
- saveFrame("image-###.png");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement