Unusual sound and visual detected after an audio file is finished playing (See Description)

I have an issue here..I am doing spectral analysis of an audio file in processing 2.2.1 with minim in netbeans..Everything is fine while the audio file is playing..After the audio file is finished I hear unusual/distorted sound playing and seeing unwanted visuals..The unusual sound is playing like forever..I need it to stop after the file is finished playing...Why the unusual behavior..Any help.Sorry for some unused code.TIA

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;
FFT fft2;
AudioPlayer ap;
AudioPlayer ap2;

 // 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\\10khz.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);

  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);
    System.out.println(startScan);
    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);

  }

    
  
    }
  


}
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
  ap.close();
  ap2.close();
  minim.stop();
  this.noLoop();
  super.stop();
}

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

Tagged:
Sign In or Register to comment.