Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

I need to visualize the spectrum of multiple audio files(see description)

$
0
0

I want a single spectrum where I can visualize multiple audio files each playing at varying frequency..So that In the spectrum multiple spikes will be generated ..So far I am able to generate a spectrum visualizing a single audio file of recorded voice like the image below..Now In the same graph I want to visualize another audio files of different frequency..Codes are given below of the work so far I have done..

ssa

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package spectrumofaudio;

import ddf.minim.AudioInput;
import ddf.minim.AudioPlayer;
import ddf.minim.Minim;
import ddf.minim.analysis.FFT;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFrame;
import processing.core.PApplet;
import static processing.core.PConstants.CENTER;
import static processing.core.PConstants.RIGHT;
import static processing.core.PConstants.TOP;

/**
 *
 * @author mezbahuddin
 */
public class Dessign extends PApplet implements ActionListener{


Minim minim;
AudioInput in;
FFT fft;
AudioPlayer ap;

 // how many individual peak bands we have (dep. binsperband)
float gain = 0; // in dB
float dB_scale = (float) 2.0;  // pixels per dB
int buffer_size = 1024;  // also sets FFT size (frequency resolution)
float sample_rate = 44100;

int spectrum_height = 200; // determines range of dB shown
int legend_height = 20;
int spectrum_width = 512; // determines how much of spectrum we see
int legend_width = 40;
int freqOffset = 0;
boolean startScan;


public void settings() {
  size(552, 220, "1processing.opengl.PGraphics2D");
}
public void setup()
{
    //size(552, 220, "1processing.opengl.PGraphics2D");
  size(552, 220, P2D);
  //textMode(SCREEN);
  textFont(createFont("Georgia", 12));

  minim = new Minim(this);

  //in = minim.getLineIn(Minim.MONO,buffer_size,sample_rate);
   String str = new File("mezbah.mp3").getAbsolutePath();
   System.out.println(str);
   String complete = "";
   while(true)
   {
       int indx = str.indexOf("\\");
       if (indx < 0)
           break;
       String fPortion = str.substring(0, indx+1);
       String sPortion = str.substring(indx+1, str.length());
       fPortion += "\\";
       complete += fPortion;
       str = sPortion;
   }
   complete += "src\\spectrumofaudio\\mezbah.mp3";
   System.out.println(complete);

   ap = minim.loadFile(complete);
  // create an FFT object that has a time-domain buffer
  // the same size as line-in's sample buffer
  fft = new FFT(ap.bufferSize(), ap.sampleRate());

  // Tapered window important for log-domain display
  fft.window(FFT.HAMMING);

  //println("buffer:"+in.bufferSize());
  //println("sampleRate:"+in.sampleRate());
  //println("peaksize:"+peaksize);
  startScan = false;


}

public void DrawAxes(int freqOffset)
{
    background(0);
    fill(255);
  stroke(255);

  int y = spectrum_height;

  line(legend_width,y,legend_width+spectrum_width,y); // horizontal line

  // x,y address of text is immediately to the left of the middle of the letters
  textAlign(CENTER,TOP);

  int spFreq=0; //for spacing

  for (float freq = (float) 0.0; freq < ap.sampleRate(); freq += 2000.0) {
    int x = legend_width+spFreq+freqOffset; // which bin holds this frequency
   //println(freq+"->"+fft.freqToIndex(freq));
    line(x,y,x,y+4); // tick mark
    text(Math.round(freq/1000) +"kHz", x, y+5); // add text label
    spFreq+=45;
  }

  // DBlevel axis
  int x = legend_width;

  line(x,0,x,spectrum_height); // vertictal line

  textAlign(RIGHT,CENTER);

  for (float level = (float) -100.0; level < 100.0; level += 20.0) {
    y = spectrum_height - (int)(dB_scale * (level+gain));

    line(x,y,x-3,y);

    text((int)level+" dB",x-5,y);
  }

}

public void draw()
{
    DrawAxes(freqOffset);

    if (startScan == true)
    {
     DrawAxes(freqOffset);
       // background(0);
  // perform a forward FFT on the samples in input buffer
  fft.forward(ap.mix);
  ap.play();

  // now draw current spectrum in brighter blue
  stroke(64,255,255);
  noFill();
  for(int i = 0; i < fft.specSize(); i++)  {

    // draw the line for frequency band i using dB scale
    float val = dB_scale*(20*((float)Math.log10(fft.getBand(i))) + gain);

    if (fft.getBand(i) == 0) {   val = -200;   }  // avoid log(0)

    int ab = spectrum_height - Math.round(val);

    if (ab > spectrum_height) { ab = spectrum_height; }

    line(legend_width+i+freqOffset, spectrum_height, legend_width+i+freqOffset, ab);

    // update the peak record
    // which peak bin are we in?
    //int peaksi = i/binsperband;
    //if (val > peaks[peaksi]) {
    //  peaks[peaksi] = val;
    //  // reset peak age counter
    //  peak_age[peaksi] = 0;
    //}
  }

    }

//  // clear window
//
//  // add legend
//  // frequency axis

}
public void keyReleased()
{
  // +/- used to adjust gain on the fly
  if (key == '+' || key == '=') {
    gain = (float) (gain + 5.0);
  } else if (key == '-' || key == '_') {
    gain = (float) (gain - 5.0);
  }
  //(.)/(/) used to adjust frequency axis
  else if(key == '/')
  {
    freqOffset = freqOffset-4;
  }

  else if( key == '.')
  {
    freqOffset = freqOffset+4;
  }
}

public void stop()
{
  // always close Minim audio classes when you finish with them
  in.close();
  minim.stop();
  this.noLoop();
  super.stop();
}

    @Override
    public void actionPerformed(ActionEvent ev) {
        if (ev.getActionCommand().equals("scan"))
            startScan = true;
    }
}


Viewing all articles
Browse latest Browse all 2896

Trending Articles