Created
February 8, 2016 00:58
-
-
Save rniwase/c56abdafeccabefc42a0 to your computer and use it in GitHub Desktop.
Processing 3.0.1でオーディオ入力からログスケールのスペクトルを描画する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* オーディオ入力からログスケールのスペクトルを描画 | |
* Processing 3.0.1 要Soundライブラリ | |
*/ | |
import processing.sound.*; | |
int bands = 256; // バンド数 | |
int average = 4; // 移動平均数 | |
float minrange = -50.0; // 表示最小振幅 [dBFS] | |
FFT fft; | |
AudioIn in; | |
float[][] spectrum = new float[average][bands]; | |
float[] spectrum_avr = new float[bands]; | |
void setup() { | |
size(1024, 256); | |
background(255); | |
noSmooth(); | |
fft = new FFT(this, bands); | |
in = new AudioIn(this, 0); // 左チャンネル入力 | |
in.start(); | |
fft.input(in); | |
} | |
void draw() { | |
background(255); | |
fft.analyze(spectrum[0]); | |
for (int b=0; b<bands; b++) { | |
spectrum_avr[b] = 0; | |
for (int a=0; a<average; a++) { | |
spectrum_avr[b] += spectrum[a][b]; | |
} | |
spectrum_avr[b] /= average; | |
} | |
pushMatrix(); | |
translate(0, height); | |
fill(0); | |
noStroke(); | |
for (int i=1; i<bands; i++) { | |
rect(bar_w(i), 0, bar_w(i+1)-bar_w(i), -bar_h(spectrum_avr[i], minrange)); | |
// リニアスケールの場合以下を使用 | |
//rect((width/bands)*i, 0, width/bands, -height*spectrum[0][i]); | |
} | |
popMatrix(); | |
for (int b=0; b<bands; b++) { | |
for (int a=average-1; a>0; a--) { | |
spectrum[a][b] = spectrum[a-1][b]; | |
} | |
} | |
} | |
int bar_h (float amp, float range) { | |
if (amp <= 0.0) { | |
return 0; | |
} | |
amp = 20 * log(amp) / log(10); | |
if (amp >= range) { | |
amp /= -range; | |
amp += 1.0; | |
} else { | |
return 0; | |
} | |
return (int)(amp * height); | |
} | |
int bar_w(int f) { | |
float freq; | |
freq = log(f)/log(bands); | |
return (int)(freq*width); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment