奇怪的是。我一直在处理或 java 崩溃,这段代码基于处理网站的示例代码。
在 pc 上它根本不起作用,在一个 mac 上它工作5秒直到它崩溃并在另一个 mac 上只是地壳,给我这个:
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: RtApiCore::probeDeviceOpen: the device (2) does not support the requested channel count.
Could not run the sketch (Target VM failed to initialize).
您认为图书馆或代码存在问题吗? 如果它是库的问题,你能推荐最好的声音库来做这样的事吗?
谢谢:)
import processing.sound.*;
FFT fft;
AudioIn in;
int bands = 512;
float[] spectrum = new float[bands];
void setup() {
size(900, 600);
background(255);
// Create an Input stream which is routed into the Amplitude analyzer
fft = new FFT(this, bands);
in = new AudioIn(this, 0);
// start the Audio Input
in.start();
// patch the AudioIn
fft.input(in);
}
void draw() {
background(255);
int midPointW = width/2;
int midPointH = height/2;
float angle = 1;
fft.analyze(spectrum);
//float radius = 200;
for(int i = 0; i < bands; i++){
// create the actions for placing points on a circle
float radius = spectrum[i]*height*10;
//float radius = 10;
float endX = midPointW+sin(angle) * radius*10;
float endY = midPointH+cos(angle) * radius*10;
float startX = midPointW+sin(angle) * radius*5;
float startY = midPointH+cos(angle) * radius*5;
// The result of the FFT is normalized
// draw the line for frequency band i scaling it up by 5 to get more amplitude.
line( startX, startY, endX, endY);
angle = angle + angle;
println(endX, "" ,endY);
// if(angle > 360){
// angle = 0;
// }
}
}
答案 0 :(得分:1)
如果您打印使用的值,例如角度并开始x,y,您会注意到:
NaN
(不是数字 - 无效)Infinity
(但不超出)其中一个主要问题是这一行:
angle = angle + angle;
你可能指数增加这个你可能不想要的值。
此外,请记住,sin()
和cos()
等三角函数使用弧度而非度数,因此值往往很小。您可以使用模运算符(%)或constrain()函数将值约束为360度或TWO_PI
弧度:
angle = (angle + 0.01) % TWO_PI;
你的angle > 360
支票显示它非常接近。不知道为什么你把它留下了评论。
这是带有调整和评论的代码
import processing.sound.*;
FFT fft;
AudioIn in;
int bands = 512;
float[] spectrum = new float[bands];
void setup() {
size(900, 600);
background(255);
// Create an Input stream which is routed into the Amplitude analyzer
fft = new FFT(this, bands);
in = new AudioIn(this, 0);
// start the Audio Input
in.start();
// patch the AudioIn
fft.input(in);
}
void draw() {
background(255);
int midPointW = width/2;
int midPointH = height/2;
float angle = 1;
fft.analyze(spectrum);
//float radius = 200;
for (int i = 0; i < bands; i++) {
// create the actions for placing points on a circle
float radius = spectrum[i] * height * 10;
//float radius = 10;
float endX = midPointW + (sin(angle) * radius * 10);
float endY = midPointH + (cos(angle) * radius * 10);
float startX = midPointW + (sin(angle) * radius * 5);
float startY = midPointH + (cos(angle) * radius * 5);
// The result of the FFT is normalized
// draw the line for frequency band i scaling it up by 5 to get more amplitude.
line( startX, startY, endX, endY);
//angle = angle + angle;
angle = (angle + 0.01) % TWO_PI;//linearly increase the angle and constrain it to a 360 degrees (2 * PI)
}
}
void exit() {
in.stop();//try to cleanly stop the audio input
super.exit();
}
草图运行超过5分钟,但在关闭草图时,我仍然在OSX上遇到JVM崩溃。 我没有太多使用这个声音库,并没有调查它的内部,但它可能是一个错误。
如果这仍然导致问题,出于实际原因,我建议通过Contribution Manager安装不同的处理库以进行FFT声音分析。 这里有几个库:
两个库都提供FFT示例。