Minim Fft

edited January 2015 in Library Questions

Hello everyone thanks for reading me.

Ok, i'm trying to make 128 horizontals lines on the height of my size(); I would like to link each lines to a specific interval of frequencies, and when a specific frequency pop on the listenning of the track i would like it to draw the line which it is link to. And when that specific frequency is not present i would like it to draw nothing. i have done it with a manual entry of 24 intervals, but in 128 it's very hard to do, if someone can help me to find a quicker way to do it it would be very nice and kind. I have tried to use the map(); function but it is hard to understand for me. Thanks again. Here is the code with the manuals entries :

import ddf.minim.*;
import ddf.minim.analysis.*;
import processing.pdf.*;

Minim minim;
AudioPlayer player;
FFT         fft;

//float x;
//float y;

//plus Value est petit plus les points de fréquences sont rapprochés
float value =2;
float v=0;

void setup()
{

  background(255);
  size(1280, 720, P3D);

  minim = new Minim (this);

  // Audiodatei aus dem data-Ordner laden
  player = minim.loadFile ("Ligeti- Lontano .mp3"); 

  fft = new FFT( player.bufferSize(), player.sampleRate() );

  // Audiodatei abspielen
  player.play ();

  //RECORD PDF
  beginRecord(PDF, "everything.pdf");

  frameRate(2);
  smooth();
}

void draw() {
  //POUR AVOIR JUSTE UN SEUL FIL
  //background(255);

  // perform a forward FFT on the samples in jingle's mix buffer,
  // which contains the mix of both the left and right channels of the file
  fft.forward( player.mix );

  // TO KNOW WHERE WE ARE
  float playPos = player.position ();
  float playLen = player.length ();
  float xpos = (playPos / playLen) * width;




  for (int i = 0; i < fft.specSize (); i++)
  {

    float m = map(value, 0, fft.getBand(i), 0, width);
    float h = fft.getBand(i)*5;
    stroke(0);
    //    strokeWeight(h/8);
    //    noStroke();
    fill(0);
    //    ellipse(xpos,400,h/10,h/10);
    //    point(xpos, (h)+height/2);
    //    point(xpos, (-h)+height/2);

    //    stroke(random(0,255));
    //    line(xpos,m,xpos+10,m);
    if (h>0 && h<0.3) {
      point(xpos, height/24);
    }
    if (h>0.3 && h<0.6) {
      point(xpos, height/24*2);
    }
    if (h>0.6 && h<0.9) {
      point(xpos, height/24*3);
    }
    if (h>0.9 && h<1.2) {
      point(xpos, height/24*4);
    }
    if (h>1.2 && h<1.5) {
      point(xpos, height/24*5);
    }
    if (h>1.5 && h<1.8) {
      point(xpos, height/24*6);
    }
    if (h>1.8 && h<2.1) {
      point(xpos, height/24*7);
    }
    if (h>2.1 && h<2.4) {
      point(xpos, height/24*8);
    }
    if (h>2.4 && h<2.7) {
      point(xpos, height/24*9);
    }
    if (h>2.7 && h<3) {
      point(xpos, height/24*10);
    }
    if (h>3 && h<3.3) {
      point(xpos, height/24*11);
    }
    if (h>3.3 && h<3.6) {
      point(xpos, height/24*12);
    }
    if (h>3.6 && h<3.9) {
      point(xpos, height/24*13);
    }
    if (h>3.9 && h<4.2) {
      point(xpos, height/24*14);
    }
    if (h>4.2 && h<4.5) {
      point(xpos, height/24*15);
    }
    if (h>4.5 && h<4.8) {
      point(xpos, height/24*16);
    }
    if (h>4.8 && h<5.1) {
      point(xpos, height/24*17);
    }
    if (h>5.1 && h<5.4) {
      point(xpos, height/24*18);
    }
    if (h>5.4 && h<5.7) {
      point(xpos, height/24*19);
    }
    if (h>5.7 && h<6) {
      point(xpos, height/24*20);
    }
    if (h>6 && h<6.3) {
      point(xpos, height/24*21);
    }
    if (h>6.3 && h<6.6) {
      point(xpos, height/24*22);
    }
    if (h>6.6 && h<6.9) {
      point(xpos, height/24*23);
    }
    if (h>6.9 && h<7.2) {
      point(xpos, height/24*24);
    }


    println(h);
  }

  stroke( 255);
  point (xpos, height/2);
}

