package com.clpc.cc.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.log4j.Logger;
/**
* 录音文件转码操作
* @author Administrator
*
*/
public class V3FileUtil {
public static final String VOX_FRAQ_24K = "6000";
public static final String VOX_FRAQ_32K = "8000";
Logger logger = Logger.getLogger(getClass());
private String voxfreq = "6000";
public V3FileUtil(){
}
public V3FileUtil(String voxFreq)
{
this.voxfreq = voxFreq;
}
public void long2Byte(byte[] output, long[] input, int len)
{
int i = 0; for (int j = 0; j < len; j += 4)
{
output[j] = ((byte)(int)(input[i] & 0xFF));
output[(j + 1)] = ((byte)(int)(input[i] >>> 8 & 0xFF));
output[(j + 2)] = ((byte)(int)(input[i] >>> 16 & 0xFF));
output[(j + 3)] = ((byte)(int)(input[i] >>> 24 & 0xFF));
i++;
}
}
public void short2Byte(byte[] output, short sh)
{
output[0] = ((byte)(int)(sh & 0xFF));
output[1] = ((byte)(int)(sh >>> 8 & 0xFF));
}
public short byte2Short(byte b)
{
return b < 0 ? (short)(b & 0xFF) : (short)b;
}
/**
* 转换录音格式
* @param v3File 输入文件
* @param wavFile 输出文件
*/
public void convert(String v3File, String wavFile)
{
BufferedInputStream fileInput = null;
BufferedOutputStream fileOut = null;
int[] indsft = { -1, -1, -1, -1, 2, 4, 6, 8 };
int[] stpsz = { 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794 };
int[][] nbl2bit = { { 0, 0, 0, 0 }, { 0, 0, 0, 1 }, { 0, 0, 1, 0 }, { 0, 0, 1, 1 }, { 0, 1, 0, 0 }, { 0, 1, 0, 1 }, { 0, 1, 1, 0 }, { 0, 1, 1, 1 }, { 1, 0, 0, 0 }, { 1, 0, 0, 1 }, { 1, 0, 1, 0 }, { 1, 0, 1, 1 }, { 1, 1, 0, 0 }, { 1, 1, 0, 1 }, { 1, 1, 1, 0 }, { 1, 1, 1, 1 } };
int[] sgns = { 1, -1 };
File file = new File(v3File);
int ssindex = 0;
if (!file.isFile())
{
logger.info("There's no file:" + v3File);
return;
}
try
{
fileInput = new BufferedInputStream(new FileInputStream(file));
fileOut = new BufferedOutputStream(new FileOutputStream(wavFile));
long[] lPCMHDR = { 1179011410L, 0L, 1163280727L, 544501094L, 16L, 65537L, 0L, 0L, 1048578L, 1635017060L, 0L };
lPCMHDR[1] = (4L * file.length() + 36L);
lPCMHDR[6] = Integer.parseInt(this.voxfreq);
lPCMHDR[7] = (2 * Integer.parseInt(this.voxfreq));
lPCMHDR[10] = (4L * file.length());
byte[] b = new byte[44];
long2Byte(b, lPCMHDR, 44);
fileOut.write(b, 0, b.length);
fileOut.flush();
short iVal = 0;
int pos = 0;
byte[] szBuf = new byte[131072];
byte[] szDesBuf = new byte[524288];
int dwRead = 0;
while ((dwRead = fileInput.read(szBuf, 0, 131072)) != -1)
{
for (int idx = 0; idx < dwRead - 1; idx++)
{
short incoded = (short)(byte2Short(szBuf[idx]) / 16);
int diff = sgns[nbl2bit[incoded][0]] * (stpsz[ssindex] * nbl2bit[incoded][1] + stpsz[ssindex] / 2 * nbl2bit[incoded][2] + stpsz[ssindex] / 4 * nbl2bit[incoded][3] + stpsz[ssindex] / 8);
ssindex += indsft[(incoded % 8)];
if (ssindex < 0)
{
ssindex = 0;
}
if (ssindex > 48)
{
ssindex = 48;
}
iVal = (short)(iVal + diff);
if (iVal > 2047)
{
iVal = 2047;
}
else if (iVal < -2047)
{
iVal = -2047;
}
byte[] b2 = new byte[2];
short2Byte(b2, (short)(iVal * 16));
szDesBuf[pos] = b2[0];
pos++;
szDesBuf[pos] = b2[1];
pos++;
incoded = (short)(byte2Short(szBuf[idx]) % 16);
diff = sgns[nbl2bit[incoded][0]] * (stpsz[ssindex] * nbl2bit[incoded][1] + stpsz[ssindex] / 2 * nbl2bit[incoded][2] + stpsz[ssindex] / 4 * nbl2bit[incoded][3] + stpsz[ssindex] / 8);
ssindex += indsft[(incoded % 8)];
if (ssindex < 0)
{
ssindex = 0;
}
if (ssindex > 48)
{
ssindex = 48;
}
iVal = (short)(iVal + diff);
if (iVal > 2047)
{
iVal = 2047;
}
else if (iVal < -2047)
{
iVal = -2047;
}
byte[] b3 = new byte[2];
short2Byte(b3, (short)(iVal * 16));
szDesBuf[pos] = b3[0];
pos++;
szDesBuf[pos] = b3[1];
pos++;
}
fileOut.write(szDesBuf, 0, dwRead * 4);
fileOut.flush();
pos = 0;
}
logger.info("Transfer v3 file to wav file successfully!");
}
catch (IOException e)
{
//logger.info("Transfer v3 file to wav file failed!");
}
finally
{
try
{
if (fileInput != null)
{
fileInput.close();
}
}
catch (IOException e)
{
logger.info("File closed error happened when transfer v3 to wav!");
}
try
{
if (fileOut != null)
{
fileOut.close();
}
}
catch (IOException e)
{
logger.info("File closed error happened when transfer v3 to wav!");
}
}
}
public static void main(String[] args) {
V3FileUtil fu = new V3FileUtil();
fu.convert("", "");
}
}
华为根据callID查询对应录音绝对路径
/**
*
* @param callId
* @return
*/
public String getRecordMonth(String callId){
String month ="";
try {
//截取callID
String[] ag = callId.split("-");
//格式化时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
SimpleDateFormat sd = new SimpleDateFormat("MM");
//基础时间1970年1月1日
long historyDate = 19700101000000L;
//转换为毫秒 将callId秒转换为毫秒
long callIdDate = Long.parseLong(ag[0]) * 1000;
// 八小时秒数 东八区需要加八小时
long eastEightArea = 28800000L;
String strDate = String.valueOf(historyDate);
long millionSeconds = sdf.parse(strDate).getTime();//毫秒
// System.out.println(millionSeconds);
Date date = new Date(millionSeconds + callIdDate +eastEightArea);
String tableMonth = sd.format(date);
month = String.valueOf(Integer.valueOf(tableMonth));
// System.out.println(month);
} catch (Exception e) {
e.printStackTrace();
logger.error("格式化时间异常:"+e);
}
return month;
}