Java實(shí)現(xiàn)Shazam聲音識別算法的實(shí)例代碼
Shazam算法采用傅里葉變換將時(shí)域信號轉(zhuǎn)換為頻域信號,并獲得音頻指紋,最后匹配指紋契合度來識別音頻。
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í)域信號轉(zhuǎn)換為頻域信號。
傅里葉變換
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;
}
// 如果信號數(shù)為奇數(shù),使用dft計(jì)算
if (n % 2 != 0) {
return dft(x);
}
// 提取下標(biāo)為偶數(shù)的原始信號值進(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ù)的原始信號值進(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í)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Java的NIO之并發(fā)環(huán)境下非阻塞IO技術(shù)詳解
這篇文章主要介紹了Java的NIO之并發(fā)環(huán)境下非阻塞IO技術(shù)詳解,Java NIO(New IO)是Java平臺提供的一種用于高效處理I/O操作的API,它引入了一組新的類和概念,以提供更好的性能和可擴(kuò)展性,需要的朋友可以參考下2023-09-09
深入剖析springBoot中的@Scheduled執(zhí)行原理
這篇文章主要介紹了springBoot中的@Scheduled執(zhí)行原理,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java中的Monad設(shè)計(jì)模式及其實(shí)現(xiàn)過程
本文介紹了Java中的Monad設(shè)計(jì)模式及其在函數(shù)式編程中的應(yīng)用,雖然Java不是函數(shù)式編程語言,但可以通過接口和泛型模擬Monad的行為,實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用和上下文管理,通過一個(gè)示例展示了如何使用OptionalMonad進(jìn)行鏈?zhǔn)秸{(diào)用,并解析了Monad接口和OptionalMonad的實(shí)現(xiàn)細(xì)節(jié)2025-03-03
解決bootstrap.yml不生效,無法優(yōu)先于application.yml文件加載問題
文章主要討論了在Spring Boot項(xiàng)目中,`bootstrap.yml`文件無法優(yōu)先于`application.yml`文件加載的問題,原因是缺少了`nacos-config`依賴,且需要確保Spring Boot版本與`nacos-config`版本匹配,作者希望通過分享個(gè)人經(jīng)驗(yàn),幫助他人解決類似問題2024-12-12
MyBatis通用Mapper實(shí)現(xiàn)原理及相關(guān)內(nèi)容
今天小編就為大家分享一篇關(guān)于MyBatis通用Mapper實(shí)現(xiàn)原理及相關(guān)內(nèi)容,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
java題解Leetcode 8字符串轉(zhuǎn)換整數(shù)
這篇文章主要為大家介紹了java題解Leetcode 8字符串轉(zhuǎn)換整數(shù)實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
SpringCloud全面解析@FeignClient標(biāo)識接口的過程
這篇文章主要介紹了SpringCloud全面解析@FeignClient標(biāo)識接口的過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Springboot SseEmitter流式輸出的實(shí)現(xiàn)代碼
本文介紹了Spring Boot中使用SseEmitter實(shí)現(xiàn)流式輸出的原理和示例代碼,通過SseEmitter,可以實(shí)現(xiàn)客戶端和服務(wù)器之間的實(shí)時(shí)通信,服務(wù)器可以分塊發(fā)送數(shù)據(jù),而客戶端可以實(shí)時(shí)接收和處理這些數(shù)據(jù),,感興趣的朋友一起看看吧2025-03-03

