Page 1 of 1

Minim examples for Processing (sound)

PostPosted: Tue Nov 25, 2014 5:33 pm
by BenChang
The Minim Library adds sound playback, input, analysis, and synthesis for Processing. It can play sound effects and background music, play AIFF, WAV, OGG, or MP3 files, do sound input and FFT analysis, and synthesize and filter sounds.

There are a bunch of examples that come with Processing; check those out, and the documentation here:

http://code.compartmental.net/minim/
Basic sound playing
Code: Select all
import ddf.minim.*;

// the minim object is the main interface for all the audio functions
Minim minim;

AudioPlayer player;

void setup()
{
   // set up the global minim object, with pointer back to my code for callbacks
   minim = new Minim(this);
   player = minim.loadFile("flutes.mp3");
}

void keyPressed ()
{
   if (key=='a')
   {
      if (player.isPlaying()==false)
      {
        player.play();
      }
      else
      {
         player.pause();
      }
   }
}

void draw()
{
 
}


Two Sound Players

PostPosted: Tue Nov 25, 2014 5:35 pm
by BenChang
Two sound players
Code: Select all
import ddf.minim.*;

// the minim object is the main interface for all the audio functions
Minim minim;

AudioPlayer player;
AudioPlayer player2;


void setup()
{
   // set up the global minim object, with pointer back to my code for callbacks
   
   minim = new Minim(this);

   player = minim.loadFile("flutes.mp3");
   player2 = minim.loadFile("dance.mp3");
}

void keyPressed ()
{
   if (key=='a')
   {
      if (player.isPlaying()==false)
      {
        player.play();
      }
      else
      {
         player.pause();
      }
   }
 
   if (key=='l')
   {
      if (player2.isPlaying()==false)
      {
        player2.play();
      }
      else
      {
         player2.pause();
         player2.cue(0);
      }
   } 
   if (key==',')
   {
      player2.skip(-10);
   }
   if (key=='.')
   {
      player2.skip(10);
   }
   
   
}

void draw()
{
 
}



Fast-fourier Transform

PostPosted: Tue Nov 25, 2014 5:38 pm
by BenChang
Code: Select all
import ddf.minim.*;
import ddf.minim.analysis.*;

// the minim object is the main interface for all the audio functions
Minim minim;
AudioPlayer player;
FFT fft;

void setup()
{
   minim = new Minim(this);
   player = minim.loadFile("flutes.mp3",1024);
   fft = new FFT(player.bufferSize(),player.sampleRate());
   
   // use 30 bands as frequency averages
   fft.linAverages(30);
   
   size(1440,900);
}

void draw()
{
    background(255);
   
    // perform "forward FFT analysis" on the L+R mix of the music
    fft.forward (player.mix);
   
    // loop through all the frequencies in the FFT spectrum
   
    float freq0 = fft.getAvg(1);
   
    fill(255,0,0);
    noStroke();
   
    if (freq0>5)
    {
      rect(400,300,freq0 * 10,freq0 * 10);
    }
      stroke(0,0,0,26);
      for (int i=0; i < fft.specSize(); i++)
      {
         // get value of each frequency band
         float b = fft.getBand(i);
         line (i,mouseY,b * 8,mouseX);
      }
}

void keyPressed ()
{
   if (key=='a')
   {
      if (player.isPlaying()==false)
      {
        player.play();
        player.cue( (int)random(0,player.length()));
      }
      else
      {
         player.pause();
      }
   }
}