gpt4 book ai didi

audio - 具有处理和音频输入的NullPointerException

转载 作者:行者123 更新时间:2023-12-02 22:32:39 26 4
gpt4 key购买 nike

我正在处理“处理”草图,在该草图中,我从音频文件获取输入,该音频文件的主要频率正映射到三个动画“蛇”的运动。该代码有时可以正常运行,有时则不能正常运行(NullPointerException)!我想这与某些细节有关,我想念/不了解,但我感到困惑,因为我的代码每次都不会中断!如果您可以检查并找出问题的所在,我将不胜感激!
这是主要的草图:

import beads.*;

// sound variables
AudioContext ac;
Gain g;
PowerSpectrum ps;
Frequency f;
String sourceFile;
SamplePlayer player;
float inputFrequency;
float mappedFrequency;
float prevFrequency;

// visual variables
ShapeA shA1;
ShapeA shA2;
ShapeA shA3;
boolean shA1pressed, shA2pressed, shA3pressed;

void setup() {
size(900, 600);
frameRate(30); // doesn't affect sound
inputFrequency = 0;
mappedFrequency = 0;
prevFrequency = 0;

// get sound source
ac = new AudioContext();
sourceFile = sketchPath("") + "data/rite1.mp3";
try {
player = new SamplePlayer(ac, new Sample(sourceFile));
}
catch(Exception e)
{
println("Exception while attempting to load sample!");
e.printStackTrace();
exit();
}
g = new Gain(ac, 2, 0.3);
g.addInput(player);
ac.out.addInput(g);

/* prerequisites for getting main frequency */
ShortFrameSegmenter sfs = new ShortFrameSegmenter(ac);
sfs.addInput(ac.out);
FFT fft = new FFT();
sfs.addListener(fft);
ps = new PowerSpectrum();
fft.addListener(ps);
f = new Frequency(44100.0f);
ps.addListener(f);
ac.out.addDependent(sfs);
/* end of prerequistes */

ac.start();

smooth();
noStroke();
//strokeWeight(2); // affecting the size of a point
shA1 = new ShapeA(3); // random factor affecting angle
shA2 = new ShapeA(6);
shA3 = new ShapeA(8);
shA1pressed = false;
shA2pressed = false;
shA3pressed = false;
}

void draw() {
//background(255);
fill(255,10);
rect(0-10,0-10,width+20,height+20);

text(" Input Frequency: ", -100, -100); // for some reason, needed!!
inputFrequency = f.getFeatures(); // ** THIS IS THE MAIN FREQUENCY **
mappedFrequency = map(inputFrequency,20,2000,0,20); // computational power!

if (shA1pressed) {
shA1.angle();
shA1.display(shA1.angle);
}
if (shA2pressed) {
shA2.angle();
shA2.display(shA2.angle);
}
if (shA3pressed) {
shA3.angle();
shA3.display(shA3.angle);
}

prevFrequency = inputFrequency;
}

void keyPressed() {
if (key == '1') {
if (shA1pressed == false) {
shA1pressed = true;
} else {
shA1pressed = false;
}
}
if (key == '2') {
if (shA2pressed == false) {
shA2pressed = true;
} else {
shA2pressed = false;
}
}
if (key == '3') {
if (shA3pressed == false) {
shA3pressed = true;
} else {
shA3pressed = false;
}
}
}

这是ShapeA类:

class ShapeA {
float angleCount = 7;
float stepSize = 3.3;
int i = 0;
float angle = radians(270);
float r;

ArrayList<PVector> points = new ArrayList<PVector>();

ShapeA(float _r) {
// set position of first point
points.add(new PVector(random(width), random(height)));
r = _r;
}

float angle() {
if (prevFrequency <= inputFrequency) {
angle = angle - radians(mappedFrequency*random(r));
} else {
angle = angle + radians(mappedFrequency*random(r));
}
return angle;
}

void display(float _angle) {
PVector point = points.get(i);
fill(0);
ellipse(point.x, point.y, 3, 3);

// add a new point
PVector prevPoint = points.get(points.size()-1);
float x = prevPoint.x + cos(_angle) * stepSize;
float y = prevPoint.y + sin(_angle) * stepSize;
points.add(new PVector(x, y));

// check if shape hits display borders
float a = (floor(random(-angleCount, angleCount)) + 0.5) * 90.0/angleCount;
if (y <= 0) {
angle = a+90;
}
else if (x >= width) {
angle = a+180;
}
else if (y >= height) {
angle = a-90;
}
else if (x <= 0) {
angle = a;
}
i++;
}

}

当我收到一条错误消息时,这是一条NullPointerException消息,突出显示了这一行(在主草图中):

inputFrequency = f.getFeatures(); // ** THIS IS THE MAIN FREQUENCY **



此外,我还注意到,通过删除以下行

text(" Input Frequency: ", -100, -100);



我也收到相同类型的消息,尽管该行没有用!

谁能解释我,为什么这些“事情”发生了?

提前致谢,
-伊利亚斯

最佳答案

将来,您应该尝试发布遇到的任何错误的全文,以及指向所使用库的链接。

但是我有一个理论:

Frequency 类具有一个称为 getFeatures() 的函数,该函数返回Float

注意该Float中的大写字母F。它是一个对象,而不是原始的小写float这意味着它可以为空

然后,当您通过拆箱将其转换为小写的float时,会得到一个NullPointerException,因为它不知道如何将null转换为float

我不知道为什么该函数返回null的;您必须检查文档并为此进行一些调试。但是您要么必须追查原因并加以阻止,要么必须添加一个空检查,仅当该值不为空时才输入代码。像这样:

Float returnedFrequency = f.getFeatures();
if(returnedFrequency != null){
inputFrequency = returnedFrequency;
//rest of your code

至于为什么需要 text()函数,我的猜测是这只是一个巧合。我猜 也许是,它会导致 Frequency类中发生的任何事情都造成足够的延迟,但是该延迟应该只有几毫秒,所以我真的怀疑是这样。无论如何,如果您查找空值的原因或添加空值检查,这将成为一个有争议的问题。

顺便说一句,您在这里发布了很多额外的代码-您的问题与键盘无关,因此所有这些代码都只会使您的问题变得混乱。下次,尝试创建 MCVE-仅运行几行即可得到相同的错误,而不是整个草图。祝好运。

关于audio - 具有处理和音频输入的NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35745755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com