java - Why is this code failing to detect DTMF tones properly? -


i trying detect dtmf tones playing on machine. have code person found on website. have rearranged code based on needs. looks code detecting tones when there none playing!

what doing wrong here? piece of code uses goertzel algorithm detecting dtmf tones.


import java.io.ioexception;  import javax.sound.sampled.audioformat; import javax.sound.sampled.audiosystem; import javax.sound.sampled.dataline; import javax.sound.sampled.lineunavailableexception; import javax.sound.sampled.targetdataline;  public class dtmfdetect {      /**      * @param args      */          float[] lowfreq = new float[]{697.0f, 770.0f, 852.0f, 941.0f};         float[] highfreq = new float[]{1209.0f, 1336.0f, 1477.0f, 1633.0f};         float[] dtmftones = new float[]{697.0f, 770.0f, 852.0f, 941.0f, 1209.0f, 1336.0f, 1477.0f, 1633.0f};         int dtmfboard[][] = { { 1, 2, 3, 12 }, { 4, 5, 6, 13 }, { 7, 8, 9, 14 }, { 10, 0, 11, 15} };         //byte[] buffer = new byte[2000];         static final char frame_size = 160;          audioformat format = getaudioformat();         //int[] buf;         public boolean wait = false;         static boolean continueparsingdtmf = false;          public dtmfdetect()          {         }          public audioformat getaudioformat()          {         //  float samplerate = 8000.0f;                  float samplerate = 44100.0f;             //int samplesizeinbits = 16;                         int samplesizeinbits = 8;         int channels = 1;                            boolean signed = true;                       boolean bigendian = true;                     return new audioformat(samplerate, samplesizeinbits, channels, signed, bigendian);         }            public static void main(string args[]) throws lineunavailableexception, ioexception         {          // start seaching audio signals          dtmfcapture dtmfcapture = dtmfdetect.new dtmfcapture();         dtmfcapture.start();                  // decode minute         try {   thread.sleep(60000); } catch (exception e){}             continueparsingdtmf = false;            }          public class dtmfcapture extends thread {                public void run() {             continueparsingdtmf = true;             try {             int tone = 0;             dataline.info info = new dataline.info(targetdataline.class, format);             targetdataline out= (targetdataline) audiosystem.getline(info);                 int[] buf ;                  out.open(format);                 out.drain();             out.start();             int count = 0;              while (continueparsingdtmf) {             byte[] buffer = new byte[2000];                          //grab audio data             count = out.read(buffer,0,buffer.length);              if(count > 0){                 decodedtmf dtmf = new decodedtmf(buffer);              if (!wait){                          dtmf.start(); //look dtmf             thread.sleep(100);              } else                   {                   thread.sleep(4000); // wait before searching again                           system.out.println(system.currenttimemillis());                   wait = false;                   }               }              }         out.close();         }   catch(exception e){         e.printstacktrace();         }          }          }          public class decodedtmf extends thread {             //             byte[] buffer;             decodedtmf(byte[] buffer) {                 this.buffer = buffer;              }              public void run() {             int[] buf;              buf = new int[buffer.length/2];              for(int j = 0; j<buffer.length/2-1; j++)             {                     buf[j] = (int) ((buffer[j*2+1] & 0xff) + (buffer[j*2] << 8));             }             int tone = finddtmf(buf);              if (tone  >=0)                 {    wait = true;                     //system.out.print(time);                     if ( tone <10)                         {system.out.println(" tone : " + tone);                          }                   if (tone ==12)                 { system.out.println(" tone :  a"  );                  }                 if (tone ==13)                 {   system.out.println(" tone : b"  );                  }                  if (tone ==14)                 {   system.out.println(" tone : c"  );                  }                 if (tone ==15)                 { system.out.println(" tone : d"  );                  }                 if (tone ==10)                 {  system.out.println(" tone : *"  );                  }                 if (tone ==11)                 {  system.out.println(" tone : #"  );                  }                   }             }                       /*                       check if sample has dtmf tone                      */                  public int finddtmf(int[] samples)                 {                 double[] goertzelvalues = new double[8];                 double lowfreqvalue = 0;                 int lowfreq = 0;                 double sumlow = 0;                 double highfreqvalue = 0;                 int highfreq = 0;                 double sumhigh = 0;                  for(int = 0; i<8; i++)                      {                         goertzelvalues[i] = goertzel(samples,dtmftones[i]);                     }                      for(int = 0; i<4; i++) // find st?rste low frequency                     {                         sumlow += goertzelvalues[i]; // sum til signal-test                         if(goertzelvalues[i] > lowfreqvalue)                          {                             lowfreqvalue = goertzelvalues[i];                             lowfreq = i;                         }                     }                     for(int = 4; i<8; i++) // find st?rste high frequency                     {                         sumhigh += goertzelvalues[i]; // sum til signal-test                         if(goertzelvalues[i] > highfreqvalue)                         {                             highfreqvalue = goertzelvalues[i];                              highfreq = i-4;                         }                     }                     if(lowfreqvalue < sumlow/2 || highfreqvalue < sumhigh/2) // test signalstyrke                     {                         return -1;                     }                         return dtmfboard[lowfreq][highfreq]; // returner dtmf tone                 }             }          public double goertzel(int[] samples, float freq)         {         double vkn = 0;         double vkn1 = 0;         double vkn2 = 0;          for(int j = 0; j<samples.length -1; j++)         {         vkn2 = vkn1;         vkn1 = vkn;         vkn = 2 * math.cos(2 * math.pi * (freq * samples.length / format.getsamplerate() ) / samples.length) * vkn1 - vkn2 + samples[j];         }         double wnk = math.exp(-2 * math.pi * (freq * samples.length / format.getsamplerate() ) / samples.length);             return math.abs(vkn - wnk * vkn1);         }  } 


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -