首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

p5js-声音可视化

演示视频:https://www.bilibili.com/video/av23136993

代码:

var song, fft;

var tx =[];

var ty = [];

var big = [];

var bb = [];

var speed= [];

var rr= [];

var gg= [];

var bb= [];

var a = 0;

var r = 0;

var now = 0;

function preload() {

// we have included both an .ogg file and an .mp3 file

soundFormats('ogg', 'mp3');

song = loadSound('data/4.mp3');

}

function setup() {

createCanvas(1500, 900);

song.play();

fft = new p5.FFT();

fft.setInput(song);

for (var i = 0; i

bb[i] = random(300);//x坐标

speed[i] = random(3,8);

rr[i] = random(255);

gg[i] = random(255);

bb[i] = random(255);

}

noStroke();

}

function mousePressed() {

if ( song.isPlaying() ) { // .isPlaying() returns a boolean

song.pause();

} else {

song.play(); // playback will resume from the pause position

}

now = 1- now ;

}

function draw() {

background(255);

var spectrum = fft.analyze();

for (var i = 0; i

{

push;

translate(700, 450);

rotate(2*PI/200);

translate(-700, -450);

rect(700, 300, 6, -spectrum[i]);

pop;

}

for (var i = 0; i

bb[i]+=speed[i];

if(bb[i]> 300)

bb[i] = 0;

tx[i] = 700+cos(r)*bb[i];//x坐标

ty[i] = 450+sin(r)*bb[i];//y坐标

r+=2*PI/200;//增加

}

var w = -0.5*PI/200;//角度

for (var i = 0; i

big[i] = spectrum[i];

fill(rr[i],gg[i],bb[i],10+mouseX);

if(now == 0)

ellipse(tx[i],ty[i],20+big[i]/8,20+big[i]/8);

else

rect(tx[i],ty[i],20+big[i]/8,20+big[i]/8);

}

w+=2*PI/200;//增加

}

工程文件:链接:https://pan.baidu.com/s/1pQdtF_unjwf8UmtKjyl8Nw 密码:ut66

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180708G0Z7Z700?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券