[英]using fft in processing
I was wondering if someone could help me. 我想知道是否有人可以帮助我。 I am currently working on a project using processing to use FFT for an aesthetics project.
我目前正在一个项目上,该项目使用处理将FFT用于美学项目。 What I wanna do is allow the user to import a song, and have the fft act as a visualizer to react and display different colors based on the song.
我想做的是允许用户导入歌曲,并让fft充当可视化工具,以根据歌曲做出反应并显示不同的颜色。 The problem I am having is I can not get the fft itself to work.
我遇到的问题是我无法让fft本身正常工作。 How can I get processing to recognize the fft?
如何获得识别FFT的处理程序?
Here is the fft code I'm using. 这是我正在使用的fft代码。
fft = new FFT(song.bufferSize(), song.sampleRate());
//DRAW FFT
fft.forward(song.mix);
colorMode(HSB, 255);
void wavewave() {
float spread = map(450, 0, width, 1, 21.5);
float x = 0;
for (int i = 0; i < song.sampleRate() && x < width; i += spread)
{
x = i/spread;
stroke(map(fft.getFreq(i), 0, 256, 0, 360) * 2, //Hue
255, //Saturation
255); //Brightness
line(x, 512, x, 512 - fft.getFreq(i) * 4);
}
//map(value, minimum1, maximum1, minimum2, maximum2);
x = 0;
for (int i = 0; i < song.sampleRate() && x < width; i += spread)
{
x = i/spread;
stroke(map(fft.getFreq(i), 0, 256, 0, 360) * 2, //Hue
255, //Saturation
255); //Brightness
line(x, 512, x, 512 + fft.getFreq(i) * 4);
}
}
I keep getting unidentified token fft, as the error 我不断收到身份不明的令牌fft,作为错误
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
import java.io.File;
import java.io.FilenameFilter;
color waveColor;
int waveIncr = 0;
int counter = 0;
int songCounter = 0;
int fadeLevel = 10;
float buttonX;
float buttonY;
float buttonW;
float buttonH;
Minim minim;
AudioPlayer player;
FFT fft;
ArrayList<Songs> s;
int k;
String filename;
boolean isSelected = false;
void setup() {
s = new ArrayList();
textSize(24);
frame.setResizable(false);
background(255);
size(600, 600);
fill(0);
stroke(0);
noFill();
buttonW = 200;
buttonH = 50;
buttonX = width - width/2 - buttonW/2;
buttonY = height/2 - buttonH/2;
// Minim stuff
minim = new Minim(this);
}
void draw() {
background(255);
fill(0);
rectMode(CORNER);
rect(buttonX, buttonY, buttonW, buttonH);
fill(255);
textAlign(LEFT);
text("Import File", buttonX+35, buttonY+30);
if (isSelected) {
// s.get(k).waveform();
s.get(k).wavewave();
}
}
void mouseClicked() {
if (mouseX>buttonX && mouseX < buttonX+buttonW && mouseY > buttonY && mouseY < buttonY+buttonH) {
selectInput("Import music file", "fileSelected");
}
}
/* Taken from Processing.org */
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or user hit cancel");
}
else {
filename = selection.getAbsolutePath();
s.add(new Songs(player, filename, "Filename"));
isSelected = true;
}
}
// stop minim and the player.
void stop() {
player.close();
minim.stop();
super.stop();
}
class Songs {
AudioPlayer song;
String directory;
String songName;
Songs(AudioPlayer song, String directory, String songName) {
song=minim.loadFile(directory);
this.song=song;
this.songName=songName;
song.play();
}
fft = new FFT(song.bufferSize(), song.sampleRate());
//DRAW FFT
fft.forward(song.mix);
colorMode(HSB, 255);
void wavewave() {
float spread = map(450, 0, width, 1, 21.5);
float x = 0;
for (int i = 0; i < song.sampleRate() && x < width; i += spread)
{
x = i/spread;
stroke(map(fft.getFreq(i), 0, 256, 0, 360) * 2, //Hue
255, //Saturation
255); //Brightness
line(x, 512, x, 512 - fft.getFreq(i) * 4);
}
//map(value, minimum1, maximum1, minimum2, maximum2);
x = 0;
for (int i = 0; i < song.sampleRate() && x < width; i += spread)
{
x = i/spread;
stroke(map(fft.getFreq(i), 0, 256, 0, 360) * 2, //Hue
255, //Saturation
255); //Brightness
line(x, 512, x, 512 + fft.getFreq(i) * 4);
}
}
}
You seem to have some syntax errors. 您似乎有一些语法错误。 Here's your code moved about a bit:
这是您的代码移动了一下:
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
import java.io.File;
import java.io.FilenameFilter;
color waveColor;
int waveIncr = 0;
int counter = 0;
int songCounter = 0;
int fadeLevel = 10;
float buttonX;
float buttonY;
float buttonW;
float buttonH;
Minim minim;
AudioPlayer player;
FFT fft;
ArrayList<Songs> s;
int k;
String filename;
boolean isSelected = false;
void setup() {
s = new ArrayList();
textSize(24);
frame.setResizable(false);
background(255);
size(600, 600);
fill(0);
stroke(0);
noFill();
buttonW = 200;
buttonH = 50;
buttonX = width - width/2 - buttonW/2;
buttonY = height/2 - buttonH/2;
// Minim stuff
minim = new Minim(this);
}
void draw() {
background(255);
fill(0);
rectMode(CORNER);
rect(buttonX, buttonY, buttonW, buttonH);
fill(255);
textAlign(LEFT);
text("Import File", buttonX+35, buttonY+30);
if (isSelected) {
// s.get(k).waveform();
s.get(k).wavewave();
}
}
void mouseClicked() {
if (mouseX>buttonX && mouseX < buttonX+buttonW && mouseY > buttonY && mouseY < buttonY+buttonH) {
selectInput("Import music file", "fileSelected");
}
}
/* Taken from Processing.org */
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or user hit cancel");
}
else {
filename = selection.getAbsolutePath();
s.add(new Songs(player, filename, "Filename"));
isSelected = true;
}
}
// stop minim and the player.
void stop() {
player.close();
minim.stop();
super.stop();
}
class Songs {
AudioPlayer song;
String directory;
String songName;
Songs(AudioPlayer song, String directory, String songName) {
song=minim.loadFile(directory);
this.song=song;
this.songName=songName;
song.play();
fft = new FFT(song.bufferSize(), song.sampleRate());
}
void wavewave() {
//DRAW FFT
fft.forward(song.mix);
colorMode(HSB, 255);
float spread = map(450, 0, width, 1, 21.5);
float x = 0;
for (int i = 0; i < song.sampleRate() && x < width; i += spread)
{
x = i/spread;
stroke(map(fft.getFreq(i), 0, 256, 0, 360) * 2, //Hue
255, //Saturation
255); //Brightness
line(x, 512, x, 512 - fft.getFreq(i) * 4);
}
//map(value, minimum1, maximum1, minimum2, maximum2);
x = 0;
for (int i = 0; i < song.sampleRate() && x < width; i += spread)
{
x = i/spread;
stroke(map(fft.getFreq(i), 0, 256, 0, 360) * 2, //Hue
255, //Saturation
255); //Brightness
line(x, 512, x, 512 + fft.getFreq(i) * 4);
}
}
}
You should try the PDE X mode, might help you with errors in the future, plus it's awesome! 您应该尝试使用PDE X模式,将来可能会帮助您解决错误,而且还很棒!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.