processing 绘制一个声波波形动态图
时间: 2024-02-04 08:02:50 浏览: 21
实现声波波形动态图需要用到 Processing 中的 `minim` 库,该库提供了音频的输入和处理功能。下面是一个简单的实现代码:
```java
import ddf.minim.*;
import ddf.minim.analysis.*;
Minim minim;
AudioInput in;
FFT fft;
void setup() {
size(800, 400);
minim = new Minim(this);
in = minim.getLineIn(Minim.MONO, 512);
fft = new FFT(in.bufferSize(), in.sampleRate());
}
void draw() {
background(0);
fft.forward(in.mix);
stroke(255);
noFill();
beginShape();
for (int i = 0; i < fft.specSize(); i++) {
vertex(map(i, 0, fft.specSize(), 0, width), map(fft.getBand(i), 0, 100, height, 0));
}
endShape();
}
```
该程序将会打开你的电脑内置麦克风,并将其输入的音频数据转换成 FFT 频谱数据,然后将其可视化为波形图。你可以尝试说话或播放一些音乐,观察波形图的变化。如果需要停止程序,可以按下 `Ctrl+C`。
相关问题
processing 绘制 一个 弹簧振动小球
好的,以下是使用 Processing 绘制弹簧振动小球的代码示例:
```java
float x = 150; //小球初始位置
float y = 150;
float velocity = 0; //小球初始速度
float acceleration = 0; //小球初始加速度
float mass = 1; //小球质量
float k = 0.1; //弹簧弹性系数
float damping = 0.9; //弹簧阻尼系数
float restLength = 100; //弹簧自然长度
float springForce; //弹簧力
float frictionForce; //摩擦力
float gravity = 0.1; //重力加速度
float ballRadius = 20; //小球半径
float springStartX = 100; //弹簧起点位置
float springStartY = 100;
float springEndX = 200; //弹簧终点位置
float springEndY = 200;
float springLength; //弹簧长度
void setup() {
size(400, 400);
}
void draw() {
background(255);
springLength = dist(springStartX, springStartY, springEndX, springEndY); //计算弹簧长度
springForce = -k * (springLength - restLength); //计算弹簧力
frictionForce = -damping * velocity; //计算摩擦力
acceleration = (springForce + frictionForce + gravity) / mass; //计算小球加速度
velocity += acceleration; //更新小球速度
y += velocity; //更新小球位置
//绘制弹簧
stroke(0);
strokeWeight(5);
line(springStartX, springStartY, x, y);
line(x, y, springEndX, springEndY);
//绘制小球
noStroke();
fill(255, 0, 0);
ellipse(x, y, ballRadius * 2, ballRadius * 2);
//限制小球运动范围
if (y > height - ballRadius) {
y = height - ballRadius;
velocity *= -1;
velocity *= damping;
}
if (y < ballRadius) {
y = ballRadius;
velocity *= -1;
velocity *= damping;
}
}
```
运行代码后,可以看到一个弹簧振动的小球。你可以根据需要调整弹簧的弹性系数、阻尼系数、重力加速度等参数,来改变小球的运动轨迹和弹簧的振动情况。
用processing三角函数绘制一个效果
以下是一个使用Processing三角函数绘制的简单例子,可以绘制一个旋转的正方形:
```
float angle = 0;
void setup() {
size(400, 400);
}
void draw() {
background(255);
translate(width/2, height/2);
rotate(radians(angle));
rect(-50, -50, 100, 100);
angle += 2;
}
```
在这个例子中,我们使用rotate()函数将坐标系旋转了一定角度(angle),然后绘制一个矩形。在每次draw()函数被调用时,我们都将角度增加2,使得矩形不断旋转。