NINのブログ

主に機械学習とか統計モデリングとか金融とか

ProcessingでSound Visualization

Processingでリアルタイム波形処理をし、可視化します。
音楽ファイルはStudioOneで作成しました。

import ddf.minim.*;

Minim minim;
AudioPlayer player;

void setup()
{
  size(600, 600, P3D);
  colorMode(RGB,256);
  minim = new Minim(this);//インスタンス
  
  // loadFile will look in all the same places as loadImage does.
  // this means you can find files that are in the data folder and the 
  // sketch folder. you can also pass an absolute path, or a URL.
  player = minim.loadFile("Mixdown.mp3");
  
  // play the file from start to finish.
  // if you want to play the file again, 
  // you need to call rewind() first.
  player.play();
}

void draw()
{
  background(0);//彩度
  stroke(200);
  // draw the waveforms
  // the values returned by left.get() and right.get() will be between -1 and 1,
  // so we need to scale them up to see the waveform
  // note that if the file is MONO, left.get() and right.get() will return the same value
  for(int i = 0; i < player.bufferSize() - 1; i++)
  {
    float x1 = map( i, 0, player.bufferSize(), 0, width );
    float x2 = map( i+1, 0, player.bufferSize(), 0, width );
    stroke(#47B8FC);   
    line( x1, 60 + player.left.get(i)*100, x2, 80 + player.left.get(i+1)*100 );
    line( x1, 520 + player.right.get(i)*100, x2, 540 + player.right.get(i+1)*100 );
    noStroke ();
    fill(#FA0D15);
    ellipse(200,200,player.left.get(i)*50,player.right.get(i)*50);
    fill(#FFFCFD);
    ellipse(200,400,player.left.get(i)*50,player.right.get(i)*50);
    fill(#FFFCFD);
    ellipse(400,200,player.left.get(i)*50,player.right.get(i)*50);
    fill(#FFFCFD);
    ellipse(400,400,player.left.get(i)*50,player.right.get(i)*50);
  }
}

出力