Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 10th, 2012  |  syntax: None  |  size: 5.45 KB  |  hits: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Counting real time audio frequency in java
  2. package STLMA;
  3.  
  4. /*
  5.  * To change this template, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8.  
  9.  
  10. /**
  11.  *
  12.  * @author CATE GABRIELLE
  13.  */
  14.  
  15. import java.io.*;
  16. import javax.sound.sampled.*;
  17.  
  18. public class SpeechDetection {
  19. boolean stopCapture = false;
  20. ByteArrayOutputStream byteArrayOutputStream;
  21. TargetDataLine targetDataLine; // This is the object that acquires data from
  22.                                // the microphone and delivers it to the program
  23.  
  24. // the declaration of three instance variables used to create a SourceDataLine
  25. // object that feeds data to the speakers on playback
  26. AudioFormat audioFormat;    
  27. AudioInputStream audioInputStream;
  28. SourceDataLine sourceDataLine;    
  29.  
  30. double voiceFreq = 0;    
  31.  
  32. FileOutputStream fout;
  33. AudioFileFormat.Type fileType;
  34. public static String closestSpeaker;
  35.  
  36. public SpeechDetection(){
  37.     captureAudio();
  38. }      
  39.  
  40. private void captureAudio(){
  41.     try{
  42.         audioFormat = getAudioFormat();
  43.         DataLine.Info dataLineInfo = new  
  44.          DataLine.Info(TargetDataLine.class,audioFormat);
  45.         // object that describes the data line that we need to handle the acquisition
  46.         // of the audio data from the microphone. The first parameter makes the audio
  47.         // data readable
  48.         targetDataLine = (TargetDataLine)AudioSystem.getLine(dataLineInfo);
  49.          //  object to handle data acquisition
  50.         targetDataLine.open(audioFormat);                
  51.            //from the microphone that matches
  52.         targetDataLine.start();                            
  53.          // the information encapsulated in the DataLine.Info object  
  54.         Thread captureThread = new Thread(new CaptureThread());
  55.         captureThread.start();
  56.     } catch (Exception e) {
  57.     System.out.println(e);
  58.     System.exit(0);
  59.     }
  60. }    
  61.  
  62. private AudioFormat getAudioFormat(){
  63.     float sampleRate = 8000.0F; // The number of samples that will be acquired
  64.     //8000,11025,16000,22050,44100  each second for each channel of audio data.
  65.     int sampleSizeInBits = 16; //The number of bits that will be used to
  66.     //8,16                        describe the value of each audio sample.
  67.     int channels = 1;           // Two channels for stereo, and one channel for mono.
  68.     //1,2
  69.     boolean signed = true;      // Whether the description of each audio sample
  70.     //true,false        
  71.      //consists of both positive and negative values, or positive values only.          
  72.     boolean bigEndian = false;
  73.     //true,false
  74.     return new AudioFormat(sampleRate,sampleSizeInBits,channels,signed,bigEndian);        
  75. }
  76.  
  77. //Inner class to capture data from microphone
  78. class CaptureThread extends Thread {
  79.     byte tempBuffer[] = new byte[8000];  
  80.     // byte buffer variable to contain the raw audio data
  81.     int countzero;                      
  82.      // counter variable to count the number of zero's              
  83.     short convert[] = new short[tempBuffer.length];
  84.     // short variable that is appropriate to
  85.  
  86.     // collect the audio input for porcessing
  87.  
  88.     //        public void start(){
  89.     //            Thread voices = new Thread(this);
  90.     //            voices.start();
  91.     //        }
  92.  
  93.     @Override
  94.     public void run(){              
  95.    // a continuous thread to process the continuous audio input
  96.         byteArrayOutputStream = new ByteArrayOutputStream(); // the object to write the
  97.  
  98.     // raw audio input to the byte buffer variable
  99.         stopCapture = false;
  100.         try{
  101.             while(!stopCapture){                    
  102.                 int cnt = targetDataLine.read(tempBuffer,0,tempBuffer.length);
  103.             // reads the raw audio input
  104.  
  105.              // and returns the number of bytes actually read
  106.                 byteArrayOutputStream.write(tempBuffer, 0, cnt);
  107.             // writing the number of bytes read to the
  108.                                                                  // container                
  109.                 try{
  110.                     countzero = 0;
  111.  
  112.                     for(int i=0; i < tempBuffer.length; i++){  
  113.                 // the loop that stores the whole audio data                                        
  114.                         convert[i] = tempBuffer[i];    
  115.                 // to the convert variable which is a short data type,
  116.                         if(convert[i] == 0){countzero++;}    
  117.                  // then counts the number of zero's
  118.                     }
  119.                     voiceFreq = (countzero/2)+1;              
  120.                 // calculates the number of frequency and
  121.                                     // stores to the voiceFreq variable
  122.                     if(voiceFreq>=80 && voiceFreq<=350)
  123.                         System.out.println("Voice"+voiceFreq);
  124.                     else
  125.                        System.out.println("Unvoice"+voiceFreq);
  126.                 }catch(StringIndexOutOfBoundsException e)  
  127.                 {System.out.println(e.getMessage());}                                                                                    
  128.                     Thread.sleep(0);                                        
  129.             }
  130.         byteArrayOutputStream.close();
  131.         }catch (Exception e) {
  132.             System.out.println(e);
  133.             System.exit(0);
  134.         }
  135.     }
  136. }          
  137.  
  138. public static void main(String [] args){
  139.     SpeechDetection voiceDetector1 = new SpeechDetection();        
  140.     //        voiceDetector1.setSize(300,100);
  141.     //        voiceDetector1.setDefaultCloseOperation(EXIT_ON_CLOSE);
  142.     //        voiceDetector1.setVisible(true);
  143. }
  144.  
  145.  
  146.  
  147. }