つよくなりたいなあ

地に足をつけず世界を生きぬくブログです

Processingでスペクトグラム

f:id:heyassy:20171231184339j:plain

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;

Minim minim;
AudioInput in;
FFT fft;

float[][] values;
int timeWidth = 150;

void setup() {
  size(640, 640, P3D);
  colorMode(HSB,100);
  minim = new Minim(this);
  in = minim.getLineIn(Minim.STEREO, 1024);
  fft = new FFT(in.bufferSize(), in.sampleRate());

  values = new float[fft.specSize()][timeWidth];
  for (int i = 0; i < fft.specSize(); i++) {
    for (int j = 0; j < timeWidth; j++) {
      values[i][j] = 0.0;
    }
  }
  fill(255);
  stroke(77,188,255);
  strokeWeight(2);
}

void draw() {
  fft.forward(in.mix);
  for (int i = 0; i< fft.specSize(); i++) {
    for (int j = 0; j < timeWidth - 1; j++) {
      if (j != timeWidth - 2) {
        values[i][j] = values[i][j+1];
      } else {
        values[i][j] = fft.getBand(i);
      }
    }
  }
  background(100);
  translate(width/2, height/2, -90);
  rotateX(PI/3);
  rotateZ(PI/12);
  for (int i = 0; i < fft.specSize(); i += 10) {
    beginShape();
    for (int j = 0; j < timeWidth; j++) {
      float x = map(j, 0, timeWidth - 2, -width/2, width/2);
      float y = map(i, 0, fft.specSize() - 2, height/2, -height/2);
      float z = values[i][j] * 10;
      vertex(x+1, y, z);
    }
    endShape();
  }
}