Sound minim

edited August 2015 in Library Questions

Como posso usar o aúdio e o "desenho de frequência" fora do draw() ?

import ddf.minim.*;

Minim minim;
AudioPlayer groove;

void setup()
{
  size(512, 200, P3D);
  minim = new Minim(this);
  groove = minim.loadFile("groove.mp3", 2048);
  groove.loop();
}

void draw()
{
  background(0);
  stroke(255);
  for(int i = 0; i < groove.bufferSize() - 1; i++)
  {
    line(i, 50  + groove.left.get(i)*50,  i+1, 50  + groove.left.get(i+1)*50);
    line(i, 150 + groove.right.get(i)*50, i+1, 150 + groove.right.get(i+1)*50);
  }
}

Answers

  • see

    http://code.compartmental.net/minim/fft_class_fft.html

    /**
     * This sketch demonstrates how to use an FFT to analyze
     * the audio being generated by an AudioPlayer.
     * <p>
     * FFT stands for Fast Fourier Transform, which is a 
     * method of analyzing audio that allows you to visualize 
     * the frequency content of a signal. You've seen 
     * visualizations like this before in music players 
     * and car stereos.
     * <p>
     * For more information about Minim and additional features, 
     * visit http://code.compartmental.net/minim/
     */
    
    import ddf.minim.analysis.*;
    import ddf.minim.*;
    
    Minim       minim;
    AudioPlayer jingle;
    FFT         fft;
    
    void setup()
    {
      size(512, 200, P3D);
    
      minim = new Minim(this);
    
      // specify that we want the audio buffers of the AudioPlayer
      // to be 1024 samples long because our FFT needs to have 
      // a power-of-two buffer size and this is a good size.
      jingle = minim.loadFile("jingle.mp3", 1024);
    
      // loop the file indefinitely
      jingle.loop();
    
      // create an FFT object that has a time-domain buffer 
      // the same size as jingle's sample buffer
      // note that this needs to be a power of two 
      // and that it means the size of the spectrum will be half as large.
      fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );
    }
    
    void draw()
    {
      background(0);
      stroke(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( jingle.mix );
    
      for (int i = 0; i < fft.specSize (); i++)
      {
        // draw the line for frequency band i, scaling it up a bit so we can see it
        line( i, height, i, height - fft.getBand(i)*8 );
      }
    }
    
  • Não é isto. Quero que esta parte do código fique fora da função draw() "

     for (int i = 0; i < fft.specSize (); i++)
    
    {
    
        // draw the line for frequency band i, scaling it up a bit so we can see it
    
        line( i, height, i, height - fft.getBand(i)*8 );
    
    }
    

    "

  • edited August 2015

    Pq vc quer o loop fora de draw()? Tá tendo problemas com performance?
    Why do you want the loop outside draw()? Having any performance issues?

    Pra isso teremos q usar um thread() separado c/ loop infinito só pra lidar c/ isso:
    For that we're gonna need to use some separate thread() w/ infinite loop just to deal w/ that:

    https://Processing.org/reference/thread_.html

    Infelizmente ñ devemos modificar o canvas do sketch fora do Thread "Animation".
    Unfortunately we shouldn't modify sketch's canvas outside "Animation" Thread.

    Ao invés disto, criamos um PGraphics separado e usamos line() ali.
    Instead, we create some separate PGraphics and use line() there.

    Então o único encargo de draw() é mostrar o resultado final do PGraphics.
    Then draw()'s solely task is to display PGraphics's final result.

    Podemos escolher image(), set() ou mesmo background() pra por o PGraphics no canvas.
    We can choose image(), set() or even background() in order to put PGraphics in the canvas.

  • GoToLoop, pq eu preciso executar outras tarefas antes desse trecho de código.

    Pode me mandar algum código de exemplo para fazer isso fora do draw()?

  • edited August 2015
    // forum.processing.org/two/discussion/12137/sound-minim
    // GoToLoop (2015-Aug-19)
    
    static final int FPS = 1, DELAY = 3 * 1000/FPS;
    PGraphics pg;
    
    void setup() {
      size(400, 300, JAVA2D);
      frameRate(FPS);
      imageMode(CORNER);
    
      pg = createGraphics(width, height, JAVA2D);
      pg.beginDraw();
      pg.smooth(4);
      pg.ellipseMode(CENTER);
      pg.rectMode(CENTER);
      pg.endDraw();
    
      thread("drawPG");
    }
    
    void draw() {
      background((color) random(#000000));
      image(pg, 0, 0);
      frame.setTitle(str(frameCount));
    }
    
    void drawPG() {
      final PGraphics pgl = pg;
      final int x = pgl.width>>1, y = pgl.height>>1, w = x, h = y;
    
      boolean isRect = true;
    
      for (;; delay(DELAY)) {
        pgl.clear();
        pgl.fill((color) random(#000000));
    
        if (isRect ^= true)  pgl.rect(x, y, w, h);
        else                 pgl.ellipse(x, y, w, h);
    
        pgl.endDraw();
      }
    }
    
  • edited August 2015

    O que faz a função bufferSize() ? Posso usá-la fora do draw() ?

     import ddf.minim.*;
    
     Minim minim;
     AudioPlayer groove;
    
     void setup()
     {
        size(512, 200, P3D);
    
        minim = new Minim(this);
        groove = minim.loadFile("groove.mp3", 2048);
        groove.loop();
    }
    
    void draw()
    {
        background(0);
    
        stroke(255);
    
        for(int i = 0; i < groove.bufferSize() - 1; i++)
        {
             line(i, 50  + groove.left.get(i)*50,  i+1, 50  + groove.left.get(i+1)*50);
             line(i, 150 + groove.right.get(i)*50, i+1, 150 + groove.right.get(i+1)*50);
        }
    }
    
  • edited August 2015

    O que faz a função bufferSize()?
    What does bufferSize() function do?

    http://code.Compartmental.net/minim/audioplayer_method_buffersize.html

    Posso usá-la fora do draw()?
    Can I use it outside draw()?

    Como afirmara antes:

    Infelizmente ñ devemos modificar o canvas do sketch fora do Thread "Animation".

    As I had stated before:

    Unfortunately we shouldn't modify sketch's canvas outside "Animation" Thread.

    Método bufferSize() do AudioPlayer ñ modifica o canvas do sketch em nada.
    AudioPlayer's bufferSize() method doesn't change sketch's canvas at all.

    Mas é claramente óbvio line() faz exatamente isso! :-\"
    But it's clearly obvious line() does exactly that! :-\"

  • O que faz a função groove.left.get(i)*50? Posso usá-la fora do draw()?

           for(int i = 0; i < groove.bufferSize() - 1; i++)
           {     
                groove.play();
                line(i, 50  + groove.left.get(i)*50,  i+1, 50  + groove.left.get(i+1)*50);
                line(i,150 + groove.right.get(i)*50, i+1,150 + groove.right.get(i+1)*50);
           }
    
  • @GoToLoop, fiz um programa usando praticamente toda a tela do computador, mas a linha não está indo até o final (height). Como posso melhorar isso?

  • edited October 2015

    Tem q saber os valores mínimo & máximo q y varia pra poder fazer o map() de 0 & height:
    https://Processing.org/reference/map_.html

Sign In or Register to comment.