#简单的音频相似度对比 Demo
##环境
AndroidStudio、MATLAB、Audacity
##基本思路和流程
1. 录音,保存音频数据
2. 从二进制文件中获取音频原始数据
3. 音频滤波
4. 计算音频信号短时能量
5. 截取音频信号有效数据
6. 对对比音频数据进行同上操作
7. 计算标准音频与对比音频数据的余弦距离
##核心代码
import java.io.DataInputStream;
import java.io.IOException;
/**
* Created by lzy on 2015/11/18.
*/
public class AudioDataOperate {
public static final int TYPE_16BIT = 1;
public static final double DATA_START_VALUE = 0.025;
public static final double DATA_END_VALUE = 0.025;
/**
* 按编码类型提取音频数据
*
* @param dis
* @param size
* @param type
* @return
*/
public static short[] getAudioData(DataInputStream dis, int size, int type) {
short[] audioData = new short[size];
try {
byte[] tempData = new byte[2];
long audioDataSize = 0;
while (dis.read(tempData) != -1) {
// 每16位读取一个音频数据
audioData[(int) audioDataSize] = (short) (((tempData[0] & 0xff) << 8) | (tempData[1] & 0xff));
audioDataSize++;
if (audioDataSize == size) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return audioData;
}
/**
* 归一化
*/
public static double[] normalize(short[] data) {
short max = findMax(data);
double[] result = new double[data.length];
for (int i = 0; i < data.length; i++) {
result[i] = ((double) data[i] / max);
}
return result;
}
/**
* 归一化
*/
public static doub