Created
February 8, 2016 12:04
-
-
Save rniwase/8efe48c365669c2db46b 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; // バンド数 | |
float minrange = -60.0; // 表示最小振幅 [dBFS] | |
FFT fft; | |
AudioIn in; | |
float[] spectrum = new float[bands]; | |
int count = 0; | |
void setup() { | |
size(1024, 512); | |
colorMode(HSB); | |
background(2*256/3, 255, 128); | |
noSmooth(); | |
fft = new FFT(this, bands); | |
in = new AudioIn(this, 0); // 左チャンネル | |
in.start(); | |
fft.input(in); | |
} | |
void draw() { | |
if (count >= width) { | |
count = 0; | |
} | |
fft.analyze(spectrum); | |
pushMatrix(); | |
translate(0, height); | |
for (int i=1; i<bands; i++) { | |
int h = regamp(spectrum[i], minrange); | |
stroke(hshift(-h, -256/3), 255, h+128); | |
line(count, -reghgt(i), count, -reghgt(i+1)); | |
} | |
popMatrix(); | |
count++; | |
} | |
int regamp (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 * 255); | |
} | |
int reghgt(int f) { | |
float freq; | |
freq = log(f)/log(bands); | |
return (int)(freq * height); | |
} | |
int hshift(int h, int s) { | |
h += s; | |
if (h>=256) { | |
h -= 256; | |
} else if (h <= 0) { | |
h += 256; | |
} | |
return h; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment