Files
sunny9898/task8/wk9/wk9_example_3c/wk9_example_3c.pde
louiscklaw 5637fbf94f update,
2025-02-01 02:07:58 +08:00

32 lines
856 B
Plaintext

import processing.sound.*;
SoundFile song;
Amplitude analyzer;
float[] history;
int index = 0;
// enlarge the bars
float scale = 1; // for beat.aiff
//float scale = 4; // for vibraphon.aiff
void setup() {
size(640, 360);
song = new SoundFile(this, "beat.aiff");
//song = new SoundFile(this, "vibraphon.aiff");
song.loop();
analyzer = new Amplitude(this);
analyzer.input(song);
// create an empty array of size equals width
history = new float[width];
}
void draw() {
background(255);
// update (shift array to the left)
for (int i=0; i<width-1; i++)
history[i] = history[i+1];
// save current amplitude at the last position
history[width-1] = analyzer.analyze();
// draw all recorded amplitude values
for (int i=0; i<width; i++)
line(i, height/2-history[i]*height*scale, i, height/2+history[i]*height*scale);
}