Aabb33666 2022-05-17 15:59 采纳率: 100%
浏览 28
已结题

sound库 控制输入音量改变图形形状 ,void drawContentControlledByAudio(倒数第二段)这一部分代码具体为什么会这样写,是什么意思?

import controlP5.;
import processing.sound.
;

// 声音
AudioIn audioInput;
Amplitude rms;
BeatDetector beatDetector;
float volume;
float rotation = 0;

PGraphics contentLayer;
// controlp5 参数控制部分
ControlP5 cp5;
float rotation_speed = 0;
float xOffSet = 50;
float yOffSet = 50;
float xStep = 15;
float yStep = 15;
float xScale = 0.005;
float yScale = 0.005;
float ellipseD = 10;
float smoothingFactor = 0.25;//平滑因素

// Used for storing the smoothed amplitude value 用于存储平滑后的振幅值
float sum;

void setup() {
size(1000, 800);

audioInput = new AudioIn(this);
audioInput.start();

beatDetector = new BeatDetector(this);
beatDetector.input(audioInput);
beatDetector.sensitivity(140);

rms = new Amplitude(this);
rms.input(audioInput);

contentLayer = createGraphics(width, height);
cp5 = new ControlP5(this);

Group g = cp5.addGroup("group")
.setPosition(50, 50)
.setWidth(300)
.activateEvent(true)
.setBackgroundColor(color(255, 80))
.setBackgroundHeight(300)
.setLabel("Noise Control");

cp5.addSlider("rotation_speed")
.setPosition(10, 10)
.setSize(180, 9)
.setRange(0, 100)
.setValue(0)
.setGroup(g);

cp5.addSlider("xOffSet")
.setPosition(10, 30)
.setSize(180, 9)
.setRange(100, 600)
.setValue(100)
.setGroup(g);

cp5.addSlider("yOffSet")
.setPosition(10, 50)
.setSize(180, 9)
.setRange(100, 600)
.setValue(100)
.setGroup(g);

cp5.addSlider("xStep")
.setPosition(10, 70)
.setSize(180, 9)
.setRange(10, 30)
.setValue(10)
.setGroup(g);

cp5.addSlider("yStep")
.setPosition(10, 90)
.setSize(180, 9)
.setRange(10, 30)
.setValue(10)
.setGroup(g);

cp5.addSlider("xScale")
.setPosition(10, 110)
.setSize(180, 9)
.setRange(1, 10)
.setValue(5)
.setGroup(g);

cp5.addSlider("yScale")
.setPosition(10, 130)
.setSize(180, 9)
.setRange(1, 10)
.setValue(5)
.setGroup(g);

cp5.addSlider("ellipseD")
.setPosition(10, 150)
.setSize(180, 9)
.setRange(5, 20)
.setValue(5)
.setGroup(g);
}

void draw() {
// ControlP5控制
// drawContentByControlP5();

// ControlP5 + Audio 控制
drawContentControlledByAudio();
image(contentLayer, 0, 0);
sum += (rms.analyze() - sum) * smoothingFactor;

// rms.analyze() return a value between 0 and 1. It's
// scaled to height/2 and then multiplied by a fixed scale factor
// Rms.analyze()返回0到1之间的值。它被缩放到高度/2然后乘以一个固定的比例因子
float rms_scaled = sum * (height/2) *2;
}

void controlEvent(ControlEvent theEvent) {
if (theEvent.isController()){
String name = theEvent.getController().getName();
if (name == "rotation_speed") {
rotation_speed = theEvent.getController().getValue();
} else if (name == "xOffSet") {
xOffSet = theEvent.getController().getValue();
} else if (name == "yOffSet") {
yOffSet = theEvent.getController().getValue();
} else if (name == "xStep") {
xStep = theEvent.getController().getValue();
} else if (name == "yStep") {
yStep = theEvent.getController().getValue();
} else if (name == "xScale") {
xScale = theEvent.getController().getValue();
} else if (name == "yScale") {
yScale = theEvent.getController().getValue();
} else if (name == "ellipseD") {
ellipseD = theEvent.getController().getValue();
}
}
}

void drawContentByControlP5() //控制图形形状
{
contentLayer.beginDraw();//内容控件
//contentLayer.background(#171717);
//contentLayer.noStroke();
//contentLayer.fill(random(255));
contentLayer.translate(width/2, height/2);
contentLayer.rotate(frameCount * rotation_speed / 1000);

//for(float x = -xOffSet; x <= xOffSet; x += xStep){
//for(float y = -yOffSet; y <= yOffSet; y += yStep){
// float noiseX = x * xScale / 1000 + 300 + frameCount * 0.01;//帧速率
// float noiseY = y * yScale / 1000 + 0 + frameCount * 0.01;
// float nf = noise(noiseX,noiseY);
// contentLayer.ellipse(x * nf, y * nf, ellipseD * nf, ellipseD * nf);
// }
// }

contentLayer.endDraw();
}

void drawContentControlledByAudio()//控制输入音量改变图形形状
{
volume = rms.analyze();

contentLayer.beginDraw();
contentLayer.colorMode(HSB);
contentLayer.background(0);
contentLayer.noStroke();

float h = volume * 1000;
float s = volume * 1000;
contentLayer.fill(h, s,255); //接触层,tuceng
contentLayer.translate(width/2, height/2);

rotation += volume * 0.65;
contentLayer.rotate(rotation);

float tmpXOffSet = xOffSet + volume * 708;
float tmpYOffSet = yOffSet + volume * 708;
float tmpXStep = xStep + volume * 100;
float tmpYStep = yStep + volume * 100;
float tmpEllipseD = ellipseD * volume * 15;
tmpEllipseD = min(10, tmpEllipseD);

for(float x = -tmpXOffSet; x <= tmpXOffSet; x += tmpXStep){
for(float y = -tmpYOffSet; y <= tmpYOffSet; y += tmpYStep){
float noiseX = x * xScale / 1000 + 300 + frameCount * 0.01;
float noiseY = y * xScale / 1000 + 0 + frameCount * 0.01;
float nf = noise(noiseX,noiseY);
contentLayer.ellipse(x * nf, y * nf, tmpEllipseD * nf, tmpEllipseD * nf);
}
}

contentLayer.endDraw();
}

void keyPressed() {
if (key == 's' || key == 'S') {
save("Day_013.png");
}
}

  • 写回答

1条回答 默认 最新

  • miamialalala 2022-05-17 21:28
    关注

    就是在xy上根据noise随机画椭圆,

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月17日
  • 已采纳回答 5月17日
  • 创建了问题 5月17日

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。