校验wav文件格式

部署运行你感兴趣的模型镜像
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;
		}
	}
}

您可能感兴趣的与本文相关的镜像

HunyuanVideo-Foley

HunyuanVideo-Foley

语音合成

HunyuanVideo-Foley是由腾讯混元2025年8月28日宣布开源端到端视频音效生成模型,用户只需输入视频和文字,就能为视频匹配电影级音效

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值