performance - 处理帧率非常低且不可更改
问题描述
我正在构建一个同时使用不断变化的背景图像、振荡器和声音文件的处理草图。
基本上每次draw(),背景都会改变,振荡器以特定频率播放并播放一个短音频文件
现在,问题是:无论我设置什么帧率,它总是低于 5。我有一个 NVIDIA 960 和 16GB 的 RAM,所以我真的不认为这是硬件限制(?)
这是我的 Setup(),我在其中设置帧速率值。
import processing.sound.*;
SoundFile voice;//Quad [] quads;
PImage bg;
//Quad quads;
ArrayList<Quad> quadsArray;
int j=0;
int randomImg;
int randomVoice;
int playVoiceorNot;
TriOsc triangle;
SawOsc saw;
Reverb reverb;
int playSaw;
int timetoChange;
int randomFrate;
int prevVoice;
int screenWidth = displayWidth;
int screenHeight = displayHeight;
void setup(){
fullScreen();
//size(1920,1080);
//quads=new Quad();
quadsArray=new ArrayList<Quad>();
triangle = new TriOsc(this);
saw = new SawOsc(this);
triangle.amp(0.1);
saw.amp(0.1);
prevVoice=0;
frameRate(30);
//background(255);
//reverb = new Reverb(this);
bg = loadImage("1.png");
bg.resize(width,height);
background(bg);
triangle.play();
saw.play();
}
void draw(){
// println(frameRate);
//frameRate(60);
randomImg=floor(random(19));
//randomImg=4;
bg=loadImage(randomImg+".png");
bg.resize(width, height);
background(bg);
//filter(INVERT);
//----controllo se è tempo di cambiare framerate. Possibilità del 2%----
//timetoChange=floor(random(100));
// randomFrate=floor(random(4,15));
//if(timetoChange>=98){
//frameRate(randomFrate);
// }
//-----------------------------------------------------------------------
//pusho un nuovo Quad nell'arrayList-------------------------------------
quadsArray.add(new Quad());
//----------PER OGNI QUAD, lo displayo e lo updato-----------------------
//----------regolo anche la stroke weight in base alla width dei quadrati
for(Quad current:quadsArray){
blendMode(MULTIPLY);
strokeWeight(4);
triangle.freq(floor(random(100,current.quadwidth/2)));
saw.freq(0);
//reverb.damp(0.8);
//reverb.room(0.7);
// reverb.process(triangle);
playSaw=floor(random(100));
if(playSaw>70){
saw.freq(floor(random(100,current.quadwidth)));
// reverb.damp(0.8);
// reverb.room(0.7);
// reverb.process(saw);
strokeWeight(8);
}
else{
saw.stop();
}
current.display();
current.update();
}
//------------------------------------------------------------------------
//---Ciclo per la gestione dei quads con lifespan terminata-------
for(int i=quadsArray.size()-1;i>=0;i--){
Quad temp=quadsArray.get(i);
if(temp.lifespan<=0){
quadsArray.remove(i);
}
}
println(quadsArray.size());
//---------------------------------------------------------------------------
//--controllo se playare una voce o meno. Possibilità del 15%----------------
playVoiceorNot=floor(random(100));
if(playVoiceorNot>=85){
// Load a soundfile from the /data folder of the sketch and play it back
while(randomVoice==prevVoice){
randomVoice=floor(random(70));
}
prevVoice=randomVoice;
voice = new SoundFile(this,randomVoice + ".wav");
voice.amp(1);
voice.play();
}
//----------------------------------------------------------------------------
}
这是四边形对象:
class Quad{
float x;
float y;
float quadwidth;
float quadheight;
float lifespan;
float moving;
Quad(){ //constructor
x=width/2;
y=height/2;
quadwidth=floor(random(80,width/2));
quadheight=floor(random(height/2));
lifespan=255;
moving=0;
}
void display(){
stroke(255,0,0,lifespan);
//strokeWeight(3);
noFill();
rectMode(CENTER);
rect(x,y,quadwidth,quadheight);
}
void update(){
//fill(125,100,30,lifespan);
lifespan=lifespan-20;
moving++;
}
}
感谢您的回复!
解决方案
这两行很可能是罪魁祸首:
bg=loadImage(randomImg+".png");
voice = new SoundFile(this,randomVoice + ".wav");
您不想每帧都加载文件 - 它非常慢。相反,在 setup() 中加载您可能需要的所有文件(例如,放入背景的ArrayList
ofPImage
中),然后在draw()循环中访问它们。
我还发现 Processing 的图像resize()函数非常昂贵,所以在加载它们时调用它,而不是在draw()循环中调用。
推荐阅读
- c# - 如何在 Acumatica 销售订单中根据客户的税区显示价值
- kubernetes - GKE 在超过 5 个时杀死 pod
- python - Plotly:如何在 Barpolar 图上添加 Scatterpolar?
- asp.net - 无法使用 .Net Framework 库在 IIS 10 .Net Core 应用程序上加载文件或程序集“Microsoft.AspNet.SessionState.SessionStateModule”
- java - 授予 Elasticsearch 访问 Java 的适当权限
- visual-studio - Git fetch 在 Visual Studio 中未检索到任何结果
- f# - F#中如何处理类似于Typescript的非结构化数据
- sql - 如何在数据透视表中使用汇总/多维数据集(汇总行)
- sql-server - 如何使用 SQL Server 获取 Join 中的所有值?
- python - 过滤掉 NOT isin 中的字典