Thursday, January 31, 2013

Glitch brush


Hello!


/* Glitch brush
 http://creativec0d1ng.blogspot.com.br/ by Italo Travenzoli

press "s" to save image
press "e" to erase
press "g" to glitch
 */
  int N=0;



void setup(){

size (851,315);

frameRate(60);

smooth();

colorMode(HSB);

background(255);

}



void draw(){

  

  if(keyPressed){

    if (key== 's'||key=='S'){

      save("image.jpg");

    }

    if (key== 'e'||key=='E'){

      background (255);

    }
   if (key== 'g'||key=='G'){
 
      Set();
 
    }     

  }


  if(mousePressed){

    float strokeSize = random(5,25);

    noStroke();

    rectMode(CENTER);

    fill(random(360),300,300);

     if(frameCount%2==0){
    rect (mouseX,mouseY,strokeSize,strokeSize);
 }
    if(frameCount%2!=0){
    ellipse (mouseX,mouseY,strokeSize,strokeSize);
 }

  }



}



int r(int a){

  return int(random(a));

}

/* 

void mouseReleased (){

  N++;

  save("glitchCover"+ (N)+".tif");

}

 */

void Set() {

  for (int i=0; i<10; i++) {

    int x = r(width);

    int y = r(height);

    set((x+r(50)-1)*int(random(-10,10)),y+r(3)-1,get(x,y,r(99),r(30)));

  }

}





Saturday, January 19, 2013

How to get frequency values from mic with Processing and Minim library



(test with this video: http://youtu.be/4G60hM1W_mk)

The idea is to get the frequency of the volume peak

import ddf.minim.*;
import ddf.minim.analysis.*;

Minim minim;
AudioInput in;
FFT fft;
String note;// name of the note
int n;//int value midi note
color c;//color
float hertz;//frequency in hertz
float midi;//float midi note
int noteNumber;//variable for the midi note


int sampleRate= 44100;//sapleRate of 44100

float [] max= new float [sampleRate/2];//array that contains the half of the sampleRate size, because FFT only reads the half of the sampleRate frequency. This array will be filled with amplitude values.
float maximum;//the maximum amplitude of the max array
float frequency;//the frequency in hertz

void setup()
{
  size(400, 200);

  minim = new Minim(this);
  minim.debugOn();
  in = minim.getLineIn(Minim.MONO, 4096, sampleRate);
  fft = new FFT(in.left.size(), sampleRate);
}

void draw()
{

  background(0);//black BG

  findNote(); //find note function

  textSize(50); //size of the text

  text (frequency-6+" hz", 50, 80);//display the frequency in hertz
  pushStyle();
  fill(c);
  text ("note "+note, 50, 150);//display the note name
  popStyle();
}

void findNote() {

  fft.forward(in.left);
  for (int f=0;f<sampleRate/2;f++) { //analyses the amplitude of each frequency analysed, between 0 and 22050 hertz
    max[f]=fft.getFreq(float(f)); //each index is correspondent to a frequency and contains the amplitude value 
  }
  maximum=max(max);//get the maximum value of the max array in order to find the peak of volume

  for (int i=0; i<max.length; i++) {// read each frequency in order to compare with the peak of volume
    if (max[i] == maximum) {//if the value is equal to the amplitude of the peak, get the index of the array, which corresponds to the frequency
      frequency= i;
    }
  }


  midi= 69+12*(log((frequency-6)/440));// formula that transform frequency to midi numbers
  n= int (midi);//cast to int


//the octave have 12 tones and semitones. So, if we get a modulo of 12, we get the note names independently of the frequency  
if (n%12==9)
  {
    note = ("a");
    c = color (255, 0, 0);
  }

  if (n%12==10)
  {
    note = ("a#");
    c = color (255, 0, 80);
  }

  if (n%12==11)
  {
    note = ("b");
    c = color (255, 0, 150);
  }

  if (n%12==0)
  {
    note = ("c");
    c = color (200, 0, 255);
  }

  if (n%12==1)
  {
    note = ("c#");
    c = color (100, 0, 255);
  }

  if (n%12==2)
  {
    note = ("d");
    c = color (0, 0, 255);
  }

  if (n%12==3)
  {
    note = ("d#");
    c = color (0, 50, 255);
  }

  if (n%12==4)
  {
    note = ("e");
    c = color (0, 150, 255);
  }

  if (n%12==5)
  {
    note = ("f");
    c = color (0, 255, 255);
  }

  if (n%12==6)
  {
    note = ("f#");
    c = color (0, 255, 0);
  }

  if (n%12==7)
  {
    note = ("g");
    c = color (255, 255, 0);
  }

  if (n%12==8)
  {
    note = ("g#");
    c = color (255, 150, 0);
  }
}

void stop()
{
  // always close Minim audio classes when you are done with them
  in.close();
  minim.stop();

  super.stop();
}

Friday, January 18, 2013

Procedural texture



I wanted to create a new cover for my twitter, so I used code to make a procedural texture. I want to share the code:

float [] sizeX= new float [10000];
float [] sizeY= new float [10000];

void setup(){
  size (700,400);
  colorMode(HSB);
  
   for(int i=0; i<2000;i++){
     for(int j=0; j<2000;j++){
       sizeX[i]=random (5,50);
       sizeY[i]=random (5,50);
    }
  }
}

void draw (){
  background (0);
  if(mousePressed){
      for(int i=0; i<2000;i++){
    for(int j=0; j<2000;j++){
   sizeX[i]=random (5,50);
   sizeY[i]=random (5,50);
      }
    } 
  }
  for(int i=0; i<2000;i+=20){
    for(int j=0; j<2000;j+=20){
      //noStroke();
      rectMode(CENTER);
      fill(i%360,300,300,i%50);
      rect (i,j,sizeX[i+j],sizeX[i+j]); 
      fill(255);
      //ellipse (i,j,2,2);  
    }
  }
  if(keyPressed){
    if (key== 's'||key=='S'){
      save("image.jpg");
    }
  }
  else{
   text("Press 's' to save image, click to generate new texture", 30,30); 
   text("PS. Don't worry, this text will desapear when you hit the key", 30,50);
  }
}

With few changes in the code, the result changes radically,