void mousePressed () 
{
  float pos = ((float) mouseX / width) * player.length ();
  player.cue ((int) pos);
}

void keyPressed () {
  if (key == 'o') {
    saveFrame("images/artwork-####.tiff");
  }
  if (key == 'p') {
    endRecord();
    exit();
  }
}


//FULL SCREEEN
//boolean sketchFullScreen() {
//  return true;
//}
Tagged:

Answers

  • up ! really need someones help please :)

  • Answer ✓

    is it what you want??? - (i have not tested your pdf recording)

    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import processing.pdf.*;
    
    Minim minim;
    AudioPlayer player;
    FFT         fft;
    
    
    //plus Value est petit plus les points de fréquences sont rapprochés
    float value =2;
    float v=0;
    
    void setup()
    {
    
      background(255);
      size(1280, 720);
    
      minim = new Minim (this);
     player = minim.loadFile ("groove.mp3"); 
    
      fft = new FFT( player.bufferSize(), player.sampleRate() );
      player.loop();
      player.play ();
      background (0);
    
     // RECORD PDF
     beginRecord(PDF, "everything.pdf");
    
      frameRate(2);
      smooth();
    }
    
    void draw() {
    //background(0);uncomment if you want only 1 colon
    
      fft.forward( player.mix );
    
      // TO KNOW WHERE WE ARE
    
      float playPos = player.position ();
      float playLen = player.length ();
      float xpos = (playPos / playLen) * width;
    
      for (int i = 0; i < fft.specSize (); i++)
      {
    
        float m = map(value, 0, fft.getBand(i)*5, 0, width);
        float h = fft.getBand(i)*5;
       //that is for fun!
        float col = map(value,0,fft.getBand(i), 0,255);
        strokeWeight(2);
        stroke(255,col,0,126);
         line(xpos,m,xpos+10,m);// here a little error you must add constrain to xpos
    
    }
    };
    
    void mousePressed () 
    {
      float pos = ((float) mouseX / width) * player.length ();
      player.cue ((int) pos);
    }
    
    void keyPressed () {
      if (key == 'o') {
        saveFrame("images/artwork-####.tiff");
      }
      if (key == 'p') {
       // endRecord();
        exit();
      }
    }
    
  • Thanks a Lot Akenaton, but this is not the thing i would like, and i have came to a similar solution on my side. But thx again, here is a picture that explain maybe better what i want, there are 24 lines here but i would like it in 128 or more !

    artwork-1549

  • not sure to understand: you want CONTINUOUS lines, 128 or more , you cannot get that with your xpos changing +10???? is this better?

    Minim minim;
    AudioPlayer player;
    FFT         fft;
    
    
    //plus Value est petit plus les points de fréquences sont rapprochés
    float value =2;
    float v=0;
    
    void setup()
    {
    
      background(255);
      size(1280, 720);
    
      minim = new Minim (this);
     player = minim.loadFile ("groove.mp3"); 
    
      fft = new FFT( player.bufferSize(), player.sampleRate() );
      player.loop();
      player.play ();
      background (0);
    
     // RECORD PDF
     beginRecord(PDF, "everything.pdf");
    
      frameRate(2);
      smooth();
    }
    
    void draw() {
    background(0);//uncomment if you want but you dont get lines!
    
      fft.forward( player.mix );
    
      // TO KNOW WHERE WE ARE
    
      float playPos = player.position ();
      float playLen = player.length ();
      //float xpos = (playPos / playLen) * width;
    
      for (int i = 0; i < fft.specSize (); i++)
      {
    
        float m = map(value, 0, fft.getBand(i)*5, 0, width);
        float h = fft.getBand(i)*5;
       //that is for fun!
        float col = map(value,0,fft.getBand(i), 255,0);
        strokeWeight(2);
        stroke(255,col,0,126);
    
         line(0,m,width,m);
    
    }
    };
    
    void mousePressed () 
    {
      float pos = ((float) mouseX / width) * player.length ();
      player.cue ((int) pos);
    }
    
    void keyPressed () {
      if (key == 'o') {
        saveFrame("images/artwork-####.tiff");
      }
      if (key == 'p') {
       // endRecord();
        exit();
      }
    }
    
  • thx again, i have already done what you tried. In fact like in the picture each lines must correspond to a specific frequency range going to minFrequency in the track to the max, the range minFreq/maxFreq must contain 128 ranges in it, and these lines are in fact points going to x =0 to x=width. So i must know the maxFreq value and divide it into 128 parts. after i got to make a peace of code that can link for example the 54th freq to the 54th point on the height. I'm sorry i'm not really good in english :)

  • ok== exactly 128! (that means that you "group" your frequency ranges with fftLin.linAverages(128); after that i dismiss my english also is too bad, i am french! - Or send some picture of what you expect exactly!

    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import processing.pdf.*;
    FFT fftLin;
    
    Minim minim;
    AudioPlayer player;
    FFT         fft;
    
    
    //plus Value est petit plus les points de fréquences sont rapprochés
    float value =2;
    float v=0;
    
    void setup()
    {
    
      background(255);
      size(1280, 720);
    
      minim = new Minim (this);
     player = minim.loadFile ("groove.mp3"); 
    
      fft = new FFT( player.bufferSize(), player.sampleRate() );
       fftLin = new FFT(player.bufferSize(), player.sampleRate());
       fftLin.linAverages(128);
      //println(player.sampleRate());
    
      player.loop();
      player.play ();
      background (0);
    
     // RECORD PDF
     beginRecord(PDF, "everything.pdf");
    
     // frameRate(2);
      smooth();
    }
    
    void draw() {
    background(0);//uncomment if you want, but you cannot see your lines
    
      fft.forward( player.mix );
    
      // TO KNOW WHERE WE ARE
    
      float playPos = player.position ();
      float playLen = player.length ();
      float xpos = (playPos / playLen) * width;
    
      for (int i = 0; i < fftLin.avgSize (); i++)
      {
    
        float m = map(value, 0, fft.getBand(i)*5, 0, width);
        float h = fft.getBand(i)*5;
       //that is for fun!
        float col = map(value,0,fft.getBand(i), 255,0);
        strokeWeight(2);
        stroke(255,col,0,126);
    
         line(0,m,width,m);// here a little error you must add constrain to xpos
    
    }
    };
    
    void mousePressed () 
    {
      float pos = ((float) mouseX / width) * player.length ();
      player.cue ((int) pos);
      println("pos====================" + (int) pos);
    }
    
    void keyPressed () {
      if (key == 'o') {
        saveFrame("images/artwork-####.tiff");
      }
      if (key == 'p') {
       // endRecord();
        exit();
      }
    }
    
  • J'étais sûr que tu étais français grâce au Akenaton, je sais pas pourquoi j'ai pensé au rap. On va parler comme ça ce sera plus simple haha. Je connaissais pas le fftLin.linAverages(128); je débute avec processing tout doucement ! Du coups maintenant les frequences du morceau sont découpées en 128 parts. Il faut que j'arrive à relier ces 128 fréquences a 128 points. Ces points sont diposés sur la hauteur de la fenêtre. Il faudrait que c'est 128 point soient associés à ces 128 frequences, et qu'ils avancent avec le (xpos) tout en laissant une trace. Dans l'image précédente que j'ai posté j'ai fais la même chose mais sur 24 points et avec une entrée manuel des correspondances fréquences/hauteur des points comme ça :

    if (h>0 && h<0.3) {
          point(xpos, height/24);
        }
        if (h>0.3 && h<0.6) {
          point(xpos, height/24*2);
        }
        if (h>0.6 && h<0.9) {
          point(xpos, height/24*3);
        }
        if (h>0.9 && h<1.2) {
          point(xpos, height/24*4);
        }
        if (h>1.2 && h<1.5) {
          point(xpos, height/24*5);
        }
        if (h>1.5 && h<1.8) {
          point(xpos, height/24*6);
        }
        if (h>1.8 && h<2.1) {
          point(xpos, height/24*7);
        }
        if (h>2.1 && h<2.4) {
          point(xpos, height/24*8);
        }
        if (h>2.4 && h<2.7) {
          point(xpos, height/24*9);
        }
        if (h>2.7 && h<3) {
          point(xpos, height/24*10);
        }
        if (h>3 && h<3.3) {
          point(xpos, height/24*11);
        }
        if (h>3.3 && h<3.6) {
          point(xpos, height/24*12);
        }
        if (h>3.6 && h<3.9) {
          point(xpos, height/24*13);
        }
        if (h>3.9 && h<4.2) {
          point(xpos, height/24*14);
        }
        if (h>4.2 && h<4.5) {
          point(xpos, height/24*15);
        }
        if (h>4.5 && h<4.8) {
          point(xpos, height/24*16);
        }
        if (h>4.8 && h<5.1) {
          point(xpos, height/24*17);
        }
        if (h>5.1 && h<5.4) {
          point(xpos, height/24*18);
        }
        if (h>5.4 && h<5.7) {
          point(xpos, height/24*19);
        }
        if (h>5.7 && h<6) {
          point(xpos, height/24*20);
        }
        if (h>6 && h<6.3) {
          point(xpos, height/24*21);
        }
        if (h>6.3 && h<6.6) {
          point(xpos, height/24*22);
        }
        if (h>6.6 && h<6.9) {
          point(xpos, height/24*23);
        }
        if (h>6.9 && h<7.2) {
          point(xpos, height/24*24);
        }
    

    maintenant j'aimerais le faire sur 128 fréquences ou voir plus avec un code qui me permettrait d'éviter le laborieux code du haut :)

    Je te remercie encore pour ton temps !

  • je rappel que h est bien sûr le fft.getBand(i)*5

  • salut charlie! bon alors, je comprends mieux si tu veux que tes points avancent il te faut xpos (moi je pensais que tu voulais des lignes, c pas pareil) si tu veux qu'ils laissent une trace il te faut pas background(); (je sais pas ce que ça va faire esthétiquement...) maintenant ce qu'il faut que tu fasses c'est que tu comprennes::

    a) que la max frequency est égale au samplerate/2; soit pour 44 = 22; (ce que tu peux savoir facile: printnl(player.sampleRate());

    b) que tu lises la java doc sur fft, not so easy, i do know

    c) que d'autre part tu peux avoir des spectres plus ou moins "précis", c'est à ça que sert la methode linAverages(); (il y a peut être mieux avec logAverages(), là je passe....

    d) que donc si tu ne veux qu'une plage de 0 à 128 ben tu groupes, comme j'ai fait

    pour le reste (points au lieu de lignes) je te trouverai la réponse mais là je suis obligé de m'arrêter pour 4 ou 5 jours: j'y vois plus, cause code!!! donc docteur ....

    mais en ts les cas pas question d'écrire comme tu le fais, suppose que tu aies 22000 fréquences tu meurs avant!

  • Merci pour tout ça Charlie en tout cas pour ce que tu me donne comme informations, je posterais de nouveau sur ce post si je parviens a avancer !

  •  @uani:: est ce que ça convient mieux???
    
    
    
    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import processing.pdf.*;
    FFT fftLin;
    
    Minim minim;
    AudioPlayer player;
    FFT         fft;
    
    
    
    float value =1;
    float v=0;
    int highest = 0;
    
    void setup()
    
    {
    
      background(255);
      size(1280, 720);
      minim = new Minim (this);
      player = minim.loadFile ("groove.mp3"); 
    
      fft = new FFT( player.bufferSize(), player.sampleRate() );
      fftLin = new FFT(player.bufferSize(), player.sampleRate());
    
      fftLin.linAverages(128);
      fftLin.avgSize ());
      player.loop();
    
      background (0);
    
      // RECORD PDF
      beginRecord(PDF, "everything.pdf");
    
    
      smooth();
    };
    
    void draw() {
    
      fftLin.forward(player.mix);
    
      // TO KNOW WHERE WE ARE
    
      float playPos = player.position ();
      float playLen = player.length ();
    
      float xpos = map(playPos,0, playLen,0,width);
    
      for (int i = 0; i < fftLin.avgSize(); i++)
    
      {
    
      if (fftLin.getAvg(i)>highest) {
     highest= (int)fftLin.getAvg(i);
    
     }
    
    float h = map (fftLin.getAvg(i), 0, highest, height, 0);
     colorMode(HSB, 100);
        float col = map(fftLin.getAvg(i), 0, highest , 0, 100);
    
        strokeWeight(5);
        stroke(col, 97, 97, 126);
    
     point(xpos, h);
    
      }
    
    };
    
    void mousePressed () 
    {
      float pos = ((float) mouseX / width) * player.length ();
      player.cue ((int) pos);
    
    }
    
    void keyPressed () {
      if (key == 'o') {
        saveFrame("images/artwork-####.tiff");
      }
      if (key == 'p') {
        // endRecord();
        exit();
      }
    }
    
Sign In or Register to comment.