国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

Java實(shí)現(xiàn)Shazam聲音識(shí)別算法的實(shí)例代碼

2024-07-14 08:42:18
字體:
供稿:網(wǎng)友

Shazam算法采用傅里葉變換將時(shí)域信號(hào)轉(zhuǎn)換為頻域信號(hào),并獲得音頻指紋,最后匹配指紋契合度來識(shí)別音頻。

1、AudioSystem獲取音頻

奈奎斯特-香農(nóng)采樣定理告訴我們,為了能捕獲人類能聽到的聲音頻率,我們的采樣速率必須是人類聽覺范圍的兩倍。人類能聽到的聲音頻率范圍大約在20Hz到20000Hz之間,所以在錄制音頻的時(shí)候采樣率大多是44100Hz。這是大多數(shù)標(biāo)準(zhǔn)MPEG-1 的采樣率。44100這個(gè)值最初來源于索尼,因?yàn)樗梢栽试S音頻在修改過的視頻設(shè)備上以25幀(PAL)或者30幀( NTSC)每秒進(jìn)行錄制,而且也覆蓋了專業(yè)錄音設(shè)備的20000Hz帶寬。所以當(dāng)你在選擇錄音的頻率時(shí),選擇44100Hz就好了。

定義音頻格式:

  public static float sampleRate = 44100;  public static int sampleSizeInBits = 16;  public static int channels = 2; // double  public static boolean signed = true; // Indicates whether the data is signed or unsigned  public static boolean bigEndian = true; // Indicates whether the audio data is stored in big-endian or little-endian order  public AudioFormat getFormat() {    return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,        bigEndian);  }

調(diào)用麥克風(fēng)獲取音頻,保存到out中

 public static ByteArrayOutputStream out = new ByteArrayOutputStream();1    try {      AudioFormat format = smartAuto.getFormat(); // Fill AudioFormat with the settings      DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);      startTime = new Date().getTime();      System.out.println(startTime);      SmartAuto.line = (TargetDataLine) AudioSystem.getLine(info);      SmartAuto.line.open(format);      SmartAuto.line.start();      new FileAnalysis().getDataToOut("");      while (smartAuto.running) {        checkTime(startTime);      }      SmartAuto.line.stop();      SmartAuto.line.close();    } catch (Throwable e) {      e.printStackTrace();    }

獲取到的out數(shù)據(jù)需要通過傅里葉變換,從時(shí)域信號(hào)轉(zhuǎn)換為頻域信號(hào)。

傅里葉變換

public Complex[] fft(Complex[] x) {    int n = x.length;    // 因?yàn)閑xp(-2i*n*PI)=1,n=1時(shí)遞歸原點(diǎn)    if (n == 1){      return x;    }    // 如果信號(hào)數(shù)為奇數(shù),使用dft計(jì)算    if (n % 2 != 0) {      return dft(x);    }    // 提取下標(biāo)為偶數(shù)的原始信號(hào)值進(jìn)行遞歸fft計(jì)算    Complex[] even = new Complex[n / 2];    for (int k = 0; k < n / 2; k++) {      even[k] = x[2 * k];    }    Complex[] evenValue = fft(even);    // 提取下標(biāo)為奇數(shù)的原始信號(hào)值進(jìn)行fft計(jì)算    // 節(jié)約內(nèi)存    Complex[] odd = even;    for (int k = 0; k < n / 2; k++) {      odd[k] = x[2 * k + 1];    }    Complex[] oddValue = fft(odd);    // 偶數(shù)+奇數(shù)    Complex[] result = new Complex[n];    for (int k = 0; k < n / 2; k++) {      // 使用歐拉公式e^(-i*2pi*k/N) = cos(-2pi*k/N) + i*sin(-2pi*k/N)      double p = -2 * k * Math.PI / n;      Complex m = new Complex(Math.cos(p), Math.sin(p));      result[k] = evenValue[k].add(m.multiply(oddValue[k]));      // exp(-2*(k+n/2)*PI/n) 相當(dāng)于 -exp(-2*k*PI/n),其中exp(-n*PI)=-1(歐拉公式);      result[k + n / 2] = evenValue[k].subtract(m.multiply(oddValue[k]));    }    return result;  }

計(jì)算out的頻域值

 private void setFFTResult(){    byte audio[] = SmartAuto.out.toByteArray();    final int totalSize = audio.length;    System.out.println("totalSize = " + totalSize);    int chenkSize = 4;    int amountPossible = totalSize/chenkSize;    //When turning into frequency domain we'll need complex numbers:     SmartAuto.results = new Complex[amountPossible][];    DftOperate dfaOperate = new DftOperate();    //For all the chunks:     for(int times = 0;times < amountPossible; times++) {      Complex[] complex = new Complex[chenkSize];      for(int i = 0;i < chenkSize;i++) {        //Put the time domain data into a complex number with imaginary part as 0:         complex[i] = new Complex(audio[(times*chenkSize)+i], 0);      }      //Perform FFT analysis on the chunk:       SmartAuto.results[times] = dfaOperate.fft(complex);    }    System.out.println("results = " + SmartAuto.results.toString());  }

總結(jié)

以上所述是小編給大家介紹的Java實(shí)現(xiàn)Shazam聲音識(shí)別算法的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VeVb武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宣威市| 洛浦县| 固原市| 本溪市| 富平县| 太和县| 龙南县| 瓮安县| 宝丰县| 得荣县| 奉新县| 永宁县| 都昌县| 华安县| 眉山市| 长寿区| 临武县| 武功县| 祁阳县| 抚顺市| 武清区| 永兴县| 丰宁| 阳东县| 阿克苏市| 莒南县| 双鸭山市| 夏邑县| 陆河县| 绥德县| 滨州市| 双桥区| 西安市| 临夏市| 定边县| 沧源| 吉水县| 霍邱县| 文成县| 增城市| 开江县|