• “MusichePerMentivuote i#4″ – Processing •

Low quality Video (VIMEO link : https://vimeo.com/77861437)

Processing Code


import beads.*;

AudioContext ac;
SamplePlayer player;
Sample buf;

FFT fft;
PowerSpectrum ps;
Frequency f;
SpectralCentroid sc;
SpectralPeaks sp;
int nPeaks;
Gain g;

String source;
SamplePlayer.LoopType tipo;

float fColor = 0;

void setup()
{
  size(512,512,P3D);
  background(0);
  ellipseMode(CENTER); 

  nPeaks = 128;

  /*schema
  sample - (input) -> gain g - (input) ->  ac.out --> USCITA
                                                  |
                                                  | --> ShortFrameSegmenter - FFT-Pot-Freq
  */ 

  ac = new AudioContext();
  g = new Gain(ac, 1, 1); 

  source=sketchPath("") + "audio/MusichePerMentivuote_i#4.aif";

  try
  {
    buf =  new Sample(source);
    player = new SamplePlayer(ac, new Sample(source));
    player.setKillOnEnd(false);
    //player.setToLoopStart();
    //player.setLoopType(tipo.LOOP_FORWARDS);
    player.start();
    g.addInput(player);
  }
  catch (Exception e)
  {
    println("ERROR !!!!");
    e.printStackTrace();
    exit();
  }
  ac.out.addInput(g);

  ShortFrameSegmenter sfs = new ShortFrameSegmenter(ac);
  sfs.addInput(ac.out);

  fft = new FFT();
  sfs.addListener(fft);
  ps = new PowerSpectrum();
  fft.addListener(ps);
  f = new Frequency((float)ac.getSampleRate());
  ps.addListener(f);
  sc = new SpectralCentroid((float)ac.getSampleRate());
  ps.addListener(sc);
  sp = new SpectralPeaks(ac,nPeaks);
  ps.addListener(sp);
  ac.out.addDependent(sfs);
  ac.start(); 

}

void draw()
{
  background(map(fColor,30,255,10,40));
  drawSpectrum();
}

void drawSpectralPeaks()
{
  if( sp.getFeatures() != null )
  {
    float x = 0;
    float y = 0;
    int s = 0;

    float[][] spectral  = sp.getFeatures(); //[Freq, Pot]
    for (int i = 0; i < nPeaks; i++)
    {
      int f = Math.min((int)(spectral[i][0] * width), width - 1);
      s = Math.min((int)(spectral[i][1] * (height-40)), height - 40- 1);
      fColor = map(spectral[i][0],0,22050,30,255);

      float f1 = random(f);
      float f2 = random(f);        

      noStroke();
      smooth();
      fill(fColor,fColor,fColor);
      ellipse(f1,f2,s,s);
      fill(255);
      stroke(5);
      ellipse(f1,f2,s/70,s/70);
    }
  }
}

void drawSpectrum()
{
  float[] features = ps.getFeatures();
  if(features != null)
  {
    for(int x = 0; x < width; x++)
    {
      int featureIndex = (x * features.length) / width;
      int barHeight = Math.min((int)(features[featureIndex] * 255), 255 - 1);
      stroke(barHeight);

      if( (x<=20) && (barHeight>0) )
      {
          line(map(x,0,20,236,256), 0, map(x,0,20,236,256), height);
          line(map(x,0,20,276,256), 0, map(x,0,20,276,256), height);
      }

      if( ((x>20) && (x<=60)) && (barHeight>0) )
      {
          line(map(x,21,60,176,216), 0, map(x,21,60,176,216), height);
          line(map(x,21,60,336,286), 0, map(x,21,60,336,286), height);
      }

      if( (x>60) && (barHeight>0) )
      {
          line(map(x,61,512,0,155), 0, map(x,61,512,0,155), height);
          line(map(x,61,512,512,355), 0, map(x,61,512,512,355), height);
      }

      if ( x>350 )
      {
        line(0, map(x,61,512,0,155), width, map(x,61,512,0,155));
        line(0, map(x,61,512,512,355), width, map(x,61,512,512,355));
      }

      if ((x>480)  && (barHeight>0) )
      {
          drawSpectralPeaks();
      }
    }
  }
}

MusichePerMentiVuote i#4 - video built with Processing (Mario Guida)