package com.suntek.eptel.util.voice;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.log4j.Logger;
import com.suntek.zj.communication.gw.util.log.CommonLogger;
/**
* <p>
* Title: 判断wav文件的格式是否满足要求:A LAW,频率11025,单通道,16位。
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2010
* </p>
* <p>
* Company: 新太科技
* </p>
*
* @author lmj
* @version 1.0
*/
public class WAVFileChecker {
public static Logger logger = CommonLogger.logger;
// wav文件头大小
private static final int FILE_HEAD_SIZE = 44;
// 符合条件的文件频率:11025
private static final int DEFAULT_FILE_FREQUENCY = 11025;
// 单声道
private static final int DEFAULT_FILE_SINGLE_TRACK = 1;
// 文件格式: A Law
private static final int DEFAULT_FILE_A_LAW = 6;
// 文件格式:PCM
private static final int DEFAULT_FILE_A_PCM = 1;
// 文件位数: 16
private static final int DEFAULT_FILE_BIT = 16;
/**
* 检查wav文件是否满足格式要求
*
* @param filePath
* 要检查文件的全路径
*
* @return
*/
public static boolean checkWAVFile(String filePath) {
// 输入参数校验
if (filePath == null || !filePath.toLowerCase().endsWith(".wav")) {
logger.error("[WAVFileChecker.checkWAVFile]该文件不是wav格式!filePath->"
+ filePath);
return false;
}
// 构造一个长为44个字节的数组,存储wav头部内容
byte[] head = new byte[FILE_HEAD_SIZE];
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
fis.read(head);
// 检查文件格式
if (!checkFileFormat(head)) {
return false;
}
// 判断文件声道
if (!checkFileTrack(head)) {
return false;
}
// 判断文件频率
if (!checkFileFrequency(head)) {
return false;
}
// 判断文件位数
if (!checkFileBit(head)) {
return false;
}
return true;
} catch (Exception e) {
logger.error("[WAVFileChecker.checkWAVFile]throws exception->" + e);
return false;
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
logger
.error("[WAVFileChecker.checkWAVFile]throws exception->"
+ e);
}
}
}
}
/**
* 判断文件格式: A LAW/PCM/U LAW
*
* @param head
* @return
*/
private static boolean checkFileFormat(byte[] head) {
short the21thByte = Short.parseShort(String.valueOf(head[21] & 0xff),
10);
short the20thByte = Short.parseShort(String.valueOf(head[20] & 0xff),
10);
int fomatCode = the21thByte * 256 + the20thByte;
// A LAW:6;PCM:1;MU LAW:7
if (fomatCode == DEFAULT_FILE_A_PCM) {
return true;
} else {
return false;
}
}
/**
* 判断文件声道:单声道/立体声
*
* @param head
* @return
*/
private static boolean checkFileTrack(byte[] head) {
short the23thByte = Short.parseShort(String.valueOf(head[23] & 0xff),
10);
short the22thByte = Short.parseShort(String.valueOf(head[22] & 0xff),
10);
int fileTrack = the23thByte * 256 + the22thByte;
// 单声道:1;立体声:2
if (fileTrack == DEFAULT_FILE_SINGLE_TRACK) {
return true;
} else {
return false;
}
}
/**
* 判断文件频率
*
* @param head
* @return
*/
private static boolean checkFileFrequency(byte[] head) {
short the27thByte = Short.parseShort(String.valueOf(head[27] & 0xff),
10);
short the26thByte = Short.parseShort(String.valueOf(head[26] & 0xff),
10);
short the25thByte = Short.parseShort(String.valueOf(head[25] & 0xff),
10);
short the24thByte = Short.parseShort(String.valueOf(head[24] & 0xff),
10);
int fileSize = the27thByte * 16777216 + the26thByte * 65536
+ the25thByte * 256 + the24thByte;
// 默认11025
if (fileSize == DEFAULT_FILE_FREQUENCY) {
return true;
} else {
return false;
}
}
/**
* 判断文件位数
*
* @param head
* @return
*/
private static boolean checkFileBit(byte[] head) {
short the34thByte = Short.parseShort(String.valueOf(head[34] & 0xff),
10);
short the35thByte = Short.parseShort(String.valueOf(head[35] & 0xff),
10);
int bitsPerSample = the35thByte * 256 + the34thByte;
// 默认16位
if (bitsPerSample == DEFAULT_FILE_BIT) {
return true;
} else {
return false;
}
}
}
校验wav文件格式
最新推荐文章于 2025-10-19 15:01:43 发布
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
HunyuanVideo-Foley
语音合成
HunyuanVideo-Foley是由腾讯混元2025年8月28日宣布开源端到端视频音效生成模型,用户只需输入视频和文字,就能为视频匹配电影级音效
1182

被折叠的 条评论
为什么被折叠?



